|
1 | 1 | import {Action} from 'typings' |
2 | | -import {send as stateMachineSend} from 'xstate' |
3 | 2 |
|
4 | 3 | declare var acquireVsCodeApi: any |
5 | 4 |
|
6 | | -// @ts-ignore |
7 | | -if (!window.acquireVsCodeApi) { |
8 | | - // @ts-ignore |
9 | | - require('./mock') |
| 5 | +interface ReceivedEvent { |
| 6 | + data: Action |
10 | 7 | } |
11 | 8 |
|
12 | | -const channel = acquireVsCodeApi() |
| 9 | +class Channel { |
| 10 | + constructor() { |
| 11 | + // setup mock if browser only |
| 12 | + // @ts-ignore |
| 13 | + if (!window.acquireVsCodeApi) { |
| 14 | + // @ts-ignore |
| 15 | + require('./mock') |
| 16 | + } |
13 | 17 |
|
| 18 | + const editor = acquireVsCodeApi() |
| 19 | + this.editorSend = editor.postMessage |
| 20 | + } |
| 21 | + public machineSend = (action: Action | string) => console.log('machine send') |
| 22 | + public editorSend = (action: Action) => console.log('editor send') |
14 | 23 |
|
15 | | -// Send to Editor |
16 | | -export const send = (action: Action) => { |
17 | | - return channel.postMessage(action) |
18 | | -} |
| 24 | + public setMachineSend(send: any) { |
| 25 | + this.machineSend = send |
| 26 | + } |
| 27 | + public receive(event: ReceivedEvent) { |
| 28 | + const action = event.data |
19 | 29 |
|
20 | | -interface ReceivedEvent { |
21 | | - data: Action |
22 | | -} |
| 30 | + // @ts-ignore // ignore browser events from plugins |
| 31 | + if (action.source) {return } |
23 | 32 |
|
24 | | -// Receive from Editor |
25 | | -export const receive = (event: ReceivedEvent): void => { |
26 | | - |
27 | | - const action = event.data |
28 | | - |
29 | | - // @ts-ignore // ignore browser events from plugins |
30 | | - if (action.source) {return } |
31 | | - |
32 | | - console.log('receive action', action) |
33 | | - // messages from core |
34 | | - switch (action.type) { |
35 | | - case 'TUTORIAL_LOADED': |
36 | | - // send action to state machine |
37 | | - stateMachineSend('TUTORIAL_LOADED') |
38 | | - console.log(stateMachineSend) |
39 | | - console.log('send action to state machine') |
40 | | - return |
41 | | - default: |
42 | | - console.warn(`Unknown received action ${action.type}`, action) |
| 33 | + console.log('receive action', action) |
| 34 | + // messages from core |
| 35 | + switch (action.type) { |
| 36 | + case 'TUTORIAL_LOADED': |
| 37 | + // send action to state machine |
| 38 | + this.machineSend('TUTORIAL_LOADED') |
| 39 | + console.log('send action to state machine') |
| 40 | + return |
| 41 | + default: |
| 42 | + console.warn(`Unknown received action ${action.type}`, action) |
| 43 | + } |
43 | 44 | } |
44 | | - |
45 | | - // if (message.type === 'SET_DATA') { |
46 | | - // // SET_DATA - set state machine context |
47 | | - // console.log('SET_DATA updated') |
48 | | - // const {progress, position} = message.payload |
49 | | - // if (process.env.REACT_APP_DEBUG) { |
50 | | - // console.log(`Position: ${position.levelId}/${position.stageId}/${position.stepId}`) |
51 | | - // // setDebuggerInfo({ progress, position }) |
52 | | - // } |
53 | | - // console.log('set currentTutorial') |
54 | | - // // currentTutorial.set({position, progress}) |
55 | | - |
56 | | - // } |
57 | 45 | } |
58 | 46 |
|
| 47 | +export default new Channel() |
| 48 | + |
| 49 | +// Send to Editor |
| 50 | +// export const send = (action: Action) => { |
| 51 | +// return |
| 52 | +// } |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | +// // Receive from Editor |
| 57 | +// export const receive = (event: ReceivedEvent): void => { |
| 58 | + |
| 59 | + |
| 60 | +// // if (message.type === 'SET_DATA') { |
| 61 | +// // // SET_DATA - set state machine context |
| 62 | +// // console.log('SET_DATA updated') |
| 63 | +// // const {progress, position} = message.payload |
| 64 | +// // if (process.env.REACT_APP_DEBUG) { |
| 65 | +// // console.log(`Position: ${position.levelId}/${position.stageId}/${position.stepId}`) |
| 66 | +// // // setDebuggerInfo({ progress, position }) |
| 67 | +// // } |
| 68 | +// // console.log('set currentTutorial') |
| 69 | +// // // currentTutorial.set({position, progress}) |
| 70 | + |
| 71 | +// // } |
| 72 | +// } |
| 73 | + |
0 commit comments