Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jmakeig
Last active February 18, 2020 06:18
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 jmakeig/da359bc7eca3c934e4a95add49a2eb57 to your computer and use it in GitHub Desktop.
Save jmakeig/da359bc7eca3c934e4a95add49a2eb57 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
//import { Machine, assign } from './xstate.js';
// Ensure correct dirty checks when Svelte is running in immutable mode.
function clone(object) {
if ('object' === typeof object) {
if (null === object) return object;
if (Array.isArray(object)) return [...object];
if (object instanceof Set) return new Set(object);
if (object instanceof Map) return new Map(object);
return Object.assign({}, object);
}
return object;
}
const detailMachine = key => {
const CONTEXT_KEY = key || 'item';
const CACHE_KEY = `${CONTEXT_KEY}_cache`;
return Machine(
{
id: 'detail',
strict: true,
initial: 'editing',
context: {},
states: {
editing: {
initial: 'clean',
entry: 'doCache',
states: {
clean: {
on: {
change: {
target: 'dirty',
internal: true
}
}
},
dirty: {
entry: 'doChange',
on: {
change: {
actions: 'doChange'
},
cancel: {
target: 'clean',
actions: 'undoCache'
}
}
}
}
},
viewing: {
on: {
edit: { target: 'editing' }
}
}
}
},
{
actions: {
doChange: assign({
[CONTEXT_KEY]: (c, e) => clone({ ...e[CONTEXT_KEY], timestamp: null })
}),
doCache: assign({ [CACHE_KEY]: (c, e) => clone(c[CONTEXT_KEY]) }),
undoCache: assign({
[CONTEXT_KEY]: (c, e) => clone(c[CACHE_KEY])
})
}
}
);
};
const machine = detailMachine('detail');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment