Skip to content

Instantly share code, notes, and snippets.

@apostopher
Last active May 3, 2020 01:26
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 apostopher/75f2bc369c99d1dc563f442402d21317 to your computer and use it in GitHub Desktop.
Save apostopher/75f2bc369c99d1dc563f442402d21317 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const receiptMachine = Machine(
{
key: 'ReceiptMachine',
initial: 'initializing',
states: {
idle: {
on: {
purchase: 'purchasing',
},
},
initializing: {
invoke: {
id: 'initIAP',
src: 'initIAP',
onDone: {
target: 'verifying',
actions: assign({
initialized: (context, event) => event.data,
}),
},
onError: {
target: 'fatal',
},
},
},
verifying: {
invoke: {
id: 'verifyReceipt',
src: 'verifyReceipt',
onDone: {
target: 'loadingSubs',
actions: assign({
purchased: (context, event) => event.data,
}),
},
onError: {
target: 'loadingSubs',
actions: assign({
purchased: () => false,
}),
},
},
},
fatal: {
on: {
init: 'initializing',
},
},
loadingSubs: {
invoke: {
id: 'loadSubs',
src: 'loadSubs',
onDone: {
target: 'idle',
actions: assign({
subs: (context, event) => event.data,
}),
},
onError: {
target: 'subsError',
},
},
},
subsError: {
on: {
getSubs: 'loadingSubs',
},
},
purchasing: {
on: {
purchaseSuccess: {
target: 'purchaseSuccess',
actions: assign((context, event) => ({
...context,
purchased: true,
receipt: event.receipt,
})),
},
purchaseError: {
target: 'purchaseError',
actions: assign({
purchased: false,
}),
},
},
invoke: {
id: 'purchaseSub',
src: 'purchaseSub',
},
},
purchaseError: {
on: {
purchase: 'purchasing',
},
},
purchaseSuccess: {
type: 'final',
},
},
},
{
services: {
initIAP: () => Promise.resolve(true),
verifyReceipt: context => Promise.resolve(true),
loadSubs: () => Promise.resolve([]),
purchaseSub: (context, event) => Promise.resolve(),
},
},
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment