Comparing version 6.0.0 to 6.1.0
# Changelog | ||
## 6.1.0 | ||
- Add optional secondary `scope` argument to `listen` | ||
## 6.0.0 | ||
@@ -4,0 +8,0 @@ |
{ | ||
"name": "diode", | ||
"version": "6.0.0", | ||
"version": "6.1.0", | ||
"description": "A simple event emitter.", | ||
@@ -8,3 +8,5 @@ "main": "src/diode.js", | ||
"test": "mocha -R dot", | ||
"test:watch": "mocha -R dot -w" | ||
"test:watch": "mocha -R dot -w", | ||
"test:cov": "istanbul cover node_modules/.bin/_mocha -- -R dot", | ||
"coveralls": "npm run test:cov && coveralls < coverage/lcov.info" | ||
}, | ||
@@ -27,4 +29,5 @@ "repository": { | ||
"devDependencies": { | ||
"istanbul": "0.3.19", | ||
"mocha": "2.3.2" | ||
} | ||
} |
@@ -17,53 +17,6 @@ [![NPM](https://nodei.co/npm/diode.png?compact=true)](https://npmjs.org/package/diode) | ||
It is also quite small (see [API](#api)). We found ourselves building | ||
It is also small (see [API](#api)). We found ourselves building | ||
something similar to it on several projects and decided it was better | ||
to keep it in one place. | ||
## Usage | ||
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 | ||
`getState` method: | ||
```javascript | ||
var React = require('react/addons'); | ||
var Stateful = require('diode/stateful'); | ||
var MyStore = require('./myStore'); | ||
var Pure = React.addons.PureRenderMixin; | ||
var Component = React.createClass({ | ||
mixins: [ Stateful, Pure ], | ||
getState: function() { | ||
return { | ||
items: MyStore.all() | ||
} | ||
}, | ||
render: function() { | ||
// render something purely | ||
} | ||
}) | ||
``` | ||
Then in your stores, execute 'publish' on the Diode when you want to | ||
propagate a change: | ||
```javascript | ||
var Diode = require('diode') | ||
var _data = [] | ||
MyStore.add = function(record) { | ||
_data = _data.concat(record) | ||
Diode.volley() | ||
} | ||
``` | ||
And that's it! | ||
## Diode as a decorator | ||
Diode is both an event emitter and a decorator that can add event | ||
@@ -90,14 +43,37 @@ subscription to another object: | ||
## Managing scope | ||
By providing a second argument to `listen`, callbacks will be executed | ||
within a given context: | ||
```javascript | ||
var emitter = new Diode() | ||
emitter.listen(function() { | ||
assert.equal(this, 'custom context') | ||
}, 'custom context') | ||
emitter.emit() | ||
``` | ||
## API | ||
### Diode | ||
### `listen(callback, &scope)` | ||
- `listen,subscribe`: Add a subscription | ||
- `ignore,unsubscribe`: Remove a subscription | ||
- `emit,publish`: Trigger all subscriptions | ||
Add a callback. If a second argument is provided, the callback will be | ||
executed within that context. | ||
### Stateful | ||
Alias: `subscribe`. | ||
- `getState`: This method is called by `Stateful` whenever the `Diode` | ||
executes `emit` to update the state of a component. **It | ||
is required.** | ||
### `ignore(callback)` | ||
Remove a callback. | ||
Alias: `unsubscribe`. | ||
### `emit(...arguments)` | ||
Trigger all subscriptions. | ||
Alias: 'publish' |
@@ -17,4 +17,4 @@ /** | ||
*/ | ||
app.listen = app.subscribe = function (callback) { | ||
callbacks.push(callback) | ||
app.listen = app.subscribe = function (callback, scope) { | ||
callbacks.push({ callback: callback, scope: scope || app }) | ||
return app | ||
@@ -29,3 +29,3 @@ } | ||
callbacks = callbacks.filter(function(entry) { | ||
return entry !== unwanted | ||
return entry.callback !== unwanted | ||
}) | ||
@@ -41,3 +41,3 @@ | ||
for (var i = 0; i < callbacks.length; i++) { | ||
callbacks[i].apply(app, arguments) | ||
callbacks[i].callback.apply(callbacks[i].scope, arguments) | ||
} | ||
@@ -44,0 +44,0 @@ |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
50509
19
422
2
78
1