Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions docs/api/Reflux.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,28 @@ Creates an event emitting Data Store. It is mixed in with functions from the `Li

@returns {Store} A data store instance

## Reflux.createReducer(initialData)

Creates a reducer, a data store that uses pure functions to mutate the data.

@param {Mixed} initialData the initial data to seed the reducer

@returns {Reducer} A reducer instance

@example
```javascript
var addUser = Reflux.createAction({ sync: true });
var reducer = Reflux.createReducer({ users: [] });

reducer.on(addUser, function(data, newUser) {
data.users.push(newUser);
return data;
})

addUser("John Doe");
reducer.peek(); // outputs { users: ["John Doe"] }
```

## Reflux.joinTrailing(...publishers)

Creates a static join for the given publishers. It emits when all the publishers have emitted at least once, and will emit with the data that was last emitted by each listenable.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"benchmark": "node test/benchmarks",
"watch:lint": "esw -w",
"watch:test": "mocha -w -R min --compilers js:babel-core/register ./test",
"watch": "parallelshell 'npm run watch:test' 'npm run watch:lint'",
"watch": "parallelshell 'npm run watch:lint' 'npm run watch:test'",
"prepublish": "npm run lint && npm run test:mocha && npm run clean && npm run compile",
"precommit": "npm run lint && npm run test:mocha",
"prepush": "npm run precommit"
Expand Down
27 changes: 27 additions & 0 deletions src/createReducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as _ from "./utils";
import PublisherMethods from "./PublisherMethods";

export default function createReducer(initialData) {

var data = initialData;

function Reducer() {
this.emitter = new _.EventEmitter();
this.eventLabel = "reducer";
}

Reducer.prototype.on = function(publisher, reducerFn) {
publisher.listen(function() {
data = reducerFn.apply(this, [ data, ...arguments ]);
this.trigger(data);
}, this);
};

Reducer.prototype.peek = function() {
return data;
};

_.extend(Reducer.prototype, PublisherMethods);

return new Reducer();
}
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import * as _ from "./utils";
const utils = _;
import createAction from "./createAction";
import createStore from "./createStore";
import createReducer from "./createReducer";

/**
* Convenience function for creating a set of actions
Expand Down Expand Up @@ -83,6 +84,7 @@ export default {
utils,
createAction,
createStore,
createReducer,
createActions,
setEventEmitter,
nextTick,
Expand Down
46 changes: 46 additions & 0 deletions test/creatingReducers.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import chai, { assert } from 'chai';
import asPromised from 'chai-as-promised';
import Reflux from "../src";
import sinon from "sinon";

chai.use(asPromised);

describe('with a reducer', function() {
describe('that has initial data', function() {
var reducer, initialData;

beforeEach(() => {
initialData = {
datum: 1337
};
reducer = Reflux.createReducer(initialData);
});

it('should be able to peek the data', function() {
assert.equal(initialData.datum, reducer.peek().datum);
});

describe('hooked to an action', function() {
var action, listener;

beforeEach(() => {
action = Reflux.createAction({sync: true});
listener = sinon.spy();
reducer.listen(listener);
});

it('should pass previous data as first argument', function() {
reducer.on(action, (a) => a);
action();
return assert.deepEqual(listener.args[0][0], initialData);
});

it('should pass arguments to the rest', function() {
reducer.on(action, (a, b, c) => { return {b: b, c: c}; });
action("dude", 1337);
return assert.deepEqual(reducer.peek(), {b: "dude", c: 1337});
});

});
});
});