Skip to content

Instantly share code, notes, and snippets.

@kalda341
Created November 12, 2019 21:13
Show Gist options
  • Save kalda341/c5c1d30396282185a53b6922e5587325 to your computer and use it in GitHub Desktop.
Save kalda341/c5c1d30396282185a53b6922e5587325 to your computer and use it in GitHub Desktop.
const saveTask = useTask(function*() {
yield timeout(1000);
yield doSaveTask.perform();
}, 'RESTARTABLE');
const doSaveTask = useTask(function*() {
// Ensure that we ALWAYS have the most recent project state
const currentState = yield getMostRecentProjectState();
const { revision } = yield updateProject(authHeaders, currentState);
yield _setProjectState(assoc('revision', revision));
// We should never have multiple of these running at the same time
}, 'ENQUEUE');
const setProjectState = newData => {
_setProjectState(assoc('data', newData));
saveTask.perform();
};
const getMostRecentProjectState = () =>
new Promise(resolve => _setProjectState(tap(resolve)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment