Last active
March 19, 2020 19:13
-
-
Save robrobbins/d74edc6992f8ba8fce1314995b3c7952 to your computer and use it in GitHub Desktop.
a user state machine that should transition to either "registered" or "guest" via a guard...
This file contains hidden or 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
// User may be in one of: | |
// * idle: no user. no registration or access attempts | |
// * guest: a present user, not registered or registration/access failed | |
// * registered: existing user | |
// * processing: a register/access fetch machine is in flight | |
const machine = Machine({ | |
id: 'user', | |
initial: 'idle', | |
context: { | |
id: undefined, | |
email: undefined, | |
name: undefined, | |
fetchResource: undefined, | |
fetchOpts: undefined, | |
error: undefined, | |
}, | |
states: { | |
idle: {}, | |
guest: {}, | |
registered: { | |
entry: 'registered' | |
}, | |
processing: { | |
invoke: { | |
id: 'fetch', | |
src: fetch, | |
data: { | |
resource: ctx => ctx.fetchResource, | |
opts: ctx => ctx.fetchOpts | |
}, | |
onDone: [{ | |
target: 'registered', | |
cond: 'userRegistered', | |
actions: 'fetched' | |
}, { | |
// an unregistered user | |
target: 'guest' | |
}] | |
}, | |
entry: send(ACTIVATE, { to: 'fetch' }) | |
}, | |
}, | |
on: { | |
REGISTER: { | |
target: 'processing', | |
actions: 'register', | |
}, | |
ACCESS: { | |
target: 'processing', | |
actions: 'access', | |
}, | |
} | |
}, { | |
guards: { | |
userRegistered: (_, e) => !!e.data.id, | |
}, | |
actions: { | |
register: assign({ | |
email: (_, e) => e.email, | |
fetchResource: (_, e) => e.fetchResource, | |
fetchOpts: (_, e) => e.fetchOpts, | |
}), | |
access: assign({ | |
// would have to be some auth/password or something... TBD | |
email: (_, e) => e.email, | |
fetchResource: (_, e) => e.fetchResource, | |
fetchOpts: (_, e) => e.fetchOpts, | |
}), | |
fetched: assign({ | |
id: (_, e) => e.data.id, | |
name: (_, e) => e.data.name, | |
error: (_,e) => e.data.error, | |
}), | |
registered: ctx => { | |
console.log(`user ${ctx.id} registered`); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
snippet from a spec that never goes beyond the "processing" state...