Comparing version 1.1.0 to 2.0.0
18
diode.js
@@ -9,3 +9,14 @@ /** | ||
var _callbacks = [] | ||
var _tick = null; | ||
/** | ||
* Callbacks are eventually executed, Diode does not promise | ||
* immediate consistency so that state propagation can be batched | ||
*/ | ||
var _flush = function() { | ||
for (var i = 0, length = _callbacks.length; i < length; i++) { | ||
_callbacks[i]() | ||
} | ||
} | ||
var Diode = { | ||
@@ -39,5 +50,6 @@ | ||
publish: function() { | ||
_callbacks.forEach(function(callback) { | ||
callback() | ||
}) | ||
if (_callbacks.length > 0) { | ||
cancelAnimationFrame(_tick) | ||
_tick = requestAnimationFrame(_flush) | ||
} | ||
} | ||
@@ -44,0 +56,0 @@ |
@@ -0,6 +1,8 @@ | ||
var production = (typeof process === 'undefined') ? false : process.env.NODE_ENV === 'production' | ||
module.exports = function(condition, message) { | ||
if ("production" !== process.env.NODE_ENV && !condition) { | ||
if (!production && !condition) { | ||
var error = new Error(message); | ||
error.framesToPop = 1; // we don't care about invariant's own frame | ||
error.framesToPop = 1 // we don't care about invariant's own frame | ||
@@ -7,0 +9,0 @@ throw error; |
{ | ||
"name": "diode", | ||
"version": "1.1.0", | ||
"description": "A simple state propagation tool for Flux/React apps", | ||
"version": "2.0.0", | ||
"description": "A simple, eventually consistent, state propagation tool for Flux/React apps", | ||
"main": "diode.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
@@ -1,4 +0,12 @@ | ||
# Diode | ||
[![NPM](https://nodei.co/npm/diode.png?compact=true)](https://npmjs.org/package/diode) | ||
A simple state propagation tool for React. It takes advantage of | ||
--- | ||
[![Build Status](https://travis-ci.org/vigetlabs/diode.png?branch=master)](https://travis-ci.org/vigetlabs/diode) | ||
[![Coverage Status](https://coveralls.io/repos/vigetlabs/diode/badge.svg)](https://coveralls.io/r/vigetlabs/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) | ||
@@ -14,2 +22,20 @@ to significantly simplify event subscription when propagating changes | ||
**Diode batches event subscriptions using `requestAnimationFrame`**. In | ||
short, this means that sequential publications will be clumped: | ||
```javascript | ||
Diode.subscribe(callback) | ||
for (var i = 1000; i > 0; i--) { | ||
Diode.publish() | ||
} | ||
// callback will only fire once | ||
``` | ||
This means that state changes which would activate `Diode.publish` | ||
multiple times, such as an action which affects multiple data stores, | ||
will trigger once. This should improve efficiency and simplify actions | ||
such as merging records. | ||
It is also quite small (see [API](#api)). We found ourselves building | ||
@@ -19,9 +45,2 @@ something similar to it on several projects and decided it was better | ||
--- | ||
[![Build Status](https://travis-ci.org/vigetlabs/diode.png?branch=master)](https://travis-ci.org/vigetlabs/diode) | ||
[![Coverage Status](https://coveralls.io/repos/vigetlabs/diode/badge.svg)](https://coveralls.io/r/vigetlabs/diode) | ||
--- | ||
## Usage | ||
@@ -28,0 +47,0 @@ |
6596
76
101