Last active
March 31, 2020 21:48
-
-
Save justinramel/d93f56d2fc5c8f827b603dd2bb9c11ba 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 isEmpty = ({ users }) => users.length === 0; | |
const notEmpty = ({ users }) => users.length > 0; | |
const meetingMachine = Machine({ | |
id: 'meeting', | |
initial: 'empty', | |
context: { | |
users: [] | |
}, | |
states: { | |
empty: { | |
on: { | |
ADD: { | |
target: 'validMeeting', | |
actions: ['addAttendee'] | |
} | |
} | |
}, | |
validMeeting: { | |
on: { | |
ADD: { | |
actions: ['addAttendee'] | |
}, | |
REMOVE: { | |
target: 'checkEmpty', | |
actions: ['removeAttendee'] | |
}, | |
MEET: { | |
target: 'meeting', | |
actions: ['createMeeting'] | |
}, | |
SCHEDULE: { | |
target: 'schedule', | |
actions: ['scheduleMeeting'], | |
} | |
} | |
}, | |
checkEmpty: { | |
on: { | |
'': [ | |
{ target: 'empty', cond: isEmpty }, | |
{ target: 'validMeeting', cond: notEmpty } | |
] | |
} | |
}, | |
meeting: { | |
type: 'final' | |
}, | |
schedule: { | |
type: 'final' | |
} | |
} | |
}, { | |
actions: { | |
addAttendee: assign({ users: context => { | |
return [...context.users, 'userId']; | |
} | |
}), | |
removeAttendee: assign({ users: context => { | |
context.users.splice(0,1); | |
return [...context.users]; | |
} | |
}) | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment