Skip to content

Instantly share code, notes, and snippets.

@orther
Last active November 13, 2020 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orther/6e94e52e5c0886ceec7ca4b251791cf8 to your computer and use it in GitHub Desktop.
Save orther/6e94e52e5c0886ceec7ca4b251791cf8 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const bleManager = Machine({
id: 'bleManager',
initial: 'disconnected',
context: {
phoneBleState: null,
session: null,
publisher: null,
streams: null,
token: null
},
states: {
disconnected: {
id: 'disconnected',
initial: 'idle',
states: {
'idle': {
id: 'idle',
},
'init': {},
'ready': {},
}
},
}
}, {});
const video = Machine({
id: 'video',
initial: 'disconnected',
context: {
sessionId: null,
session: null,
publisher: null,
streams: null,
token: null
},
states: {
disconnected: {
id: 'disconnected',
initial: 'idle',
states: {
'idle': {
id: 'idle',
on: {
'START': 'init'
}
},
'init': {
id: 'init',
entry: ['initSession'],
invoke: [
{
id: 'initPublisher',
src: 'invokeInitPublisher'
},
{
id: 'createToken',
src: 'invokeCreateToken',
onDone: {
actions: 'assignToken'
}
}
],
on: {
'VIDEO_ELEMENT_CREATED': {
target: 'ready',
cond: 'checkToken',
actions: 'assignPublisher'
}
}
},
'ready': {
id: 'ready',
on: {
'CONNECT': '#connected'
}
}
}
},
connected: {
id: 'connected',
type: 'parallel',
states: {
'session': {
id: 'session',
invoke: {
id: 'connectSession',
src: 'invokeConnectSession'
},
on: {
'SESSION_DISCONNECTED': '#disconnected'
}
},
'subscriber': {
id: 'subscriber',
on: {
'SUBSCRIBER_ADDED': {
actions: 'addNewSusbscriber'
},
'SUBSCRIBER_REMOVED': {
actions: 'removeSusbscriber'
},
'AUDIO_LEVEL_UPDATE': {
actions: 'updateAudioLevel'
},
'STREAM_PROPERTY_CHANGED': {
actions: 'updateStreamProperty'
},
'VIDEO_ELEMENT_CREATED': {
actions: 'updateSubscriberVideo'
}
}
},
'publisher': {
id: 'publisher',
on: {
'AUDIO_LEVEL_UPDATE': {
actions: 'updateAudioLevel'
},
'STREAM_PROPERTY_CHANGED': {
actions: 'updateStreamProperty'
}
}
}
},
on: {
'DISCONNECT': '#disconnected'
}
}
}
}, {
actions: {
addNewSusbscriber: () => (console.log('Subscriber Added')),
assignToken: assign({ token: (ctx, e) => e.data.token }),
assignPublisher: () => (console.log('Update Publisher')),
initSession: () => (console.log('Initialize Session')),
removeSusbscriber: () => (console.log('Subscriber Removed')),
updateAudioLevel: () => (console.log('Audio Level Update')),
updateStreamProperty: () => (console.log('Stream Property Updated')),
updateSubscriberVideo: () => (console.log('Subscriber Video Updated'))
},
guards: {
checkToken: () => true
},
services: {
invokeInitPublisher: (ctx) => (cb) => (cb('VIDEO_ELEMENT_CREATED')),
invokeCreateToken: () => { new Promise((resolve) => resolve({ token: 'token' })) },
invokeConnectSession: (ctx) => (cb) => (cb('VIDEO_ELEMENT_CREATED'))
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment