Skip to content

Fix/continue #301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
cleanup machine actions
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
  • Loading branch information
ShMcK committed Apr 26, 2020
commit a87e5c7f6107bea0a1710c4ae9652b644a5c1945
12 changes: 8 additions & 4 deletions web-app/src/components/Router/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@ declare let acquireVsCodeApi: any

const editor = acquireVsCodeApi()
const editorSend = (action: T.Action) => {
logger(`CLIENT TO EXT: "${action.type}"`)
logger(`TO EXT: "${action.type}"`)
return editor.postMessage(action)
}

// router finds first state match of <Route path='' />
const useRouter = (): Output => {
const [state, send] = useMachine<T.MachineContext, any>(createMachine({ editorSend }))

const sendWithLog = (action: T.Action): void => {
logger(`SEND: ${action.type}`, action)
send(action)
}

logger(`STATE: ${JSON.stringify(state.value)}`)

// event bus listener
Expand All @@ -38,8 +43,7 @@ const useRouter = (): Output => {
if (action.source) {
return
}
logger(`CLIENT RECEIVED: "${action.type}"`)
send(action)
sendWithLog(action)
}
window.addEventListener(listener, handler)
return () => {
Expand Down Expand Up @@ -74,7 +78,7 @@ const useRouter = (): Output => {

return {
context: state.context,
send,
send: sendWithLog,
Router,
Route,
}
Expand Down
4 changes: 2 additions & 2 deletions web-app/src/containers/Tutorial/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ const TutorialPage = (props: PageProps) => {

const onContinue = (): void => {
props.send({
type: 'LEVEL_NEXT',
type: 'NEXT_LEVEL',
payload: {
LevelId: position.levelId,
levelId: position.levelId,
},
})
}
Expand Down
13 changes: 7 additions & 6 deletions web-app/src/services/state/actions/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as TT from 'typings/tutorial'
import { assign, send, ActionFunctionMap } from 'xstate'
import * as selectors from '../../selectors'
import onError from '../../../services/sentry/onError'
import logger from 'services/logger'

const contextActions: ActionFunctionMap<T.MachineContext, T.MachineEvent> = {
// @ts-ignore
Expand Down Expand Up @@ -33,7 +34,7 @@ const contextActions: ActionFunctionMap<T.MachineContext, T.MachineEvent> = {
},
}),
// @ts-ignore
startNewTutorial: assign({
startTutorial: assign({
position: (context: T.MachineContext, event: T.MachineEvent): any => {
const position: T.Position = selectors.initialPosition(context)
return position
Expand Down Expand Up @@ -119,8 +120,7 @@ const contextActions: ActionFunctionMap<T.MachineContext, T.MachineEvent> = {
// @ts-ignore
updatePosition: assign({
position: (context: T.MachineContext, event: T.MachineEvent): any => {
const { position } = event.payload
return position
return event.payload
},
}),
loadNext: send(
Expand All @@ -140,7 +140,7 @@ const contextActions: ActionFunctionMap<T.MachineContext, T.MachineEvent> = {
// NEXT STEP
if (hasNextStep) {
const nextPosition = { ...position, stepId: steps[stepIndex + 1].id }
return { type: 'NEXT_STEP', payload: { position: nextPosition } }
return { type: 'NEXT_STEP', payload: nextPosition }
}

// has next level?
Expand All @@ -164,7 +164,7 @@ const contextActions: ActionFunctionMap<T.MachineContext, T.MachineEvent> = {
levelId: nextLevel.id,
stepId: nextLevel.steps[0].id,
}
return { type: 'NEXT_LEVEL', payload: { position: nextPosition } }
return { type: 'NEXT_LEVEL', payload: nextPosition }
}

// COMPLETED
Expand Down Expand Up @@ -230,8 +230,9 @@ const contextActions: ActionFunctionMap<T.MachineContext, T.MachineEvent> = {
error: (): any => null,
}),
// @ts-ignore
checkEmptySteps: send((context: T.MachineContext) => {
checkLevelCompleted: send((context: T.MachineContext) => {
// no step id indicates no steps to complete
logger(context.position)
return {
type: context.position.stepId === null ? 'START_COMPLETED_LEVEL' : 'START_LEVEL',
}
Expand Down
10 changes: 5 additions & 5 deletions web-app/src/services/state/machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export const createMachine = (options: any) => {
on: {
NEW_TUTORIAL: 'ValidateSetup',
CONTINUE_TUTORIAL: {
target: '#tutorial-level',
target: 'StartTutorial',
actions: ['continueConfig'],
},
CONTINUE_FAILED: {
Expand Down Expand Up @@ -127,7 +127,7 @@ export const createMachine = (options: any) => {
},
},
StartTutorial: {
onEntry: ['startNewTutorial'],
onEntry: ['startTutorial'],
after: {
0: '#tutorial',
},
Expand Down Expand Up @@ -157,7 +157,7 @@ export const createMachine = (options: any) => {
initial: 'Load',
states: {
Load: {
onEntry: ['loadLevel', 'loadStep', 'checkEmptySteps'],
onEntry: ['loadLevel', 'loadStep', 'checkLevelCompleted'],
on: {
START_LEVEL: 'Normal',
START_COMPLETED_LEVEL: 'LevelComplete',
Expand Down Expand Up @@ -214,9 +214,9 @@ export const createMachine = (options: any) => {
onEntry: ['updateLevelProgress'],
onExit: ['syncLevelProgress'],
on: {
LEVEL_NEXT: {
NEXT_LEVEL: {
target: '#tutorial-load-next',
actions: ['testClear'],
actions: ['testClear', 'updatePosition'],
},
},
},
Expand Down