Skip to content

Commit 1844994

Browse files
author
Tom Kirkpatrick
committed
Update readme
1 parent ea6d6ac commit 1844994

File tree

1 file changed

+85
-16
lines changed

1 file changed

+85
-16
lines changed

README.md

Lines changed: 85 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,110 @@
22

33
[![Circle CI](https://circleci.com/gh/fullcube/loopback-component-fsm.svg?style=svg)](https://circleci.com/gh/fullcube/loopback-component-fsm) [![Dependencies](http://img.shields.io/david/fullcube/loopback-component-fsm.svg?style=flat)](https://david-dm.org/fullcube/loopback-component-fsm) [![Coverage Status](https://coveralls.io/repos/github/fullcube/loopback-component-fsm/badge.svg?branch=master)](https://coveralls.io/github/fullcube/loopback-component-fsm?branch=master)
44

5-
65
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.
76

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
910

1011
1. Install in you loopback project:
1112

1213
`npm install --save loopback-component-fsm`
1314

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

16-
3. Configure options inside `component-config.json`. *(see configuration section)*
17+
3. Enable the component inside `component-config.json`.
1718

1819
```json
1920
{
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+
]
2239
}
2340
}
2441
```
2542

26-
### Usage
43+
**Options:**
2744

28-
1. Define a state machine config on Model.STATEMACHINE property.
45+
- `stateProperty`
2946

30-
2. Load and use the state machine for a given instance
47+
[String] : The name of the model's state property. *(default: 'state')*
3148

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.
38107

39108

40-
### TODO:
109+
## More Information
41110

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

Comments
 (0)