Skip to content

Instantly share code, notes, and snippets.

@chrisdhanaraj
Last active July 16, 2019 21:35
Show Gist options
  • Select an option

  • Save chrisdhanaraj/e6a52598aade491affc02010caf938d5 to your computer and use it in GitHub Desktop.

Select an option

Save chrisdhanaraj/e6a52598aade491affc02010caf938d5 to your computer and use it in GitHub Desktop.

Revisions

  1. chrisdhanaraj revised this gist Jul 16, 2019. No changes.
  2. chrisdhanaraj revised this gist Jul 16, 2019. No changes.
  3. chrisdhanaraj revised this gist Jul 16, 2019. 1 changed file with 13 additions and 1 deletion.
    14 changes: 13 additions & 1 deletion machine.js
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,19 @@ const suggestionMachine = Machine({
    states: {
    idle: {},
    fetching: {},
    successWithResults: {},
    successWithResults: {
    on: {
    ADD_ITEM: {
    target: 'idle',
    action: sendParent(context => {
    return {
    type: 'ADD_ITEM',
    data: context
    }
    })
    }
    }
    },
    successNoResults: {},
    failure: {}
    }
  4. chrisdhanaraj revised this gist Jul 16, 2019. 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
    @@ -84,7 +84,8 @@ const searchResultsMachine = Machine({
    on: {
    FETCH: "fetching"
    }
    }
    },
    failure: {}
    }
    });

  5. chrisdhanaraj revised this gist Jul 16, 2019. No changes.
  6. chrisdhanaraj revised this gist Jul 16, 2019. 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
    @@ -92,7 +92,7 @@ const searchMachine = Machine({
    id: "search",
    initial: "idle",
    context: {
    selectedItems: {}
    selectedItems: new Map()
    },
    states: {
    idle: {
  7. chrisdhanaraj revised this gist Jul 16, 2019. No changes.
  8. chrisdhanaraj revised this gist Jul 16, 2019. 1 changed file with 7 additions and 2 deletions.
    9 changes: 7 additions & 2 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,8 @@
    // - raise
    // - actions
    // - XState (all XState exports)
    const suggestionMachine = Machine({

    const suggestionMachine = Machine({
    id: "suggestion",
    initial: "idle",
    context: {
    @@ -61,7 +62,11 @@ const searchResultsMachine = Machine({
    id: "searchResults",
    initial: "fetching",
    context: {
    facets: {}
    searchResults: [],
    page: 0,
    pagePer: 25,
    sortKey: null,
    sortDir: 'desc'
    },
    states: {
    idle: {
  9. chrisdhanaraj revised this gist Jul 16, 2019. 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
    @@ -92,7 +92,7 @@ const searchMachine = Machine({
    states: {
    idle: {
    entry: assign({
    suggestionRef: () => spawn(suggestionMachine)
    suggestionRef: () => spawn(suggestionMachine, 'suggestions')
    }),
    on: {
    ADD_ITEM: "active",
  10. chrisdhanaraj revised this gist Jul 16, 2019. 1 changed file with 12 additions and 4 deletions.
    16 changes: 12 additions & 4 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -43,8 +43,16 @@ const facetMachine = Machine({
    FAILURE: "failure"
    }
    },
    success: {},
    failure: {}
    success: {
    on: {
    FETCH: 'fetching'
    }
    },
    failure: {
    on: {
    FETCH: 'fetching'
    }
    }
    }
    });

    @@ -93,8 +101,8 @@ const searchMachine = Machine({
    },
    active: {
    entry: assign({
    facetRef: () => spawn(facetMachine),
    searchResultsRef: () => spawn(searchResultsMachine)
    facetRef: () => spawn(facetMachine, 'facet'),
    searchResultsRef: () => spawn(searchResultsMachine, 'searchResults')
    }),
    on: {
    ADD_ITEM: {
  11. chrisdhanaraj revised this gist Jul 16, 2019. 1 changed file with 100 additions and 107 deletions.
    207 changes: 100 additions & 107 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -9,118 +9,111 @@
    // - 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: 'failure'
    // }
    // },
    // success: {
    // type: 'final'
    // },
    // failure: {
    // on: {
    // RETRY: {
    // target: 'loading',
    // actions: assign({
    // retries: (context, event) => context.retries + 1
    // })
    // }
    // }
    // }
    // }
    // });


    const facetMachine = Machine({
    type: 'parallel',
    id: 'facet',
    initial: 'fetching',
    context: {
    facets: {}
    const suggestionMachine = Machine({
    id: "suggestion",
    initial: "idle",
    context: {
    suggestions: []
    },
    states: {
    idle: {},
    fetching: {},
    successWithResults: {},
    successNoResults: {},
    failure: {}
    }
    });

    const facetMachine = Machine({
    type: "parallel",
    id: "facet",
    initial: "fetching",
    context: {
    facets: {}
    },
    states: {
    idle: {
    on: {
    FETCH: "fetching"
    }
    },
    fetching: {
    on: {
    SUCCESS: "success",
    FAILURE: "failure"
    }
    },
    states: {
    idle: {
    on: {
    FETCH: 'fetching'
    }
    },
    fetching: {
    on: {
    SUCCESS: 'success',
    FAILURE: 'failure'
    }
    success: {},
    failure: {}
    }
    });

    const searchResultsMachine = Machine({
    type: "parallel",
    id: "searchResults",
    initial: "fetching",
    context: {
    facets: {}
    },
    states: {
    idle: {
    on: {
    FETCH: "fetching"
    }
    },
    fetching: {
    on: {
    SUCCESS: "success",
    FAILURE: "failure"
    }
    },
    success: {
    on: {
    FETCH: "fetching"
    }
    }
    });

    // const searchResultsMachine = Machine({
    // type: 'parallel',
    // id: 'searchResults',
    // initial: 'fetching',
    // context: {
    // facets: {}
    // },
    // states: {
    // idle: {
    // on: {
    // FETCH: 'fetching'
    // }
    // },
    // fetching: {
    // on: {
    // SUCCESS: 'success',
    // FAILURE: 'failure'
    // }
    // },
    // success: {
    // on: {
    // FETCH: 'fetching'
    // }
    // }
    // }
    // });

    const searchMachine = Machine({
    id: 'search',
    initial: 'idle',
    context: {
    selectedItems: {}
    }
    });

    const searchMachine = Machine({
    id: "search",
    initial: "idle",
    context: {
    selectedItems: {}
    },
    states: {
    idle: {
    entry: assign({
    suggestionRef: () => spawn(suggestionMachine)
    }),
    on: {
    ADD_ITEM: "active",
    SET_ITEM: "active"
    }
    },
    states: {
    idle: {
    on: {
    ADD_ITEM: 'active',
    SET_ITEM: 'active'
    }
    },
    active: {
    entry:
    assign({
    facetRef: () => spawn(facetMachine)
    }),
    on: {
    ADD_ITEM: {
    actions: send('FETCH', {
    active: {
    entry: assign({
    facetRef: () => spawn(facetMachine),
    searchResultsRef: () => spawn(searchResultsMachine)
    }),
    on: {
    ADD_ITEM: {
    target: "active",
    actions: [
    send("FETCH", {
    to: context => {
    console.log(context);
    return context.facetRef
    }
    }),
    send("FETCH", {
    to: context => context.facetRef
    })
    },
    SET_ITEM: 'active',
    REMOVE_ITEM: 'active'
    }
    ]
    },
    SET_ITEM: "active",
    REMOVE_ITEM: "active"
    }
    }
    })
    }
    });
  12. chrisdhanaraj revised this gist Jul 16, 2019. 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
    @@ -114,7 +114,7 @@
    }),
    on: {
    ADD_ITEM: {
    actions: send('PING', {
    actions: send('FETCH', {
    to: context => context.facetRef
    })
    },
  13. chrisdhanaraj revised this gist Jul 16, 2019. 1 changed file with 35 additions and 29 deletions.
    64 changes: 35 additions & 29 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -67,32 +67,32 @@
    }
    });

    const searchResultsMachine = Machine({
    type: 'parallel',
    id: 'searchResults',
    initial: 'fetching',
    context: {
    facets: {}
    },
    states: {
    idle: {
    on: {
    FETCH: 'fetching'
    }
    },
    fetching: {
    on: {
    SUCCESS: 'success',
    FAILURE: 'failure'
    }
    },
    success: {
    on: {
    FETCH: 'fetching'
    }
    }
    }
    });
    // const searchResultsMachine = Machine({
    // type: 'parallel',
    // id: 'searchResults',
    // initial: 'fetching',
    // context: {
    // facets: {}
    // },
    // states: {
    // idle: {
    // on: {
    // FETCH: 'fetching'
    // }
    // },
    // fetching: {
    // on: {
    // SUCCESS: 'success',
    // FAILURE: 'failure'
    // }
    // },
    // success: {
    // on: {
    // FETCH: 'fetching'
    // }
    // }
    // }
    // });

    const searchMachine = Machine({
    id: 'search',
    @@ -108,10 +108,16 @@
    }
    },
    active: {
    ...facetMachine,
    ...searchResultsMachine,
    entry:
    assign({
    facetRef: () => spawn(facetMachine)
    }),
    on: {
    ADD_ITEM: 'active',
    ADD_ITEM: {
    actions: send('PING', {
    to: context => context.facetRef
    })
    },
    SET_ITEM: 'active',
    REMOVE_ITEM: 'active'
    }
  14. chrisdhanaraj revised this gist Jul 16, 2019. 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
    @@ -68,6 +68,7 @@
    });

    const searchResultsMachine = Machine({
    type: 'parallel',
    id: 'searchResults',
    initial: 'fetching',
    context: {
  15. chrisdhanaraj revised this gist Jul 16, 2019. 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
    @@ -46,6 +46,7 @@


    const facetMachine = Machine({
    type: 'parallel',
    id: 'facet',
    initial: 'fetching',
    context: {
  16. chrisdhanaraj revised this gist Jul 16, 2019. 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
    @@ -83,6 +83,11 @@
    SUCCESS: 'success',
    FAILURE: 'failure'
    }
    },
    success: {
    on: {
    FETCH: 'fetching'
    }
    }
    }
    });
  17. chrisdhanaraj revised this gist Jul 16, 2019. 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
    @@ -102,6 +102,7 @@
    },
    active: {
    ...facetMachine,
    ...searchResultsMachine,
    on: {
    ADD_ITEM: 'active',
    SET_ITEM: 'active',
  18. chrisdhanaraj revised this gist Jul 16, 2019. 1 changed file with 21 additions and 0 deletions.
    21 changes: 21 additions & 0 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -66,6 +66,27 @@
    }
    });

    const searchResultsMachine = Machine({
    id: 'searchResults',
    initial: 'fetching',
    context: {
    facets: {}
    },
    states: {
    idle: {
    on: {
    FETCH: 'fetching'
    }
    },
    fetching: {
    on: {
    SUCCESS: 'success',
    FAILURE: 'failure'
    }
    }
    }
    });

    const searchMachine = Machine({
    id: 'search',
    initial: 'idle',
  19. chrisdhanaraj revised this gist Jul 16, 2019. 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
    @@ -47,7 +47,7 @@

    const facetMachine = Machine({
    id: 'facet',
    initial: 'idle',
    initial: 'fetching',
    context: {
    facets: {}
    },
  20. chrisdhanaraj created this gist Jul 16, 2019.
    91 changes: 91 additions & 0 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,91 @@

    // 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: 'failure'
    // }
    // },
    // success: {
    // type: 'final'
    // },
    // failure: {
    // on: {
    // RETRY: {
    // target: 'loading',
    // actions: assign({
    // retries: (context, event) => context.retries + 1
    // })
    // }
    // }
    // }
    // }
    // });


    const facetMachine = Machine({
    id: 'facet',
    initial: 'idle',
    context: {
    facets: {}
    },
    states: {
    idle: {
    on: {
    FETCH: 'fetching'
    }
    },
    fetching: {
    on: {
    SUCCESS: 'success',
    FAILURE: 'failure'
    }
    }
    }
    });

    const searchMachine = Machine({
    id: 'search',
    initial: 'idle',
    context: {
    selectedItems: {}
    },
    states: {
    idle: {
    on: {
    ADD_ITEM: 'active',
    SET_ITEM: 'active'
    }
    },
    active: {
    ...facetMachine,
    on: {
    ADD_ITEM: 'active',
    SET_ITEM: 'active',
    REMOVE_ITEM: 'active'
    }
    }
    }
    })