immstruct-actions
A library helping structure functions intended as actions in a React or Omniscient.js architecture, for providing opinionated helpers and tools for easier use.
Can this be achieved with just functions? Yes. This is just a guide for helping organizing functions and streamlining how to pass functions around. A action dispatcher object has subscribe possibilites, but if used with immstruct, this isn't needed. But there is nothing forcing you to use immstruct with this library, even though it is called immstruct-actions
.
Install
Install immstruct-actions
through npm.
$ npm install --save immstruct-actions
Example Usage
// New empty actionsvar actions = ; // Immutable action dispatchers. Returns new onesvar myActions = actions; var otherActions = actions; // Combine two actionsvar combined = myActions; // invoke one actionvar num = combined; //> 8 // Create composed invokervar numPlus2 = combined4; //> 10
Example In Omniscient
// Passing to components in React or Omniscient.js var actions = ;var component = ; var MyButton = ; // Use destructuring to unwrap changeSomethingvar App = ; var myActions = actions; myActions = myActions;; { // Fetch all functions from myActions React;}
API Reference
actions
Returned when requiring the module.
Create a pocket of actions as defined by initial optional actions and optional subscribers. Used to create new immutable action dispatchers.
Example
var actions = ;var myActions = actions;var double2 = myActions; //> 4var double = myActionsfndouble;
Properties
property | type | description |
---|---|---|
actions |
Object.<String, Function> | attached to the dispatcher |
subscribers |
Array. | attached to the dispatcher |
fns |
Array. | attached to the dispatcher |
actions.invoke(actionName, rest)
Create a pocket of actions as defined by initial optional actions and optional subscribers. Used to create new immutable action dispatchers.
Example
var double2 = actions; //> 4
Parameters
param | type | description |
---|---|---|
actionName |
Array.,String | Invoke a named function |
rest |
Object | Optional arguments passed to action |
Returns Any
,
actions.subscribe(fn)
Add subscriber. Is triggered when any action is invoked. This returns a new actions dispatcher, as action dispatchers are immutable.
Example
var newActionsWithSubscriber = actions;
Parameters
param | type | description |
---|---|---|
fn |
Function | Subscriber function |
Returns actions
,
actions.unsubscribe(fn)
Remove subscriber. This returns a new actions dispatcher, as action dispatchers are immutable.
Example
{ // ...}var newActionsWithoutSubscriber = actions;
Parameters
param | type | description |
---|---|---|
fn |
Function | Subscriber function |
Returns actions
,
actions.register([actionName], fn)
Register new action. This returns a new actions dispatcher, as action dispatchers are immutable.
If not actionName
is defined, it uses name of function passed in.
Example
var actions = ;var myActions = actions;var double2 = myActions; //> 4var double = myActionsfndouble;
Parameters
param | type | description |
---|---|---|
[actionName] |
String | optional: Name of action function |
fn |
Function | Subscriber function |
Returns actions
,
actions.remove(fn)
Remove existing action. This returns a new actions dispatcher, as action dispatchers are immutable.
Example
var actions = ;var myActions = actions;myActions = myActions;
Parameters
param | type | description |
---|---|---|
fn |
Function | Subscriber function |
Returns actions
,
actions.createComposedInvoker(functions)
Create a composed invoker of two or more actions.
Example
var actions = ;var myActions = actions;myActions = actions;var doublePlus2 = myActions;
Parameters
param | type | description |
---|---|---|
functions |
Array. | Functions names of actions to compose |
Returns Function
,
undefined
fns
actions.combine(functions)
Combine one or more action dispatchers. Returns a new action dispatcher which has all the actions and subscribers of the action dispatchers passed as input.
Example
var actions = ;var myAction1 = actions;var myAction2 = actions;var myActions = myAction1;doublePlus2 = myActions;
Parameters
param | type | description |
---|---|---|
functions |
Array. | Functions names of actions to compose |
Returns Actions
,
actions.fn
Property getting all functions added as actions. Will trigger subscribers but can be used as standalone functions.
Example
var actions = ;var myAction1 = actions;var double = myActionsfndouble;// or with destructuringvar double = myActionsfn;