Skip to content

Instantly share code, notes, and snippets.

@pavankataria
Last active January 2, 2021 12:57
Show Gist options
  • Save pavankataria/ebf7713fd251dac1471593f88d4ed865 to your computer and use it in GitHub Desktop.
Save pavankataria/ebf7713fd251dac1471593f88d4ed865 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const allData = new Array(25).fill(0).map((_valu, i) => i + 1);
const perPage = 10;
const dataMachine = new Machine({
id: 'dataMachine',
initial: 'loading',
context: {
data: []
},
states: {
loading: {
invoke: {
id: 'dataLoader',
src: (context, _event) => {
return (callback, _onEvent) => {
setTimeout(() => {
const {data} = context;
const newData = allData.slice(data.length, data.length + perPage);
const hasMore = newData.length === perPage;
if(hasMore) {
callback({ type: "DONE_MORE", newData });
}
else {
callback({ type: "DONE_COMPLETE", newData })
}
}, 1000);
};
}
},
on: {
DONE_MORE: {
target:'more',
actions: assign({
data: ({ data }, { newData = [] }) => [...data, ...newData]
})
},
DONE_COMPLETE: {
target:"complete",
actions: assign({
data: ({ data }, { newData = [] }) => [...data, ...newData]
})
},
FAIL: 'failure'
}
},
more: {
on: {
LOAD: 'loading'
}
},
complete: {
type: 'final'
},
failure: {
type: 'final',
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment