Skip to content

Instantly share code, notes, and snippets.

@ekafyi
Last active June 24, 2021 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ekafyi/1a9fa3270bc2b712559f0617450fe13e to your computer and use it in GitHub Desktop.
Save ekafyi/1a9fa3270bc2b712559f0617450fe13e to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const fooMachine = Machine({
id: 'foo',
initial: 'loading',
// Local context for entire machine
context: {
retries: 0,
activeFilter: {},
},
// State definitions
states: {
loading: {
entry: 'handleLoading',
on: {
RESOLVE: { target: 'loaded' },
REJECT: { target: 'failure' },
},
meta: {
message: 'Please wait.....'
}
},
loaded: {
type: 'compound',
initial: 'all', // Initial child state
on: {
FILTER: {
target: 'loaded.filtering',
actions: 'handleFilter',
},
},
// Child states
states: {
all: {},
filtering: {
on: {
RESOLVE_FILTER: {
target: 'filtered',
}
}
},
filtered: {
on: {
RESET: {
target: 'all',
},
}
},
},
},
failure: {
on: {
RETRY: {
target: 'loading',
actions: 'addRetry',
}
},
meta: {
message: 'The request failed.'
}
},
}
}, {
actions: {
// action implementation
handleFilter: (context, event) => {
// ?? send, cancel?
// ...
},
handleLoading: (context, event) => {
console.log('hmm', context, event); // context: retries, fruit
},
addRetry: assign({
retries: (context) => context.retries + 1,
fruit: '🍌', // pass this prop to the next (receiving) state
}),
},
activities: {},
delays: {},
guards: {},
services: {}
});
// yang bisa difilter:
// - cities
// - ages
// - registration
// - targetGroups
// Example state
// const someState = fooMachine.transition('loaded.filtering', {
// type: 'APPLY_FILTER', fruit: '🍏',
// });
// console.log("??", someState);
// Good discussion: https://github.com/davidkpiano/xstate/issues/434
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment