Comparing version 4.2.0 to 4.3.0
# Changelog | ||
## 4.3.0 | ||
- Added `subscribe` alias for `listen` | ||
- Added `unsubscribe` alias for `ignore` | ||
- Added `publish` alias for `emit` | ||
- Diode can be called with the `new` Operator | ||
- Diode can decorate objects as a function itself (in addition to decorate) | ||
``` | ||
Compressed : 724 bytes | ||
Gzipped : 389 bytes | ||
``` | ||
## 4.2.0 | ||
All Diode methods now return the target. For example, if decorating an existing object: | ||
```javascript | ||
Diode.decorate(object) | ||
object.listen(callback) | ||
.listen(anotherCallback) | ||
``` | ||
## 4.1.0 | ||
@@ -4,0 +28,0 @@ |
{ | ||
"name": "diode", | ||
"version": "4.2.0", | ||
"description": "A simple, eventually consistent, state propagation tool for Flux/React apps", | ||
"version": "4.3.0", | ||
"description": "A simple event emitter with tools for eventual consistency", | ||
"main": "src/diode.js", | ||
"scripts": { | ||
"test": "karma start", | ||
"test": "karma start --single-run", | ||
"coveralls": "CONTINUOUS_INTEGRATION=true npm test && coveralls < coverage/report-lcov/lcov.info" | ||
@@ -9,0 +9,0 @@ }, |
@@ -10,7 +10,4 @@ [![NPM](https://nodei.co/npm/diode.png?compact=true)](https://npmjs.org/package/diode) | ||
A simple, eventually consistent, state propagation tool for React. It | ||
takes advantage of | ||
[components that are pure](http://facebook.github.io/react/docs/pure-render-mixin.html) | ||
to significantly simplify event subscription when propagating changes | ||
in the data layer. | ||
A simple event emitter with tools for eventual consistency. Diode only | ||
has one event. | ||
@@ -22,6 +19,2 @@ ```javascript | ||
**Diode is an event emitter with one event**. By including the `Stateful` | ||
mixin, an expected `getState` method is called every time the Diode | ||
publishes a change. | ||
**Diode can batch event subscriptions using `volley`**. In | ||
@@ -51,2 +44,6 @@ short, this means that sequential publications will be clumped: | ||
For React projects, Diode includes a `Stateful` mixin, it expects a | ||
`getState` method that is called every time Diode publishes a | ||
change. | ||
First include the `Stateful` mixin into a component, and provide a | ||
@@ -91,2 +88,25 @@ `getState` method: | ||
## Diode as a decorator | ||
Diode is both an event emitter and a decorator that can add event | ||
subscription to another object: | ||
```javascript | ||
var MyData = Diode({ | ||
data: [], | ||
add: function(record) { | ||
this.data.push(record) | ||
this.publish() | ||
} | ||
}) | ||
``` | ||
## New instances of Diode | ||
Diode also supports the `new` operator: | ||
```javascript | ||
var myDiode = new Diode() | ||
``` | ||
## API | ||
@@ -96,10 +116,6 @@ | ||
- `listen`: Remove a callback. If only using the `Stateful` mixin | ||
this probably never needs to be called | ||
- `ignore`: Add a callback. If only using the `Stateful` mixin | ||
this probably never needs to be called | ||
- `emit`: Propagate a change. Call this whenever a data store of | ||
some kind changes (leaning on smart `shouldComponentUpdate` methods | ||
within your React component tree) | ||
- `volley`: Propagate a change lazily. | ||
- `listen,subscribe`: Add a subscription | ||
- `ignore,unsubscribe`: Remove a subscription | ||
- `emit,publish`: Trigger all subscriptions | ||
- `volley`: Trigger a change lazily, batched together | ||
@@ -106,0 +122,0 @@ ### Stateful |
@@ -5,2 +5,26 @@ var Diode = require('../diode') | ||
function isDiode(object) { | ||
let truth = new Diode() | ||
for (let key in truth) { | ||
object.should.have.property(key) | ||
} | ||
} | ||
it ('is an event emitter itself', function() { | ||
isDiode(Diode) | ||
}) | ||
it ('is a decorator function', function() { | ||
let emitter = Diode({ prop: 'yes' }) | ||
isDiode(emitter) | ||
emitter.should.have.property('prop', 'yes') | ||
}) | ||
it ('defaults to an empty object when decorating', function() { | ||
isDiode(Diode()) | ||
}) | ||
it ('does not flush if there are no callbacks', function() { | ||
@@ -94,2 +118,24 @@ let spy = sinon.spy(window, 'requestAnimationFrame') | ||
}) | ||
describe('instantiation', function() { | ||
it ('can be called with the new operator', function() { | ||
let target = new Diode() | ||
target.should.have.property('listen') | ||
}) | ||
}) | ||
describe('aliases', function() { | ||
it ('aliases `listen` to `subscribe` callbacks', function() { | ||
Diode.should.have.property('subscribe', Diode.listen) | ||
}) | ||
it ('aliases `ignore` to `unsubscribe` callbacks', function() { | ||
Diode.should.have.property('unsubscribe', Diode.ignore) | ||
}) | ||
it ('aliases `emit` to `publish` callbacks', function() { | ||
Diode.should.have.property('publish', Diode.emit) | ||
}) | ||
}) | ||
}) |
@@ -11,3 +11,7 @@ /** | ||
target = target || {} | ||
if (this instanceof Diode) { | ||
target = this | ||
} else { | ||
target = target || {} | ||
} | ||
@@ -32,3 +36,3 @@ /** | ||
*/ | ||
target.listen = function(callback) { | ||
target.listen = target.subscribe = function(callback) { | ||
_callbacks = _callbacks.concat(callback) | ||
@@ -43,3 +47,3 @@ | ||
*/ | ||
target.ignore = function(callback) { | ||
target.ignore = target.unsubscribe = function(callback) { | ||
_callbacks = _callbacks.filter(function(i) { | ||
@@ -55,3 +59,3 @@ return i !== callback | ||
*/ | ||
target.emit = function() { | ||
target.emit = target.publish = function() { | ||
_flush.apply(target, arguments) | ||
@@ -81,3 +85,3 @@ | ||
module.exports = Diode() | ||
module.exports = Diode(Diode) | ||
module.exports.decorate = Diode |
16008
297
122