Skip to content

Instantly share code, notes, and snippets.

@justinramel
Last active March 31, 2020 21:48
Show Gist options
  • Save justinramel/d93f56d2fc5c8f827b603dd2bb9c11ba to your computer and use it in GitHub Desktop.
Save justinramel/d93f56d2fc5c8f827b603dd2bb9c11ba to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// 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