Skip to content

Commit 1dfb9f8

Browse files
committed
resolve git async issue
1 parent 71a1efa commit 1dfb9f8

File tree

6 files changed

+37
-50
lines changed

6 files changed

+37
-50
lines changed

src/actions/tutorialConfig.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@ import * as git from '../services/git'
55
interface TutorialConfigParams {
66
tutorial: G.Tutorial,
77
alreadyConfigured?: boolean
8+
onComplete?(): void
89
}
910

10-
const tutorialConfig = async ({tutorial, alreadyConfigured}: TutorialConfigParams) => {
11+
const tutorialConfig = async ({tutorial, alreadyConfigured, onComplete}: TutorialConfigParams) => {
1112
if (!alreadyConfigured) {
1213
// setup git, add remote
1314
await git.initIfNotExists()
1415

1516
// TODO: if remote not already set
1617
await git.setupRemote(tutorial.repo.uri)
18+
if (onComplete) {onComplete()}
1719
}
1820

1921
// TODO: allow multiple coding languages in a tutorial

src/channel/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ class Channel implements Channel {
7474
const tutorialData = action.payload.tutorial
7575
this.context.setTutorial(this.workspaceState, tutorialData)
7676
tutorialConfig({
77-
tutorial: tutorialData
77+
tutorial: tutorialData,
78+
// must await async git setup or commit loading fails
79+
onComplete: () => this.send({type: 'TUTORIAL_CONFIGURED'})
7880
})
7981
return
8082
case 'EDITOR_TUTORIAL_CONTINUE_CONFIG':

web-app/src/services/channel/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ class Channel {
4040
case 'NEW_TUTORIAL':
4141
this.machineSend(action)
4242
return
43+
case 'TUTORIAL_CONFIGURED':
44+
this.machineSend(action)
45+
return
4346
case 'CONTINUE_TUTORIAL':
4447
this.machineSend(action)
4548
return

web-app/src/services/state/actions/editor.ts

+23-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import * as G from 'typings/graphql'
22
import * as CR from 'typings'
33
import * as selectors from '../../selectors'
44
import channel from '../../channel'
5+
import client from '../../apollo'
6+
import tutorialQuery from '../../apollo/queries/tutorial'
57

68
export default {
79
loadStoredTutorial() {
@@ -23,16 +25,29 @@ export default {
2325
},
2426
initializeTutorial(context: CR.MachineContext, event: CR.MachineEvent) {
2527
// setup test runner and git
26-
const {tutorial} = event.data.payload
27-
28-
if (!tutorial) {
29-
throw new Error('Invalid tutorial for tutorial config')
28+
if (!context.tutorial) {
29+
throw new Error('Tutorial not available to load')
3030
}
31-
console.log('EDITOR: TUTORIAL_CONFIG', tutorial)
32-
channel.editorSend({
33-
type: 'EDITOR_TUTORIAL_CONFIG',
34-
payload: {tutorial},
31+
32+
client.query({
33+
query: tutorialQuery,
34+
variables: {
35+
tutorialId: context.tutorial.id,
36+
version: context.tutorial.version.version,
37+
}
38+
}).then((result) => {
39+
if (!result || !result.data || !result.data.tutorial) {
40+
return Promise.reject('No tutorial returned from tutorial config query')
41+
}
42+
43+
channel.editorSend({
44+
type: 'EDITOR_TUTORIAL_CONFIG',
45+
payload: {tutorial: result.data.tutorial},
46+
})
3547
})
48+
.catch((error: Error) => {
49+
return Promise.reject(`Failed to load tutorial config ${error.message}`)
50+
})
3651
},
3752
continueConfig() {
3853
channel.editorSend({

web-app/src/services/state/actions/invoke.ts

-30
This file was deleted.

web-app/src/services/state/machine.ts

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {Machine, MachineOptions} from 'xstate'
22
import * as CR from 'typings'
33
import actions from './actions'
4-
import * as invoke from './actions/invoke'
54

65
const options: MachineOptions<CR.MachineContext, CR.MachineEvent> = {
76
// @ts-ignore
@@ -65,15 +64,11 @@ export const machine = Machine<CR.MachineContext, CR.MachineStateSchema, CR.Mach
6564
states: {
6665
// TODO: move Initialize into New Tutorial setup
6766
Initialize: {
68-
invoke: {
69-
id: 'loadTutorial',
70-
src: invoke.loadTutorial,
71-
onDone: {
72-
target: 'Summary',
73-
actions: ['initializeTutorial']
74-
},
75-
onError: 'Initialize' // TODO: handle load tutorial error
76-
},
67+
onEntry: ['initializeTutorial'],
68+
on: {
69+
TUTORIAL_CONFIGURED: 'Summary',
70+
// TUTORIAL_CONFIG_ERROR: 'Start' // TODO: should handle error
71+
}
7772
},
7873
LoadNext: {
7974
id: 'tutorial-load-next',

0 commit comments

Comments
 (0)