Skip to content

Instantly share code, notes, and snippets.

@karkowg
Last active December 3, 2020 16:14
Show Gist options
  • Select an option

  • Save karkowg/58e6769261451a5d656e9854c497dacc to your computer and use it in GitHub Desktop.

Select an option

Save karkowg/58e6769261451a5d656e9854c497dacc to your computer and use it in GitHub Desktop.

Revisions

  1. karkowg revised this gist Dec 3, 2020. 1 changed file with 24 additions and 16 deletions.
    40 changes: 24 additions & 16 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -71,7 +71,7 @@ const webcastMachine = {
    on: {
    '': [
    { target: 'starting', cond: 'isStarting' },
    { target: 'ready', cond: 'isReady' },
    { target: 'pre', cond: 'isPre' },
    { target: 'preparing', cond: 'isPreparing' },
    { target: 'idle' },
    ],
    @@ -111,23 +111,29 @@ const webcastMachine = {
    }
    },
    },
    medialive: {
    mediaLive: {
    initial: 'default',
    states: {
    default: {
    on: {
    '': [
    { target: 'ready', cond: 'isMLReady' },
    { target: 'preparing' },
    { target: 'ready', cond: 'isMediaLiveReady' },
    { target: 'preparing', cond: 'isPreparingMediaLive' },
    { target: 'idle' },
    ],
    },
    },
    idle: {
    on: {
    PREPARE_WEBCAST: 'preparing',
    },
    },
    preparing: {
    on: {
    ML_CHANNEL_READY: {
    target: 'ready',
    actions: assign({
    isMLReady: true,
    isMediaLiveReady: true,
    }),
    },
    },
    @@ -140,12 +146,12 @@ const webcastMachine = {
    },
    on: {
    READY: {
    target: 'ready',
    cond: ctx => [ctx.isPexipReady, ctx.isMLReady].every(Boolean),
    target: 'pre',
    cond: ctx => [ctx.isPexipReady, ctx.isMediaLiveReady].every(Boolean),
    },
    },
    },
    ready: {
    pre: {
    on: {
    GO_LIVE: 'starting',
    },
    @@ -229,11 +235,12 @@ const makeControlRoomMachine = event => Machine({
    countdownCounter: undefined,
    elapsed: undefined,
    hasFinished: event.event_mode === 'after',
    isPexipReady: event.isPexipReady,
    isMLReady: event.isMLReady,
    isLive: event.event_mode === 'live',
    isReady: event.event_mode === 'ready',
    isMediaLiveReady: event.media_live_state === 'ready',
    isPexipReady: event.pexip_state === 'ready',
    isPreparing: event.event_mode === 'preparing',
    isPreparingMediaLive: event.media_live_state === 'preparing',
    isPre: event.event_mode === 'pre',
    isStarting: event.event_mode === 'starting',
    startedAt: event.start_timecode ?? null,
    uid: event.uid,
    @@ -275,17 +282,18 @@ const makeControlRoomMachine = event => Machine({
    guards: {
    hasFinished: ctx => ctx.hasFinished,
    isLive: ctx => ctx.isLive,
    isPreparing: ctx => ctx.isPreparing,
    isMediaLiveReady: ctx => ctx.isMediaLiveReady,
    isPexipReady: ctx => ctx.isPexipReady,
    isMLReady: ctx => ctx.isMLReady,
    isPreparing: ctx => ctx.isPreparing,
    isPreparingMediaLive: ctx => ctx.isPreparingMediaLive,
    isPre: ctx => ctx.isPre,
    isStarting: ctx => ctx.isStarting,
    isReady: ctx => ctx.isReady,
    },
    });

    makeControlRoomMachine({
    uid: 'abc123',
    event_mode: 'preparing',
    isPexipReady: false,
    isMLReady: false,
    pexip_state: 'ready',
    media_live_state: 'idle',
    });
  2. karkowg revised this gist Dec 2, 2020. 1 changed file with 71 additions and 3 deletions.
    74 changes: 71 additions & 3 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -83,8 +83,66 @@ const webcastMachine = {
    },
    },
    preparing: {
    type: 'parallel',
    states: {
    pexip: {
    initial: 'default',
    states: {
    default: {
    on: {
    '': [
    { target: 'ready', cond: 'isPexipReady' },
    { target: 'preparing' },
    ],
    },
    },
    preparing: {
    on: {
    PEXIP_VA_READY: {
    target: 'ready',
    actions: assign({
    isPexipReady: true,
    }),
    },
    },
    },
    ready: {
    type: 'final',
    }
    },
    },
    medialive: {
    initial: 'default',
    states: {
    default: {
    on: {
    '': [
    { target: 'ready', cond: 'isMLReady' },
    { target: 'preparing' },
    ],
    },
    },
    preparing: {
    on: {
    ML_CHANNEL_READY: {
    target: 'ready',
    actions: assign({
    isMLReady: true,
    }),
    },
    },
    },
    ready: {
    type: 'final',
    },
    },
    },
    },
    on: {
    READY: 'ready',
    READY: {
    target: 'ready',
    cond: ctx => [ctx.isPexipReady, ctx.isMLReady].every(Boolean),
    },
    },
    },
    ready: {
    @@ -100,11 +158,12 @@ const webcastMachine = {
    on: {
    COUNTDOWN: {
    actions: 'setLiveContext',
    target: 'countdown',
    target: '#countdown',
    },
    },
    },
    countdown: {
    id: 'countdown',
    invoke: {
    id: 'countdown',
    src: (ctx) => makeCountdownMachine(ctx.countdownCounter),
    @@ -170,6 +229,8 @@ const makeControlRoomMachine = event => Machine({
    countdownCounter: undefined,
    elapsed: undefined,
    hasFinished: event.event_mode === 'after',
    isPexipReady: event.isPexipReady,
    isMLReady: event.isMLReady,
    isLive: event.event_mode === 'live',
    isReady: event.event_mode === 'ready',
    isPreparing: event.event_mode === 'preparing',
    @@ -215,9 +276,16 @@ const makeControlRoomMachine = event => Machine({
    hasFinished: ctx => ctx.hasFinished,
    isLive: ctx => ctx.isLive,
    isPreparing: ctx => ctx.isPreparing,
    isPexipReady: ctx => ctx.isPexipReady,
    isMLReady: ctx => ctx.isMLReady,
    isStarting: ctx => ctx.isStarting,
    isReady: ctx => ctx.isReady,
    },
    });

    makeControlRoomMachine({ uid: 'abc123', event_mode: 'pre' });
    makeControlRoomMachine({
    uid: 'abc123',
    event_mode: 'preparing',
    isPexipReady: false,
    isMLReady: false,
    });
  3. karkowg revised this gist Dec 1, 2020. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -100,12 +100,11 @@ const webcastMachine = {
    on: {
    COUNTDOWN: {
    actions: 'setLiveContext',
    target: '#countdown',
    target: 'countdown',
    },
    },
    },
    countdown: {
    id: 'countdown',
    invoke: {
    id: 'countdown',
    src: (ctx) => makeCountdownMachine(ctx.countdownCounter),
  4. karkowg revised this gist Dec 1, 2020. 1 changed file with 63 additions and 104 deletions.
    167 changes: 63 additions & 104 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -54,43 +54,68 @@ const makeCountdownMachine = counter => Machine({

    const webcastMachine = {
    id: 'webcast',
    initial: 'pre',
    initial: 'default',
    states: {
    pre: {
    initial: 'idle',
    default: {
    on: {
    '': [
    { target: 'live', cond: 'isLive' },
    { target: 'upcoming' },
    ],
    },
    },
    upcoming: {
    initial: 'default',
    states: {
    default: {
    on: {
    '': [
    { target: 'starting', cond: 'isStarting' },
    { target: 'ready', cond: 'isReady' },
    { target: 'preparing', cond: 'isPreparing' },
    { target: 'idle' },
    ],
    },
    },
    idle: {
    on: {
    PREPARE_INFRA: 'preparingInfra',
    PREPARE: 'preparing',
    },
    },
    preparingInfra: {
    preparing: {
    on: {
    INFRA_READY: 'ready',
    READY: 'ready',
    },
    },
    ready: {
    entry: 'setInfraReady',
    on: {
    GO_LIVE: 'preparingLive',
    GO_LIVE: 'starting',
    },
    },
    preparingLive: {
    starting: {
    invoke: {
    id: 'goLive',
    src: ctx => goLive(ctx.uid),
    onDone: {
    },
    on: {
    COUNTDOWN: {
    actions: 'setLiveContext',
    target: 'countdown',
    target: '#countdown',
    },
    },
    },
    countdown: {
    id: 'countdown',
    invoke: {
    id: 'countdown',
    src: () => makeCountdownMachine(10),
    src: (ctx) => makeCountdownMachine(ctx.countdownCounter),
    onDone: '#live',
    },
    on: {
    'COUNTDOWN.UPDATE': {
    actions: 'setCountdownCounter',
    },
    },
    },
    },
    },
    @@ -105,7 +130,6 @@ const webcastMachine = {
    actions: 'setViewingSessionStats',
    },
    STOP_LIVE: 'after',
    PING: '#live',
    },
    invoke: [
    // { id: 'elapsed', src: ctx => makeElapsedMachine(ctx.startedAt) },
    @@ -136,100 +160,21 @@ const webcastMachine = {
    target: '#controlRoom.done',
    },
    },
    type: 'final',
    },
    },
    };

    const userMachine = {
    initial: 'disconnected',
    states: {
    disconnected: {
    on: {
    CONNECT: {
    target: 'connected',
    cond: 'isInfraReady',
    },
    },
    },
    connected: {
    type: 'parallel',
    on: {
    DISCONNECT: 'disconnected',
    },
    states: {
    audio: {
    initial: 'disabled',
    states: {
    enabled: {
    on: {
    TOGGLE_AUDIO: 'disabled',
    MUTE: 'disabled',
    },
    },
    disabled: {
    on: {
    TOGGLE_AUDIO: 'enabled',
    },
    },
    },
    },
    video: {
    initial: 'disabled',
    states: {
    enabled: {
    on: {
    TOGGLE_VIDEO: 'disabled',
    },
    },
    disabled: {
    on: {
    TOGGLE_VIDEO: 'enabled',
    },
    },
    },
    },
    screenSharing: {
    initial: 'inactive',
    states: {
    active: {
    on: {
    TOGGLE_SCREEN_SHARING: 'inactive',
    },
    },
    inactive: {
    on: {
    TOGGLE_SCREEN_SHARING: 'active',
    },
    },
    },
    },
    spotlight: {
    initial: 'inactive',
    states: {
    active: {
    on: {
    TOGGLE_SPOTLIGHT: 'inactive',
    },
    },
    inactive: {
    on: {
    TOGGLE_SPOTLIGHT: 'active',
    },
    },
    },
    },
    },
    },
    },
    };

    const makeControlRoomMachine = event => Machine({
    id: 'controlRoom',
    initial: 'interactive',
    initial: 'default',
    context: {
    countdownCounter: undefined,
    elapsed: undefined,
    isInfraReady: false,
    hasFinished: event.event_mode === 'after',
    isLive: event.event_mode === 'live',
    isReady: event.event_mode === 'ready',
    isPreparing: event.event_mode === 'preparing',
    isStarting: event.event_mode === 'starting',
    startedAt: event.start_timecode ?? null,
    uid: event.uid,
    viewingSessions: {
    @@ -239,10 +184,17 @@ const makeControlRoomMachine = event => Machine({
    },
    },
    states: {
    default: {
    on: {
    '': [
    { target: 'done', cond: 'hasFinished' },
    { target: 'interactive' },
    ],
    },
    },
    interactive: {
    type: 'parallel',
    states: {
    user: { ...userMachine },
    webcast: { ...webcastMachine },
    },
    },
    @@ -252,14 +204,21 @@ const makeControlRoomMachine = event => Machine({
    },
    }, {
    actions: {
    setCountdownCounter: assign({ countdownCounter: (ctx, event) => event.counter }),
    setElapsed: assign({ elapsed: (ctx, event) => event.elapsed }),
    setInfraReady: assign({ isInfraReady: true }),
    setLiveContext: assign({ startedAt: (ctx, event) => dayjs(event.data.start_timecode) }),
    setLiveContext: assign({
    startedAt: (ctx, event) => dayjs(event.starts_at),
    countdownCounter: (ctx, event) => event?.countdown ?? 10,
    }),
    setViewingSessionStats: assign({ viewingSessions: (ctx, event) => event.viewingSessions }),
    },
    guards: {
    isInfraReady: ctx => ctx.isInfraReady,
    hasFinished: ctx => ctx.hasFinished,
    isLive: ctx => ctx.isLive,
    isPreparing: ctx => ctx.isPreparing,
    isStarting: ctx => ctx.isStarting,
    isReady: ctx => ctx.isReady,
    },
    });

    makeControlRoomMachine({ uid: 'abc123' });
    makeControlRoomMachine({ uid: 'abc123', event_mode: 'pre' });
  5. karkowg revised this gist Nov 27, 2020. 1 changed file with 75 additions and 74 deletions.
    149 changes: 75 additions & 74 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -2,31 +2,56 @@ function dayjs() {
    return new Date();
    }

    function randomInt(max) {
    return Math.floor(Math.random() * Math.floor(max));
    }

    function getViewingSessionsStats(uid) {
    console.log('getViewingSessionsStats', { uid });
    return Promise.resolve({
    data: {
    online: randomInt(100),
    total: randomInt(100),
    peak: randomInt(100),
    },
    });
    }

    function goLive(uid) {
    console.log('goLive', { uid });
    return Promise.resolve({ startedAt: dayjs() });
    }

    function stopLive(uid) {
    console.log('stopLive', { uid });
    return Promise.resolve();
    }

    const makeCountdownMachine = counter => Machine({
    id: 'countdown',
    initial: 'running',
    context: {
    counter,
    },
    states: {
    running: {
    invoke: {
    id: 'countdownInterval',
    src: () => callback => {
    const intervalId = setInterval(() => {
    callback('TICK');
    }, 1000);

    return () => clearInterval(intervalId);
    },
    },
    on: {
    '': {
    target: 'done',
    cond: ctx => ctx.counter <= 0,
    },
    TICK: {
    actions: ['decrement', 'notify'],
    },
    },
    },
    done: {
    type: 'final',
    },
    },
    }, {
    actions: {
    decrement: assign({ counter: ctx => ctx.counter - 1 }),
    notify: sendParent(ctx => ({
    type: 'COUNTDOWN.UPDATE',
    counter: ctx.counter,
    })),
    },
    });

    const webcastMachine = {
    id: 'webcast',
    initial: 'pre',
    @@ -41,41 +66,51 @@ const webcastMachine = {
    },
    preparingInfra: {
    on: {
    INFRA_READY: 'ready'
    INFRA_READY: 'ready',
    },
    },
    ready: {
    entry: ['setInfraReady'],
    entry: 'setInfraReady',
    on: {
    GO_LIVE: 'goingLive',
    GO_LIVE: 'preparingLive',
    },
    },
    goingLive: {
    type: 'final',
    preparingLive: {
    invoke: {
    id: 'goLive',
    src: ctx => goLive(ctx.uid),
    onDone: {
    target: '#live',
    actions: 'setLiveContext',
    target: 'countdown',
    },
    },
    },
    countdown: {
    invoke: {
    id: 'countdown',
    src: () => makeCountdownMachine(10),
    onDone: '#live',
    },
    },
    },
    },
    live: {
    id: 'live',
    initial: 'streaming',
    on: {
    SET_VIEWING_SESSION_STATS: {
    actions: ['setViewingSessionStats'],
    'ELAPSED.UPDATE': {
    actions: 'setElapsed',
    },
    'STATS.UPDATE': {
    actions: 'setViewingSessionStats',
    },
    STOP_LIVE: 'after',
    PING: '#live',
    },
    invoke: {
    id: 'stats',
    src: ctx => createViewingSessionsStatsMachine(ctx.uid),
    },
    invoke: [
    // { id: 'elapsed', src: ctx => makeElapsedMachine(ctx.startedAt) },
    // { id: 'stats', src: ctx => makeStatsMachine(ctx.uid) },
    ],
    states: {
    playback: {
    on: {
    @@ -90,7 +125,10 @@ const webcastMachine = {
    },
    },
    after: {
    actions: send('DONE', { to: 'stats' }),
    actions: [
    send('DONE', { to: 'elapsed' }),
    send('DONE', { to: 'stats' }),
    ],
    invoke: {
    id: 'stopLive',
    src: ctx => stopLive(ctx.uid),
    @@ -186,52 +224,14 @@ const userMachine = {
    },
    };

    const createViewingSessionsStatsMachine = uid => Machine({
    id: 'stats',
    initial: 'idle',
    context: { uid },
    states: {
    idle: {
    after: {
    TIMER: 'fetching',
    },
    on: {
    DONE: 'done',
    },
    },
    fetching: {
    on: {
    DONE: 'done',
    },
    invoke: {
    id: 'refreshStats',
    src: ctx => getViewingSessionsStats(ctx.uid),
    onDone: {
    target: 'idle',
    actions: 'sendStats',
    },
    },
    },
    done: {
    type: 'final',
    },
    }
    }, {
    actions: {
    sendStats: sendParent((ctx, event) => ({ type: 'SET_VIEWING_SESSION_STATS', viewingSessions: event.data.data })),
    },
    delays: {
    TIMER: 10 * 1000,
    },
    });

    const createControlRoomMachine = ({ uid }) => Machine({
    const makeControlRoomMachine = event => Machine({
    id: 'controlRoom',
    initial: 'interactive',
    context: {
    uid,
    elapsed: undefined,
    isInfraReady: false,
    startedAt: null,
    startedAt: event.start_timecode ?? null,
    uid: event.uid,
    viewingSessions: {
    online: 0,
    peak: 0,
    @@ -252,13 +252,14 @@ const createControlRoomMachine = ({ uid }) => Machine({
    },
    }, {
    actions: {
    setElapsed: assign({ elapsed: (ctx, event) => event.elapsed }),
    setInfraReady: assign({ isInfraReady: true }),
    setLiveContext: assign({ startedAt: (ctx, event) => event.data.startedAt }),
    setLiveContext: assign({ startedAt: (ctx, event) => dayjs(event.data.start_timecode) }),
    setViewingSessionStats: assign({ viewingSessions: (ctx, event) => event.viewingSessions }),
    },
    guards: {
    isInfraReady: ctx => ctx.isInfraReady,
    },
    });

    createControlRoomMachine({ uid: 'abc123' });
    makeControlRoomMachine({ uid: 'abc123' });
  6. karkowg revised this gist Nov 25, 2020. 1 changed file with 78 additions and 16 deletions.
    94 changes: 78 additions & 16 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -2,13 +2,28 @@ function dayjs() {
    return new Date();
    }

    // TODO
    function randomInt(max) {
    return Math.floor(Math.random() * Math.floor(max));
    }

    function getViewingSessionsStats(uid) {
    console.log('getViewingSessionsStats', { uid });
    return Promise.resolve({
    data: {
    online: randomInt(100),
    total: randomInt(100),
    peak: randomInt(100),
    },
    });
    }

    function goLive(uid) {
    console.log('goLive', { uid });
    return Promise.resolve({ startedAt: dayjs() });
    }

    // TODO
    function stopLive(uid) {
    console.log('stopLive', { uid });
    return Promise.resolve();
    }

    @@ -52,30 +67,38 @@ const webcastMachine = {
    id: 'live',
    initial: 'streaming',
    on: {
    SET_VIEWING_SESSION_STATS: {
    actions: ['setViewingSessionStats'],
    },
    STOP_LIVE: 'after',
    },
    invoke: {
    id: 'stats',
    src: ctx => createViewingSessionsStatsMachine(ctx.uid),
    },
    states: {
    streaming: {
    playback: {
    on: {
    PLAY_VIDEO: 'playback',
    STOP_VIDEO: 'streaming',
    },
    },
    playback: {
    streaming: {
    on: {
    STOP_VIDEO: 'streaming',
    PLAY_VIDEO: 'playback',
    },
    },
    },
    },
    after: {
    type: 'final',
    actions: send('DONE', { to: 'stats' }),
    invoke: {
    id: 'stopLive',
    src: ctx => stopLive(ctx.uid),
    onDone: {
    target: '#controlRoom.done',
    },
    },
    type: 'final',
    },
    },
    };
    @@ -163,24 +186,64 @@ const userMachine = {
    },
    };

    const createViewingSessionsStatsMachine = uid => Machine({
    id: 'stats',
    initial: 'idle',
    context: { uid },
    states: {
    idle: {
    after: {
    TIMER: 'fetching',
    },
    on: {
    DONE: 'done',
    },
    },
    fetching: {
    on: {
    DONE: 'done',
    },
    invoke: {
    id: 'refreshStats',
    src: ctx => getViewingSessionsStats(ctx.uid),
    onDone: {
    target: 'idle',
    actions: 'sendStats',
    },
    },
    },
    done: {
    type: 'final',
    },
    }
    }, {
    actions: {
    sendStats: sendParent((ctx, event) => ({ type: 'SET_VIEWING_SESSION_STATS', viewingSessions: event.data.data })),
    },
    delays: {
    TIMER: 10 * 1000,
    },
    });

    const createControlRoomMachine = ({ uid }) => Machine({
    id: 'controlRoom',
    initial: 'interactive',
    context: {
    uid,
    isInfraReady: false,
    startedAt: null,
    viewingSessions: {
    online: 0,
    peak: 0,
    total: 0,
    },
    },
    states: {
    interactive: {
    type: 'parallel',
    states: {
    user: {
    ...userMachine,
    },
    webcast: {
    ...webcastMachine,
    },
    user: { ...userMachine },
    webcast: { ...webcastMachine },
    },
    },
    done: {
    @@ -190,9 +253,8 @@ const createControlRoomMachine = ({ uid }) => Machine({
    }, {
    actions: {
    setInfraReady: assign({ isInfraReady: true }),
    setLiveContext: assign({
    startedAt: (ctx, event) => event.data.startedAt,
    }),
    setLiveContext: assign({ startedAt: (ctx, event) => event.data.startedAt }),
    setViewingSessionStats: assign({ viewingSessions: (ctx, event) => event.viewingSessions }),
    },
    guards: {
    isInfraReady: ctx => ctx.isInfraReady,
  7. karkowg revised this gist Nov 24, 2020. 1 changed file with 42 additions and 9 deletions.
    51 changes: 42 additions & 9 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,17 @@
    function dayjs() {
    return new Date();
    }

    // TODO
    function goLive(uid) {
    return Promise.resolve({ startedAt: dayjs() });
    }

    // TODO
    function stopLive(uid) {
    return Promise.resolve();
    }

    const webcastMachine = {
    id: 'webcast',
    initial: 'pre',
    @@ -16,10 +30,20 @@ const webcastMachine = {
    },
    },
    ready: {
    type: 'final',
    entry: ['setInfraReady'],
    on: {
    GO_LIVE: '#live',
    GO_LIVE: 'goingLive',
    },
    },
    goingLive: {
    type: 'final',
    invoke: {
    id: 'goLive',
    src: ctx => goLive(ctx.uid),
    onDone: {
    target: '#live',
    actions: 'setLiveContext',
    },
    },
    },
    },
    @@ -45,8 +69,12 @@ const webcastMachine = {
    },
    after: {
    type: 'final',
    on: {
    '': '#controlRoom.done',
    invoke: {
    id: 'stopLive',
    src: ctx => stopLive(ctx.uid),
    onDone: {
    target: '#controlRoom.done',
    },
    },
    },
    },
    @@ -135,11 +163,13 @@ const userMachine = {
    },
    };

    const controlRoomMachine = Machine({
    const createControlRoomMachine = ({ uid }) => Machine({
    id: 'controlRoom',
    initial: 'interactive',
    context: {
    uid,
    isInfraReady: false,
    startedAt: null,
    },
    states: {
    interactive: {
    @@ -159,11 +189,14 @@ const controlRoomMachine = Machine({
    },
    }, {
    actions: {
    setInfraReady: assign({
    isInfraReady: true,
    setInfraReady: assign({ isInfraReady: true }),
    setLiveContext: assign({
    startedAt: (ctx, event) => event.data.startedAt,
    }),
    },
    guards: {
    isInfraReady: context => context.isInfraReady,
    isInfraReady: ctx => ctx.isInfraReady,
    },
    });
    });

    createControlRoomMachine({ uid: 'abc123' });
  8. karkowg revised this gist Nov 20, 2020. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -145,12 +145,12 @@ const controlRoomMachine = Machine({
    interactive: {
    type: 'parallel',
    states: {
    webcast: {
    ...webcastMachine,
    },
    user: {
    ...userMachine,
    },
    webcast: {
    ...webcastMachine,
    },
    },
    },
    done: {
  9. karkowg created this gist Nov 20, 2020.
    169 changes: 169 additions & 0 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,169 @@
    const webcastMachine = {
    id: 'webcast',
    initial: 'pre',
    states: {
    pre: {
    initial: 'idle',
    states: {
    idle: {
    on: {
    PREPARE_INFRA: 'preparingInfra',
    },
    },
    preparingInfra: {
    on: {
    INFRA_READY: 'ready'
    },
    },
    ready: {
    type: 'final',
    entry: ['setInfraReady'],
    on: {
    GO_LIVE: '#live',
    },
    },
    },
    },
    live: {
    id: 'live',
    initial: 'streaming',
    on: {
    STOP_LIVE: 'after',
    },
    states: {
    streaming: {
    on: {
    PLAY_VIDEO: 'playback',
    },
    },
    playback: {
    on: {
    STOP_VIDEO: 'streaming',
    },
    },
    },
    },
    after: {
    type: 'final',
    on: {
    '': '#controlRoom.done',
    },
    },
    },
    };

    const userMachine = {
    initial: 'disconnected',
    states: {
    disconnected: {
    on: {
    CONNECT: {
    target: 'connected',
    cond: 'isInfraReady',
    },
    },
    },
    connected: {
    type: 'parallel',
    on: {
    DISCONNECT: 'disconnected',
    },
    states: {
    audio: {
    initial: 'disabled',
    states: {
    enabled: {
    on: {
    TOGGLE_AUDIO: 'disabled',
    MUTE: 'disabled',
    },
    },
    disabled: {
    on: {
    TOGGLE_AUDIO: 'enabled',
    },
    },
    },
    },
    video: {
    initial: 'disabled',
    states: {
    enabled: {
    on: {
    TOGGLE_VIDEO: 'disabled',
    },
    },
    disabled: {
    on: {
    TOGGLE_VIDEO: 'enabled',
    },
    },
    },
    },
    screenSharing: {
    initial: 'inactive',
    states: {
    active: {
    on: {
    TOGGLE_SCREEN_SHARING: 'inactive',
    },
    },
    inactive: {
    on: {
    TOGGLE_SCREEN_SHARING: 'active',
    },
    },
    },
    },
    spotlight: {
    initial: 'inactive',
    states: {
    active: {
    on: {
    TOGGLE_SPOTLIGHT: 'inactive',
    },
    },
    inactive: {
    on: {
    TOGGLE_SPOTLIGHT: 'active',
    },
    },
    },
    },
    },
    },
    },
    };

    const controlRoomMachine = Machine({
    id: 'controlRoom',
    initial: 'interactive',
    context: {
    isInfraReady: false,
    },
    states: {
    interactive: {
    type: 'parallel',
    states: {
    webcast: {
    ...webcastMachine,
    },
    user: {
    ...userMachine,
    },
    },
    },
    done: {
    type: 'final',
    },
    },
    }, {
    actions: {
    setInfraReady: assign({
    isInfraReady: true,
    }),
    },
    guards: {
    isInfraReady: context => context.isInfraReady,
    },
    });