Last active
February 18, 2020 06:18
-
-
Save jmakeig/da359bc7eca3c934e4a95add49a2eb57 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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