Skip to content

Instantly share code, notes, and snippets.

@superhawk610
Created September 3, 2018 06:18
Show Gist options
  • Save superhawk610/aee28c50dc2233d26aa0ccddd52fe1b9 to your computer and use it in GitHub Desktop.
Save superhawk610/aee28c50dc2233d26aa0ccddd52fe1b9 to your computer and use it in GitHub Desktop.
Example implementation for extending queue.reducer (Guppy)
/* ... */
case QUEUE_MODIFY_PROJECT: {
const { projectId, settings } = action;
return produce(state, draftState => {
// get existing project queue, or create it if this
// is the first entry
const projectQueue = draftState[projectId] || [];
// get existing modification queue for this project, or
// create it if it doesn't exist
let modificationQueue = projectQueue.find(
q => q.action === 'modify' && !q.active
);
if (!modificationQueue) {
modificationQueue = {
action: 'modify',
active: false,
settings: {},
};
projectQueue.push(modificationQueue);
}
// maintain existing settings and override duplicate
// keys with the newer values
modificationQueue.settings = {
...modificationQueue.settings,
...settings,
};
// update the project's modify queue
draftState[projectId] = projectQueue;
});
}
case INSTALL_DEPENDENCIES_START:
case UNINSTALL_DEPENDENCIES_START:
case MODIFY_PROJECT_START: {
const { projectId } = action;
return produce(state, draftState => {
// mark the next item in the queue as active
draftState[projectId][0].active = true;
});
}
case INSTALL_DEPENDENCIES_ERROR:
case INSTALL_DEPENDENCIES_FINISH:
case UNINSTALL_DEPENDENCIES_ERROR:
case UNINSTALL_DEPENDENCIES_FINISH:
case MODIFY_PROJECT_ERROR:
case MODIFY_PROJECT_FINISH: {
const { projectId } = action;
return produce(state, draftState => {
// remove oldest item in queue (it's just been
// completed by the dependency saga)
draftState[projectId].shift();
// remove the project's queue if this was the
// last entry
if (draftState[projectId].length === 0) {
delete draftState[projectId];
}
});
}
/* ... */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment