Skip to content

Instantly share code, notes, and snippets.

@tejovanthn
Last active April 6, 2021 12:57
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 tejovanthn/a6f073644230f7fde3ba2da002c1a144 to your computer and use it in GitHub Desktop.
Save tejovanthn/a6f073644230f7fde3ba2da002c1a144 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
let randomID = () => Math.random().toString(36).substring(7);
const addWriter = () =>
assign({
writers: (context, event) => [randomID(), ...context.writers]
});
const addEditor = () =>
assign({
editors: (context, event) => [randomID(), ...context.editors]
});
const creatorStates = ({
creatorType,
create,
createdEvent,
nextState,
otherCreateEvents = {}
}) => {
const unassigned = `${create}-unassigned`;
const held = `${create}-held`;
return {
id: create,
// on: { [createdEvent]: nextState },
initial: "unassigned",
states: {
unassigned: {
id: unassigned,
on: {
ASSIGN: {
target: "held",
actions: [`add-${creatorType}`]
}
}
},
held: {
id: held,
on: {
ACCEPT: create,
PASS: "unassigned",
REDACTED: "unassigned"
}
},
[create]: {
on: {
PASS: "unassigned",
REDACTED: "unassigned",
[createdEvent]: `#${nextState}`,
...otherCreateEvents
}
}
}
};
};
const writerMachine = creatorStates({
creatorType: "writer",
create: "writing",
createdEvent: "WRITTEN",
nextState: "editing"
});
const editorMachine = creatorStates({
creatorType: "editor",
create: "editing",
createdEvent: "EDITED",
nextState: "checking",
otherCreateEvents: {
REWORK_WRITER: "#writing-held",
REJECT_WRITER: "#writing"
}
});
const fetchMachine = Machine(
{
initial: "new",
key: "flow",
context: {
writers: [],
editors: []
},
states: {
new: {
on: {
READY: "writing"
}
},
writing: {
...writerMachine
},
editing: {
...editorMachine
},
checking: {
id: "checking",
on: {
ACCEPTED: "client",
REWORK: "#writing-held",
REJECT_WRITER: "writing",
REJECT_EDITOR: "editing"
}
},
client: {
on: {
ACCEPTED: "finished",
REWORK: "#writing-held",
REJECT_WRITER: "writing",
REJECT_EDITOR: "editing"
}
},
finished: {
type: "final"
}
}
},
{
actions: {
"add-writer": addWriter(),
"add-editor": addEditor()
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment