Last active
May 3, 2020 01:26
-
-
Save apostopher/75f2bc369c99d1dc563f442402d21317 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
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