Skip to content

Instantly share code, notes, and snippets.

@cristiano-belloni
Last active March 20, 2021 03:39
Show Gist options
  • Select an option

  • Save cristiano-belloni/be02f8904713cb8203a4e0baebdf627c to your computer and use it in GitHub Desktop.

Select an option

Save cristiano-belloni/be02f8904713cb8203a4e0baebdf627c to your computer and use it in GitHub Desktop.

Revisions

  1. cristiano-belloni revised this gist Sep 22, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion machine.js
    Original file line number Diff line number Diff line change
    @@ -31,7 +31,7 @@

    },
    ready: { on: {
    START: 'requesting',
    START: 'started',
    }},
    started: {
    on: { '': [
  2. cristiano-belloni revised this gist Sep 21, 2020. 1 changed file with 6 additions and 7 deletions.
    13 changes: 6 additions & 7 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -12,9 +12,8 @@

    // We should move the defaults in (if possible, delays and guards), but only if this helps readability
    // We should know how to get / override arguments (maxRetries, time??, see useQuery). Some are needed and some have a default
    // We should remove resolve / reject and just pass a Promise as argument
    // We should allow cancellation?
    // Maybe START / PAUSE events?
    // Maybe START / PAUSE / CANCEL events?

    const fetchMachine = Machine({
    id: 'backoff',
    @@ -32,15 +31,15 @@

    },
    ready: { on: {
    START: 'loading',
    START: 'requesting',
    }},
    started: {
    on: { '': [
    { target: 'loading', cond: 'canRetry'},
    { target: 'requesting', cond: 'canRetry'},
    { target: 'failure' }
    ]}
    },
    loading: {
    requesting: {
    invoke: {
    id: 'fetch',
    src: (context, event) => new Promise((_, reject) => reject("rejected")),
    @@ -49,7 +48,7 @@
    actions: assign({ user: (context, event) => event.data })
    },
    onError: {
    target: 'wait',
    target: 'waiting',
    actions: [assign({ error: (context, event) => event.data }),
    assign({
    attempts: (context, event) => context.attempts + 1
    @@ -64,7 +63,7 @@
    failure: {
    type: 'final'
    },
    wait: {
    waiting: {
    after: {
    DELAY: {
    target: 'started',
  3. cristiano-belloni revised this gist Sep 21, 2020. 1 changed file with 4 additions and 7 deletions.
    11 changes: 4 additions & 7 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -28,7 +28,7 @@
    { target: 'ready', cond: 'validateConf' },
    { target: 'failure' }
    ]},
    exit: ['setConfiguration']
    entry: ['setConfiguration']

    },
    ready: { on: {
    @@ -82,14 +82,11 @@
    canRetry: (context, event) => {
    return context.attempts < 3;
    },
    validateConf: (event) => {
    console.log(event.promise)
    return !!event.promise //maybe??
    }
    validateConf: (context, event) => !!event.promise //maybe??

    },
    actions: {
    setConfiguration: assign({ promise: (_, event) => {
    return event.promise} })
    setConfiguration: assign({ promise: (_, event) => event.promise })
    }

    });
  4. cristiano-belloni revised this gist Sep 21, 2020. 1 changed file with 11 additions and 5 deletions.
    16 changes: 11 additions & 5 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -27,11 +27,12 @@
    on: {CONFIGURE: [
    { target: 'ready', cond: 'validateConf' },
    { target: 'failure' }
    ]}

    ]},
    exit: ['setConfiguration']

    },
    ready: { on: {
    START: 'loading'
    START: 'loading',
    }},
    started: {
    on: { '': [
    @@ -81,9 +82,14 @@
    canRetry: (context, event) => {
    return context.attempts < 3;
    },
    validateConf: (context) => {
    return !!context.promise //maybe
    validateConf: (event) => {
    console.log(event.promise)
    return !!event.promise //maybe??
    }
    },
    actions: {
    setConfiguration: assign({ promise: (_, event) => {
    return event.promise} })
    }

    });
  5. cristiano-belloni revised this gist Sep 21, 2020. 1 changed file with 15 additions and 5 deletions.
    20 changes: 15 additions & 5 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -17,13 +17,20 @@
    // Maybe START / PAUSE events?

    const fetchMachine = Machine({
    id: 'linear_backoff',
    initial: 'idle',
    id: 'backoff',
    initial: 'init',
    context: {
    attempts: 0
    },
    states: {
    idle: { on: {
    init: {
    on: {CONFIGURE: [
    { target: 'ready', cond: 'validateConf' },
    { target: 'failure' }
    ]}

    },
    ready: { on: {
    START: 'loading'
    }},
    started: {
    @@ -58,21 +65,24 @@
    },
    wait: {
    after: {
    LINEAR_DELAY: {
    DELAY: {
    target: 'started',
    }
    }
    }
    }
    }, {
    delays: {
    LINEAR_DELAY: (context, event) => {
    DELAY: (context, event) => {
    return Math.min(1000 * 2 ** context.attempts, 30000);
    },
    },
    guards: {
    canRetry: (context, event) => {
    return context.attempts < 3;
    },
    validateConf: (context) => {
    return !!context.promise //maybe
    }
    }

  6. cristiano-belloni revised this gist Sep 21, 2020. No changes.
  7. cristiano-belloni revised this gist Sep 21, 2020. 1 changed file with 8 additions and 6 deletions.
    14 changes: 8 additions & 6 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,6 @@
    // We should move the defaults in (if possible, delays and guards), but only if this helps readability
    // We should know how to get / override arguments (maxRetries, time??, see useQuery). Some are needed and some have a default
    // We should remove resolve / reject and just pass a Promise as argument
    // We should have a real exponential backoff algo
    // We should allow cancellation?
    // Maybe START / PAUSE events?

    @@ -24,15 +23,18 @@
    attempts: 0
    },
    states: {
    idle: {
    idle: { on: {
    START: 'loading'
    }},
    started: {
    on: { '': [
    { target: 'loading', cond: 'canRetry'},
    { target: 'failure' }
    ]}
    },
    loading: {
    invoke: {
    id: 'getUser',
    id: 'fetch',
    src: (context, event) => new Promise((_, reject) => reject("rejected")),
    onDone: {
    target: 'success',
    @@ -57,20 +59,20 @@
    wait: {
    after: {
    LINEAR_DELAY: {
    target: 'idle',
    target: 'started',
    }
    }
    }
    }
    }, {
    delays: {
    LINEAR_DELAY: (context, event) => {
    return (context.attempts + 1) * 500;
    return Math.min(1000 * 2 ** context.attempts, 30000);
    },
    },
    guards: {
    canRetry: (context, event) => {
    return context.attempts < 5;
    return context.attempts < 3;
    }
    }

  8. cristiano-belloni revised this gist Sep 20, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion machine.js
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@
    // We should remove resolve / reject and just pass a Promise as argument
    // We should have a real exponential backoff algo
    // We should allow cancellation?
    // Maybe START event?
    // Maybe START / PAUSE events?

    const fetchMachine = Machine({
    id: 'linear_backoff',
  9. cristiano-belloni revised this gist Sep 20, 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
    @@ -15,6 +15,7 @@
    // We should remove resolve / reject and just pass a Promise as argument
    // We should have a real exponential backoff algo
    // We should allow cancellation?
    // Maybe START event?

    const fetchMachine = Machine({
    id: 'linear_backoff',
  10. cristiano-belloni revised this gist Sep 20, 2020. 1 changed file with 15 additions and 6 deletions.
    21 changes: 15 additions & 6 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -30,10 +30,22 @@
    ]}
    },
    loading: {
    on: {
    RESOLVE: 'success',
    REJECT: 'wait'
    invoke: {
    id: 'getUser',
    src: (context, event) => new Promise((_, reject) => reject("rejected")),
    onDone: {
    target: 'success',
    actions: assign({ user: (context, event) => event.data })
    },
    onError: {
    target: 'wait',
    actions: [assign({ error: (context, event) => event.data }),
    assign({
    attempts: (context, event) => context.attempts + 1
    })
    ]
    }
    }
    },
    success: {
    type: 'final'
    @@ -45,9 +57,6 @@
    after: {
    LINEAR_DELAY: {
    target: 'idle',
    actions: assign({
    attempts: (context, event) => context.attempts + 1
    }),
    }
    }
    }
  11. cristiano-belloni revised this gist Sep 20, 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
    @@ -14,6 +14,7 @@
    // We should know how to get / override arguments (maxRetries, time??, see useQuery). Some are needed and some have a default
    // We should remove resolve / reject and just pass a Promise as argument
    // We should have a real exponential backoff algo
    // We should allow cancellation?

    const fetchMachine = Machine({
    id: 'linear_backoff',
  12. cristiano-belloni revised this gist Sep 20, 2020. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,11 @@
    // - actions
    // - XState (all XState exports)

    // We should move the defaults in (if possible, delays and guards), but only if this helps readability
    // We should know how to get / override arguments (maxRetries, time??, see useQuery). Some are needed and some have a default
    // We should remove resolve / reject and just pass a Promise as argument
    // We should have a real exponential backoff algo

    const fetchMachine = Machine({
    id: 'linear_backoff',
    initial: 'idle',
  13. cristiano-belloni revised this gist Sep 20, 2020. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -12,12 +12,12 @@

    const fetchMachine = Machine({
    id: 'linear_backoff',
    initial: 'starting',
    initial: 'idle',
    context: {
    attempts: 0
    },
    states: {
    starting: {
    idle: {
    on: { '': [
    { target: 'loading', cond: 'canRetry'},
    { target: 'failure' }
    @@ -26,7 +26,7 @@
    loading: {
    on: {
    RESOLVE: 'success',
    REJECT: 'retry'
    REJECT: 'wait'
    }
    },
    success: {
    @@ -35,10 +35,10 @@
    failure: {
    type: 'final'
    },
    retry: {
    wait: {
    after: {
    LINEAR_DELAY: {
    target: 'starting',
    target: 'idle',
    actions: assign({
    attempts: (context, event) => context.attempts + 1
    }),
    @@ -49,7 +49,7 @@
    }, {
    delays: {
    LINEAR_DELAY: (context, event) => {
    return context.attempts * 500;
    return (context.attempts + 1) * 500;
    },
    },
    guards: {
  14. cristiano-belloni revised this gist Sep 20, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion machine.js
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@
    // - XState (all XState exports)

    const fetchMachine = Machine({
    id: 'fetch',
    id: 'linear_backoff',
    initial: 'starting',
    context: {
    attempts: 0
  15. cristiano-belloni revised this gist Sep 18, 2020. 1 changed file with 23 additions and 11 deletions.
    34 changes: 23 additions & 11 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -12,15 +12,16 @@

    const fetchMachine = Machine({
    id: 'fetch',
    initial: 'idle',
    initial: 'starting',
    context: {
    retries: 0
    attempts: 0
    },
    states: {
    idle: {
    on: {
    FETCH: 'loading'
    }
    starting: {
    on: { '': [
    { target: 'loading', cond: 'canRetry'},
    { target: 'failure' }
    ]}
    },
    loading: {
    on: {
    @@ -31,20 +32,31 @@
    success: {
    type: 'final'
    },
    failure: {
    type: 'final'
    },
    retry: {
    after: {
    LINEAR_DELAY: {
    target: 'idle',
    target: 'starting',
    actions: assign({
    retries: (context, event) => context.retries + 1
    attempts: (context, event) => context.attempts + 1
    }),
    }
    }
    }
    }
    }, {delays: {
    }, {
    delays: {
    LINEAR_DELAY: (context, event) => {
    return context.retries * 500;
    return context.attempts * 500;
    },
    }});
    },
    guards: {
    canRetry: (context, event) => {
    return context.attempts < 5;
    }
    }

    });

  16. cristiano-belloni created this gist Sep 18, 2020.
    50 changes: 50 additions & 0 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@

    // 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: 'retry'
    }
    },
    success: {
    type: 'final'
    },
    retry: {
    after: {
    LINEAR_DELAY: {
    target: 'idle',
    actions: assign({
    retries: (context, event) => context.retries + 1
    }),
    }
    }
    }
    }
    }, {delays: {
    LINEAR_DELAY: (context, event) => {
    return context.retries * 500;
    },
    }});