Skip to content

Instantly share code, notes, and snippets.

@xralphack
Last active February 25, 2021 04:18
Show Gist options
  • Save xralphack/23a12a3034ad319138febe0e9eb23f19 to your computer and use it in GitHub Desktop.
Save xralphack/23a12a3034ad319138febe0e9eb23f19 to your computer and use it in GitHub Desktop.

Revisions

  1. xralphack revised this gist Feb 25, 2021. 1 changed file with 17 additions and 19 deletions.
    36 changes: 17 additions & 19 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,6 @@ const yardianStateMachine = Machine({
    context: {
    retries: 0,
    deviceState: null,
    interval: 500,
    },
    states: {
    fetchingDeviceState: {
    @@ -60,29 +59,30 @@ const yardianStateMachine = Machine({
    }
    },
    offline: {

    },
    watering: {

    },
    firmwareUpdating: {

    },
    idle: {

    },
    },
    watering: {
    after: {
    500: {
    target: 'parsingDeviceState'
    }
    }
    },
    firmwareUpdating: {
    },
    idle: {
    },
    failure: {
    on: {
    '': {
    target: "fatal",
    cond: (context) => context.retries >= 3
    }
    },
    after: {
    1000: {
    target: 'fetchingDeviceState',
    }
    }
    // after: {
    // 1000: {
    // target: 'fetchingDeviceState',
    // }
    // }
    },
    fatal: {
    on: {
    @@ -104,8 +104,6 @@ const yardianStateMachine = Machine({
    guards: {
    'isOffline': ctx => ctx.deviceState && ctx.deviceState.offline,
    'isWatering': ctx => {

    console.log('check isWatering')
    return ctx.deviceState && ctx.deviceState.isWatering
    },
    'isFirmwareUpdating': ctx => ctx.deviceState && ctx.deviceState.isFirmwareUpdating
  2. xralphack revised this gist Feb 25, 2021. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    const fetchDeviceState = () => Promise.resolve({
    'online': true,
    'isWatering': Math.random() > 0.5,
    'isWatering': Math.random() > 0.1,
    'isFirmwareUpdating': false
    })

    @@ -103,7 +103,11 @@ const yardianStateMachine = Machine({
    },
    guards: {
    'isOffline': ctx => ctx.deviceState && ctx.deviceState.offline,
    'isWatering': ctx => ctx.deviceState && ctx.deviceState.isWatering,
    'isWatering': ctx => {

    console.log('check isWatering')
    return ctx.deviceState && ctx.deviceState.isWatering
    },
    'isFirmwareUpdating': ctx => ctx.deviceState && ctx.deviceState.isFirmwareUpdating
    }
    });
  3. xralphack revised this gist Feb 25, 2021. 1 changed file with 25 additions and 24 deletions.
    49 changes: 25 additions & 24 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@ const yardianStateMachine = Machine({
    id: 'fetchDeviceState',
    src: fetchDeviceState,
    onDone: {
    target: 'parseDeviceState',
    target: 'parsingDeviceState',
    actions: [
    'resetRetries',
    'setDeviceState',
    @@ -38,12 +38,25 @@ const yardianStateMachine = Machine({
    // }
    // }
    },
    parseDeviceState: {
    parsingDeviceState: {
    on: {
    '': {
    target: 'offline',
    cond: 'isOffline'
    }
    '': [
    {
    target: 'offline',
    cond: 'isOffline'
    },
    {
    target: 'watering',
    cond: 'isWatering'
    },
    {
    target: 'firmwareUpdating',
    cond: 'isFirmwareUpdating'
    },
    {
    target: 'idle',
    }
    ]
    }
    },
    offline: {
    @@ -52,24 +65,12 @@ const yardianStateMachine = Machine({
    watering: {

    },
    // {
    // target: "ready.watering",
    // cond: 'isWatering'
    // }, {
    // target: "ready.firmwareUpdating",
    // cond: "isFirmwareUpdating"
    // }, {
    // target: "ready.idle"
    // }
    // states: {

    // firmwareUpdating: {

    // },
    // idle: {

    // },
    // }
    firmwareUpdating: {

    },
    idle: {

    },
    failure: {
    on: {
    '': {
  4. xralphack revised this gist Feb 25, 2021. 1 changed file with 7 additions and 6 deletions.
    13 changes: 7 additions & 6 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -39,7 +39,12 @@ const yardianStateMachine = Machine({
    // }
    },
    parseDeviceState: {

    on: {
    '': {
    target: 'offline',
    cond: 'isOffline'
    }
    }
    },
    offline: {

    @@ -93,11 +98,7 @@ const yardianStateMachine = Machine({
    'resetRetries': assign({ retries: 0 }),
    'setDeviceState': assign({ deviceState: (_, event) => {
    console.log(event.data)
    return event.data


    }
    })
    return event.data }})
    },
    guards: {
    'isOffline': ctx => ctx.deviceState && ctx.deviceState.offline,
  5. xralphack revised this gist Feb 25, 2021. 1 changed file with 26 additions and 28 deletions.
    54 changes: 26 additions & 28 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -19,10 +19,10 @@ const yardianStateMachine = Machine({
    id: 'fetchDeviceState',
    src: fetchDeviceState,
    onDone: {
    target: 'ready',
    target: 'parseDeviceState',
    actions: [
    'resetRetries',
    'setDeviceState',
    // 'resetRetries',
    ]
    },
    onError: {
    @@ -38,35 +38,33 @@ const yardianStateMachine = Machine({
    // }
    // }
    },
    ready: {
    on: {
    '': [{
    target: "ready.offline",
    cond: "isOffline",
    }, {
    target: "ready.watering",
    cond: 'isWatering'
    }, {
    target: "ready.firmwareUpdating",
    cond: "isFirmwareUpdating"
    }, {
    target: "ready.idle"
    }]
    },
    states: {
    offline: {
    },
    watering: {
    parseDeviceState: {

    },
    firmwareUpdating: {
    },
    offline: {

    },
    watering: {

    },
    // {
    // target: "ready.watering",
    // cond: 'isWatering'
    // }, {
    // target: "ready.firmwareUpdating",
    // cond: "isFirmwareUpdating"
    // }, {
    // target: "ready.idle"
    // }
    // states: {

    // firmwareUpdating: {

    },
    idle: {
    // },
    // idle: {

    },
    }
    },
    // },
    // }
    failure: {
    on: {
    '': {
  6. xralphack revised this gist Feb 25, 2021. 1 changed file with 15 additions and 7 deletions.
    22 changes: 15 additions & 7 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    const fetchDeviceState = () => Promise.resolve({
    'online': true,
    'isWatering': Math.random() > 0.5
    'isWatering': Math.random() > 0.5,
    'isFirmwareUpdating': false
    })

    const yardianStateMachine = Machine({
    @@ -9,6 +10,7 @@ const yardianStateMachine = Machine({
    context: {
    retries: 0,
    deviceState: null,
    interval: 500,
    },
    states: {
    fetchingDeviceState: {
    @@ -20,7 +22,8 @@ const yardianStateMachine = Machine({
    target: 'ready',
    actions: [
    'setDeviceState',
    'resetRetries', ]
    // 'resetRetries',
    ]
    },
    onError: {
    target: 'failure'
    @@ -54,7 +57,7 @@ const yardianStateMachine = Machine({
    offline: {
    },
    watering: {

    },
    firmwareUpdating: {

    @@ -90,13 +93,18 @@ const yardianStateMachine = Machine({
    actions: {
    'incrementRetries': assign({ retries: context => context.retries + 1 }),
    'resetRetries': assign({ retries: 0 }),
    'setDeviceState': assign({ deviceState: (_, event) => event.data }),
    'parseDeviceState': (context, event) => {
    // console.log(event.data);
    'setDeviceState': assign({ deviceState: (_, event) => {
    console.log(event.data)
    return event.data


    }
    })
    },
    guards: {
    'isWatering': ctx => ctx.deviceState && ctx.deviceState.isWatering
    'isOffline': ctx => ctx.deviceState && ctx.deviceState.offline,
    'isWatering': ctx => ctx.deviceState && ctx.deviceState.isWatering,
    'isFirmwareUpdating': ctx => ctx.deviceState && ctx.deviceState.isFirmwareUpdating
    }
    });

  7. xralphack revised this gist Feb 25, 2021. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion machine.js
    Original file line number Diff line number Diff line change
    @@ -52,7 +52,6 @@ const yardianStateMachine = Machine({
    },
    states: {
    offline: {

    },
    watering: {

  8. xralphack revised this gist Feb 25, 2021. 1 changed file with 21 additions and 3 deletions.
    24 changes: 21 additions & 3 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -36,11 +36,29 @@ const yardianStateMachine = Machine({
    // }
    },
    ready: {
    actions: ['parseDeviceState'],
    initial: 'watering',
    on: {
    '': [{
    target: "ready.offline",
    cond: "isOffline",
    }, {
    target: "ready.watering",
    cond: 'isWatering'
    }, {
    target: "ready.firmwareUpdating",
    cond: "isFirmwareUpdating"
    }, {
    target: "ready.idle"
    }]
    },
    states: {
    offline: {

    },
    watering: {
    cond: 'isWatering'

    },
    firmwareUpdating: {

    },
    idle: {

  9. xralphack revised this gist Feb 25, 2021. 1 changed file with 12 additions and 7 deletions.
    19 changes: 12 additions & 7 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,8 @@ const yardianStateMachine = Machine({
    onDone: {
    target: 'ready',
    actions: [
    'resetRetries', 'onFetchDeviceStateSuccess']
    'setDeviceState',
    'resetRetries', ]
    },
    onError: {
    target: 'failure'
    @@ -35,12 +36,15 @@ const yardianStateMachine = Machine({
    // }
    },
    ready: {
    actions: ['resetRetries', 'parseDeviceState'],
    actions: ['parseDeviceState'],
    initial: 'watering',
    states: {
    watering: {
    cond: 'isWatering'
    },
    idle: {

    }
    },
    }
    },
    failure: {
    @@ -69,12 +73,13 @@ const yardianStateMachine = Machine({
    actions: {
    'incrementRetries': assign({ retries: context => context.retries + 1 }),
    'resetRetries': assign({ retries: 0 }),
    'onFetchDeviceStateSuccess': (_, event) => {
    console.log(event)
    },
    'setDeviceState': assign({ deviceState: (_, event) => event.data }),
    'parseDeviceState': (context, event) => {
    console.log(event.data);
    // console.log(event.data);
    }
    },
    guards: {
    'isWatering': ctx => ctx.deviceState && ctx.deviceState.isWatering
    }
    });

  10. xralphack revised this gist Feb 25, 2021. 1 changed file with 14 additions and 5 deletions.
    19 changes: 14 additions & 5 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,14 @@
    const fetchDeviceState = () =>
    Math.random() > 0.8 ? Promise.resolve() : Promise.reject()
    const fetchDeviceState = () => Promise.resolve({
    'online': true,
    'isWatering': Math.random() > 0.5
    })

    const yardianStateMachine = Machine({
    id: 'yardian-state-machine',
    initial: 'fetchingDeviceState',
    context: {
    retries: 0
    retries: 0,
    deviceState: null,
    },
    states: {
    fetchingDeviceState: {
    @@ -14,7 +17,9 @@ const yardianStateMachine = Machine({
    id: 'fetchDeviceState',
    src: fetchDeviceState,
    onDone: {
    target: 'success'
    target: 'ready',
    actions: [
    'resetRetries', 'onFetchDeviceStateSuccess']
    },
    onError: {
    target: 'failure'
    @@ -29,8 +34,9 @@ const yardianStateMachine = Machine({
    // }
    // }
    },
    success: {
    ready: {
    actions: ['resetRetries', 'parseDeviceState'],
    initial: 'watering',
    states: {
    watering: {

    @@ -63,6 +69,9 @@ const yardianStateMachine = Machine({
    actions: {
    'incrementRetries': assign({ retries: context => context.retries + 1 }),
    'resetRetries': assign({ retries: 0 }),
    'onFetchDeviceStateSuccess': (_, event) => {
    console.log(event)
    },
    'parseDeviceState': (context, event) => {
    console.log(event.data);
    }
  11. xralphack revised this gist Feb 24, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -30,7 +30,7 @@ const yardianStateMachine = Machine({
    // }
    },
    success: {
    entry: ['resetRetries', 'parseDeviceState'],
    actions: ['resetRetries', 'parseDeviceState'],
    states: {
    watering: {

    @@ -63,7 +63,7 @@ const yardianStateMachine = Machine({
    actions: {
    'incrementRetries': assign({ retries: context => context.retries + 1 }),
    'resetRetries': assign({ retries: 0 }),
    parseDeviceState: (context, event) => {
    'parseDeviceState': (context, event) => {
    console.log(event.data);
    }
    }
  12. xralphack revised this gist Feb 24, 2021. 1 changed file with 10 additions and 2 deletions.
    12 changes: 10 additions & 2 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -30,7 +30,12 @@ const yardianStateMachine = Machine({
    // }
    },
    success: {
    entry: 'resetRetries'
    entry: ['resetRetries', 'parseDeviceState'],
    states: {
    watering: {

    }
    }
    },
    failure: {
    on: {
    @@ -57,7 +62,10 @@ const yardianStateMachine = Machine({
    }, {
    actions: {
    'incrementRetries': assign({ retries: context => context.retries + 1 }),
    'resetRetries': assign({ retries: 0 })
    'resetRetries': assign({ retries: 0 }),
    parseDeviceState: (context, event) => {
    console.log(event.data);
    }
    }
    });

  13. xralphack revised this gist Feb 24, 2021. 1 changed file with 22 additions and 25 deletions.
    47 changes: 22 additions & 25 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -9,58 +9,55 @@ const yardianStateMachine = Machine({
    },
    states: {
    fetchingDeviceState: {
    // invoke: {
    // id: 'fetchDeviceState',
    // src: fetchDeviceState,
    // onDone: {
    // target: 'success'
    // },
    // onError: {
    // target: 'failure'
    // }
    // }
    on: {
    success: {
    entry: ['incrementRetries'],
    invoke: {
    id: 'fetchDeviceState',
    src: fetchDeviceState,
    onDone: {
    target: 'success'
    },
    failure: {
    onError: {
    target: 'failure'
    }
    }
    // on: {
    // success: {
    // target: 'success'
    // },
    // failure: {
    // target: 'failure'
    // }
    // }
    },
    success: {
    entry: () => assign({
    retires: 0
    })
    entry: 'resetRetries'
    },
    failure: {
    entry: (context) => {
    console.log(context)
    },
    on: {
    '': {
    target: "fatal",
    cond: (context) => context.retries > 2
    cond: (context) => context.retries >= 3
    }
    },
    after: {
    1000: {
    target: 'fetchingDeviceState',
    actions: assign({
    retries: context => context.retries+ 1
    })
    }
    }
    },
    fatal: {
    on: {
    RETRY: {
    target: 'fetchingDeviceState'
    target: 'fetchingDeviceState',
    actions: ['resetRetries']
    }
    }
    }
    }
    }, {

    actions: {
    'incrementRetries': assign({ retries: context => context.retries + 1 }),
    'resetRetries': assign({ retries: 0 })
    }
    });

  14. xralphack revised this gist Feb 24, 2021. 1 changed file with 24 additions and 13 deletions.
    37 changes: 24 additions & 13 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -9,18 +9,29 @@ const yardianStateMachine = Machine({
    },
    states: {
    fetchingDeviceState: {
    invoke: {
    id: 'fetchDeviceState',
    src: fetchDeviceState,
    onDone: {
    // invoke: {
    // id: 'fetchDeviceState',
    // src: fetchDeviceState,
    // onDone: {
    // target: 'success'
    // },
    // onError: {
    // target: 'failure'
    // }
    // }
    on: {
    success: {
    target: 'success'
    },
    onError: {
    failure: {
    target: 'failure'
    }
    }
    },
    success: {
    entry: () => assign({
    retires: 0
    })
    },
    failure: {
    entry: (context) => {
    @@ -38,18 +49,18 @@ const yardianStateMachine = Machine({
    actions: assign({
    retries: context => context.retries+ 1
    })
    },
    // RETRY: {
    // target: 'fetchingDeviceState',
    // actions: assign({
    // retries: (context, event) => context.retries + 1
    // })
    // }
    }
    }
    },
    fatal: {

    on: {
    RETRY: {
    target: 'fetchingDeviceState'
    }
    }
    }
    }
    }, {

    });

  15. xralphack revised this gist Feb 24, 2021. 1 changed file with 25 additions and 9 deletions.
    34 changes: 25 additions & 9 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,5 @@
    const fetchDeviceState = () => Promise.resolve({
    'online': true,
    'isWatering': Math.random() > 0.5
    })
    const fetchDeviceState = () =>
    Math.random() > 0.8 ? Promise.resolve() : Promise.reject()

    const yardianStateMachine = Machine({
    id: 'yardian-state-machine',
    @@ -12,19 +10,34 @@ const yardianStateMachine = Machine({
    states: {
    fetchingDeviceState: {
    invoke: {
    id: 'fetch'
    id: 'fetchDeviceState',
    src: fetchDeviceState,
    onDone: {
    target: 'success'
    },
    onError: {
    target: 'failure'
    }
    }
    },
    success: {
    type: 'final'
    },
    failure: {
    invoke: {
    entry: (context) => {
    console.log(context)
    },
    on: {
    '': {
    target: 'fetchingDeviceState'
    target: "fatal",
    cond: (context) => context.retries > 2
    }
    },
    after: {
    1000: {
    target: 'fetchingDeviceState',
    actions: assign({
    retries: context => context.retries+ 1
    })
    },
    // RETRY: {
    // target: 'fetchingDeviceState',
    @@ -33,6 +46,9 @@ const yardianStateMachine = Machine({
    // })
    // }
    }
    },
    fatal: {

    }
    }
    });
  16. xralphack revised this gist Feb 24, 2021. 1 changed file with 33 additions and 25 deletions.
    58 changes: 33 additions & 25 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -1,31 +1,39 @@

    const fetchDeviceState = () => Promise.resolve({
    'online': true,
    'isWatering': Math.random() > 0.5
    })

    const yardianStateMachine = Machine({
    id: 'yardian-state-machine',
    initial: 'fetchingDeviceState',
    context: {
    retries: 0
    const yardianStateMachine = Machine({
    id: 'yardian-state-machine',
    initial: 'fetchingDeviceState',
    context: {
    retries: 0
    },
    states: {
    fetchingDeviceState: {
    invoke: {
    id: 'fetch'
    }
    },
    states: {
    fetchingDeviceState: {
    on: {
    RESOLVE: 'success',
    REJECT: 'failure'
    }
    },
    success: {
    type: 'final'
    success: {
    type: 'final'
    },
    failure: {
    invoke: {

    },
    failure: {
    on: {
    RETRY: {
    target: 'fetchingDeviceState',
    actions: assign({
    retries: (context, event) => context.retries + 1
    })
    }
    }
    on: {
    '': {
    target: 'fetchingDeviceState'
    },
    // RETRY: {
    // target: 'fetchingDeviceState',
    // actions: assign({
    // retries: (context, event) => context.retries + 1
    // })
    // }
    }
    }
    });
    }
    });

  17. xralphack created this gist Feb 24, 2021.
    31 changes: 31 additions & 0 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@


    const yardianStateMachine = Machine({
    id: 'yardian-state-machine',
    initial: 'fetchingDeviceState',
    context: {
    retries: 0
    },
    states: {
    fetchingDeviceState: {
    on: {
    RESOLVE: 'success',
    REJECT: 'failure'
    }
    },
    success: {
    type: 'final'
    },
    failure: {
    on: {
    RETRY: {
    target: 'fetchingDeviceState',
    actions: assign({
    retries: (context, event) => context.retries + 1
    })
    }
    }
    }
    }
    });