[RFC] A StateMachine type
See original GitHub issueRequest For Comments on a new field type StateMachine
:
StateMachine
Field Type
The StateMachine
field type allows you specify a Finite State Machine to control the possible states and transitions between those states in a declarative syntax, ideal for setting up publishing workflows or user onboarding walkthroughs.
The StateMachine
field type is powered by the xstate.js
state chart library.
Example
keystone.createList('Post', {
fields: {
name: { type: Text },
status: {
type: StateMachine,
defaultValue: 'draft',
states: {
draft: {
on: {
ready: [
{ target: 'ready', cond: 'isAdmin' },
{ target: 'ready', cond: 'isAuthor' },
],
},
},
ready: {
on: {
published: [{ target: 'published', cond: 'isAdmin' }],
rejected: [{ target: 'rejected', cond: 'isAdmin' }],
},
},
rejected: {
on: {
draft: [
{ target: 'draft', cond: 'isAdmin' },
{ target: 'draft', cond: 'isAuthor' },
],
},
},
published: {
on: {
draft: [{ target: 'draft', cond: 'isAdmin' }],
},
},
},
guards: {
isAdmin: ({ context }) => context.authedItem && context.authedItem.isAdmin,
isAuthor: ({ context, existingItem }) => context.authedItem && context.authedItem.id === existingItem.author,
},
},
},
});
The states
and guards
are passed straight to xstate
.
See the equivalent xstate state machine here (try changing the values returned from isAdmin
/isAuthor
to see the different transitions which are enabled/disabled):
The AdminUI Field would display as a drop-down of the current state and any possible states which can be reached from there. Something like:
Whatchya think?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:28
- Comments:6 (2 by maintainers)
Top Results From Across the Web
RFC 4137 - State Machines for Extensible Authentication ...
State Machines for Extensible Authentication Protocol (EAP) Peer and Authenticator (RFC 4137, August 2005.
Read more >RFC 3215: LDP State Machine
RFC 3215 LDP State Machine January 2002 Table Of Contents 1. ... For each LSP, there are 2 kinds of state machines involved:...
Read more >File:RFC3588 peer state machine 1.png - Wikimedia Commons
Original file (2,748 × 2,295 pixels, file size: 298 KB, MIME type: image/png) ... English: RFC 3588 Diameter Peer State Machine Connecting.
Read more >The state machine for authentication implied by RFC 959.
... the finite state machine for user authentication, shown in Figure 1, that can be derived through a careful analysis of the FTP...
Read more >EAP and AAA Update
IEEE 802.1X state machine is not the EAP ... Authentication occurs via EAP TLS (RFC 2716) or equivalent ... Rate of Method Type...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Is there any update?
Hi, everbody there is state machine library in javascript called xstate it might can be used for this use case