Last active
October 26, 2020 23:13
-
-
Save faneder/86df7fb4a1704be708bf05a41b8a5e3a to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or 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 amendDraft = (context) => { | |
console.log("saving", context) | |
let resolver; | |
const promise = new Promise((resolve) => resolver = resolve) | |
setTimeout(() => resolver(), 200); | |
return promise; | |
} | |
Machine( | |
{ | |
id: 'SWAPI', | |
initial: 'idle', | |
context: { | |
queue: [], | |
pendingQueue: [], | |
}, | |
on: { | |
SAVE_SETTING: { | |
actions: (context, event) => context.queue.push(1), | |
}, | |
}, | |
states: { | |
idle: { | |
on: { | |
SAVE_SETTING: { | |
target: 'waiting', | |
// actions: (context, event) => context.queue.push(2), | |
}, | |
}, | |
}, | |
waiting: { | |
after: { | |
200: { | |
target: 'saving', | |
}, | |
}, | |
}, | |
saving: { | |
entry: ['assignPendingQueue', 'clearQueue'], | |
invoke: { | |
id: 'fetchLuke', | |
src: amendDraft, | |
onDone: [ | |
{ | |
target: 'waiting', | |
cond: context => context.queue.length > 0, | |
}, | |
{ | |
target: 'idle', | |
// actions: send((_, event) => { | |
// return { | |
// type: 'SETTINGS_SAVED', | |
// data: event.data, | |
// }; | |
// }), | |
}, | |
], | |
onError: { | |
actions: sendParent('SAVE_FAILED'), | |
}, | |
}, | |
exit: 'clearPendingQueue', | |
}, | |
}, | |
}, | |
{ | |
actions: { | |
assignPendingQueue: assign({ | |
pendingQueue: context => context.queue, | |
}), | |
clearQueue: assign({ | |
queue: _ => [], | |
}), | |
clearPendingQueue: assign({ | |
pendingQueue: _ => [], | |
}), | |
}, | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment