Skip to content

Instantly share code, notes, and snippets.

@hosmelq
Last active March 11, 2020 18:30
Show Gist options
  • Save hosmelq/dd2aeeb5d477e2049334cd66f9201e40 to your computer and use it in GitHub Desktop.
Save hosmelq/dd2aeeb5d477e2049334cd66f9201e40 to your computer and use it in GitHub Desktop.

Revisions

  1. hosmelq revised this gist Mar 11, 2020. 1 changed file with 1 addition and 5 deletions.
    6 changes: 1 addition & 5 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -136,11 +136,7 @@ Machine(
    onDone: `done`,
    onError: [
    {
    target: `done`,
    actions: (ctx, event) => {
    console.log(`ctx: `, ctx)
    console.log(`event: `, event)
    }
    target: `done`
    }
    ]
    }
  2. hosmelq revised this gist Mar 11, 2020. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion machine.js
    Original file line number Diff line number Diff line change
    @@ -136,7 +136,11 @@ Machine(
    onDone: `done`,
    onError: [
    {
    target: `done`
    target: `done`,
    actions: (ctx, event) => {
    console.log(`ctx: `, ctx)
    console.log(`event: `, event)
    }
    }
    ]
    }
  3. hosmelq revised this gist Mar 11, 2020. 1 changed file with 1 addition and 5 deletions.
    6 changes: 1 addition & 5 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -136,11 +136,7 @@ Machine(
    onDone: `done`,
    onError: [
    {
    target: `done`,
    actions: (ctx, event) => {
    console.log(`ctx: `, ctx)
    console.log(`event: `, event)
    }
    target: `done`
    }
    ]
    }
  4. hosmelq revised this gist Mar 11, 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
    @@ -22,7 +22,7 @@ Machine(
    initial: `initializing`,
    context: {
    fetchKey: 1,
    online: true,
    online: false,
    offer: {
    id: null,
    receivedAt: null
  5. hosmelq revised this gist Mar 11, 2020. 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
    @@ -10,7 +10,7 @@ const ORDER_STATE = {
    IN_PROGRESS: `IN_PROGRESS`,
    WAITING_COURIER: `WAITING_COURIER`
    }
    const CURRENT_ORDER_TYPE = `HIRED`
    const CURRENT_ORDER_TYPE = `DELIVERY`
    const ORDER_TYPE = {
    DELIVERY: `DELIVERY`,
    HIRED: `HIRED`
    @@ -22,7 +22,7 @@ Machine(
    initial: `initializing`,
    context: {
    fetchKey: 1,
    online: false,
    online: true,
    offer: {
    id: null,
    receivedAt: null
  6. hosmelq revised this gist Mar 11, 2020. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ const ORDER_STATE = {
    IN_PROGRESS: `IN_PROGRESS`,
    WAITING_COURIER: `WAITING_COURIER`
    }
    const CURRENT_ORDER_TYPE = `DELIVERY`
    const CURRENT_ORDER_TYPE = `HIRED`
    const ORDER_TYPE = {
    DELIVERY: `DELIVERY`,
    HIRED: `HIRED`
    @@ -105,15 +105,14 @@ Machine(
    },
    {
    cond: `isHiredOrder`,
    target: `assignment`
    actions: `incrementFetchKey`,
    target: `waiting`
    }
    ],
    onError: `waiting`
    }
    },

    assignment: {},

    offer: {
    onDone: `waiting`,
    initial: `waiting`,
  7. hosmelq revised this gist Mar 11, 2020. 1 changed file with 3 additions and 6 deletions.
    9 changes: 3 additions & 6 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ Machine(
    initial: `initializing`,
    context: {
    fetchKey: 1,
    online: true,
    online: false,
    offer: {
    id: null,
    receivedAt: null
    @@ -182,7 +182,7 @@ Machine(
    setOnline: assign({
    online: () => true,
    }),

    setOfferId: assign({
    offer: () => ({
    acceptedAt: new Date(),
    @@ -225,10 +225,7 @@ Machine(
    isAssigned: (ctx, event) => ORDER_STATE.IN_PROGRESS === CURRENT_ORDER_STATE,
    isDeliveryOrder: (ctx, event) => ORDER_TYPE.DELIVERY === CURRENT_ORDER_TYPE,
    isHiredOrder: (ctx, event) => ORDER_TYPE.HIRED === CURRENT_ORDER_TYPE,

    hasEnoughTimeToAcceptOrder: ({offer: {receivedAt}}) => {
    return true
    },

    doesNotHaveEnoughTimeToAcceptOrder: ({offer: {receivedAt}}) => {
    return false
    }
  8. hosmelq revised this gist Mar 11, 2020. 1 changed file with 1 addition and 18 deletions.
    19 changes: 1 addition & 18 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -116,25 +116,8 @@ Machine(

    offer: {
    onDone: `waiting`,
    initial: `validating`,
    initial: `waiting`,
    states: {
    validating: {
    invoke: {
    id: `validateOrder`,
    src: `validateOrder`,
    onDone: [
    {
    cond: `hasEnoughTimeToAcceptOrder`,
    target: `waiting`
    },
    {
    target: `done`
    }
    ],
    onError: `done`
    }
    },

    waiting: {
    entry: `vibrate`,
    exit: `cancelVibration`,
  9. hosmelq revised this gist Mar 11, 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
    @@ -5,7 +5,7 @@ function buildVibrationPattern(seconds) {
    }

    const DEFAULT_WAIT_TIME = 30000
    const CURRENT_ORDER_STATE = `IN_PROGRESS`
    const CURRENT_ORDER_STATE = `WAITING_COURIER`
    const ORDER_STATE = {
    IN_PROGRESS: `IN_PROGRESS`,
    WAITING_COURIER: `WAITING_COURIER`
  10. hosmelq revised this gist Mar 11, 2020. 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
    @@ -244,10 +244,10 @@ Machine(
    isHiredOrder: (ctx, event) => ORDER_TYPE.HIRED === CURRENT_ORDER_TYPE,

    hasEnoughTimeToAcceptOrder: ({offer: {receivedAt}}) => {
    return Math.floor(receivedAt + DEFAULT_WAIT_TIME - Date.now()) > 2500
    return true
    },
    doesNotHaveEnoughTimeToAcceptOrder: ({offer: {receivedAt}}) => {
    return Math.floor(receivedAt + DEFAULT_WAIT_TIME - Date.now()) < 2500
    return false
    }
    }
    }
  11. hosmelq revised this gist Mar 11, 2020. No changes.
  12. hosmelq revised this gist Mar 11, 2020. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion machine.js
    Original file line number Diff line number Diff line change
    @@ -95,6 +95,10 @@ Machine(
    actions: `incrementFetchKey`,
    target: `waiting`
    },
    {
    cond: `doesNotHaveEnoughTimeToAcceptOrder`,
    target: `waiting`
    },
    {
    cond: `isDeliveryOrder`,
    target: `offer`
    @@ -240,9 +244,11 @@ Machine(
    isHiredOrder: (ctx, event) => ORDER_TYPE.HIRED === CURRENT_ORDER_TYPE,

    hasEnoughTimeToAcceptOrder: ({offer: {receivedAt}}) => {
    return true
    return Math.floor(receivedAt + DEFAULT_WAIT_TIME - Date.now()) > 2500
    },
    doesNotHaveEnoughTimeToAcceptOrder: ({offer: {receivedAt}}) => {
    return Math.floor(receivedAt + DEFAULT_WAIT_TIME - Date.now()) < 2500
    }
    }
    }
    )
  13. hosmelq revised this gist Mar 11, 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
    @@ -212,7 +212,7 @@ Machine(
    }),

    incrementFetchKey: assign({
    fetchKey: (ctx) => ++ctx.fetchKey,
    fetchKey: (ctx) => ctx.fetchKey + 1,
    }),

    vibrate: () => {
  14. hosmelq revised this gist Mar 11, 2020. 1 changed file with 2 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -93,7 +93,7 @@ Machine(
    {
    cond: `isAssigned`,
    actions: `incrementFetchKey`,
    target: `offer`
    target: `waiting`
    },
    {
    cond: `isDeliveryOrder`,
    @@ -212,9 +212,7 @@ Machine(
    }),

    incrementFetchKey: assign({
    fetchKey: (ctx) => ({
    fetchKey: ++ctx.fetchKey
    }),
    fetchKey: (ctx) => ++ctx.fetchKey,
    }),

    vibrate: () => {
  15. hosmelq revised this gist Mar 11, 2020. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,6 @@ function buildVibrationPattern(seconds) {
    .reduce(prev => [...prev, 0, 1000, 500], [])
    }

    const ONLINE = true
    const DEFAULT_WAIT_TIME = 30000
    const CURRENT_ORDER_STATE = `IN_PROGRESS`
    const ORDER_STATE = {
    @@ -23,7 +22,7 @@ Machine(
    initial: `initializing`,
    context: {
    fetchKey: 1,
    online: ONLINE,
    online: true,
    offer: {
    id: null,
    receivedAt: null
  16. hosmelq revised this gist Mar 11, 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
    @@ -4,6 +4,7 @@ function buildVibrationPattern(seconds) {
    .reduce(prev => [...prev, 0, 1000, 500], [])
    }

    const ONLINE = true
    const DEFAULT_WAIT_TIME = 30000
    const CURRENT_ORDER_STATE = `IN_PROGRESS`
    const ORDER_STATE = {
    @@ -22,7 +23,7 @@ Machine(
    initial: `initializing`,
    context: {
    fetchKey: 1,
    online: false,
    online: ONLINE,
    offer: {
    id: null,
    receivedAt: null
  17. hosmelq revised this gist Mar 11, 2020. 1 changed file with 13 additions and 7 deletions.
    20 changes: 13 additions & 7 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -196,13 +196,6 @@ Machine(
    online: () => true,
    }),

    vibrate: () => {
    console.log(`vibrate`)
    },
    cancelVibration: () => {
    console.log(`cancelVibration`)
    },

    setOfferId: assign({
    offer: () => ({
    acceptedAt: new Date(),
    @@ -217,6 +210,19 @@ Machine(
    receivedAt: null,
    }),
    }),

    incrementFetchKey: assign({
    fetchKey: (ctx) => ({
    fetchKey: ++ctx.fetchKey
    }),
    }),

    vibrate: () => {
    console.log(`vibrate`)
    },
    cancelVibration: () => {
    console.log(`cancelVibration`)
    },
    },
    delays: {
    ORDER_DELAY: ctx => {
  18. hosmelq revised this gist Mar 11, 2020. 1 changed file with 16 additions and 9 deletions.
    25 changes: 16 additions & 9 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,12 @@ function buildVibrationPattern(seconds) {
    }

    const DEFAULT_WAIT_TIME = 30000
    const CURRENT_ORDER_STATE = `IN_PROGRESS`
    const ORDER_STATE = {
    IN_PROGRESS: `IN_PROGRESS`,
    WAITING_COURIER: `WAITING_COURIER`
    }
    const CURRENT_ORDER_TYPE = `DELIVERY`
    const ORDER_TYPE = {
    DELIVERY: `DELIVERY`,
    HIRED: `HIRED`
    @@ -15,6 +21,7 @@ Machine(
    id: `courier-state-and-jobs`,
    initial: `initializing`,
    context: {
    fetchKey: 1,
    online: false,
    offer: {
    id: null,
    @@ -74,13 +81,6 @@ Machine(
    ORDER_RECEIVED: {
    actions: `setOffer`,
    target: `fetchingOrder`
    },
    ORDER_ASSIGNED: {
    actions: `reloadOrderList`
    },
    ORDER_OFFER_RECEIVED: {
    actions: `setOffer`,
    target: `offer`
    }
    }
    },
    @@ -90,6 +90,11 @@ Machine(
    id: `fetchOrder`,
    src: `fetchOrder`,
    onDone: [
    {
    cond: `isAssigned`,
    actions: `incrementFetchKey`,
    target: `offer`
    },
    {
    cond: `isDeliveryOrder`,
    target: `offer`
    @@ -226,10 +231,12 @@ Machine(
    isOffline: ctx => !ctx.online,
    isOnline: ctx => ctx.online,

    isDeliveryOrder: (ctx, event) => ORDER_TYPE.DELIVERY === event.type,
    isHiredOrder: (ctx, event) => ORDER_TYPE.HIRED === event.type,
    isAssigned: (ctx, event) => ORDER_STATE.IN_PROGRESS === CURRENT_ORDER_STATE,
    isDeliveryOrder: (ctx, event) => ORDER_TYPE.DELIVERY === CURRENT_ORDER_TYPE,
    isHiredOrder: (ctx, event) => ORDER_TYPE.HIRED === CURRENT_ORDER_TYPE,

    hasEnoughTimeToAcceptOrder: ({offer: {receivedAt}}) => {
    return true
    return Math.floor(receivedAt + DEFAULT_WAIT_TIME - Date.now()) > 2500
    },
    }
  19. hosmelq revised this gist Mar 11, 2020. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ Machine(
    id: `courier-state-and-jobs`,
    initial: `initializing`,
    context: {
    online: true,
    online: false,
    offer: {
    id: null,
    receivedAt: null
    @@ -71,7 +71,10 @@ Machine(
    states: {
    waiting: {
    on: {
    ORDER_RECEIVED: `fetchingOrder`,
    ORDER_RECEIVED: {
    actions: `setOffer`,
    target: `fetchingOrder`
    },
    ORDER_ASSIGNED: {
    actions: `reloadOrderList`
    },
  20. hosmelq revised this gist Mar 11, 2020. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,10 @@ function buildVibrationPattern(seconds) {
    }

    const DEFAULT_WAIT_TIME = 30000
    const ORDER_TYPE = {
    DELIVERY: `DELIVERY`,
    HIRED: `HIRED`
    }

    Machine(
    {
    @@ -218,6 +222,10 @@ Machine(
    guards: {
    isOffline: ctx => !ctx.online,
    isOnline: ctx => ctx.online,

    isDeliveryOrder: (ctx, event) => ORDER_TYPE.DELIVERY === event.type,
    isHiredOrder: (ctx, event) => ORDER_TYPE.HIRED === event.type,

    hasEnoughTimeToAcceptOrder: ({offer: {receivedAt}}) => {
    return Math.floor(receivedAt + DEFAULT_WAIT_TIME - Date.now()) > 2500
    },
  21. hosmelq revised this gist Mar 11, 2020. 1 changed file with 24 additions and 4 deletions.
    28 changes: 24 additions & 4 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,6 @@ Machine(
    context: {
    online: true,
    offer: {
    acceptedAt: null,
    id: null,
    receivedAt: null
    }
    @@ -68,7 +67,7 @@ Machine(
    states: {
    waiting: {
    on: {
    '*': `offer`,
    ORDER_RECEIVED: `fetchingOrder`,
    ORDER_ASSIGNED: {
    actions: `reloadOrderList`
    },
    @@ -79,6 +78,26 @@ Machine(
    }
    },

    fetchingOrder: {
    invoke: {
    id: `fetchOrder`,
    src: `fetchOrder`,
    onDone: [
    {
    cond: `isDeliveryOrder`,
    target: `offer`
    },
    {
    cond: `isHiredOrder`,
    target: `assignment`
    }
    ],
    onError: `waiting`
    }
    },

    assignment: {},

    offer: {
    onDone: `waiting`,
    initial: `validating`,
    @@ -119,6 +138,7 @@ Machine(
    onDone: `done`,
    onError: [
    {
    target: `done`,
    actions: (ctx, event) => {
    console.log(`ctx: `, ctx)
    console.log(`event: `, event)
    @@ -135,11 +155,11 @@ Machine(
    onDone: `done`,
    onError: [
    {
    target: `done`,
    actions: (ctx, event) => {
    console.log(`ctx: `, ctx)
    console.log(`event: `, event)
    },
    target: `done`
    }
    }
    ]
    }
  22. hosmelq revised this gist Mar 6, 2020. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -68,11 +68,11 @@ Machine(
    states: {
    waiting: {
    on: {
    '*': `offer`,
    ORDER_ASSIGNED: {
    actions: `reloadOrderList`
    },
    ORDER_OFFER_RECEIVED: {
    cond: `isValidOffer`,
    actions: `setOffer`,
    target: `offer`
    }
    @@ -201,7 +201,6 @@ Machine(
    hasEnoughTimeToAcceptOrder: ({offer: {receivedAt}}) => {
    return Math.floor(receivedAt + DEFAULT_WAIT_TIME - Date.now()) > 2500
    },
    isValidOffer: () => true,
    }
    }
    )
  23. hosmelq revised this gist Mar 6, 2020. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -68,6 +68,9 @@ Machine(
    states: {
    waiting: {
    on: {
    ORDER_ASSIGNED: {
    actions: `reloadOrderList`
    },
    ORDER_OFFER_RECEIVED: {
    cond: `isValidOffer`,
    actions: `setOffer`,
  24. hosmelq revised this gist Mar 6, 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
    @@ -68,7 +68,7 @@ Machine(
    states: {
    waiting: {
    on: {
    OFFER_RECEIVED: {
    ORDER_OFFER_RECEIVED: {
    cond: `isValidOffer`,
    actions: `setOffer`,
    target: `offer`
  25. hosmelq revised this gist Mar 6, 2020. 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
    @@ -195,8 +195,8 @@ Machine(
    guards: {
    isOffline: ctx => !ctx.online,
    isOnline: ctx => ctx.online,
    hasEnoughTimeToAcceptOrder: () => {
    return true
    hasEnoughTimeToAcceptOrder: ({offer: {receivedAt}}) => {
    return Math.floor(receivedAt + DEFAULT_WAIT_TIME - Date.now()) > 2500
    },
    isValidOffer: () => true,
    }
  26. hosmelq revised this gist Mar 6, 2020. 1 changed file with 14 additions and 2 deletions.
    16 changes: 14 additions & 2 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -84,7 +84,15 @@ Machine(
    invoke: {
    id: `validateOrder`,
    src: `validateOrder`,
    onDone: `waiting`,
    onDone: [
    {
    cond: `hasEnoughTimeToAcceptOrder`,
    target: `waiting`
    },
    {
    target: `done`
    }
    ],
    onError: `done`
    }
    },
    @@ -127,7 +135,8 @@ Machine(
    actions: (ctx, event) => {
    console.log(`ctx: `, ctx)
    console.log(`event: `, event)
    }
    },
    target: `done`
    }
    ]
    }
    @@ -186,6 +195,9 @@ Machine(
    guards: {
    isOffline: ctx => !ctx.online,
    isOnline: ctx => ctx.online,
    hasEnoughTimeToAcceptOrder: () => {
    return true
    },
    isValidOffer: () => true,
    }
    }
  27. hosmelq revised this gist Mar 6, 2020. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -4,8 +4,7 @@ function buildVibrationPattern(seconds) {
    .reduce(prev => [...prev, 0, 1000, 500], [])
    }

    // DEFAULT_WAIT_TIME
    const WAIT_TIME = 30000
    const DEFAULT_WAIT_TIME = 30000

    Machine(
    {
    @@ -178,10 +177,10 @@ Machine(
    delays: {
    ORDER_DELAY: ctx => {
    if (!ctx.time) {
    return WAIT_TIME
    return DEFAULT_WAIT_TIME
    }

    return Math.floor(ctx.time + WAIT_TIME - Date.now())
    return Math.floor(ctx.time + DEFAULT_WAIT_TIME - Date.now())
    },
    },
    guards: {
  28. hosmelq revised this gist Mar 6, 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
    @@ -12,7 +12,7 @@ Machine(
    id: `courier-state-and-jobs`,
    initial: `initializing`,
    context: {
    online: false,
    online: true,
    offer: {
    acceptedAt: null,
    id: null,
    @@ -187,6 +187,7 @@ Machine(
    guards: {
    isOffline: ctx => !ctx.online,
    isOnline: ctx => ctx.online,
    isValidOffer: () => true,
    }
    }
    )
  29. hosmelq revised this gist Mar 6, 2020. 1 changed file with 16 additions and 6 deletions.
    22 changes: 16 additions & 6 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ Machine(
    id: `courier-state-and-jobs`,
    initial: `initializing`,
    context: {
    online: true,
    online: false,
    offer: {
    acceptedAt: null,
    id: null,
    @@ -70,7 +70,8 @@ Machine(
    waiting: {
    on: {
    OFFER_RECEIVED: {
    actions: `setOfferId`,
    cond: `isValidOffer`,
    actions: `setOffer`,
    target: `offer`
    }
    }
    @@ -84,11 +85,11 @@ Machine(
    invoke: {
    id: `validateOrder`,
    src: `validateOrder`,
    onError: `done`,
    onDone: `waiting`,
    },
    onError: `done`
    }
    },

    waiting: {
    entry: `vibrate`,
    exit: `cancelVibration`,
    @@ -121,11 +122,20 @@ Machine(
    invoke: {
    id: `rejectOrder`,
    src: `rejectOrder`,
    onDone: `done`
    onDone: `done`,
    onError: [
    {
    actions: (ctx, event) => {
    console.log(`ctx: `, ctx)
    console.log(`event: `, event)
    }
    }
    ]
    }
    },

    done: {
    entry: `resetOffer`,
    type: `final`
    }
    }
  30. hosmelq revised this gist Mar 6, 2020. No changes.