npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

loopback-component-fsm

v1.3.2

Published

Finite State Machine for Loopback Models.

Downloads

49

Readme

Loopback Finite State Machine

Greenkeeper badge

Circle CI Dependencies Coverage Status

This loopback component provides a finite state machine (powered by https://github.com/vstirbu/fsm-as-promised) for loopback model instances, enabling precise control over when model instance methods may be called.

When a model method that is controlled by the Finite State Machine is called it will set a global lock that will prevent other copies of the same instance from being transitioned whist the existing transition is still underway. The state machine governs which state transitions may take place at any time given the current state of the instance and handles state change persistence on completion of a given transition.

Installation

  1. Install in you loopback project:

npm install --save loopback-component-fsm

  1. Create a component-config.json file in your server folder (if you don't already have one)

  2. Enable the component inside component-config.json.

{
  "loopback-component-fsm": { }
}

Configuration

  1. Define a state machine events in the mixin configuration for you models.
"mixins": {
  "StateMachine": {
    "stateProperty": "status",
    "settings": {
      "allowForce": true
    },
    "events": [
      { "name": "activate", "from": "none", "to": "active", "transitionOptions": { "skipBeforeSave" : true } },
      { "name": "cancel", "from": "active", "to": "canceled" },
      { "name": "reactivate", "from": "canceled", "to": "active" },
      { "name": "expire", "from": [ "active", "canceled" ], "to": "expired" }
    ]
  }
}

Options:

  • stateProperty

    [String] : The name of the model's state property. (default: 'state')

  • settings

    [Object] : Settings used to control state machine operations. Currently the only supported option is allowForce which when set to true makes it possible to carry out an otherwise invalid state change by passing in { force: true } at method call time. This option can also be set per event. (default: {})

  • events

    [Array] : A list of events available to the state machine. Refer to the FSM As Promised documentation for details on how events should be defined. (default: [])

Implementation:

For each event in the State Machine, a series of model notifications will be sent - one for each stage in a transition - in the following order:

| callback | state in which the notification executes | description | | --- | --- | --- | | fsm:onleave{stateName} | from | do something when leaving state stateName | | fsm:onleave | from | do something when leaving any state | | fsm:on{eventName} | from | do something when executing the transition | | fsm:onenter{stateName} | from | do something when entering state stateName | | fsm:onenter | from | do something when entering any state | | fsm:onentered{stateName} | to | do something after entering state stateName (transition is complete) | | fsm:onentered | to | do something after entering any state (transition is complete) |

You can act on any of these transition stages by observing the notification. For example:

MyModel.observe('fsm:oncancel', ctx => ctx.instance.doSomething().then(() => ctx))

If you intend to perform an asynchronous operation in a given transition stage, your observer should return a promise that resolves to the ctx argument that was passed to it. Otherwise, you should simply return the ctx object.

Return values

The ctx object will be passed through the entire transition call chain and returned to the original caller. If you would like your caller to receive something other than the full ctx object you can set ctx.res which will be returned instead. More information

Usage

Prototype methods will be attached to model instances for each of the named events in your mixin configuration. For example, the above mixin configuration will result in the following methods being added to MyModel.

  • MyModel.prototype.activate
  • MyModel.prototype.cancel
  • MyModel.prototype.reactivate
  • MyModel.prototype.expire

These methods all accept a first argument that is a settings object that is used by the fsm to determine how it functions (eg passing { force: true } (see allowForce above). All arguments will be available from within the various fsm notifications.

MyModel.findOne()
  .then(instance => {
    log.debug(`Current state is: ${instance.state}`) // Current state is: active
    return instance.cancel()
  })
  .then(instance => {
    log.debug(`Current state is: ${instance.state}`) // Current state is: canceled
    return instance.reactivate({ force: true })
  })
  .then(instance => {
    log.debug(`Current state is: ${instance.state}`) // Current state is: active
  })

In this example, a model instance is loaded from the database and a finite state machine is initialized using the current status of the model instance. The instance is then transitioned to the canceled state, then back to the active state.

More Information

Please refer to the FSM As Promised documentation for more detail on the internals of the state machine implementation.