Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit97b878a

Browse files
committed
initialize state machine with tutorial model
1 parent0dd047f commit97b878a

File tree

4 files changed

+83
-60
lines changed

4 files changed

+83
-60
lines changed

‎src/extension.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import * as vscode from 'vscode'
2-
import { setWorkspaceRoot } from './services/node'
2+
import {setWorkspaceRoot} from './services/node'
3+
import Tutorial from './services/tutorial'
34
import StateMachine from './state'
45
import Editor from './editor'
56

7+
export const tutorial = new Tutorial()
68
// state machine that governs application logic
7-
export const machine = new StateMachine({dispatch: vscode.commands.executeCommand})
9+
export const machine = new StateMachine({dispatch: vscode.commands.executeCommand, tutorial})
810

911
// vscode editor
1012
export const editor = new Editor({
11-
machine,
12-
setWorkspaceRoot,
13+
machine,
14+
setWorkspaceRoot,
1315
})
1416

1517
// activate run on vscode extension initialization

‎src/services/tutorial/index.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,34 @@
11
import * as G from 'typings/graphql'
22
import * as CR from 'typings'
33

4+
interface TutorialConfig {
5+
codingLanguage: G.EnumCodingLanguage
6+
testRunner: G.EnumTestRunner
7+
}
8+
49
class Tutorial {
510
public repo: G.TutorialRepo
6-
public config:{codingLanguage: G.EnumCodingLanguage, testRunner: G.EnumTestRunner}
11+
public config:TutorialConfig
712
private version: G.TutorialVersion
813
private position: CR.Position
914
private progress: CR.Progress
1015

11-
constructor(tutorial: G.Tutorial) {
16+
constructor() {
17+
// initialize types, will be assigned when tutorial is selected
18+
this.repo = {} as G.TutorialRepo
19+
this.config = {} as TutorialConfig
20+
this.version = {} as G.TutorialVersion
21+
this.position = {} as CR.Position
22+
this.progress = {} as CR.Progress
23+
}
24+
25+
public init = (tutorial: G.Tutorial) => {
1226
this.repo = tutorial.repo
1327
this.config = {
1428
codingLanguage: tutorial.codingLanguage,
1529
testRunner: tutorial.testRunner,
1630
}
17-
//TODO: allow specificversion, currently defaults to latest
31+
// version containing level data
1832
this.version = tutorial.version
1933
// set initial position
2034
this.position = {
@@ -29,9 +43,11 @@ class Tutorial {
2943
steps: {},
3044
complete: false,
3145
}
46+
47+
// set position, progress, tutorial locally
3248
}
33-
public load = () => {
34-
//
49+
public load = (tutorial: G.Tutorial) => {
50+
//TODO: load from localStorage
3551
}
3652
public level = (levelId: string): G.Level | null => {
3753
const level: G.Level | undefined = this.version.levels.find((l: G.Level) => l.id === levelId)

‎src/state/index.ts

Lines changed: 54 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { interpret, Interpreter } from 'xstate'
1+
import {interpret, Interpreter} from 'xstate'
2+
import Tutorial from '../services/tutorial'
23
import * as CR from 'typings'
34
import createMachine from './machine'
45

@@ -7,61 +8,64 @@ import createMachine from './machine'
78

89
// convert state into a readable string
910
const stateToString = (state: string | object, str: string = ''): string => {
10-
if (typeof state === 'object') {
11-
const keys = Object.keys(state)
12-
if (keys && keys.length) {
13-
const key = keys[0]
14-
return stateToString(state[key], str.length ? `${str}.${key}` : key)
15-
}
16-
return str
17-
} else if (typeof state === 'string') {
18-
return state
19-
}
20-
return ''
11+
if (typeof state === 'object') {
12+
const keys = Object.keys(state)
13+
if (keys && keys.length) {
14+
const key = keys[0]
15+
return stateToString(state[key], str.length ? `${str}.${key}` : key)
16+
}
17+
return str
18+
} else if (typeof state === 'string') {
19+
return state
20+
}
21+
return ''
2122
}
2223

2324
interface Props {
24-
dispatch: CR.EditorDispatch
25+
dispatch: CR.EditorDispatch
26+
tutorial: Tutorial
2527
}
2628

2729
class StateMachine {
28-
private dispatch: CR.EditorDispatch
29-
private machineOptions = {
30-
devTools: true,
31-
deferEvents: true,
32-
execute: true,
33-
}
34-
private service: Interpreter<CR.MachineContext, CR.MachineStateSchema, CR.MachineEvent>
35-
constructor({ dispatch }: Props) {
36-
this.dispatch = dispatch
37-
const machine = createMachine(dispatch)
38-
this.service = interpret(machine, this.machineOptions)
39-
// logging
40-
.onTransition(state => {
41-
if (state.changed) {
42-
console.log(`STATE: ${stateToString(state.value)}`)
43-
dispatch('coderoad.send_state', { state: state.value, data: state.context })
44-
} else {
45-
dispatch('coderoad.send_data', { data: state.context })
46-
}
47-
})
48-
}
49-
public activate() {
50-
// initialize
51-
this.service.start()
52-
}
53-
public deactivate() {
54-
this.service.stop()
55-
}
56-
public refresh() {
57-
console.log('service refresh')
58-
console.log(this.service.state)
59-
const { value, context } = this.service.state
60-
this.dispatch('coderoad.send_state', { state: value, data: context })
61-
}
62-
public send(action: string | CR.Action) {
63-
this.service.send(action)
64-
}
30+
private dispatch: CR.EditorDispatch
31+
private tutorial: Tutorial
32+
private machineOptions = {
33+
devTools: true,
34+
deferEvents: true,
35+
execute: true,
36+
}
37+
private service: Interpreter<CR.MachineContext, CR.MachineStateSchema, CR.MachineEvent>
38+
constructor({dispatch, tutorial}: Props) {
39+
this.dispatch = dispatch
40+
this.tutorial = tutorial
41+
const machine = createMachine(dispatch)
42+
this.service = interpret(machine, this.machineOptions)
43+
// logging
44+
.onTransition(state => {
45+
if (state.changed) {
46+
console.log(`STATE: ${stateToString(state.value)}`)
47+
dispatch('coderoad.send_state', {state: state.value, data: state.context})
48+
} else {
49+
dispatch('coderoad.send_data', {data: state.context})
50+
}
51+
})
52+
}
53+
public activate() {
54+
// initialize
55+
this.service.start()
56+
}
57+
public deactivate() {
58+
this.service.stop()
59+
}
60+
public refresh() {
61+
console.log('service refresh')
62+
console.log(this.service.state)
63+
const {value, context} = this.service.state
64+
this.dispatch('coderoad.send_state', {state: value, data: context})
65+
}
66+
public send(action: string | CR.Action) {
67+
this.service.send(action)
68+
}
6569
}
6670

6771
export default StateMachine

‎tslint.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"triple-equals": true,
1515
"forin": false,
1616
"no-console": false,
17-
"no-submodule-imports": false
17+
"no-submodule-imports": false,
18+
"no-object-literal-type-assertion": false
1819
},
1920
"defaultSeverity": "warning",
2021

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp