Last active
December 6, 2019 14:38
-
-
Save JamieMason/9a6209257c27f6eab50e87e7c15ee0f9 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains 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
// 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