Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept stores as object #3

Merged
merged 5 commits into from
Dec 24, 2015
Merged
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
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