Skip to content
Permalink
Browse files

feat(syntagme): add listen method

  • Loading branch information...
rymizuki committed Apr 6, 2016
1 parent bc77f56 commit 67d2020f75a5cf24f9a816fd655eff3f35050d7e
Showing with 65 additions and 18 deletions.
  1. +12 −0 script/syntagme/index.js
  2. +7 −4 script/syntagme/store.js
  3. +25 −4 syntagme.js
  4. +9 −4 test/syntagme-dispatch-spec.js
  5. +12 −6 test/syntagme-reducer-spec.js
@@ -10,18 +10,30 @@ class Syntagme {
this.dispatcher = args.dispatcher || new Dispatcher
this.utils = utils
this.config = config
this.listening_fg = false
this.connect()
}
connect () {
this.dispatcher.register(this.store.handle.bind(this.store))
this.connected_fg = true
}
subscribe(fn) {
this.store.subscribe(fn)
}
listen (cb) {
this.store.listen(() => {
this.listening_fg = true
this.dispatcher.dispatch({source: 'SYNTAGME', action: {type: 'INIT'}})
if (cb) cb.call(null)
})
}
reducer (reducer) {
return this.store.reducer(reducer)
}
dispatch (payload) {
if (!this.listening_fg) {
throw new Error('syntagme was not listening. call `app.listen()`')
}
this.dispatcher.dispatch(payload)
}
handleAction (type, fn) {
@@ -41,11 +41,11 @@ function remove (array, element) {

export default class Store {
constructor () {
this.listeners = []
this.subscribers = []
this.reducers = []
}
subscribe (fn) {
this.listeners.push(fn)
this.subscribers.push(fn)
}
handle (payload) {
var current_state = null
@@ -56,8 +56,8 @@ export default class Store {
}
if (this.state != current_state) {
this.state = current_state
for (let i = 0; i < this.listeners.length; i++) {
this.listeners[i](current_state)
for (let i = 0; i < this.subscribers.length; i++) {
this.subscribers[i](current_state)
}
}
}
@@ -77,6 +77,9 @@ export default class Store {
this.reducers = this.reducers.concat(reducers)
return reducers
}
listen (cb) {
if (cb) cb.call(null)
}
getState () {
return this.state
}
@@ -92,19 +92,32 @@ return /******/ (function(modules) { // webpackBootstrap
this.dispatcher = args.dispatcher || new _dispatcher2.default();
this.utils = _utils2.default;
this.config = _config2.default;
this.listening_fg = false;
this.connect();
}

_createClass(Syntagme, [{
key: 'connect',
value: function connect() {
this.dispatcher.register(this.store.handle.bind(this.store));
this.connected_fg = true;
}
}, {
key: 'subscribe',
value: function subscribe(fn) {
this.store.subscribe(fn);
}
}, {
key: 'listen',
value: function listen(cb) {
var _this = this;

this.store.listen(function () {
_this.listening_fg = true;
_this.dispatcher.dispatch({ source: 'SYNTAGME', action: { type: 'INIT' } });
if (cb) cb.call(null);
});
}
}, {
key: 'reducer',
value: function reducer(_reducer) {
@@ -113,6 +126,9 @@ return /******/ (function(modules) { // webpackBootstrap
}, {
key: 'dispatch',
value: function dispatch(payload) {
if (!this.listening_fg) {
throw new Error('syntagme was not listening. call `app.listen()`');
}
this.dispatcher.dispatch(payload);
}
}, {
@@ -254,14 +270,14 @@ return /******/ (function(modules) { // webpackBootstrap
function Store() {
_classCallCheck(this, Store);

this.listeners = [];
this.subscribers = [];
this.reducers = [];
}

_createClass(Store, [{
key: 'subscribe',
value: function subscribe(fn) {
this.listeners.push(fn);
this.subscribers.push(fn);
}
}, {
key: 'handle',
@@ -274,8 +290,8 @@ return /******/ (function(modules) { // webpackBootstrap
}
if (this.state != current_state) {
this.state = current_state;
for (var _i = 0; _i < this.listeners.length; _i++) {
this.listeners[_i](current_state);
for (var _i = 0; _i < this.subscribers.length; _i++) {
this.subscribers[_i](current_state);
}
}
}
@@ -297,6 +313,11 @@ return /******/ (function(modules) { // webpackBootstrap
this.reducers = this.reducers.concat(reducers);
return reducers;
}
}, {
key: 'listen',
value: function listen(cb) {
if (cb) cb.call(null);
}
}, {
key: 'getState',
value: function getState() {
@@ -3,10 +3,11 @@ var syntagme = require('syntagme')
describe('syntagme.dispatch', function () {
var handler
var stuff
beforeEach(function () {
beforeEach(function (done) {
stuff = syntagme()
handler = sinon.stub()
stuff.dispatcher.register(handler)
stuff.listen(done)
})
describe('execute with payload', function () {
beforeEach(function () {
@@ -16,17 +17,21 @@ describe('syntagme.dispatch', function () {
})
})

describe('handler called once', function () {
describe('handler called twice', function () {
it('should be true', function () {
assert.ok(handler.calledOnce)
assert.ok(handler.calledTwice)
})
})
describe('handle payload', function () {
it('should be equal', function () {
assert.deepEqual({
source: 'SYNTAGME',
action: {type: 'INIT'},
}, handler.args[0][0])
assert.deepEqual({
source: 'TEST',
action: {type: 'TEST_ACTION'},
}, handler.args[0][0])
}, handler.args[1][0])
})
})
})
@@ -6,13 +6,17 @@ describe('syntagme.reducer', function () {
})
describe('single reduce', function () {
var subscriber
beforeEach(function () {
beforeEach(function (done) {
subscriber = sinon.stub()
this.syntagme.subscribe(subscriber)
this.syntagme.reducer(function (payload, previous) {
previous || (previous = {})
return { count: (previous.count || 0) + payload.action.count }
if (payload.action.type == 'TEST')
return { count: (previous.count || 0) + payload.action.count }
else
return previous
})
this.syntagme.listen(done)
})
describe('when handled', function () {
describe('subscriber', function () {
@@ -24,22 +28,22 @@ describe('syntagme.reducer', function () {
})
describe('called', function () {
it('should be true', function () {
assert.ok(subscriber.calledOnce)
assert.ok(subscriber.calledTwice)
})
})
describe('state', function () {
it('should be reduced', function () {
assert.deepEqual({
count: 1
}, subscriber.args[0][0])
}, subscriber.args[1][0])
})
})
})
})
})
describe('multiple reduce', function () {
var subscriber
beforeEach(function () {
beforeEach(function (done) {
subscriber = sinon.stub()
this.syntagme.subscribe(subscriber)
this.syntagme.reducer(function (payload, previous) {
@@ -58,6 +62,7 @@ describe('syntagme.reducer', function () {
data.count = 2
return data
})
this.syntagme.listen(done)
})
describe('when handled action', function () {
beforeEach(function () {
@@ -88,7 +93,7 @@ describe('syntagme.reducer', function () {
})
describe('order reduce', function () {
var subscriber
beforeEach(function () {
beforeEach(function (done) {
subscriber = sinon.stub()
this.syntagme.subscribe(subscriber)
this.syntagme.reducer([
@@ -114,6 +119,7 @@ describe('syntagme.reducer', function () {
return data
}
])
this.syntagme.listen(done)
})
describe('when handled action', function () {
beforeEach(function () {

0 comments on commit 67d2020

Please sign in to comment.
You can’t perform that action at this time.