Skip to content

Instantly share code, notes, and snippets.

@m-revetria
Last active October 28, 2021 02:41
Show Gist options
  • Select an option

  • Save m-revetria/cbed96eaf4a2cdddd246e17359ec599a to your computer and use it in GitHub Desktop.

Select an option

Save m-revetria/cbed96eaf4a2cdddd246e17359ec599a to your computer and use it in GitHub Desktop.

Revisions

  1. m-revetria revised this gist Oct 28, 2021. 1 changed file with 10 additions and 4 deletions.
    14 changes: 10 additions & 4 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -27,9 +27,12 @@ const browserStates = {
    PAUSE: 'paused',
    SWITCH_CAM: {
    target: 'streaming',
    actions: ['assignCurrentCamera']
    actions: ['assignSwitchCamera'],
    },
    SWITCHED_CAM: {
    target: 'streaming',
    actions: ['unassignSwitchCamera'],
    },
    SWITCHED_CAM: 'streaming',
    CAM_COUNT: {
    target: 'streaming',
    actions: ['assignCamerasCount']
    @@ -134,8 +137,11 @@ const livestreamMachine = Machine(
    camerasCount: (_context, event) => event.camerasCount
    }),
    assignCurrentCamera: assign({
    currentCamera: (context, _event) => (context.currentCamera || 0) + 1,
    })
    switchCamera: (_context, _event) => true,
    }),
    unassignSwitchCamera: assign({
    switchCamera: (_context, _event) => undefined,
    }),
    },
    },
    );
  2. m-revetria revised this gist Oct 28, 2021. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion machine.js
    Original file line number Diff line number Diff line change
    @@ -25,10 +25,11 @@ const browserStates = {
    streaming: {
    on: {
    PAUSE: 'paused',
    SWAP_CAM: {
    SWITCH_CAM: {
    target: 'streaming',
    actions: ['assignCurrentCamera']
    },
    SWITCHED_CAM: 'streaming',
    CAM_COUNT: {
    target: 'streaming',
    actions: ['assignCamerasCount']
  3. m-revetria revised this gist Oct 27, 2021. 1 changed file with 14 additions and 3 deletions.
    17 changes: 14 additions & 3 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,10 @@ const browserStates = {
    streaming: {
    on: {
    PAUSE: 'paused',
    SWAP_CAM: 'streaming',
    SWAP_CAM: {
    target: 'streaming',
    actions: ['assignCurrentCamera']
    },
    CAM_COUNT: {
    target: 'streaming',
    actions: ['assignCamerasCount']
    @@ -55,6 +58,7 @@ const livestreamMachine = Machine(
    type: 'parallel',
    context: {
    camerasCount: undefined,
    currentCamera: undefined,
    error: undefined,
    },
    states: {
    @@ -122,8 +126,15 @@ const livestreamMachine = Machine(
    },
    {
    actions: {
    assignError: (_context, event) => event.error,
    assignCamerasCount: (_context, event) => event.camerasCount,
    assignError: assign({
    error: (_context, event) => event.error,
    }),
    assignCamerasCount: assign({
    camerasCount: (_context, event) => event.camerasCount
    }),
    assignCurrentCamera: assign({
    currentCamera: (context, _event) => (context.currentCamera || 0) + 1,
    })
    },
    },
    );
  4. m-revetria revised this gist Oct 27, 2021. 1 changed file with 125 additions and 42 deletions.
    167 changes: 125 additions & 42 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -1,46 +1,129 @@
    const encoderStates = {
    initial: 'streaming',
    states: {
    streaming: {
    on: {
    ENCODER_STOPPED: 'paused',
    },
    },
    paused: {
    on: {
    ENCODER_RESUMED: 'streaming',
    },
    },
    },
    };

    // Available variables:
    // - Machine
    // - interpret
    // - assign
    // - send
    // - sendParent
    // - spawn
    // - raise
    // - actions
    // - XState (all XState exports)

    const fetchMachine = Machine({
    id: 'fetch',
    initial: 'idle',
    const browserStates = {
    initial: 'processing',
    states: {
    processing: {
    on: {
    PROCESSED: 'streaming',
    },
    },
    streaming: {
    on: {
    PAUSE: 'paused',
    SWAP_CAM: 'streaming',
    CAM_COUNT: {
    target: 'streaming',
    actions: ['assignCamerasCount']
    },
    ERROR: {
    target: 'error',
    actions: ['assignError'],
    },
    },
    },
    paused: {
    on: {
    GO_LIVE: 'streaming',
    },
    },
    error: {
    on: {
    GO_LIVE: 'streaming',
    },
    },
    },
    };

    const livestreamMachine = Machine(
    {
    id: 'livestream-hierarchical',
    type: 'parallel',
    context: {
    retries: 0
    camerasCount: undefined,
    error: undefined,
    },
    states: {
    idle: {
    on: {
    FETCH: 'loading'
    }
    },
    loading: {
    on: {
    RESOLVE: 'success',
    REJECT: 'failure'
    }
    },
    success: {
    type: 'final'
    },
    failure: {
    on: {
    RETRY: {
    target: 'loading',
    actions: assign({
    retries: (context, event) => context.retries + 1
    })
    }
    }
    }
    }
    });

    stage: {
    initial: 'browserCanStream',
    states: {
    browserCanStream: {
    on: {
    GO_LIVE: 'browser',
    FROM_ENCODER: 'encoderProcessing',
    END: 'completed',
    },
    },
    encoderProcessing: {
    on: {
    PROCESSED: 'encoderCanStream',
    FROM_BROWSER: 'browserCanStream',
    END: 'completed',
    },
    },
    encoderCanStream: {
    on: {
    ENCODER_STARTED: 'encoder',
    FROM_BROWSER: 'browserCanStream',
    },
    },
    encoder: {
    on: {
    ERROR: {
    target: 'error',
    actions: ['assignError'],
    },
    END: 'completed',
    },
    ...encoderStates,
    },
    browser: {
    on: {
    END: 'completed',
    },
    ...browserStates,
    },
    error: {
    type: 'final',
    },
    completed: {
    type: 'final',
    },
    },
    },
    type: {
    initial: 'test',
    states: {
    test: {
    on: {
    EVENT: 'event',
    },
    },
    event: {
    type: 'final',
    },
    },
    },
    },
    },
    {
    actions: {
    assignError: (_context, event) => event.error,
    assignCamerasCount: (_context, event) => event.camerasCount,
    },
    },
    );
  5. m-revetria created this gist Oct 27, 2021.
    46 changes: 46 additions & 0 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@

    // Available variables:
    // - Machine
    // - interpret
    // - assign
    // - send
    // - sendParent
    // - spawn
    // - raise
    // - actions
    // - XState (all XState exports)

    const fetchMachine = Machine({
    id: 'fetch',
    initial: 'idle',
    context: {
    retries: 0
    },
    states: {
    idle: {
    on: {
    FETCH: 'loading'
    }
    },
    loading: {
    on: {
    RESOLVE: 'success',
    REJECT: 'failure'
    }
    },
    success: {
    type: 'final'
    },
    failure: {
    on: {
    RETRY: {
    target: 'loading',
    actions: assign({
    retries: (context, event) => context.retries + 1
    })
    }
    }
    }
    }
    });