Created
July 2, 2020 19:57
-
-
Save JM-Mendez/f9e8b548c4168828b666e6bee2e85fe8 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
// Available variables: | |
// - Machine | |
// - interpret | |
// - assign | |
// - send | |
// - sendParent | |
// - spawn | |
// - raise | |
// - actions | |
// - XState (all XState exports) | |
const DELETE_QUERY_CONSTRAINT = 'queryController/constraint/delete' | |
const ADD_QUERY_CONSTRAINT = 'queryController/constraint/add' | |
const ADD_ORGANISM_CONSTRAINT = 'organism/constraint/add' | |
const REMOVE_ORGANISM_CONSTRAINT = 'organism/constraint/remove' | |
const APPLY_ORGANISM_CONSTRAINT = 'organism/constraint/apply' | |
const CONSTRAINT_LIMIT_REACHED = 'global/contraint/limit_reached' | |
const organismMachine = Machine( | |
{ | |
id: 'Organism', | |
initial: 'inactive', | |
context: { | |
selectedOrganisms: [], | |
constraintsApplied: false, | |
}, | |
states: { | |
inactive: { | |
on: { | |
[ADD_ORGANISM_CONSTRAINT]: { | |
target: 'constraintsUpdated', | |
actions: 'addConstraint', | |
}, | |
[REMOVE_ORGANISM_CONSTRAINT]: { | |
target: 'constraintsUpdated', | |
actions: 'removeConstraint', | |
cond: 'constraintListNotEmpty', | |
}, | |
}, | |
}, | |
constraintsUpdated: { | |
on: { | |
[ADD_ORGANISM_CONSTRAINT]: { | |
actions: 'addConstraint', | |
}, | |
[REMOVE_ORGANISM_CONSTRAINT]: { | |
actions: 'removeConstraint', | |
}, | |
[CONSTRAINT_LIMIT_REACHED]: 'limitReached', | |
[APPLY_ORGANISM_CONSTRAINT]: 'inactive', | |
}, | |
}, | |
limitReached: { | |
on: { | |
[REMOVE_ORGANISM_CONSTRAINT]: { | |
actions: 'removeConstraint', | |
}, | |
}, | |
}, | |
}, | |
}, | |
{ | |
actions: { | |
// addConstraint: assign((ctx, event) => { | |
// // @ts-ignore | |
// ctx.selectedOrganisms.push(event.constraint) | |
// }), | |
// removeConstraint: assign((ctx, event) => { | |
// // @ts-ignore | |
// ctx.selectedOrganisms.push(event.constraint) | |
// }), | |
addConstraint: assign({ | |
// @ts-ignore | |
selectedOrganisms: (ctx, event) => [...ctx.selectedOrganisms, event.constraint], | |
}), | |
removeConstraint: assign({ | |
selectedOrganisms: (ctx, event) => | |
// @ts-ignore | |
ctx.selectedOrganisms.filter((cs) => cs.value === event.contraint.value), | |
}), | |
}, | |
guards: { | |
constraintListNotEmpty: (ctx) => { | |
return ctx.selectedOrganisms.length > 0 | |
}, | |
}, | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment