Skip to content

Migrate to xstate as app skeleton #2

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 11 commits into from
Jun 2, 2019
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
setup tutorial initialization
  • Loading branch information
ShMcK committed Jun 2, 2019
commit 04653e2f736790df082abc0abddf0f8fc69ccd13
11 changes: 9 additions & 2 deletions src/state/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ let initialProgress: CR.Progress = {

export default {
start: async () => {
// verify that the user has a tutorial & progress
// verify git is setup with a coderoad remote
const [tutorial, progress, hasGit, hasGitRemote] = await Promise.all([
storage.getTutorial(),
storage.getProgress(),
Expand All @@ -22,9 +24,11 @@ export default {
initialTutorial = tutorial
initialProgress = progress
const canContinue = !!(tutorial && progress && hasGit && hasGitRemote)
// if a tutorial exists, "CONTINUE"
// otherwise start from "NEW"
send(canContinue ? 'CONTINUE' : 'NEW')
},
loadTutorial: assign({
tutorialLoad: assign({
// load initial data, progress & position
data(): CR.TutorialData {
if (!initialTutorial) {
Expand All @@ -33,7 +37,9 @@ export default {
return initialTutorial.data

},
progress(): CR.Progress { return initialProgress },
progress(): CR.Progress {
return initialProgress
},
position() {
if (!initialTutorial) {
throw new Error('No Tutorial loaded')
Expand All @@ -53,4 +59,5 @@ export default {
return position
}
}),

}
6 changes: 5 additions & 1 deletion src/state/guards/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import * as CR from 'typings'

export default {

// skip to the stage if the level has already been started
hasNoNextLevelProgress: (context: CR.MachineContext): boolean => {
return false
},
}
19 changes: 8 additions & 11 deletions src/state/machine.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Machine } from 'xstate'
import { Machine, send } from 'xstate'
import * as CR from 'typings'

import actions from './actions'
Expand Down Expand Up @@ -43,28 +43,25 @@ export const tutorialMachine = Machine<

},
ContinueTutorial: {
onEntry: 'loadTutorial',
onEntry: 'tutorialLoad',
on: {
TUTORIAL_START: {
target: 'Tutorial',
target: 'Tutorial.LoadNext',
}
}
},
}
},
Tutorial: {
initial: 'Initialize',
initial: 'Summary',
states: {
Initialize: {
LoadNext: {
onEntry: () => send('LOAD_NEXT'),
on: {
TUTORIAL_LOADED: [
{
target: 'Summary',
cond: 'hasNoProgress',
},
LOAD_NEXT: [
{
target: 'Level',
cond: 'hasNoLevelProgress',
cond: 'hasNoNextLevelProgress',
},
{
target: 'Stage',
Expand Down
2 changes: 1 addition & 1 deletion src/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export interface MachineStateSchema {
}
Tutorial: {
states: {
Initialize: {}
LoadNext: {}
Summary: {}
Level: {}
Stage: {
Expand Down