Last active
May 9, 2017 13:57
-
-
Save okunishinishi/5aa6de9ca58c98480c187001867e3a4b to your computer and use it in GitHub Desktop.
Reduxを使ってちゃんとしたアプリをちゃんと作るのがあまりに面倒なんでなんとかしてなんとかする方法を模索してみた ref: http://qiita.com/okunishinishi@github/items/5c4860d3be8cbcc59286
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createCounterWithNamedType(counterName = '') { | |
return function counter(state = 0, action) { | |
switch (action.type) { | |
case `INCREMENT_${counterName}`: | |
return state + 1; | |
case `DECREMENT_${counterName}`: | |
return state - 1; | |
default: | |
return state; | |
} | |
} | |
} | |
function createCounterWithNameData(counterName = '') { | |
return function counter(state = 0, action) { | |
const {name} = action; | |
if(name !== counterName) return state; | |
switch (action.type) { | |
case `INCREMENT`: | |
return state + 1; | |
case `DECREMENT`: | |
return state - 1; | |
default: | |
return state; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const theStore = require('the-store') | |
const { Scope } = theStore | |
// Scoped state class | |
class CounterScope extends Scope { | |
// Define initial state | |
static get initialState () { | |
return 0 | |
} | |
// Define reducer factory | |
static get reducerFactories () { | |
return { | |
increment (amount) { | |
return (state) => state + amount | |
} | |
} | |
} | |
} | |
async function tryExample () { | |
let store = theStore() | |
// Create state instance and attach to the store | |
store.load(CounterScope, 'counterA') | |
store.load(CounterScope, 'counterB') | |
{ | |
// Access to loaded store | |
let { counterA } = store | |
// Each instance has dispatcher methods which share signatures with reducerFactories | |
counterA.increment(1) // This will dispatch action and reduce into state | |
// Access to the scoped state | |
console.log(counterA.state) // -> 1 | |
} | |
// Store it self has all state | |
console.log(store.state) // -> { counterA: 1 } | |
} | |
tryExample().catch((err) => console.error(err)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CounterScope extends Scope { | |
static get reducerFactories () { | |
return { | |
increment (amount) { | |
return (state) => state + amount | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function tryExample () { | |
let store = theStore() | |
// Create state instance and attach to the store | |
store.load(CounterScope, 'counterA') | |
store.load(CounterScope, 'counterB') | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
// Access to loaded store | |
let { counterA } = store | |
// Each instance has dispatcher methods which share signatures with reducerFactories | |
counterA.increment(1) // This will dispatch action and reduce into state | |
// Access to the scoped state | |
console.log(counterA.state) // -> 1 | |
} | |
// Store it self has all state | |
console.log(store.state) // -> { counterA: 1 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"type": "counterA/increment", | |
"payload": [1] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(state) => state + amount` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment