11import { ActionsType } from "hyperapp"
22import { get , merge , set } from "./immutable"
3+ import { getPath } from "./selectors"
34
45import * as api from "./api"
56
@@ -79,23 +80,22 @@ function appendActionEvent(
7980}
8081
8182function toggleAction (
82- runs : api . Runs ,
83- runId : string ,
83+ state : api . State ,
84+ run : string ,
8485 actionPath : number [ ]
85- ) : api . Runs {
86- const path : any [ ] = [ runId ]
87- actionPath . forEach ( index => {
88- path . push ( "actions" , index )
89- } )
90- path . push ( "collapsed" )
91-
92- const collapsed = get ( runs , path )
93- if ( typeof collapsed !== "boolean" ) {
86+ ) : Partial < api . State > {
87+ const path = getPath ( run , actionPath )
88+
89+ const existingAction = get ( state . runs , path )
90+ if ( typeof existingAction !== "object" ) {
9491 console . log ( "WARN: try to collapse invalid action, path: " , actionPath )
95- return runs
92+ return state
9693 }
9794
98- return set ( runs , path , ! collapsed )
95+ const collapsed = ! existingAction . collapsed
96+ const action = { ...existingAction , collapsed }
97+ const runs : api . Runs = set ( state . runs , path , action )
98+ return { runs }
9999}
100100
101101export const INITIAL_ACTION = "%%% INITIAL STATE %%%"
@@ -123,19 +123,18 @@ export const actions: ActionsType<api.State, api.Actions> = {
123123 collapsed : false
124124 }
125125
126- return { runs, selectedAction : action }
126+ return { runs, selectedAction : { run : event . runId , path : [ 0 ] } }
127127 } ,
128128 logAction : ( event : api . ActionEvent ) => state => {
129129 const runs = { ...state . runs }
130130 const run = runs [ event . runId ]
131131 const actions = [ ...run . actions ]
132132 run . actions = actions
133133 const prevAction = actions . pop ( )
134- let selectedAction : api . AppAction
135134 if ( prevAction . done ) {
136135 // previous action done: create new action and append
137136 if ( ! event . callDone ) {
138- selectedAction = {
137+ const action = {
139138 done : false ,
140139 collapsed : false ,
141140 actions : [ ] ,
@@ -144,15 +143,20 @@ export const actions: ActionsType<api.State, api.Actions> = {
144143 previousState : prevAction . nextState
145144 }
146145
147- actions . push ( prevAction , selectedAction )
146+ actions . push ( prevAction , action )
148147 } else {
149148 // error!, should we log it here?
150149 console . log ( "Previous action is done and event.callDone" , state , event )
151150 }
152151 } else {
153152 // previous action not done: find parent action, create and append
154- selectedAction = appendActionEvent ( prevAction , event )
155- actions . push ( selectedAction )
153+ const action = appendActionEvent ( prevAction , event )
154+ actions . push ( action )
155+ }
156+
157+ const selectedAction = {
158+ run : event . runId ,
159+ path : [ actions . length - 1 ]
156160 }
157161
158162 return { runs, selectedAction }
@@ -164,10 +168,9 @@ export const actions: ActionsType<api.State, api.Actions> = {
164168 } ,
165169 toggleAction : ( payload : api . ToggleActionPayload ) => state => {
166170 const { run, path } = payload
167- const runs = toggleAction ( state . runs , run , path )
168- return { runs }
171+ return toggleAction ( state , run , path )
169172 } ,
170- select : ( selectedAction : api . AppAction | null ) => {
173+ select : ( selectedAction : api . SelectedAction | null ) => {
171174 return { selectedAction }
172175 } ,
173176 showPane : ( paneShown : boolean ) => {
0 commit comments