Skip to content

Instantly share code, notes, and snippets.

@sukima
Created March 26, 2020 22:27
Show Gist options
  • Save sukima/def2b403ef411152c1152f25162287b8 to your computer and use it in GitHub Desktop.
Save sukima/def2b403ef411152c1152f25162287b8 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
//import { Machine, assign } from 'xstate';
//import { isPresent } from '@ember/utils';
function createMachine() {
return Machine({
id: 'rule-group-data-manager',
initial: 'prefetching',
context: {
items: [],
clauses: null,
error: null,
},
states: {
'prefetching': {
invoke: 'filter',
on: {
FILTER: '',
DONE: [
{ target: 'unfiltered.withItems', cond: 'hasItems', actions: 'storeItems' },
{ target: 'unfiltered.empty' },
],
ERROR: { target: 'unfiltered.errored', actions: 'storeError' },
},
},
'unfiltered': {
states: {
'fetching': {
invoke: 'filter',
on: {
FILTER: '',
DONE: [
{ target: 'withItems', cond: 'hasItems', actions: 'storeItems' },
{ target: 'empty' },
],
ERROR: { target: 'errored', actions: 'storeError' },
},
},
'withItems': {},
'empty': {
entry: 'clearItems',
},
'errored': {
exit: 'clearError',
},
},
},
'filtered': {
states: {
'fetching': {
invoke: 'filter',
on: {
FILTER: '',
DONE: [
{ target: 'withItems', cond: 'hasItems', actions: 'storeItems' },
{ target: 'empty' },
],
ERROR: { target: 'errored', actions: 'storeError' },
},
},
'withItems': {},
'empty': {
entry: 'clearItems',
},
'errored': {
exit: 'clearError',
},
},
},
},
on: {
FILTER: [
{ target: 'filtered.fetching', cond: 'hasClauses', actions: 'storeClauses' },
{ target: 'unfiltered.fetching', actions: 'clearClauses' },
],
},
}, {
actions: {
clearClauses: assign({ clauses: null }),
storeClauses: assign({ clauses: (_, evt) => evt.clauses }),
clearItems: assign({ items: [] }),
storeItems: assign({ items: (_, evt) => evt.data }),
clearError: assign({ error: null }),
storeError: assign({ error: (_, evt) => evt.error }),
},
guards: {
hasClauses: (_, evt) => isPresent(evt.clauses),
hasItems: (_, evt) => isPresent(evt.data),
},
});
}
function isPresent(v) {
return v && (v.length && v.length > 0) || !!v;
}
createMachine();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment