Skip to content

Instantly share code, notes, and snippets.

@achambers
Last active October 18, 2021 13:45
Show Gist options
  • Select an option

  • Save achambers/2227902ef722b647629c75e4c14079a4 to your computer and use it in GitHub Desktop.

Select an option

Save achambers/2227902ef722b647629c75e4c14079a4 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 STORE = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
const COUNT = 5;
function fetchItems(last = 5, before = null) {
let idx = STORE.length;
if (before) {
idx = STORE.indexOf(before);
}
let items = STORE.slice(idx - last, idx);
let hasMoreBefore = items[0] !== STORE[0];
return {
items,
info: {
hasMoreAfter: false,
hasMoreBefore,
},
};
}
const fetchMachine = Machine({
id: 'infinte-scroll',
initial: 'idle',
context: {
items: [],
pageInfo: {},
},
states: {
idle: {
on: {
FETCH: 'loading',
},
},
loading: {
invoke: {
id: 'loadInitialItems',
src() {
let data = fetchItems(COUNT);
return Promise.resolve(data);
},
onDone: {
target: 'success',
actions: assign({
items: (_, { data }) => [...data.items],
pageInfo: (_, { data }) => data.info,
}),
},
},
},
loadMoreBefore: {
invoke: {
id: 'loadItemsBefore',
src({ items }) {
let data = fetchItems(COUNT, items[0]);
return Promise.resolve(data);
},
onDone: {
target: 'success',
actions: assign({
items: ({ items }, { data }) => [...data.items, ...items],
pageInfo: ({ pageInfo }, { data }) => {
return { ...pageInfo, hasMoreBefore: data.info.hasMoreBefore };
},
}),
},
},
},
success: {
on: {
LOAD_MORE_BEFORE: {
target: 'loadMoreBefore',
cond({ pageInfo }, _) {
return pageInfo.hasMoreBefore;
},
},
},
},
failure: {
type: 'final',
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment