Skip to content

Proposal: Observable finite state machine #368

Open
@mjstahl

Description

@mjstahl

This was discussed on a recent live stream (19:10) and a previous live stream (33:18).

Problem:
I found ViewModels with many properties to be error prone when switching between logical states in the ViewModel. Forgetting which properties to update when the state was updated was problematic.

Example API:

import DefineGraph from "can-define/graph/graph";

const H20 = DefineGraph.extend('H20', {
  initial: {
    // actions must reference existing states
    frozen: 'ice',
    boiling: 'steam',

    // DefineMap
    values: {
      temp: '60F'
    }
  },
  ice: {
    boiling: 'steam',
    warm: 'initial',

    values: {
      temp: '32F'
    }
  },
  steam: {
    cool: 'initial',
    frozen: 'ice',

    values: {
      temp: '212F'
    }
  }
})

const water = new H20()
water.temp //-> '60F'

// 'is' switches from once state to the next
// and the ViewModel would then be set to values defined in that state's `values` property.
// (Not happy with this function name either)
water.is('frozen')
water.state //-> 'ice'
water.temp //-> '32F'

// 'to' also allows to the developer to set the values of the new state. This would be a merge.
water.is('warm', { temp: '75F' })
water.state //-> 'default'
water.temp //-> '75F'

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions