From aa9bba2fe9193272f43843dfb15645895cb03d64 Mon Sep 17 00:00:00 2001 From: Umut Sirin Date: Wed, 23 Dec 2015 16:58:57 -0800 Subject: [PATCH 1/5] Use more es6 sugar --- src/NuclearModule.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/NuclearModule.js b/src/NuclearModule.js index a9e199c..dc20495 100644 --- a/src/NuclearModule.js +++ b/src/NuclearModule.js @@ -5,13 +5,8 @@ import isArray from 'lodash.isarray' import each from 'lodash.foreach' import reduce from 'lodash.reduce' -export default function NuclearModule(options) { - let stores = options.stores || {} - let actions = options.actions || {} - let getters = options.getters || {} - - return function(reactor, StoreFactory) { - StoreFactory = StoreFactory || Store +export default function NuclearModule({ stores = {}, actions = {}, getters = {} }) { + return (reactor, StoreFactory = Store) => { let _stores = stores.reduce((acc, store) => { let storeName = store.name From c23dfc734c3b16e6ea47c508624ec085fb022e5d Mon Sep 17 00:00:00 2001 From: Umut Sirin Date: Wed, 23 Dec 2015 16:59:29 -0800 Subject: [PATCH 2/5] Accept stores as objects, rather than arrays, and remove name --- src/NuclearModule.js | 5 +--- test/test.js | 56 ++++++++++++++++++++++++-------------------- 2 files changed, 31 insertions(+), 30 deletions(-) diff --git a/src/NuclearModule.js b/src/NuclearModule.js index dc20495..39d2538 100644 --- a/src/NuclearModule.js +++ b/src/NuclearModule.js @@ -8,10 +8,7 @@ import reduce from 'lodash.reduce' export default function NuclearModule({ stores = {}, actions = {}, getters = {} }) { return (reactor, StoreFactory = Store) => { - let _stores = stores.reduce((acc, store) => { - let storeName = store.name - let storeDefinition = omit(store, 'name') - + let _stores = reduce(stores, (acc, storeDefinition, storeName) => { if (reactor.evaluate([storeName])) { // TODO: Show a warning unless `debug/dev` mode. return diff --git a/test/test.js b/test/test.js index a47ef00..fafb9ab 100644 --- a/test/test.js +++ b/test/test.js @@ -9,10 +9,11 @@ describe('NuclearModule', function() { describe('#constructor', function() { it('registers nuclear stores to given reactor', function() { var CounterModule = NuclearModule({ - stores: [{ - name: 'counter', - getInitialState: () => 1 - }] + stores: { + counter: { + getInitialState: () => 1 + } + } }) var reactor = new Nuclear.Reactor @@ -27,10 +28,11 @@ describe('NuclearModule', function() { var expectedReactor = null var CounterModule = NuclearModule({ - stores: [{ - name: 'counter', - getInitialState: () => 1 - }], + stores: { + counter: { + getInitialState: () => 1 + } + }, actions: { increment: function(reactor) { expectedReactor = reactor @@ -48,14 +50,15 @@ describe('NuclearModule', function() { it('registers handlers from store definition', function() { var CounterModule = NuclearModule({ - stores: [{ - name: 'counter', - getInitialState() { return 1 }, - handlers: [{ - type: 'INCREMENT', - handler: (state) => state + 1 - }] - }], + stores: { + count: { + getInitialState() { return 1 }, + handlers: [{ + type: 'INCREMENT', + handler: (state) => state + 1 + }] + } + }, actions: { increment: (reactor) => reactor.dispatch('INCREMENT') } @@ -66,25 +69,26 @@ describe('NuclearModule', function() { counter.actions.increment() - expect(reactor.evaluate(['counter'])).toBe(2) + expect(reactor.evaluate(['count'])).toBe(2) }) it('exports getters', function() { var CounterModule = NuclearModule({ - stores: [{ - name: 'counter', - getInitialState() { return 1 }, - handlers: [{ - type: 'INCREMENT', - handler: (state) => state + 1 - }] - }], + stores: { + count: { + getInitialState() { return 1 }, + handlers: [{ + type: 'INCREMENT', + handler: (state) => state + 1 + }] + } + }, actions: { increment: (reactor) => reactor.dispatch('INCREMENT') }, getters: { - count: ['counter'] + count: ['count'] } }) From 923eba27d89bad7e685a764ae2bb97f8167aaf7b Mon Sep 17 00:00:00 2001 From: Umut Sirin Date: Wed, 23 Dec 2015 17:03:51 -0800 Subject: [PATCH 3/5] Update readme with new stores as objects concept --- Readme.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Readme.md b/Readme.md index 46e84a2..874c0b7 100644 --- a/Readme.md +++ b/Readme.md @@ -53,7 +53,9 @@ var actionTypes = require('../actionTypes') // storeDefinition to be used to initialize a Nuclear.Store module.exports = { - name: 'counter', + getInitialState() { + return 0 + }, handlers: [ { type: actionTypes.INCREMENT, @@ -75,7 +77,7 @@ module.exports = { // counter/getters.js module.exports = { - count: ['counter'] + count: ['count'] } ``` @@ -91,9 +93,9 @@ the actions has `reactor` instance as their first argument. NuclearModule = require('nuclear-module') module.exports = NuclearModule({ - stores: [ - require('./stores/counter') - ], + stores: { + count: require('./stores/counter') + }, actions: require('./actions'), getters: require('./getters') }) From fce803a9ecbf04b602034fcc4fccdd769c381429 Mon Sep 17 00:00:00 2001 From: Umut Sirin Date: Wed, 23 Dec 2015 17:04:26 -0800 Subject: [PATCH 4/5] Update example with new store registration --- example/counter/getters.js | 2 +- example/counter/index.js | 6 +++--- example/counter/stores/counter.js | 4 +++- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/example/counter/getters.js b/example/counter/getters.js index ce94773..58efbee 100644 --- a/example/counter/getters.js +++ b/example/counter/getters.js @@ -1,4 +1,4 @@ module.exports = { - count: ['counter'] + count: ['count'] } diff --git a/example/counter/index.js b/example/counter/index.js index b9fc77f..2da8388 100644 --- a/example/counter/index.js +++ b/example/counter/index.js @@ -1,9 +1,9 @@ var NuclearModule = require('../../index') module.exports = NuclearModule({ - stores: [ - require('./stores/counter') - ], + stores: { + count: require('./stores/counter') + }, actions: require('./actions'), getters: require('./getters') }) diff --git a/example/counter/stores/counter.js b/example/counter/stores/counter.js index 7c91681..5b88af7 100644 --- a/example/counter/stores/counter.js +++ b/example/counter/stores/counter.js @@ -4,7 +4,9 @@ var actionTypes = require('../actionTypes') // storeDefinition to be used to initialize a Nuclear.Store module.exports = { - name: 'counter', + getInitialState() { + return 0 + }, handlers: [ { type: actionTypes.INCREMENT, From 3e6a2bc9807899721830598b13042c0540840e3a Mon Sep 17 00:00:00 2001 From: Umut Sirin Date: Wed, 23 Dec 2015 17:05:09 -0800 Subject: [PATCH 5/5] Bump version to 0.3.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3e241c3..fdc3ddf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nuclear-module", - "version": "0.2.0", + "version": "0.3.0", "description": "An opiniated way of writing nuclear-js modules.", "main": "lib/index.js", "scripts": {