import Pool from '@duckness/pool'
import CounterDuck from './ducks/CounterDuck'
const CounterPool = Pool({
buildStore: ({ initialCounter = 0 } = {}) => {
return { counter: initialCounter }
}
})
CounterPool.addDuck(CounterDuck)
CounterPool.build({initialCounter: 0})
CounterPool.store
// => [redux store]
- Example
-
API
- Create Pool
.addDuck(duck)
.preReducer(reducer)
.postReducer(reducer)
.build(props)
.store
.dispatch(...)
.reduce([state,] action)
.select(?selector)
.fetch(selector, ?resolver)
.trigger(selector, callback, ?resolver)
.setErrorReporter(reporterFn)
.reportError(error)
.addMiddleware(middleware)
- Pool Streams - Pool plugins
.ducks
-
.getDuckByName(duckPath)
.middlewares
.streams
.props
- Examples
- @Duckness packages:
const myPool = Pool({
?poolName: String, // pool name, default is 'pool'
?ducks: Array<Duck>, // array of pool ducks
?middlewares: Array<Middleware>, // additional middlewares for Redux store
?streams: Array<PoolStream>, // pool plugins
?buildStore: (props, { refProps, refDucks, refErrorReporter }) => storeState, // build initial store state
?buildRootReducer: ({ refDucks, refErrorReporter }) => rootReducer // build custom root reducer from ducks instead of default root reducer
})
Optional function to build init store state
(props, { refProps, refDucks, refReducers, refErrorReporter }) => storeState
-
props
are passed to pool whenpool.build(props)
are called. -
refProps.current
- props passed topool.build(props)
function -
refDucks.current
- current array of ducks. -
refReducers.root
- pool root reducer -
refReducers.pre
- pool pre reducer -
refReducers.post
- pool post reducer -
refErrorReporter.current
- current error reporter function.
If not specified store state will be set to {}
.
Optional function for custom root reducer
(ducks, { refProps, refDucks, refReducers, refErrorReporter }) => rootReducer
-
ducks
- array of ducks -
refProps.current
- props passed topool.build(props)
function -
refDucks.current
- current array of ducks. -
refReducers.pre
- pool pre reducer -
refReducers.post
- pool post reducer -
refErrorReporter.current
- current error reporter function.
Default root reducer will combine all duck root reducers via
ducks.reduce((state, duck) => {
return duck(state, action)
}, state)
with every duck root reducer wrapped in try/catch returning unmodified state in case of exception inside duck root reducer.
Add duck to pool.
MyPool.addDuck(myDuck)
Add pool pre reducer. Pre reducers will run BEFORE duck reducers and will receive all actions. Will replace previously set pre reducer.
MyPool.preReducer(function globalPreReducer(state, action) {/* ... */} )
Add pool post reducer. Post reducers will run AFTER duck reducers and will receive all actions. Will replace previously set post reducer.
MyPool.postReducer(function globalPostReducer(state, action) {/* ... */} )
Build pool state from some props. props
will be passed to buildStore
function.
MyPool.build({ initialCounter: 0 })
Reference to built redux store.
MyPool.store.subscribe(/* ... */)
- Dispatches an action (action is a plain object).
const action = { type: 'actionType', payload: {} }
MyPool.dispatch(action)
- Dispatches and action from 'poolName/duckName' duck. This will first look for a duck by 'duckName' and current 'poolName', then duck's action creator by 'actionName' and call it with 'actionParams' to build action to dispatch.
MyPool.dispatch('duckName', 'actionName', ...actionParams)
Example:
const MyDuck = Duck('myDuck', 'myPool')
MyDuck.action('someAction')
const MyPool = Pool({ poolName: 'myPool' })
MyPool.addDuck(MyDuck)
MyPool.dispatch('myDuck', 'someAction', ...actionParams)
// equal to
MyPool.dispatch(MyDuck.action.someAction(...actionParams))
- Dispatches and action from 'customPoolName/duckName' duck. This will first look for a duck by 'duckName' and 'poolName', then duck's action creator by 'actionName' and call it with 'actionParams' to build action to dispatch.
MyPool.dispatch(['customPoolName', 'duckName'], 'actionName', ...actionParams)
Reduce state with action. Does not change pool's store. Can be used to look ahead what the state will be after dispatching an action.
MyPool.reduce(action) // reduce pool's store state
MyPool.reduce(state, action) // reduce custom state
Select something from store. Returns store state if selector
undefined.
MyPool.select(MyDuck.select.something)
// => selected value
MyPool.select()
// => current store state
Returns Promise
function resolver(selected, resolve, prevSelected)
Fetch value from the store:
- first
selector
is called to produce selected value - if
resolver
is defined: selected value is passed to resolver with previous selected value (undefined
for the first run) andresolve
function that can be used to resolve thisfetch
- if
resolver
is not defined: selected value will be resolved if it is notundefined
- if nothing was resolved: repeat when store is updated (after next action dispatch).
fetch
will subscribe to store updates (if it was not resolved after first select) and unsubscribe when it is resolved.
// resolves with items when items are loaded into store or are already present
const items = await ListPool.fetch(ItemsDuck.select.items)
// resolves when counter is reset, returning counter value before reset
const counterResetFrom = await MyPool.fetch(
state => state.counter,
(selected, resolve, prevSelected) => {
if (null != prevSelected && 0 !== prevSelected && 0 === selected) {
resolve(selected)
}
}
)
Returns clear trigger function.
function callback(selected)
function resolver(selected, resolve, prevSelected)
Trigger callback
every time:
- selected value changes (
resolver
is undefined) - or
resolver
callsresolve
trigger
calls resolver
first time trigger is set and later on store updates.
To unsubscribe call clear trigger function.
// console.log value for 10 seconds
const clearTrigger = MyPool.trigger(
MyDuck.select.someValue,
console.log
)
setTimeout(clearTrigger, 10000)
Set exception reporter function. Will also overwrite errorReporters set in ducks.
MyPool.setErrorReporter(error => {
window.Sentry.captureException(error)
})
Call assigned error reporter
MyPool.reportError(new Error('Clean pool!'))
Add custom Redux middleware (applied on .build
)
Pool Stream is a Pool plugin that can add middlewares to Redux store. One example is @duckness/pool-saga-stream that adds @duckness/saga support to Pool (ducks can have Redux Sagas).
Adds Pool Stream to Pool
{
// array of middlewares for Redux store
middlewares({ refDucks, refProps, refReducers, refErrorReporter }) {
// refDucks.current - current array of ducks
// refProps.current - props passed to build function
// refReducers.root - root reducer function
// refReducers.pre - pool pre reducer function
// refReducers.post - pool post reducer function
// refErrorReporter.current - current error reporter function
// ...
return [...streamMiddlewares]
},
// called before redux store is built
beforeBuild({ refDucks, refProps, refReducers, refErrorReporter }) {
// refDucks.current - current array of ducks
// refProps.current - props passed to build function
// refReducers.root - root reducer function
// refReducers.pre - pool pre reducer function
// refReducers.post - pool post reducer function
// refErrorReporter.current - current error reporter function
// ...
},
// called after redux store is built
afterBuild({ refStore, refDucks, refProps, refReducers, refErrorReporter }) {
// refStore.current - current Redux store
// refDucks.current - current array of ducks
// refProps.current - props passed to build function
// refReducers.root - root reducer function
// refReducers.pre - pool pre reducer function
// refReducers.post - pool post reducer function
// refErrorReporter.current - current error reporter function
// ...
}
}
Array of added ducks (read only)
Find added duck by it's path. Returns null
if duck is not found.
- Array:
[poolName, duckName]
- String:
duckName
(use current poolName)
Array of added middlewares (read only)
Array of added streams (read only)
Props used to build pool (read only)
https://github.com/hitosu/duckness/tree/master/stories
- @duckness/duck - Modular Redux Ducks hatchery
- @duckness/saga - Redux Saga extension for @duckness/duck
- @duckness/epic - Redux-Observable extension for @duckness/duck
- @duckness/pool - @duckness/duck + Redux
- @duckness/pool-saga-stream - @duckness/saga plugin for @duckness/pool
- @duckness/pool-epic-stream - @duckness/epic plugin for @duckness/pool
- @duckness/react-redux-pool - @duckness/pool + React-Redux
- @duckness/use-redux - React hook for Redux store
- @duckness/use-pool - React hook for @duckness/pool.
- @duckness/store - simple store for React components
- @duckness/reactor - reactive data flow builder