Created
November 14, 2019 17:13
-
-
Save balazsbajorics/d50d9c736298ef878a33adf5f16953af to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const bundlerMachine = Machine({ | |
id: 'bundlermachine', | |
type: 'parallel', | |
context: { | |
queuedUpdateFiles: {}, | |
weWantToEmit: false, | |
fileQueuedForProcessing: null, | |
}, | |
states: { | |
worker: { | |
initial: 'uninitialized', | |
states: { | |
uninitialized: { | |
on: { | |
INITIALIZE: { | |
target: 'initializing', | |
}, | |
}, | |
}, | |
initializing: { | |
invoke: { | |
src: 'initilizeBundler', | |
id: 'initializeBundler', | |
onDone: { | |
target: 'idle', | |
}, | |
}, | |
}, | |
idle: { | |
entry: [send('POP')], | |
on: { | |
PUSH: { | |
actions: [send('POP')], | |
}, | |
PROCESS_FILE_FROM_QUEUE: { | |
target: 'processing', | |
actions: [ | |
actions.log((context, event) => { | |
console.log('YEEEHAW', context, event) | |
}), | |
], | |
}, | |
}, | |
}, | |
processing: { | |
invoke: { | |
id: 'sendUpdateFileMessage', | |
src: 'sendUpdateFileMessage', | |
onDone: { | |
target: 'idle', | |
actions: [ | |
assign({ | |
fileQueuedForProcessing: (context, event) => { | |
return null | |
}, | |
}), | |
], | |
}, | |
}, | |
}, | |
}, | |
}, | |
queue: { | |
initial: 'empty', | |
states: { | |
empty: { | |
on: { | |
PUSH: { | |
target: 'pushing', | |
}, | |
}, | |
}, | |
ready: { | |
on: { | |
PUSH: { | |
target: 'pushing', | |
}, | |
POP: { | |
target: 'popping', | |
}, | |
}, | |
}, | |
pushing: { | |
entry: [ | |
assign({ | |
queuedUpdateFiles: (context, event) => { | |
const updatedQueue = { | |
...context.queuedUpdateFiles, | |
[event.payload.fileName]: event.payload.fileContent, | |
} | |
return updatedQueue | |
}, | |
weWantToEmit: (context, event) => event.payload.weWantToEmit, | |
}), | |
], | |
on: { | |
'': [ | |
{ | |
target: 'empty', | |
cond: (context) => Object.keys(context.queuedUpdateFiles).length === 0, | |
}, | |
{ | |
target: 'ready', | |
}, | |
], | |
}, | |
}, | |
popping: { | |
entry: [ | |
assign((context) => { | |
const filenames = Object.keys(context.queuedUpdateFiles) | |
const queueLength = filenames.length | |
const lastOne = queueLength === 1 | |
// Only emit if this is the last queued file and we actually wanted it to emit | |
const emitWithThisFile = context.weWantToEmit && lastOne | |
const emitNextBuild = lastOne ? false : context.weWantToEmit | |
const filename = filenames[0] | |
const content = context.queuedUpdateFiles[filename] | |
const updatedQueue = { | |
...context.queuedUpdateFiles, | |
} | |
delete updatedQueue[filename] | |
return { | |
...context, | |
queuedUpdateFiles: updatedQueue, | |
fileQueuedForProcessing: { | |
fileName: filename, | |
fileContent: content, | |
shouldEmit: emitWithThisFile, | |
}, | |
weWantToEmit: emitNextBuild, | |
} | |
}), | |
send('PROCESS_FILE_FROM_QUEUE'), | |
], | |
on: { | |
'': [ | |
{ | |
target: 'empty', | |
cond: (context) => Object.keys(context.queuedUpdateFiles).length === 0, | |
}, | |
{ | |
target: 'ready', | |
}, | |
], | |
}, | |
}, | |
}, | |
}, | |
}, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment