Skip to content

Instantly share code, notes, and snippets.

@apnerve
Last active January 8, 2021 16:48
Show Gist options
  • Select an option

  • Save apnerve/ea76bcf9824cd93c2466d8fd2c801448 to your computer and use it in GitHub Desktop.

Select an option

Save apnerve/ea76bcf9824cd93c2466d8fd2c801448 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const roomMachine = Machine({
id: '100ms rooms',
initial: 'home',
context: {
isPermissionGiven: false,
isNew: true
},
states: {
home: {
on: {
CREATE: 'create_room',
JOIN: 'join_room'
}
},
create_room: {
on: {
BACK: 'home',
CREATE: [
{
target: 'permission',
cond: context => !context.isPermissionGiven
},
{
target: 'preview',
cond: context => context.isPermissionGiven && context.isNew
},
{
target: 'room',
cond: context => context.isPermissionGiven && !context.isNew
}
]
}
},
join_room: {
on: {
BACK: 'home',
JOIN: [
{
target: 'permission',
cond: context => !context.isPermissionGiven
},
{
target: 'preview',
cond: context => context.isPermissionGiven && context.isNew
},
{
target: 'room',
cond: context => context.isPermissionGiven && !context.isNew
}
]
}
},
permission: {
on: {
ALLOW: {
target: 'preview',
actions: assign({
isPermissionGiven: true
})
},
BLOCK: {
target: 'permission',
actions: assign({
isPermissionGiven: false
})
}
}
},
preview: {
on: {
JOIN: {
target:'room',
actions: assign({
isNew: false
})
},
BACK: 'join_room'
}
},
room: {
on: {
LEAVE: 'home',
RELOAD: 'room'
}
},
url: {
on: {
CLICK: [
{
target: 'permission',
cond: context => !context.isPermissionGiven
},
{
target: 'room',
cond: context => context.isPermissionGiven && !context.isNew
},
{
target: 'preview',
cond: context => context.isPermissionGiven && context.isNew
}
]
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment