Skip to content

Instantly share code, notes, and snippets.

@JamieMason
Last active December 6, 2019 14:38
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 JamieMason/9a6209257c27f6eab50e87e7c15ee0f9 to your computer and use it in GitHub Desktop.
Save JamieMason/9a6209257c27f6eab50e87e7c15ee0f9 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const setDistance = () => {};
const pullToRefreshMachine = Machine(
{
id: "pullToRefresh",
initial: "dirty",
context: {
distance: 0,
scrollTop: null,
threshold: 150
},
states: {
dirty: {
on: {
"": [
{ cond: "isScrolledToTop", target: "idle" },
{ cond: "hasScrolledDown", target: "disabled" }
],
SCROLL: {
target: "dirty",
actions: ["storeScrollTop"]
}
}
},
idle: {
on: {
PULL_MOVE: {
target: "pulling",
actions: ["storeDistance", "setDistance"]
}
}
},
pulling: {
on: {
PULL_MOVE: {
target: "pulling",
actions: ["storeDistance", "setDistance"]
},
PULL_END: [
{ cond: "yPosIsAboveThreshold", target: "loading" },
{ target: "idle" }
]
}
},
loading: {
initial: "unresolved",
states: {
unresolved: {
on: { RESOLVE: "resolved", REJECT: "rejected" }
},
resolved: {
on: { "": "#pullToRefresh.dirty" }
},
rejected: {
on: { "": "#pullToRefresh.dirty" }
}
}
},
disabled: {
on: {
SCROLL: {
target: "dirty",
actions: ["storeScrollTop"]
}
}
}
}
},
{
actions: {
setDistance,
storeDistance: assign({
distance: (context, event) =>
Math.min(context.threshold, Math.max(0, event.distance))
}),
storeScrollTop: assign({
scrollTop: (context, event) => event.scrollTop
})
},
guards: {
isScrolledToTop({ scrollTop }) {
return scrollTop === 0;
},
hasScrolledDown({ scrollTop }) {
return scrollTop !== null && scrollTop > 0;
},
yPosIsAboveThreshold({ distance, threshold }) {
return distance >= threshold;
}
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment