Comparing version 0.0.7 to 0.0.8
@@ -85,2 +85,25 @@ 'use strict'; | ||
}); | ||
describe('when we use the `disconnect` function', function () { | ||
it('should detach the mapping', function () { | ||
var mapping = sinon.spy(); | ||
var machine = _.Machine.create('A', { | ||
state: { name: 'idle' }, | ||
transitions: { | ||
idle: { run: 'running' }, | ||
running: { stop: 'idle' } | ||
} | ||
}); | ||
var disconnect = (0, _connect2.default)().with('A').map(function (A) { | ||
mapping(A.state.name); | ||
}); | ||
machine.run(); | ||
disconnect(); | ||
machine.stop(); | ||
expect(mapping).to.be.calledTwice; | ||
expect(mapping.firstCall).to.be.calledWith(sinon.match('idle')); | ||
expect(mapping.secondCall).to.be.calledWith(sinon.match('running')); | ||
}); | ||
}); | ||
}); |
@@ -39,2 +39,6 @@ 'use strict'; | ||
done.apply(undefined, machines); | ||
return function () { | ||
if (mappings && mappings[id]) delete mappings[id]; | ||
}; | ||
}; | ||
@@ -41,0 +45,0 @@ |
{ | ||
"name": "stent", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"description": "Stent is a state machines container made for UI development", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -13,3 +13,3 @@ # Stent | ||
* [`<action-handler>`](#action-handler) | ||
* [`connect`](#connect) | ||
* [`connect` and `disconnect`](#connect-and-disconnect) | ||
* [Helpers used inside generators](#helpers-used-inside-generators) | ||
@@ -269,3 +269,3 @@ * [Middlewares](#middlewares) | ||
### `connect` | ||
### `connect` and `disconnect` | ||
@@ -297,5 +297,18 @@ `connect` is the short way to do `Machine.get` and retrieving one or more created machines. | ||
There's also a helper for integration with React. It creates a [HOC](https://github.com/krasimir/react-in-patterns/tree/master/patterns/higher-order-components): | ||
You may also need to `disconnect` which makes sense if you use the `map` function. If you are connecting with `mapOnce` your mapping function is getting called only once anyway. | ||
```js | ||
const disconnect = connect() | ||
.with('MachineA', 'MachineB') | ||
.mapOnce((MachineA, MachineB) => { | ||
// this gets called only once | ||
}); | ||
// at some point later | ||
disconnect(); | ||
``` | ||
There's also a helper for integrating with React. It creates a [HOC](https://github.com/krasimir/react-in-patterns/tree/master/patterns/higher-order-components): | ||
```js | ||
import React from 'react'; | ||
@@ -321,3 +334,3 @@ import { connect } from 'stent/react'; | ||
The result of the `map` function goes as props to our component. Similarly to [Redux's connect](https://github.com/reactjs/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options) function. | ||
The result of the `map` function goes as props to our component. Similarly to [Redux's connect](https://github.com/reactjs/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options) function. And of course the mapping function is disconnected when the component is unmounted. | ||
@@ -390,3 +403,3 @@ ### Helpers used inside generators | ||
The hooks above are getting called just before running the internal Stent's logic. At this moment nothing in the machine is changing/executing. Calling `next` will pass the control flow to Stent. | ||
The hooks above are getting called just before running the internal Stent's logic. At this moment nothing in the machine is changing/executing. Calling `next` will pass the control flow to Stent. Also have in mind that these methods are fired with the machine as a context. Which means that you have an access to the current state and methods. | ||
@@ -393,0 +406,0 @@ ## Examples |
@@ -80,2 +80,25 @@ import connect from '../connect'; | ||
}); | ||
describe('when we use the `disconnect` function', function () { | ||
it('should detach the mapping', function () { | ||
const mapping = sinon.spy(); | ||
const machine = Machine.create('A', { | ||
state: { name: 'idle' }, | ||
transitions: { | ||
idle: { run: 'running' }, | ||
running: { stop: 'idle' } | ||
} | ||
}); | ||
const disconnect = connect().with('A').map(A => { | ||
mapping(A.state.name); | ||
}); | ||
machine.run(); | ||
disconnect(); | ||
machine.stop(); | ||
expect(mapping).to.be.calledTwice; | ||
expect(mapping.firstCall).to.be.calledWith(sinon.match('idle')); | ||
expect(mapping.secondCall).to.be.calledWith(sinon.match('running')); | ||
}); | ||
}); | ||
}); |
@@ -24,2 +24,6 @@ import { Machine } from '../'; | ||
done(...machines); | ||
return () => { | ||
if (mappings && mappings[id]) delete mappings[id]; | ||
} | ||
} | ||
@@ -26,0 +30,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
266834
48
2570
548
1