Skip to content

Instantly share code, notes, and snippets.

@rosschapman
Last active April 25, 2020 02:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rosschapman/23f96b7ed9e0768bf1aa787af29f9cf2 to your computer and use it in GitHub Desktop.
Save rosschapman/23f96b7ed9e0768bf1aa787af29f9cf2 to your computer and use it in GitHub Desktop.
AppConductor with action manager and state machine
processBookCreate = async (payload) => {
// Update component status (sync)
this.statusMachine(this.statuses.waiting);
// Post request (async)
await this.apiAdapater.books.post(action.payload);
// Update component status (sync)
this.statusMachine(this.statuses.success);
};
statusMachine = (nextStatus: Status) => {
switch (nextStatus) {
case this.statuses.waiting:
if (
this.status === this.statuses.idle ||
this.status === this.statuses.hasData ||
this.status === this.statuses.hasError
) {
return this.setState({ status: nextStatus });
}
case this.statuses.hasData:
if (this.status === this.statuses.success) {
return this.setState({ status: nextStatus });
}
case this.statuses.success:
if (this.status === this.statuses.waiting) {
return this.setState({ status: nextStatus });
}
default:
console.error("Logical fallacy achieved!");
}
};
actionDispatch = async (action) => {
switch (action.type) {
case "SUBMIT_FORM":
console.time("actionManager:SUBMIT_FORM");
await this.processBookCreate(action.payload);
console.timeEnd("actionManager:SUBMIT_FORM");
console.timeLog("actionManager:SUBMIT_FORM");
break;
default:
console.error("It should be impossible to get here");
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment