question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[RFC] A StateMachine type

See original GitHub issue

Request 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):

visualisation of Post.status state chart

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:

Select Dropdown showing 4 options with 2 disabled, 1 enabled, and 1 selected.

Whatchya think?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:28
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
marianocodescommented, Mar 23, 2020

Is there any update?

0reactions
joseDaKingcommented, Sep 28, 2020

Hi, everbody there is state machine library in javascript called xstate it might can be used for this use case

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found