Global

Methods

createScopedDuckFactory(duck) → {function}

Creates a function thas can scope a given duck.

Takes a duck and returns a function that is able to create scoped versions of the given duck.

Parameters:
Name Type Description
duck object
Source:
Returns:

duck factory - a function that takes a scope and returns a scoped version of the given duck

Type
function
Example
import * as counterDuck from "src/counter/duck"
import { createScopedDuckFactory } from "redux-scoped-ducks"

// create a factory for counter duck
export const counterDuckFactory = createScopedDuckFactory(counterDuck)
 
// now you can use the factory to create multiple versions of the counter duck, and therfore its whole redux logic, that are scoped and do not interfere with each other 
const counterADuck = counterDuckFactory("counterA")
const counterBDuck = counterDuckFactory("counterB")

scopeAction(scope, action) → {object|function}

Scopes an action creator/object/type.

the given action can be an action creator/object/type.

The given action is a string: will be treated as action type. scopeAction will return an action object wich will contain the scoped action type aswell as some meta data. If not duck complient, the action type of the returned action object will remain unscoped and no meta data will be added

The given action is an object: will be treated as action object. scopeAction will modify the object by scoping its action type aswell as adding some meta data. If not duck complient, the action object will be returned as it is.

The given action is a function: will be treated as action creator. scopeAction will return an action creator that will return the output of the given action creator but scopes the action type aswell as adding some meta data. If not duck complient, the given action creator will be returned as it is.

Parameters:
Name Type Description
scope string

the scope: will replace the reducer part of the actions action type

action string | object | function

the action (type/object/creator) that will be scoped

Source:
Returns:

scoped action object or action creator

Type
object | function
Example
// basic usage
...
const scopedAction = scopeAction("reducerB", action)

// given action is an action type
const action = "app/reducerA/DO_STUFF";

console.log(scopeAction("reducerB", action));
// { 
//   type: "app/reducerB/DO_STUFF",
//   meta: { 
//     unscopedActionType: "app/reducerA/DO_STUFF" 
//   }
// }

// given action is an action object
const action = { 
 type: "app/reducerA/DO_STUFF",
 payload: 42
};

console.log(scopeAction("reducerB", action));
// { 
//   type: "app/reducerB/DO_STUFF",
//   payload: 42,
//   meta: {
//     unscopedActionType: "app/reducerA/DO_STUFF"
//   }
// }

// given action is an action creator
const action = payload => ({ type: "app/reducerA/DO_STUFF", payload })
console.log(action(42))
// {
//   type: "app/reducerA/DO_STUFF",
//   payload: 42
// }
const scopedAction = scopeAction("reducerB", action)
console.log(scopedAction(42));
// {
//   type: "app/reducerB/DO_STUFF",
//   payload: 42,
//   meta: {
//     unscopedActionType: "app/reducerA/DO_STUFF"
//   }
// }

scopeActionType(scope, actionType) → {string}

Scope an action type.

Replaces reducer in a duck complient action type with param scope". If not duck complient, the given action type is returned unproccessed.

Parameters:
Name Type Description
scope string

the string that will replace the reducer part of the action type

actionType string

the action type that will be scoped

Source:
Returns:

scoped action type

Type
string
Example
const actionType = "app/reducerA/DO_STUFF";
console.log(scopeActionType("reducerB", actionType)); // app/reducerB/DO_STUFF

scopeDuck(scope, duck) → {object}

Scopes a whole duck.

Takes a duck and returns a scoped version. All scrion types, action creators and the reducer are scoped.

Parameters:
Name Type Description
scope string
duck object
Source:
Returns:

scoped duck - all action types/creators and the reducer are scoped

Type
object
Example
// src/valueDuck.js
export const SET_VALUE = "app/reducerA/SET_VALUE"
export const setValue = payload => ({
 type: SET_VALUE,
 payload
})
export default (state = 0, action) => action.type === SET_VALUE
 ? action.payload
 : state

// src/valueADuck.js
import * as valueDuck from "./valueDuck"
export const duck = scopeDuck("valueA", valueDuck)

// src/valueBDuck.js
import * as valueDuck from "./valueDuck"
export const duck = scopeDuck("valueB", valueDuck)

scopeReducer(scope, reducer) → {function}

Scopes a reducer function.

Accepts a scope and a reducer function and returns a scoped reducer fucnction. A scoped reducer works and can be used like a regular reducer - the only difference is that the scoped reducer will ignore all actions that aren't scoped the same. If not of type "function", the given reducer is returned as it is.

Parameters:
Name Type Description
scope string
reducer function
Source:
Returns:
Type
function
Example
const setValue = payload => ({
 type: "app/reducerA/SET_VALUE",
 payload
})

const scopedSetValue = scopeAction("reducerB", setValue)

const reducer = (state = 0, action) => action.type === "app/reducerA/SET_VALUE" ? action.payload : state
const scopedReducer = scopeReducer("reducerB", reducer)

// scoped reducer ignores unscoped action
console.log(scopedReducer(0, setValue(42)))
// 0

// scoped reducer handles scoped action
console.log(scopedReducer(0, scopedSetValue(42)))
// 42