|
2 | 2 |
|
3 | 3 | [](https://circleci.com/gh/fullcube/loopback-component-fsm) [](https://david-dm.org/fullcube/loopback-component-fsm) [](https://coveralls.io/github/fullcube/loopback-component-fsm?branch=master)
|
4 | 4 |
|
5 |
| - |
6 | 5 | 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.
|
7 | 6 |
|
8 |
| -### Installation |
| 7 | +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. |
| 8 | + |
| 9 | +## Installation |
9 | 10 |
|
10 | 11 | 1. Install in you loopback project:
|
11 | 12 |
|
12 | 13 | `npm install --save loopback-component-fsm`
|
13 | 14 |
|
14 | 15 | 2. Create a component-config.json file in your server folder (if you don't already have one)
|
15 | 16 |
|
16 |
| -3. Configure options inside `component-config.json`. *(see configuration section)* |
| 17 | +3. Enable the component inside `component-config.json`. |
17 | 18 |
|
18 | 19 | ```json
|
19 | 20 | {
|
20 |
| - "loopback-component-fsm": { |
21 |
| - "{option}": "{value}" |
| 21 | + "loopback-component-fsm": { } |
| 22 | + } |
| 23 | + ``` |
| 24 | + |
| 25 | +## Configuration |
| 26 | + |
| 27 | +1. Define a state machine events in the mixin configuration for you models. |
| 28 | + |
| 29 | + ```json |
| 30 | + "mixins": { |
| 31 | + "StateMachine": { |
| 32 | + "stateProperty": "status", |
| 33 | + "events": [ |
| 34 | + { "name": "activate", "from": "none", "to": "active" }, |
| 35 | + { "name": "cancel", "from": "active", "to": "canceled" }, |
| 36 | + { "name": "reactivate", "from": "canceled", "to": "active" }, |
| 37 | + { "name": "expire", "from": [ "active", "canceled" ], "to": "expired" } |
| 38 | + ] |
22 | 39 | }
|
23 | 40 | }
|
24 | 41 | ```
|
25 | 42 |
|
26 |
| -### Usage |
| 43 | +**Options:** |
27 | 44 |
|
28 |
| -1. Define a state machine config on Model.STATEMACHINE property. |
| 45 | +- `stateProperty` |
29 | 46 |
|
30 |
| -2. Load and use the state machine for a given instance |
| 47 | + [String] : The name of the model's state property. *(default: 'state')* |
31 | 48 |
|
32 |
| - ```(javascript) |
33 |
| - const fsm = app.getStateMachine(instance) |
34 |
| - fsm.doSomething(instance) |
35 |
| - .then(() => cb()) |
36 |
| - .catch(cb) |
37 |
| - ``` |
| 49 | +- `events` |
| 50 | + |
| 51 | + [Array] : A list of events available to the state machine. Refer to the [FSM As Promised documentation](https://github.com/vstirbu/fsm-as-promised) for details on how events should be defined. *(default: [])* |
| 52 | + |
| 53 | +## Implementation: |
| 54 | + |
| 55 | +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: |
| 56 | + |
| 57 | +| callback | state in which the callback executes | |
| 58 | +| --- | --- | |
| 59 | +| fsm:onleave{stateName} | from | |
| 60 | +| fsm:onleave | from | |
| 61 | +| fsm:on{eventName} | _from_ | |
| 62 | +| fsm:onenter{stateName} | _from_ | |
| 63 | +| fsm:onenter | _from_ | |
| 64 | +| fsm:onentered{stateName} | to | |
| 65 | +| fsm:onentered | to | |
| 66 | + |
| 67 | +You can act on any of these transition stages by observing the notification. For example: |
| 68 | + |
| 69 | +```javascript |
| 70 | +MyModel.observe('fsm:oncancel', ctx => ctx.instance.doSomething().then(() => ctx)) |
| 71 | +``` |
| 72 | + |
| 73 | +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. |
| 74 | + |
| 75 | +**Return values** |
| 76 | + |
| 77 | +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](https://github.com/vstirbu/fsm-as-promised#returned-values) |
| 78 | + |
| 79 | +## Usage |
| 80 | + |
| 81 | +Prototype methods will be attached to model instances for each of the named events in your mixin configuration. For |
| 82 | +example, the above mixin configuration will result in the following methods being added to MyModel. |
| 83 | + |
| 84 | +- `MyModel.prototype.activate` |
| 85 | +- `MyModel.prototype.cancel` |
| 86 | +- `MyModel.prototype.reactivate` |
| 87 | +- `MyModel.prototype.expire` |
| 88 | + |
| 89 | +These methods can be called as any other: |
| 90 | + |
| 91 | +```javascript |
| 92 | +MyModel.findOne() |
| 93 | + .then(instance => { |
| 94 | + log.debug(`Current state is: ${instance.state}`) // Current state is: active |
| 95 | + return instance.cancel() |
| 96 | + }) |
| 97 | + .then(instance => { |
| 98 | + log.debug(`Current state is: ${instance.state}`) // Current state is: canceled |
| 99 | + return instance.reactivate() |
| 100 | + }) |
| 101 | + .then(instance => { |
| 102 | + log.debug(`Current state is: ${instance.state}`) // Current state is: active |
| 103 | + }) |
| 104 | +``` |
| 105 | + |
| 106 | +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. |
38 | 107 |
|
39 | 108 |
|
40 |
| -### TODO: |
| 109 | +## More Information |
41 | 110 |
|
42 |
| -- Provide/document a means to control what data gets returned. |
| 111 | +Please refer to the [FSM As Promised](https://github.com/vstirbu/fsm-as-promised) documentation for more detail on the internals of the state machine implementation. |
0 commit comments