Last active
March 20, 2021 03:39
-
-
Save cristiano-belloni/be02f8904713cb8203a4e0baebdf627c to your computer and use it in GitHub Desktop.
Revisions
-
cristiano-belloni revised this gist
Sep 22, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -31,7 +31,7 @@ }, ready: { on: { START: 'started', }}, started: { on: { '': [ -
cristiano-belloni revised this gist
Sep 21, 2020 . 1 changed file with 6 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 allow cancellation? // Maybe START / PAUSE / CANCEL events? const fetchMachine = Machine({ id: 'backoff', @@ -32,15 +31,15 @@ }, ready: { on: { START: 'requesting', }}, started: { on: { '': [ { target: 'requesting', cond: 'canRetry'}, { target: 'failure' } ]} }, 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: 'waiting', actions: [assign({ error: (context, event) => event.data }), assign({ attempts: (context, event) => context.attempts + 1 @@ -64,7 +63,7 @@ failure: { type: 'final' }, waiting: { after: { DELAY: { target: 'started', -
cristiano-belloni revised this gist
Sep 21, 2020 . 1 changed file with 4 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -28,7 +28,7 @@ { target: 'ready', cond: 'validateConf' }, { target: 'failure' } ]}, entry: ['setConfiguration'] }, ready: { on: { @@ -82,14 +82,11 @@ canRetry: (context, event) => { return context.attempts < 3; }, validateConf: (context, event) => !!event.promise //maybe?? }, actions: { setConfiguration: assign({ promise: (_, event) => event.promise }) } }); -
cristiano-belloni revised this gist
Sep 21, 2020 . 1 changed file with 11 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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', }}, started: { on: { '': [ @@ -81,9 +82,14 @@ canRetry: (context, event) => { return context.attempts < 3; }, validateConf: (event) => { console.log(event.promise) return !!event.promise //maybe?? } }, actions: { setConfiguration: assign({ promise: (_, event) => { return event.promise} }) } }); -
cristiano-belloni revised this gist
Sep 21, 2020 . 1 changed file with 15 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -17,13 +17,20 @@ // Maybe START / PAUSE events? const fetchMachine = Machine({ id: 'backoff', initial: 'init', context: { attempts: 0 }, states: { init: { on: {CONFIGURE: [ { target: 'ready', cond: 'validateConf' }, { target: 'failure' } ]} }, ready: { on: { START: 'loading' }}, started: { @@ -58,21 +65,24 @@ }, wait: { after: { DELAY: { target: 'started', } } } } }, { delays: { 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 } } -
cristiano-belloni revised this gist
Sep 21, 2020 . No changes.There are no files selected for viewing
-
cristiano-belloni revised this gist
Sep 21, 2020 . 1 changed file with 8 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 allow cancellation? // Maybe START / PAUSE events? @@ -24,15 +23,18 @@ attempts: 0 }, states: { idle: { on: { START: 'loading' }}, started: { on: { '': [ { target: 'loading', cond: 'canRetry'}, { target: 'failure' } ]} }, loading: { invoke: { id: 'fetch', src: (context, event) => new Promise((_, reject) => reject("rejected")), onDone: { target: 'success', @@ -57,20 +59,20 @@ wait: { after: { LINEAR_DELAY: { target: 'started', } } } } }, { delays: { LINEAR_DELAY: (context, event) => { return Math.min(1000 * 2 ** context.attempts, 30000); }, }, guards: { canRetry: (context, event) => { return context.attempts < 3; } } -
cristiano-belloni revised this gist
Sep 20, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 / PAUSE events? const fetchMachine = Machine({ id: 'linear_backoff', -
cristiano-belloni revised this gist
Sep 20, 2020 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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', -
cristiano-belloni revised this gist
Sep 20, 2020 . 1 changed file with 15 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -30,10 +30,22 @@ ]} }, loading: { 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', } } } -
cristiano-belloni revised this gist
Sep 20, 2020 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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', -
cristiano-belloni revised this gist
Sep 20, 2020 . 1 changed file with 5 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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', -
cristiano-belloni revised this gist
Sep 20, 2020 . 1 changed file with 6 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,12 +12,12 @@ const fetchMachine = Machine({ id: 'linear_backoff', initial: 'idle', context: { attempts: 0 }, states: { idle: { on: { '': [ { target: 'loading', cond: 'canRetry'}, { target: 'failure' } @@ -26,7 +26,7 @@ loading: { on: { RESOLVE: 'success', REJECT: 'wait' } }, success: { @@ -35,10 +35,10 @@ failure: { type: 'final' }, wait: { after: { LINEAR_DELAY: { target: 'idle', actions: assign({ attempts: (context, event) => context.attempts + 1 }), @@ -49,7 +49,7 @@ }, { delays: { LINEAR_DELAY: (context, event) => { return (context.attempts + 1) * 500; }, }, guards: { -
cristiano-belloni revised this gist
Sep 20, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,7 +11,7 @@ // - XState (all XState exports) const fetchMachine = Machine({ id: 'linear_backoff', initial: 'starting', context: { attempts: 0 -
cristiano-belloni revised this gist
Sep 18, 2020 . 1 changed file with 23 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,15 +12,16 @@ const fetchMachine = Machine({ id: 'fetch', initial: 'starting', context: { attempts: 0 }, states: { 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: 'starting', actions: assign({ attempts: (context, event) => context.attempts + 1 }), } } } } }, { delays: { LINEAR_DELAY: (context, event) => { return context.attempts * 500; }, }, guards: { canRetry: (context, event) => { return context.attempts < 5; } } });
-
cristiano-belloni created this gist
Sep 18, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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; }, }});