Skip to content

Commit

Permalink
Merge pull request #3 from usirin/accept-stores-as-object
Browse files Browse the repository at this point in the history
Accept stores as object
  • Loading branch information
usirin committed Dec 24, 2015
2 parents ce176c4 + 3e6a2bc commit d02d2ee
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 48 deletions.
12 changes: 7 additions & 5 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -75,7 +77,7 @@ module.exports = {
// counter/getters.js

module.exports = {
count: ['counter']
count: ['count']
}
```

Expand All @@ -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')
})
Expand Down
2 changes: 1 addition & 1 deletion example/counter/getters.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
count: ['counter']
count: ['count']
}

6 changes: 3 additions & 3 deletions example/counter/index.js
Original file line number Diff line number Diff line change
@@ -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')
})
4 changes: 3 additions & 1 deletion example/counter/stores/counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
14 changes: 3 additions & 11 deletions src/NuclearModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,10 @@ 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

let _stores = stores.reduce((acc, store) => {
let storeName = store.name
let storeDefinition = omit(store, 'name')
export default function NuclearModule({ stores = {}, actions = {}, getters = {} }) {
return (reactor, StoreFactory = Store) => {

let _stores = reduce(stores, (acc, storeDefinition, storeName) => {
if (reactor.evaluate([storeName])) {
// TODO: Show a warning unless `debug/dev` mode.
return
Expand Down
56 changes: 30 additions & 26 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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')
}
Expand All @@ -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']
}
})

Expand Down

0 comments on commit d02d2ee

Please sign in to comment.