Skip to content

Instantly share code, notes, and snippets.

@Jpadilla1
Jpadilla1 / machine.js
Last active December 22, 2019 20:13
Generated by XState Viz: https://xstate.js.org/viz
const lightBulbMachine = Machine({
id: "house",
initial: "init",
states: {
init: {
entry: "spawnLightBulbs",
on: {
"": "lightsOn"
}
},
@Jpadilla1
Jpadilla1 / machine.js
Created December 11, 2019 19:03
Generated by XState Viz: https://xstate.js.org/viz
const statusMachine = Machine({
id: 'status-machine',
type: 'parallel',
states: {
polling: {
initial: 'loading',
states: {
loading: {
invoke: {
src: 'sparkRequest',
@Jpadilla1
Jpadilla1 / machine.js
Created December 11, 2019 17:31
Generated by XState Viz: https://xstate.js.org/viz
const fetchMachine = Machine({
id: "spark-status-machine",
initial: "loading",
states: {
loading: {
invoke: {
src: "sparkRequest",
onDone: "success",
onError: "failure"
}
@Jpadilla1
Jpadilla1 / machine.js
Created December 11, 2019 17:29
Generated by XState Viz: https://xstate.js.org/viz
const sparkStatus = Machine({
id: 'status-machine',
initial: 'setup',
context: {
sparkActor: null,
},
states: {
setup: {
entry: 'spawnSparkStatusUpdater',
on: {
@Jpadilla1
Jpadilla1 / machine.js
Last active September 5, 2019 22:02
Generated by XState Viz: https://xstate.js.org/viz
const wiseLoadingMachine = Machine({
id: 'wiseLoadingMachine',
initial: 'idle',
context: {
region: null
},
states: {
idle: {
on: {
setTab: 'settingTab'
@Jpadilla1
Jpadilla1 / useMachine.js
Last active September 3, 2019 19:26
Custom useMachine hook
import { useMachine as useXStateMachine } from '@xstate/react';
const getStateStat = (state: any): string => {
if (typeof state === 'string') {
return state;
}
if (Object.keys(state).length > 1) {
throw new Error('useMachine does not support emitting stats for parallel states');
}
@Jpadilla1
Jpadilla1 / feedback.js
Last active September 3, 2019 18:23
Feedback component
import { useMachine } from '@xstate/react';
import feedbackMachine from './feedbackMachine';
const Feedback = () => {
const [current, send] = useMachine(feedbackMachine);
return ...;
};
@Jpadilla1
Jpadilla1 / machine.js
Created September 3, 2019 17:26
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@Jpadilla1
Jpadilla1 / feedbackMachine.js
Created September 3, 2019 17:23
Feedback Machine
const feedbackMachine = Machine({
id: "feedback",
initial: "idle",
states: {
idle: {
on: {
good: "thanks",
bad: "tellUsWhy"
}
},
/*eslint-disable */
const PUSH = '@@nav/PUSH';
const POP = '@@nav/POP';
const INDEX = '@@articles/INDEX';
const ARTICLE = '@@articles/ARTICLE';
const COMMENTS = '@@articles/COMMENTS';