Skip to content

Instantly share code, notes, and snippets.

@kellyjandrews
Last active July 9, 2020 16:40
Show Gist options
  • Save kellyjandrews/9553a4aadf2c59a756bcd52c7ee6e7b1 to your computer and use it in GitHub Desktop.
Save kellyjandrews/9553a4aadf2c59a756bcd52c7ee6e7b1 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const session = Machine({
id: 'session',
initial: 'disconnected',
context: {
sessionId: null,
session: null,
token: null,
publisher: null,
subscribers: new Map()
},
states: {
disconnected: {
id: 'disconnected',
initial: 'idle',
states: {
'idle': {
id: 'idle',
on: {
'START': {
target: 'init',
cond: 'checkSessionId',
actions: 'assignSessionId'
}
}
},
'init': {
id: 'init',
entry: ['initSession', 'initPublisher'],
invoke: [
{
id: 'createToken',
src: 'invokeCreateToken',
onDone: {
actions: 'assignToken'
},
onError: {
target: '#error',
actions: (ctx, e) => console.log(e)
}
}
],
on: {
'': {
target: 'ready',
cond: 'checkReady'
}
}
},
'ready': {
id: 'ready',
on: {
'CONNECT': '#connected'
}
}
}
},
connected: {
id: 'connected',
invoke: {
id: 'connectSession',
src: 'invokeConnectSession'
},
on: {
'SUBSCRIBER_ADDED': { actions: 'addNewSusbscriber' },
'SUBSCRIBER_REMOVED': { actions: 'removeSusbscriber' },
'AUDIO_PROPERTY_CHANGED': {
actions: 'sendToggleAudioEvent'
},
'VIDEO_PROPERTY_CHANGED': {
actions: 'sendToggleVideoEvent'
},
'DISCONNECT': '#disconnected'
}
},
error: {
id: 'error'
}
}
}, {
actions: {
addNewSusbscriber: assign({
subscribers: (ctx, e) => ctx.subscribers.set(e.streamId, "new stream actor")
}),
assignToken: assign({ token: () => "token" }),
assignSessionId: assign({
sessionId: (ctx, e) => e.sessionId
}),
initSession: assign({ session: () => "session object" }),
removeSusbscriber: assign({ subscribers: (ctx,e) => {
ctx.subscribers.delete(e.streamId);
return ctx.subscribers;
}}),
initPublisher: assign({
publisher: (ctx, e) => "new stream actor"
}),
sendToggleAudioEvent: () => {},
sendToggleVideoEvent: () => {}
},
guards: {
checkReady: (ctx) => ctx.publisher && ctx.token,
checkSessionId: (ctx, e) => !!e.sessionId
},
services: {
invokeCreateToken: () => {},
invokeConnectSession: () => {}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment