|
1 | 1 | import * as React from 'react'
|
2 |
| -import * as CR from 'typings' |
3 |
| -import channel from '../../services/channel' |
4 |
| -import messageBusReceiver from '../../services/channel/receiver' |
5 |
| -import machine from '../../services/state/machine' |
| 2 | +import { createMachine } from '../../services/state/machine' |
6 | 3 | import { useMachine } from '../../services/xstate-react'
|
7 |
| -import debuggerWrapper from '../Debugger/debuggerWrapper' |
8 | 4 | import Route from './Route'
|
9 | 5 | import onError from '../../services/sentry/onError'
|
10 | 6 |
|
11 | 7 | interface Props {
|
12 | 8 | children: any
|
13 | 9 | }
|
14 | 10 |
|
15 |
| -interface CloneElementProps { |
16 |
| - context: CR.MachineContext |
17 |
| - send(action: CR.Action): void |
18 |
| -} |
| 11 | +declare let acquireVsCodeApi: any |
19 | 12 |
|
20 |
| -// router finds first state match of <Route path='' /> |
21 |
| -const Router = ({ children }: Props): React.ReactElement<CloneElementProps> | null => { |
22 |
| - const [state, send] = useMachine(machine, { |
23 |
| - interpreterOptions: { |
24 |
| - logger: console.log.bind('XSTATE:'), |
25 |
| - }, |
26 |
| - }) |
| 13 | +const editor = acquireVsCodeApi() |
27 | 14 |
|
28 |
| - channel.setMachineSend(send) |
| 15 | +// router finds first state match of <Route path='' /> |
| 16 | +const useRouter = () => { |
| 17 | + const [state, send] = useMachine(createMachine({ editorSend: editor.postMessage })) |
29 | 18 |
|
30 | 19 | // event bus listener
|
31 |
| - React.useEffect(messageBusReceiver, []) |
32 |
| - |
33 |
| - const childArray = React.Children.toArray(children) |
34 |
| - for (const child of childArray) { |
35 |
| - const { path } = child.props |
36 |
| - let pathMatch |
37 |
| - if (typeof path === 'string') { |
38 |
| - pathMatch = state.matches(path) |
39 |
| - } else if (Array.isArray(path)) { |
40 |
| - pathMatch = path.some(p => state.matches(p)) |
41 |
| - } else { |
42 |
| - throw new Error(`Invalid route path ${JSON.stringify(path)}`) |
| 20 | + React.useEffect(() => { |
| 21 | + const listener = 'message' |
| 22 | + // propograte channel event to state machine |
| 23 | + const handler = (action: any) => { |
| 24 | + // NOTE: must call event.data, cannot destructure. VSCode acts odd |
| 25 | + const event = action.data |
| 26 | + // ignore browser events from plugins |
| 27 | + if (event.source) { |
| 28 | + return |
| 29 | + } |
| 30 | + send(event) |
| 31 | + } |
| 32 | + window.addEventListener(listener, handler) |
| 33 | + return () => { |
| 34 | + window.removeEventListener(listener, handler) |
43 | 35 | }
|
44 |
| - if (pathMatch) { |
45 |
| - const element = React.cloneElement<CloneElementProps>(child.props.children, { send, context: state.context }) |
46 |
| - return debuggerWrapper(element, state) |
| 36 | + }, []) |
| 37 | + |
| 38 | + const Router = ({ children }: Props): React.ReactElement<any> | null => { |
| 39 | + const childArray = React.Children.toArray(children) |
| 40 | + for (const child of childArray) { |
| 41 | + // match path |
| 42 | + const { path } = child.props |
| 43 | + let pathMatch |
| 44 | + if (typeof path === 'string') { |
| 45 | + pathMatch = state.matches(path) |
| 46 | + } else if (Array.isArray(path)) { |
| 47 | + pathMatch = path.some(p => state.matches(p)) |
| 48 | + } else { |
| 49 | + throw new Error(`Invalid route path ${JSON.stringify(path)}`) |
| 50 | + } |
| 51 | + if (pathMatch) { |
| 52 | + // @ts-ignore |
| 53 | + return child.props.children |
| 54 | + } |
47 | 55 | }
|
| 56 | + const message = `No Route matches for ${JSON.stringify(state)}` |
| 57 | + onError(new Error(message)) |
| 58 | + console.warn(message) |
| 59 | + return null |
48 | 60 | }
|
49 |
| - const message = `No Route matches for ${JSON.stringify(state)}` |
50 |
| - onError(new Error(message)) |
51 |
| - console.warn(message) |
52 |
| - return null |
53 |
| -} |
54 | 61 |
|
55 |
| -Router.Route = Route |
| 62 | + return { |
| 63 | + context: state.context, |
| 64 | + send, |
| 65 | + Router, |
| 66 | + Route, |
| 67 | + } |
| 68 | +} |
56 | 69 |
|
57 |
| -export default Router |
| 70 | +export default useRouter |
0 commit comments