Skip to content

Instantly share code, notes, and snippets.

@farskid
Created July 4, 2021 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save farskid/78d891ac57e2ff7d4419d0dec8fb88bb to your computer and use it in GitHub Desktop.
Save farskid/78d891ac57e2ff7d4419d0dec8fb88bb to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const editorPanelMachine = Machine({
context: {immediateUpdate: true},
initial: 'booting',
states: {
booting: {},
active: {},
updating: {
entry: send('UPDATE_MACHINE_PRESSED'),
always: 'active',
},
compiling: {
invoke: {
src: (ctx) => {
const uri = ctx.editorRef.Uri.parse(ctx.mainFile);
const compiledJSPromise = ctx
.editorRef.languages.typescript.getTypeScriptWorker()
.then((worker) => worker(uri))
.then((client) => client.getEmitOutput(uri.toString()))
.then((result) => result.outputFiles[0].text);
return compiledJSPromise.then((js) => {
const machines = parseMachines(removeExportsImports(js));
return machines;
});
},
onDone: {
target: 'updating',
actions: [
assign({
machines: (_, e) => e.data,
}),
],
},
onError: {
target: 'active',
actions: [
(_, e) => console.error(e.data),
send((_, e) => ({
type: 'EDITOR_ENCOUNTERED_ERROR',
message: e.data.message,
})),
],
},
},
},
},
on: {
EDITOR_READY: [
{
cond: (ctx) => ctx.immediateUpdate,
actions: [
assign({ editorRef: (_, e) => e.editorRef }),
],
target: 'compiling',
},
{
target: 'active',
actions: assign({ editorRef: (_, e) => e.editorRef }),
},
],
EDITOR_CHANGED_VALUE: {
actions: [assign({ code: (_, e) => e.code })],
},
EDITOR_ENCOUNTERED_ERROR: {
actions: (_, e) => console.error(e.data)
},
UPDATE_MACHINE_PRESSED: {
actions: 'onChange',
},
COMPILE: 'compiling',
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment