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

Commite88651e

Browse files
committed
setup xstate with interpreter
1 parentcc176e6 commite88651e

File tree

14 files changed

+97
-85
lines changed

14 files changed

+97
-85
lines changed

‎.vscode/settings.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"source.fixAll":true,
1717
},
1818
"tslint.enable":true,
19-
"tslint.jsEnable":true,
2019
"[javascript]": {
2120
"editor.formatOnSave":true
2221
},

‎package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,21 @@
1111
],
1212
"publisher":"Shawn McKay <shawn.j.mckay@gmail.com>",
1313
"activationEvents": [
14-
"onCommand:coderoad.tutorial_load"
14+
"onCommand:coderoad.start"
1515
],
1616
"main":"./out/extension.js",
1717
"contributes": {
1818
"commands": [
1919
{
20-
"command":"coderoad.tutorial_load",
21-
"title":"Load Tutorial",
20+
"command":"coderoad.start",
21+
"title":"Start",
2222
"category":"CodeRoad"
2323
}
2424
]
2525
},
2626
"scripts": {
2727
"vscode:prepublish":"npm run compile",
28+
"machine":"node ./out/state/index.js",
2829
"compile":"tsc -p ./",
2930
"watch":"tsc -watch -p ./",
3031
"postinstall":"node ./node_modules/vscode/bin/install",

‎src/editor/commands/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import*asvscodefrom'vscode'
22

33
// import runTest from './runTest'
4-
importtutorialLoadfrom'./tutorialLoad'
4+
importstartfrom'./start'
55
// import loadSolution from './loadSolution'
66
// import quit from './quit'
77

88
constCOMMANDS={
99
// TUTORIAL_SETUP: 'coderoad.tutorial_setup',
10-
TUTORIAL_LOAD:'coderoad.tutorial_load',
10+
START:'coderoad.start',
1111
// RUN_TEST: 'coderoad.test_run',
1212
// LOAD_SOLUTION: 'coderoad.solution_load',
1313
// QUIT: 'coderoad.quit',
1414
}
1515

1616
exportdefault(context:vscode.ExtensionContext):void=>{
1717
constcommands={
18-
[COMMANDS.TUTORIAL_LOAD]():void{
19-
tutorialLoad(context)
18+
[COMMANDS.START]():void{
19+
console.log('TUTORIAL_START')
20+
start(context)
2021
},
2122
// [COMMANDS.RUN_TEST]: runTest,
2223
// [COMMANDS.LOAD_SOLUTION]: loadSolution,

‎src/editor/commands/tutorialLoad.tsrenamed to‎src/editor/commands/start.ts

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
import*asvscodefrom'vscode'
22
import*asCRfrom'typings'
33

4-
importapifrom'../../services/api'
54
importtutorialSetupfrom'../../services/tutorialSetup'
6-
import{loadProgressPosition}from'../../services/position'
7-
import*asstoragefrom'../../services/storage'
8-
importrootSetupfrom'../../services/rootSetup'
95
import{isEmptyWorkspace,openReadme}from'../workspace'
10-
import*asgitfrom'../../services/git'
6+
import{setWorkspaceRoot}from'../../services/node'
7+
import{setStorage}from'../../editor/storage'
8+
importcreateStateMachinefrom'../../state'
119

1210
/*
1311
new
1412
if current workspace is empty, use it
1513
if not, open a new folder then start
1614
*/
1715

18-
asyncfunctioncontinueTutorial(){
19-
// TODO: verify that tutorial is loaded in workspace
20-
// TODO: verify progress
21-
// TODO: verify setup
22-
awaitloadProgressPosition()
23-
awaitopenReadme()
24-
}
16+
//async function continueTutorial() {
17+
// // TODO: verify that tutorial is loaded in workspace
18+
// // TODO: verify progress
19+
// // TODO: verify setup
20+
// await loadProgressPosition()
21+
// await openReadme()
22+
//}
2523

2624
asyncfunctionnewTutorial(tutorial:CR.Tutorial){
2725
// if workspace isn't empty, clear it out if given permission
@@ -40,23 +38,15 @@ async function newTutorial(tutorial: CR.Tutorial) {
4038
}
4139

4240

43-
asyncfunctionvalidateCanContinue():Promise<boolean>{
44-
// validate tutorial & progress found in local storage
45-
// validate git is setup with a remote
46-
const[tutorial,progress,hasGit,hasGitRemote]=awaitPromise.all([
47-
storage.getTutorial(),
48-
storage.getProgress(),
49-
git.gitVersion(),
50-
git.gitCheckRemoteExists(),
51-
])
52-
return!!(tutorial&&progress&&hasGit&&hasGitRemote)
53-
}
54-
55-
exportdefaultasyncfunctiontutorialLoad(context:vscode.ExtensionContext):Promise<void>{
56-
console.log(`tutorialLoad${JSON.stringify(context)}`)
41+
exportdefaultasyncfunctionstart(context:vscode.ExtensionContext):Promise<void>{
42+
console.log('start',context)
5743

5844
// setup connection to workspace
59-
awaitrootSetup(context)
45+
awaitsetWorkspaceRoot()
46+
// set workspace context path
47+
awaitsetStorage(context.workspaceState)
48+
// initiate the state machine
49+
createStateMachine()
6050
return;
6151

6252
// const modes = ['New']

‎src/editor/index.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

‎src/editor/init.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// The module 'vscode' contains the VS Code extensibility API
2+
// Import the module and reference it with the alias vscode in your code below
3+
import*asvscodefrom'vscode'
4+
5+
importcreateCommandsfrom'./commands'
6+
importcreateViewsfrom'./views'
7+
importcreateStateMachinefrom'../state'
8+
9+
// this method is called when your extension is activated
10+
// your extension is activated the very first time the command is executed
11+
exportfunctionactivate(context:vscode.ExtensionContext){
12+
console.log('ACTIVATE!')
13+
14+
// commands
15+
createCommands(context)
16+
17+
// tasks
18+
// add tasks here
19+
20+
// views
21+
createViews(context)
22+
23+
24+
}
25+
26+
// this method is called when your extension is deactivated
27+
exportfunctiondeactivate(context:vscode.ExtensionContext):void{
28+
// cleanup subscriptions/tasks
29+
console.log('deactivate context',context)
30+
for(constdisposableofcontext.subscriptions){
31+
disposable.dispose()
32+
}
33+
}
File renamed without changes.
File renamed without changes.

‎src/extension.ts

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,2 @@
1-
// The module 'vscode' contains the VS Code extensibility API
2-
// Import the module and reference it with the alias vscode in your code below
3-
import*asvscodefrom'vscode'
1+
export{activate,deactivate}from'./editor/init'
42

5-
importcreateCommandsfrom'./editor/commands'
6-
importcreateViewsfrom'./views'
7-
8-
// this method is called when your extension is activated
9-
// your extension is activated the very first time the command is executed
10-
exportfunctionactivate(context:vscode.ExtensionContext){
11-
console.log('ACTIVATE!')
12-
13-
// commands
14-
createCommands(context)
15-
16-
// tasks
17-
// add tasks here
18-
19-
// views
20-
createViews(context)
21-
}
22-
23-
// this method is called when your extension is deactivated
24-
exportfunctiondeactivate(context:vscode.ExtensionContext):void{
25-
// cleanup subscriptions/tasks
26-
console.log('deactivate context',context)
27-
for(constdisposableofcontext.subscriptions){
28-
disposable.dispose()
29-
}
30-
}

‎src/services/rootSetup.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

‎src/state/actions/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ let initialProgress: CR.Progress = {
1313

1414
exportdefault{
1515
start:async()=>{
16+
console.log('ACTION: start')
1617
// verify that the user has a tutorial & progress
1718
// verify git is setup with a coderoad remote
1819
const[tutorial,progress,hasGit,hasGitRemote]=awaitPromise.all([
@@ -31,16 +32,19 @@ export default {
3132
tutorialLoad:assign({
3233
// load initial data, progress & position
3334
data():CR.TutorialData{
35+
console.log('ACTION: tutorialLoad.data')
3436
if(!initialTutorial){
3537
thrownewError('No Tutorial loaded')
3638
}
3739
returninitialTutorial.data
3840

3941
},
4042
progress():CR.Progress{
43+
console.log('ACTION: tutorialLoad.progress')
4144
returninitialProgress
4245
},
4346
position(){
47+
console.log('ACTION: tutorialLoad.position')
4448
if(!initialTutorial){
4549
thrownewError('No Tutorial loaded')
4650
}

‎src/state/guards/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as CR from 'typings'
33
exportdefault{
44
// skip to the stage if the level has already been started
55
hasNoNextLevelProgress:(context:CR.MachineContext):boolean=>{
6+
console.log('GUARD: hasNoNextLevelProgress')
67
returnfalse
78
},
89
}

‎src/state/index.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import{interpret}from'xstate'
2+
importmachinefrom'./machine'
3+
4+
5+
constcreateStateMachine=()=>{
6+
constmachineOptions={
7+
logger:console.log,
8+
devTools:true,
9+
deferEvents:true,
10+
execute:true
11+
}
12+
// machine interpreter
13+
// https://xstate.js.org/docs/guides/interpretation.html
14+
constservice=interpret(machine,machineOptions)
15+
// logging
16+
.onTransition(state=>{
17+
console.log('state',state)
18+
if(state.changed){
19+
console.log('transition')
20+
console.log(state.value)
21+
}
22+
})
23+
// initialize
24+
service.start()
25+
returnservice
26+
}
27+
28+
exportdefaultcreateStateMachine

‎src/state/machine.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ import actions from './actions'
55
importguardsfrom'./guards'
66
importinitialContextfrom'./context'
77

8-
// TODO: replace with API
9-
10-
exportconsttutorialMachine=Machine<
8+
exportconstmachine=Machine<
119
CR.MachineContext,
1210
CR.MachineStateSchema,
1311
CR.MachineEvent
@@ -18,6 +16,7 @@ export const tutorialMachine = Machine<
1816
initial:'Start',
1917
states:{
2018
Start:{
19+
initial:'Initial',
2120
states:{
2221
Initial:{
2322
onEntry:'start',
@@ -157,3 +156,5 @@ export const tutorialMachine = Machine<
157156
activities:{},
158157
},
159158
)
159+
160+
exportdefaultmachine

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp