Skip to content

Commit b482ef8

Browse files
committed
update events as typings
1 parent af502ed commit b482ef8

File tree

12 files changed

+149
-126
lines changed

12 files changed

+149
-126
lines changed

src/channel/index.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import * as T from 'typings'
1+
import * as CR from 'typings'
22
import * as G from 'typings/graphql'
3+
import { EditorEvent, ClientEvent } from 'typings/events'
34
import * as vscode from 'vscode'
45
import saveCommit from '../actions/saveCommit'
56
import setupActions from '../actions/setupActions'
@@ -10,18 +11,18 @@ import logger from '../services/logger'
1011
import Context from './context'
1112

1213
interface Channel {
13-
receive(action: T.Action): Promise<void>
14-
send(action: T.Action): Promise<void>
14+
receive(action: EditorEvent): Promise<void>
15+
send(action: ClientEvent): Promise<void>
1516
}
1617

1718
interface ChannelProps {
18-
postMessage: (action: T.Action) => Thenable<boolean>
19+
postMessage: (action: ClientEvent) => Thenable<boolean>
1920
workspaceState: vscode.Memento
2021
workspaceRoot: vscode.WorkspaceFolder
2122
}
2223

2324
class Channel implements Channel {
24-
private postMessage: (action: T.Action) => Thenable<boolean>
25+
private postMessage: (action: ClientEvent) => Thenable<boolean>
2526
private workspaceState: vscode.Memento
2627
private workspaceRoot: vscode.WorkspaceFolder
2728
private context: Context
@@ -34,10 +35,10 @@ class Channel implements Channel {
3435
}
3536

3637
// receive from webview
37-
public receive = async (action: T.Action) => {
38+
public receive = async (action: EditorEvent) => {
3839
// action may be an object.type or plain string
3940
const actionType: string = typeof action === 'string' ? action : action.type
40-
const onError = (error: T.ErrorMessage) => this.send({ type: 'ERROR', payload: { error } })
41+
const onError = (error: CR.ErrorMessage) => this.send({ type: 'ERROR', payload: { error } })
4142

4243
switch (actionType) {
4344
case 'ENV_GET':
@@ -126,7 +127,7 @@ class Channel implements Channel {
126127
}
127128
}
128129
// send to webview
129-
public send = async (action: T.Action) => {
130+
public send = async (action: ClientEvent) => {
130131
// action may be an object.type or plain string
131132
const actionType: string = typeof action === 'string' ? action : action.type
132133
switch (actionType) {

typings/events.d.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import * as CR from './index'
2+
import * as G from './graphql'
3+
4+
/* --- Editor Events --- */
5+
6+
export type EnvGetEvent = { type: 'ENV_LOAD'; payload: { env: CR.Environment } }
7+
export type EditorTutorialConfigEvent = { type: 'EDITOR_TUTORIAL_CONFIG'; payload: { tutorial: G.Tutorial } }
8+
export type StepActionsEvent = { type: 'SETUP_ACTIONS'; payload: CR.StepActions }
9+
export type SolutionActionsEvent = { type: 'SOLUTION_ACTIONS'; payload: CR.StepActions }
10+
11+
export type EditorEvents =
12+
| EnvGetEvent
13+
| { type: 'EDITOR_TUTORIAL_LOAD' }
14+
| { type: 'TUTORIAL_CLEAR' }
15+
| EditorTutorialConfigEvent
16+
| { type: 'EDITOR_TUTORIAL_CONTINUE_CONFIG' }
17+
| StepActionsEvent
18+
| SolutionActionsEvent
19+
20+
/* --- Client Events --- */
21+
22+
export type EventLoadEvent = { type: 'ENV_LOAD'; payload: { env: CR.Environment } }
23+
export type ErrorEvent = { type: 'ERROR'; payload: { error: string } }
24+
export type CommandStartEvent = { type: 'COMMAND_START'; payload: { process: CR.ProcessEvent } }
25+
export type CommandSuccessEvent = { type: 'COMMAND_SUCCESS'; payload: { process: CR.ProcessEvent } }
26+
export type CommandFailEvent = { type: 'COMMAND_FAIL'; payload: { process: CR.ProcessEvent } }
27+
export type TestPassEvent = { type: 'TEST_PASS'; payload: { stepId: string } }
28+
export type TestFailEvent = { type: 'TEST_FAIL'; payload: { stepId: string } }
29+
export type NextStepEvent = { type: 'NEXT_STEP'; payload: { position: CR.Position } }
30+
export type NextLevelEvent = { type: 'NEXT_LEVEL'; payload: { position: CR.Position } }
31+
export type TestRunningEvent = { type: 'TEST_RUNNING'; payload: { stepId: string } }
32+
export type TestErrorEvent = { type: 'TEST_ERROR'; payload: { stepId: string } }
33+
export type LoadNextStepEvent = { type: 'LOAD_NEXT_STEP'; payload: { step: string } }
34+
export type ContinueTutorialEvent = {
35+
type: 'CONTINUE_TUTORIAL'
36+
payload: { tutorial: G.Tutorial; progress: CR.Progress; position: CR.Position }
37+
}
38+
export type LoadTutorialEvent = { type: 'LOAD_TUTORIAL'; payload: { tutorial: G.Tutorial } }
39+
export type TutorialSelectedEvent = { type: 'TUTORIAL_SELECTED'; payload: { tutorial: G.Tutorial } }
40+
41+
export type AuthenticateEvents = EventLoadEvent | { type: 'AUTHENTICATED' } | ErrorEvent
42+
43+
export type PlayTutorialEvents =
44+
| CommandStartEvent
45+
| CommandSuccessEvent
46+
| CommandFailEvent
47+
| ErrorEvent
48+
| NextStepEvent
49+
| NextLevelEvent
50+
| { type: 'COMPLETED' }
51+
| TestRunningEvent
52+
| { type: 'STEP_SOLUTION_LOAD' }
53+
| TestPassEvent
54+
| TestFailEvent
55+
| TestErrorEvent
56+
| LoadNextStepEvent
57+
| { type: 'LEVEL_COMPLETE' }
58+
| { type: 'EXIT' }
59+
60+
export type SelectTutorialEvents =
61+
| ContinueTutorialEvent
62+
| LoadTutorialEvent
63+
| { type: 'NEW_TUTORIAL' }
64+
| { type: 'BACK' }
65+
| TutorialSelectedEvent
66+
| { type: 'TUTORIAL_CONFIGURED' }
67+
| { type: 'SELECT_NEW_TUTORIAL' }
68+
| { type: 'TUTORIAL_START' }
69+
| ErrorEvent
70+
71+
type ClientEvents = MachineEvent | AuthenticateEvents | SelectTutorialEvents | PlayTutorialEvents

web-app/src/containers/New/TutorialList/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ interface Props {
1010
const TutorialList = (props: Props) => {
1111
const onSelect = (tutorial: G.Tutorial) => {
1212
channel.machineSend({
13-
type: 'SELECT_NEW_TUTORIAL',
13+
type: 'LOAD_TUTORIAL',
1414
payload: {
1515
tutorial,
1616
},

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
import { MachineEvent } from '../state/machine'
2-
import { MachineEvent as AuthenticateEvent } from '../state/authenticate'
3-
import { MachineEvent as SelectTutorialEvent } from '../state/selectTutorial'
4-
import { MachineEvent as PlayTutorialEvent } from '../state/playTutorial'
1+
import * as CR from 'typings'
2+
import * as G from 'typings/graphql'
3+
import { EditorEvent, ClientEvent } from 'typings/events'
54

65
declare let acquireVsCodeApi: any
76

8-
type SendEvent = MachineEvent | AuthenticateEvent | SelectTutorialEvent | PlayTutorialEvent
9-
107
interface ReceivedEvent {
11-
data: SendEvent
8+
data: ClientEvent
129
}
1310

1411
class Channel {
@@ -26,10 +23,10 @@ class Channel {
2623
this.editorSend = editor.postMessage
2724
}
2825

29-
public machineSend = (event: SendEvent) => {
26+
public machineSend = (event: ClientEvent) => {
3027
/* implemented by `setMachineSend` in router on startup */
3128
}
32-
public editorSend = (event: SendEvent) => {
29+
public editorSend = (event: EditorEvent) => {
3330
/* */
3431
}
3532

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import * as CR from 'typings'
22
import * as G from 'typings/graphql'
3+
import { AuthenticateEvent, EnvGetEvent } from 'typings/events'
34
import { assign, ActionFunctionMap } from 'xstate'
45
import client from '../../apollo'
56
import { setAuthToken } from '../../apollo/auth'
67
import authenticateMutation from '../../apollo/mutations/authenticate'
78
import channel from '../../channel'
89
import onError from '../../../services/sentry/onError'
9-
import { MachineContext, MachineEvent } from './index'
10+
import { MachineContext } from './index'
1011

1112
interface AuthenticateData {
1213
editorLogin: {
@@ -21,10 +22,10 @@ interface AuthenticateVariables {
2122
editor: 'VSCODE'
2223
}
2324

24-
const actions: ActionFunctionMap<MachineContext, MachineEvent> = {
25+
const actions: ActionFunctionMap<MachineContext, AuthenticateEvent> = {
2526
// @ts-ignore
2627
setEnv: assign({
27-
env: (context: MachineContext, event: { type: 'ENV_LOAD'; payload: { env: CR.Environment } }): CR.Environment => ({
28+
env: (context: MachineContext, event: EnvGetEvent): CR.Environment => ({
2829
...context.env,
2930
...event.payload.env,
3031
}),
@@ -55,7 +56,7 @@ const actions: ActionFunctionMap<MachineContext, MachineEvent> = {
5556
description: error.message,
5657
}
5758
}
58-
channel.receive({ data: { type: 'ERROR', payload: { error: message } } })
59+
channel.receive({ data: ErrorEvent })
5960
return
6061
})
6162

web-app/src/services/state/authenticate/index.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import * as CR from 'typings'
2+
import { AuthenticateEvent } from 'typings/events'
23
import { Machine } from 'xstate'
34
import actions from './actions'
45

5-
export type MachineEvent =
6-
| { type: 'ENV_LOAD'; payload: { env: CR.Environment } }
7-
| { type: 'AUTHENTICATED' }
8-
| { type: 'ERROR'; payload: { error: Error } }
9-
106
export type StateSchema = {
117
states: {
128
LoadEnvironment: {}
@@ -24,7 +20,7 @@ const options = {
2420
actions,
2521
}
2622

27-
export const authenticateMachine = Machine<MachineContext, StateSchema, MachineEvent>(
23+
export const authenticateMachine = Machine<MachineContext, StateSchema, AuthenticateEvent>(
2824
{
2925
id: 'authenticate',
3026
context: {

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ import { authenticateMachine } from './authenticate'
55
import { selectTutorialMachine } from './selectTutorial'
66
import { playTutorialMachine } from './playTutorial'
77

8-
export type MachineEvent = {
9-
type: 'NONE'
10-
}
11-
128
export type MachineContext = {
139
env: CR.Environment
1410
error: CR.ErrorMessage | null
@@ -25,7 +21,7 @@ export type MachineStateSchema = {
2521
}
2622
}
2723

28-
export const machine = Machine<MachineContext, MachineStateSchema, MachineEvent>({
24+
export const machine = Machine<MachineContext, MachineStateSchema>({
2925
id: 'root',
3026
initial: 'Initializing',
3127
context: {

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

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import * as CR from 'typings'
22
import * as G from 'typings/graphql'
3+
import * as Event from 'typings/events'
34
import { assign, send, ActionFunctionMap } from 'xstate'
4-
import { MachineContext, MachineEvent } from './index'
5+
import { MachineContext } from './index'
56
import channel from '../../channel'
67
import onError from '../../sentry/onError'
78
import * as selectors from '../../selectors'
89

9-
const actions: ActionFunctionMap<MachineContext, MachineEvent> = {
10+
const actions: ActionFunctionMap<MachineContext, Event.PlayTutorialEvents> = {
1011
userTutorialComplete(context: MachineContext) {
1112
console.log('should update user tutorial as complete')
1213
},
@@ -29,27 +30,21 @@ const actions: ActionFunctionMap<MachineContext, MachineEvent> = {
2930
}),
3031
// @ts-ignore
3132
commandSuccess: assign({
32-
processes: (
33-
{ processes }: MachineContext,
34-
event: { type: 'COMMAND_SUCCESS'; payload: { process: CR.ProcessEvent } },
35-
): CR.ProcessEvent[] => {
33+
processes: ({ processes }: MachineContext, event: Event.CommandSuccessEvent): CR.ProcessEvent[] => {
3634
const { process } = event.payload
3735
return processes.filter(p => p.title !== process.title)
3836
},
3937
}),
4038
// @ts-ignore
4139
commandFail: assign({
42-
processes: (
43-
{ processes }: MachineContext,
44-
event: { type: 'COMMAND_FAIL'; payload: { process: CR.ProcessEvent } },
45-
): CR.ProcessEvent[] => {
40+
processes: ({ processes }: MachineContext, event: Event.CommandFailEvent): CR.ProcessEvent[] => {
4641
const { process } = event.payload
4742
return processes.filter(p => p.title !== process.title)
4843
},
4944
}),
5045
// @ts-ignore
5146
updateStepPosition: assign({
52-
position: (context: MachineContext, event: MachineEvent): CR.Position => {
47+
position: (context: MachineContext, event: Event.PlayTutorialEvents): CR.Position => {
5348
// TODO calculate from progress
5449

5550
const { position } = context
@@ -97,7 +92,7 @@ const actions: ActionFunctionMap<MachineContext, MachineEvent> = {
9792
}),
9893
// @ts-ignore
9994
updateLevelProgress: assign({
100-
progress: (context: MachineContext, event: MachineEvent): CR.Progress => {
95+
progress: (context: MachineContext, event: Event.PlayTutorialEvents): CR.Progress => {
10196
// update progress by tracking completed
10297
const { progress, position } = context
10398

@@ -110,7 +105,7 @@ const actions: ActionFunctionMap<MachineContext, MachineEvent> = {
110105
}),
111106
// @ts-ignore
112107
updateStepProgress: assign({
113-
progress: (context: MachineContext, event: { type: 'TEST_PASS'; payload: { stepId: string } }): CR.Progress => {
108+
progress: (context: MachineContext, event: Event.TestPassEvent): CR.Progress => {
114109
// update progress by tracking completed
115110
const currentProgress: CR.Progress = context.progress
116111

@@ -123,12 +118,7 @@ const actions: ActionFunctionMap<MachineContext, MachineEvent> = {
123118
}),
124119
// @ts-ignore
125120
updatePosition: assign({
126-
position: (
127-
context: MachineContext,
128-
event:
129-
| { type: 'NEXT_STEP'; payload: { position: CR.Position } }
130-
| { type: 'NEXT_LEVEL'; payload: { position: CR.Position } },
131-
): CR.Progress => {
121+
position: (context: MachineContext, event: Event.NextStepEvent | Event.NextLevelEvent): CR.Position => {
132122
const { position } = event.payload
133123
return position
134124
},
@@ -210,7 +200,7 @@ const actions: ActionFunctionMap<MachineContext, MachineEvent> = {
210200
),
211201
// @ts-ignore
212202
setError: assign({
213-
error: (context: MachineContext, event: { type: 'ERROR'; payload: { error: string } }): string | null => {
203+
error: (context: MachineContext, event: Event.ErrorEvent): string | null => {
214204
return event.payload.error
215205
},
216206
}),

web-app/src/services/state/playTutorial/index.ts

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as CR from 'typings'
22
import * as G from 'typings/graphql'
3+
import { PlayTutorialEvents } from 'typings/events'
34
import { Machine, MachineOptions } from 'xstate'
45
import actions from './actions'
56

@@ -23,23 +24,6 @@ export type StateSchema = {
2324
}
2425
}
2526

26-
export type MachineEvent =
27-
| { type: 'COMMAND_START'; payload: { process: CR.ProcessEvent } }
28-
| { type: 'COMMAND_SUCCESS'; payload: { process: CR.ProcessEvent } }
29-
| { type: 'COMMAND_FAIL'; payload: { process: CR.ProcessEvent } }
30-
| { type: 'ERROR'; payload: { error: string } }
31-
| { type: 'NEXT_STEP'; payload: { position: CR.Position } }
32-
| { type: 'NEXT_LEVEL'; payload: { position: CR.Position } }
33-
| { type: 'COMPLETED' }
34-
| { type: 'TEST_RUNNING'; payload: { stepId: string } }
35-
| { type: 'STEP_SOLUTION_LOAD' }
36-
| { type: 'TEST_PASS'; payload: { stepId: string } }
37-
| { type: 'TEST_FAIL'; payload: { stepId: string } }
38-
| { type: 'TEST_ERROR'; payload: { stepId: string } }
39-
| { type: 'LOAD_NEXT_STEP'; payload: { step: string } }
40-
| { type: 'LEVEL_COMPLETE' }
41-
| { type: 'EXIT' }
42-
4327
export type MachineContext = {
4428
error: CR.ErrorMessage | null
4529
env: CR.Environment
@@ -49,15 +33,15 @@ export type MachineContext = {
4933
processes: CR.ProcessEvent[]
5034
}
5135

52-
const options: MachineOptions<MachineContext, MachineEvent> = {
36+
const options: MachineOptions<MachineContext, PlayTutorialEvents> = {
5337
activities: {},
5438
actions,
5539
guards: {},
5640
services: {},
5741
delays: {},
5842
}
5943

60-
export const playTutorialMachine = Machine<MachineContext, StateSchema, MachineEvent>(
44+
export const playTutorialMachine = Machine<MachineContext, StateSchema, PlayTutorialEvents>(
6145
{
6246
context: {
6347
error: null,

0 commit comments

Comments
 (0)