ac

actions-creator

Actions Creator is an awesome tiny javascript package that allows you to dynamically and easily create callbackable-actions standardized objects.

Showing:

Popularity

Downloads/wk

16.2M

GitHub Stars

6

Maintenance

Last Commit

5mos ago

Contributors

0

Package

Dependencies

1

Size (min+gzip)

0.8KB

License

MIT

Type Definitions

Tree-Shakeable

No?

Categories

Readme

Actions Creator

Description

Actions Creator is an awesome tiny javascript package that allows you to dynamically and easily create callbackable-actions standardized objects. \ It was originally used to create redux actions, but it can be used anywhere when it is needed.


Installation

npm install actions-creator

Usage

import actionsCreator from "actions-creator"
//      CommonJS usage
//      const actionsCreator = require("actions-creator")

const first_action = actionsCreator.MY.FIRST.ACTION("arg1", "arg2")
console.log(first_action)
//      {
//          type: "MY/FIRST/ACTION",
//          args: ["arg1", "arg2"],
//          cb: f() identity,
//          _index: 1
//      } 

const second_action = actionsCreator.This.is.my.second.action(2021)
console.log(second_action)
//      {
//          type: "This/is/my/second/action",
//          args: [2021],
//          cb: f() identity,
//          _index: 2
//      } 



//      If we just need to generate an action type as a string,
//      we can do it easily
const type1 = String(actionsCreator.MY.FIRST.ACTION)
console.log(type1)
//      "MY/FIRST/ACTION"

//      or when doing any string conversion
const type2 = actionsCreator.MY.FIRST.ACTION + ""
console.log(type2)
//      "MY/FIRST/ACTION"

As we see in the example above, every action object has following fields:

  • type \ action type as a string
  • args \ any array that contains all the data needed for the action
  • cb \ callback function by default is identity function (x => x), but we can pass any function if we need to have a callback. The next example below shows how to pass that.
  • _index \ positive integer number (1, 2, 3, ...)․ It is created automatically and shows action order by index. It can be used as a unique identifier or help understand which action was created earlier(in rare cases, it may be necessary)

Actions With Callback

Sometimes we need the action to have callback capability. It might be necessary in many cases. \ Actions Creator allows us to do this in a beautiful way: \ When we try to generate an action object, we can pass the callback function as the last argument. Actions Creator will check and if the last argument is a function, it will be considered as a callback function.

import actionsCreator from "actions-creator"


const my_callback = () => {
    console.log("Hello, I am callback!!!")
}

const callbackable_action = actionsCreator.CALLBACKABLE.EXAMPLE(1, 2, 3, my_callback)

console.log(callbackable_action)
//      {
//          type: "CALLBACKABLE/EXAMPLE",
//          args: [1, 2, 3],
//          cb: f() my_callback,
//          _index: 1
//      }

callbackable_action.cb()
//      "Hello, I am callback!!!"


Customize

We do not recommend to customize, but if you need to do it, you can easily do that: \ see: Produce By Path Pattern


Maintainers


License

MIT

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100