Created
October 26, 2020 22:55
-
-
Save faneder/b3425868f6638d51626feb8a6ad9eaee 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.pendingQueue) | |
let resolver; | |
const promise = new Promise((resolve) => resolver = resolve) | |
setTimeout(() => resolver(), 200); | |
return promise; | |
} | |
const fetchMachine = Machine( | |
{ | |
id: 'SWAPI', | |
initial: 'idle', | |
context: { | |
queue: [], | |
pendingQueue: [], | |
}, | |
on: { | |
SAVE_SETTING: { | |
actions: (context, event) => context.queue.push(event.amendment), | |
}, | |
}, | |
states: { | |
idle: { | |
after: { | |
200: [ | |
{ | |
target: 'saving', | |
cond: context => context.queue.length > 0, | |
}, | |
{ | |
target: 'idle', | |
}, | |
], | |
}, | |
}, | |
saving: { | |
entry: ['assignPendingQueue', 'clearQueue'], | |
invoke: { | |
id: 'fetchLuke', | |
src: amendDraft, | |
onDone: { | |
actions: [ | |
'clearPendingQueue', | |
// sendParent((_, event) => { | |
// return { | |
// type: 'SETTINGS_SAVED', | |
// data: event.data, | |
// }; | |
// }), | |
], | |
target: 'idle', | |
}, | |
onError: { | |
// actions: sendParent('SAVE_FAILED'), | |
}, | |
}, | |
}, | |
}, | |
}, | |
{ | |
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