Skip to content

Instantly share code, notes, and snippets.

@pangratz
Last active March 30, 2020 16:14
Show Gist options
  • Save pangratz/03e09d293c228c77ce8e4f83d6ab9b45 to your computer and use it in GitHub Desktop.
Save pangratz/03e09d293c228c77ce8e4f83d6ab9b45 to your computer and use it in GitHub Desktop.

Revisions

  1. pangratz revised this gist Mar 30, 2020. 1 changed file with 12 additions and 1 deletion.
    13 changes: 12 additions & 1 deletion machine.js
    Original file line number Diff line number Diff line change
    @@ -36,7 +36,18 @@
    initial: "empty",
    states: {
    empty: {},
    filled: {}
    filled: {
    on: {
    MOVE: {
    target: "empty",
    actions: assign({
    current: null,
    past: context => [...context.past, context.current]
    }),
    cond: "hasCurrent"
    }
    }
    }
    }
    },
    queue: {
  2. pangratz revised this gist Mar 30, 2020. 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
    @@ -48,7 +48,8 @@
    MOVE: {
    target: "empty",
    actions: assign({
    current: context => context.queued[0]
    current: context => context.queued[0],
    queued: context => context.queued.slice(1)
    }),
    cond: "hasNext"
    }
  3. pangratz revised this gist Mar 30, 2020. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion machine.js
    Original file line number Diff line number Diff line change
    @@ -47,7 +47,9 @@
    on: {
    MOVE: {
    target: "empty",
    actions: assign({}),
    actions: assign({
    current: context => context.queued[0]
    }),
    cond: "hasNext"
    }
    }
  4. pangratz revised this gist Mar 30, 2020. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -59,6 +59,7 @@
    }
    }, {
    guards: {
    hasCurrent: context => !!context.current,
    hasNext: context => context.queue.length > 0
    }
    });
  5. pangratz revised this gist Mar 30, 2020. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion machine.js
    Original file line number Diff line number Diff line change
    @@ -47,7 +47,8 @@
    on: {
    MOVE: {
    target: "empty",
    actions: assign({})
    actions: assign({}),
    cond: "hasNext"
    }
    }
    }
    @@ -56,5 +57,9 @@
    }
    }
    }
    }, {
    guards: {
    hasNext: context => context.queue.length > 0
    }
    });

  6. pangratz revised this gist Mar 30, 2020. 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
    @@ -46,7 +46,8 @@
    filled: {
    on: {
    MOVE: {
    target: "empty"
    target: "empty",
    actions: assign({})
    }
    }
    }
  7. pangratz revised this gist Mar 30, 2020. 1 changed file with 27 additions and 3 deletions.
    30 changes: 27 additions & 3 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -25,9 +25,33 @@
    type: "parallel",

    states: {
    past: {},
    current: {},
    queue: {}
    past: {
    initial: "empty",
    states: {
    empty: {},
    filled: {}
    }
    },
    current: {
    initial: "empty",
    states: {
    empty: {},
    filled: {}
    }
    },
    queue: {
    initial: "empty",
    states: {
    empty: {},
    filled: {
    on: {
    MOVE: {
    target: "empty"
    }
    }
    }
    }
    }
    }
    }
    }
  8. pangratz created this gist Mar 30, 2020.
    35 changes: 35 additions & 0 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@

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

    const fetchMachine = Machine({
    id: 'fetch',
    initial: 'active',

    context: {
    past: [],
    current: null,
    queue: [1, 2, 3]
    },

    states: {
    active: {
    type: "parallel",

    states: {
    past: {},
    current: {},
    queue: {}
    }
    }
    }
    });