Skip to content

Instantly share code, notes, and snippets.

@tbergman
Forked from valscion/machine.js
Created October 8, 2019 11:35
Show Gist options
  • Select an option

  • Save tbergman/746e7a1fbed68bd9cdc21e0b46ce66eb to your computer and use it in GitHub Desktop.

Select an option

Save tbergman/746e7a1fbed68bd9cdc21e0b46ce66eb to your computer and use it in GitHub Desktop.

Revisions

  1. @valscion valscion revised this gist Sep 24, 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
    @@ -3,7 +3,7 @@ const handleRequestSuccess = assign({
    });

    const handleHistoryPopState = assign({
    resultx: (_ctx, event) => event.results
    results: (_ctx, event) => event.results
    });

    const searchFSM = Machine({
  2. @valscion valscion created this gist Sep 24, 2019.
    86 changes: 86 additions & 0 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,86 @@
    const handleRequestSuccess = assign({
    results: (_ctx, event) => event.results
    });

    const handleHistoryPopState = assign({
    resultx: (_ctx, event) => event.results
    });

    const searchFSM = Machine({
    id: 'search',
    initial: 'boot',
    context: {
    results: []
    },
    states: {
    boot: {
    on: {
    '': {
    target: 'idle',
    actions: ['replaceHistoryState']
    }
    }
    },
    idle: {
    on: {
    HISTORY_POPSTATE: {
    actions: [handleHistoryPopState]
    },
    CHANGE_TEXT_INPUT: 'debouncing',
    CHANGE_OPTION: 'fetching'
    }
    },
    debouncing: {
    enter: ['startDebounceTimer'],
    exit: ['clearDebounceTimer'],
    on: {
    HISTORY_POPSTATE: {
    target: 'idle',
    actions: [handleHistoryPopState]
    },
    CHANGE_TEXT_INPUT: 'debouncing',
    DEBOUNCE_TIMER: 'fetching',
    CHANGE_OPTION: 'fetching'
    }
    },
    fetching: {
    exit: ['clearSpinnerTimer', 'clearPendingRequest'],
    entry: ['startRequest', 'startSpinnerTimer'],
    on: {
    CHANGE_TEXT_INPUT: 'debouncing',
    CHANGE_OPTION: 'fetching',
    HISTORY_POPSTATE: {
    target: 'idle',
    actions: [handleHistoryPopState]
    },
    SEARCH_REQUEST_SUCCESS: {
    target: 'idle',
    actions: [handleRequestSuccess]
    },
    SEARCH_REQUEST_FAILURE: {
    target: 'error',
    actions: ['handleRequestFailure']
    }
    },
    initial: 'start',
    states: {
    start: {
    on: {
    TIMER: 'spinner'
    }
    },
    spinner: {
    type: 'final'
    }
    }
    },
    error: {
    on: {
    '': {
    target: 'idle',
    actions: ['logRequestError']
    }
    }
    }
    }
    });