provide-multi
Provider factory for creating multiple instances of another provider.
Table of contents
Installation
npm install provide-multi --save
Usage
provideMulti (Object props)
The props
object must contain the following keys:
name
(String) - Suffix used (in proper form) for namespaced actions (Eg: 'counter' will generate 'addCounter' and 'removeCounter')provider
(Object) - Provider object with 'actions' and 'reducers' keysactionTypes
(Object) - Enumeration of all action strings exported by the provider (Eg: { INCREMENT, DECREMENT })initialState
(Object) - Initial state exported by the provider
This will generate a provider with an add/remove action, and one action for every one that our single provider has (Eg: increment, decrement). These copied actions receive an index as parameted, and return the dispatch function.
Also, every reducer will be replicated with a 'Multi' suffix, holding an array of single reducers.
Example
- Refer action in single provider
<MyElement onClick=increment) />
- Refer action in in multi provider, for instance index '0'
<MyElement onClick=) />
- Dispatch action in single provider
;
- Dispatch action in multi provider, for instance index '0'
;
Provider definition:
// test/counter-multi.js ;; const counterMulti = ; ;
Example counter provider used to add multiple instance functionality:
// test/counter.js const INCREMENT = 'INCREMENT';const DECREMENT = 'DECREMENT';const INITIAL_STATE = value: 0const update = Object const actions = { return type: INCREMENT ; } { return type: DECREMENT ; } const reducers = { return state; } actions reducers;
You'll then have a provider with the following actions:
addCounter ()
- Adds an instance to our multiple counter providerremoveCounter ()
- Removes an instance to our multiple counter providerincrement (Int index)
- Returns a function which is equivalent to the single provider action (not dispatched until executed)decrement (Int index)
- Returns a function which is equivalent to the single provider action (not dispatched until executed)
And reducers:
counters