Skip to content

Instantly share code, notes, and snippets.

@MichielDeMey
Last active March 5, 2021 16:34
Show Gist options
  • Save MichielDeMey/347acaed6a6cc38392ae19a33e64e7ee to your computer and use it in GitHub Desktop.
Save MichielDeMey/347acaed6a6cc38392ae19a33e64e7ee to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const mockPoll = () => {
return new Promise((resolve, reject) => {
console.log('Polling...')
setTimeout(() => {
console.log('Done polling')
return resolve([{
id: 'foo',
name: 'Foo'
}, {
id: 'bar',
name: 'Bar'
}]);
}, 3000);
})
}
const fetchMachine = Machine({
initial: 'fetching',
states: {
fetching: {
invoke: {
src: mockPoll,
onError: 'failure',
onDone: 'success'
},
after: { TIMEOUT: 'fetching' }
},
failure: {
after: {
INTERVAL: 'fetching'
}
},
success: { type: 'final' }
}
}, {
delays: { INTERVAL: 2000, TIMEOUT: 10000 }
});
const monitrOnboardingMachine = Machine({
id: 'onboarding',
strict: true,
initial: 'addIntegration',
context: {
hasIntegrations: false,
hasEntities: false,
entities: []
},
states: {
addIntegration: {
on: {
NEXT: 'importing',
},
},
importing: {
invoke: {
src: mockPoll,
onDone: {
target: 'configureEntities',
actions: assign({
entities: (context, event) => event.data
})
},
onError: {
after: {
IMPORT_INTERVAL: 'importing'
}
}
}
},
configureEntities: {
initial: 'setupReporting',
states: {
setupReporting: {
on: {
NEXT: '#onboarding.finish',
},
},
},
},
finish: {
type: 'final',
},
},
}, {
delays: { IMPORT_INTERVAL: 2000 }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment