All Projects → aigoncharov → flux-action-class

aigoncharov / flux-action-class

Licence: MIT license
Boilerplate free class-based action creator. Following flux-standard-action spec. Built with TypeScript. Works with redux and ngrx.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to flux-action-class

Deox
Functional Type-safe Flux Standard Utilities
Stars: ✭ 200 (+809.09%)
Mutual labels:  flux, ngrx, action
redux-routines
👷 Predefined actions creators and action types for common async tasks in a single object called "routine"
Stars: ✭ 12 (-45.45%)
Mutual labels:  redux-actions, action-creator
Typesafe Actions
Typesafe utilities for "action-creators" in Redux / Flux Architecture
Stars: ✭ 2,343 (+10550%)
Mutual labels:  redux-actions, action-creator
hermes-js
Universal action dispatcher for JavaScript apps
Stars: ✭ 15 (-31.82%)
Mutual labels:  flux, action
ts-action-operators
TypeScript action operators for NgRx and redux-observable
Stars: ✭ 34 (+54.55%)
Mutual labels:  ngrx, action
RxReduxK
Micro-framework for Redux implemented in Kotlin
Stars: ✭ 65 (+195.45%)
Mutual labels:  flux, action
Tinystate
A tiny, yet powerful state management library for Angular
Stars: ✭ 207 (+840.91%)
Mutual labels:  flux, ngrx
redux-reducer-async
Create redux reducers for async behaviors of multiple actions.
Stars: ✭ 14 (-36.36%)
Mutual labels:  flux, action
ngrx-slice
Moved to https://github.com/nartc/nartc-workspace
Stars: ✭ 50 (+127.27%)
Mutual labels:  ngrx
todo-app-ngrx
TodoMV* - Angular + Redux (ngrx)
Stars: ✭ 42 (+90.91%)
Mutual labels:  ngrx
mutable
State containers with dirty checking and more
Stars: ✭ 32 (+45.45%)
Mutual labels:  flux
k8s-gitops
Homelab GitOps repository. Cluster definition state via code.
Stars: ✭ 47 (+113.64%)
Mutual labels:  flux
profile-readme
🗣 Display profile activity and other cool widgets in your profile README.md
Stars: ✭ 49 (+122.73%)
Mutual labels:  action
rubocop-linter-action
Rubocop Linter Action: A GitHub Action to run Rubocop against your code!
Stars: ✭ 86 (+290.91%)
Mutual labels:  action
action-ansible-playbook
⚙️ A GitHub Action for running Ansible playbooks
Stars: ✭ 133 (+504.55%)
Mutual labels:  action
ascii-art-action
GitHub Action for printing out ASCII art text
Stars: ✭ 15 (-31.82%)
Mutual labels:  action
continuous-analytics-examples
A collection of examples of continuous analytics.
Stars: ✭ 17 (-22.73%)
Mutual labels:  flux
kubectl-action
Github Action for kubectl
Stars: ✭ 18 (-18.18%)
Mutual labels:  action
NaiveNASflux.jl
Your local Flux surgeon
Stars: ✭ 20 (-9.09%)
Mutual labels:  flux
ionic2.0-angularfire
this a basic application for Ionic 2.0.rc5 with AngularFire2 with ngrx/store & ngrx/effects to manage state
Stars: ✭ 71 (+222.73%)
Mutual labels:  ngrx

flux-action-class Build Status Coverage Status Tweet

Boilerplate free class-based action creator. Following flux-standard-action spec. Built with TypeScript. Works flawlessly with JavaScript and TypeScript right out of the box.

Installation

npm i flux-action-class

Quick start

import { ActionStandard } from 'flux-action-class'

class ActionToDoIncrement extends ActionStandard {}
// Creates action with no payload, no meta and type 'flux-action-class:ActionToDoIncrement'

const reducer = (state, action) => {
  switch (action.type) {
    case ActionToDoIncrement.type:
    // Do stuff
  }
}

In depth

ActionStandard is an abstract class with two generics, payload and meta, set to undefined by default. It has:

  • Static type getter based on class' name to easily use it in your reducers
  • Own (non-static) type getter which uses the static one to comply with flux-standard-action
  • Own (non-static) payload readonly field typed as the first generic (undefined by default)
  • Own (non-static) meta readonly field typed as the second generic (undefined by default)
  • Own (non-static) error readonly boolean field set to true if payload is an instance of Error
  • Protected static prefix field which is the the first part of type set to 'flux-action-class:' by default

Examples

Create an action with no payload and no meta

import { ActionStandard } from 'flux-action-class'

class ActionTest1 extends ActionStandard {}
// Creates action with no payload, no meta, type 'flux-action-class:ActionTest1' and error = false

// TS compiler expects no arguments provided to the constructor
new ActionTest1() // Correct
new ActionTest1('test') // Failure

Create an action with payload and no meta

import { ActionStandard } from 'flux-action-class'

class ActionTest2 extends ActionStandard<number> {}
// Creates action with payload of type number, no meta, type 'flux-action-class:ActionTest2' and error = false

// TS compiler expects one argument provided to the constructor
new ActionTest2(5) // Correct
new ActionTest2() // Failure
new ActionTest2('test') // Failure
new ActionTest2(5, 'test') // Failure

Create an action with payload and meta

import { ActionStandard } from 'flux-action-class'

class ActionTest3 extends ActionStandard<number, string> {}
// Creates action with payload of type number, meta of type string, type 'flux-action-class:ActionTest3' and error = false

// TS compiler expects two arguments provided to the constructor
new ActionTest3(5, 'test') // Correct
new ActionTest3() // Failure
new ActionTest3('test') // Failure
new ActionTest3(5, 45) // Failure

Create an action with no payload and meta

import { ActionStandard } from 'flux-action-class'

class ActionTest4 extends ActionStandard<undefined, string> {}
// Creates action with no payload, meta of type string, type 'flux-action-class:ActionTest4' and error = false

// TS compiler expects two arguments provided to the constructor
new ActionTest4(undefined, 'test') // Correct
new ActionTest4() // Failure
new ActionTest4('test') // Failure
new ActionTest4(5, 45) // Failure

Create an error action with no meta

import { ActionStandard } from 'flux-action-class'

class ActionTest5 extends ActionStandard<Error> {}
// Creates action with error payload, no meta, type 'flux-action-class:ActionTest5' and error = true

// TS compiler expects one argument provided to the constructor
new ActionTest5(new Error()) // Correct
new ActionTest3() // Failure
new ActionTest3('test') // Failure
new ActionTest3(5, 45) // Failure

Create an error action with meta

import { ActionStandard } from 'flux-action-class'

class ActionTest6 extends ActionStandard<Error, string> {}
// Creates action with error payload, meta of type string, type 'flux-action-class:ActionTest6' and error = true

// TS compiler expects one argument provided to the constructor
new ActionTest6(new Error(), 'string') // Correct
new ActionTest6() // Failure
new ActionTest6('test') // Failure
new ActionTest6(5, 45) // Failure

Customize action prefix

Sub-classing

import { ActionStandard } from 'flux-action-class'

abstract class AppBaseAction<Payload = undefined, Meta = undefined> extends ActionStandard<Payload, Meta> {
  protected static readonly prefix = 'app:'
}

class ActionTest7 extends AppBaseAction {}
// Creates action with no payload, no meta, type 'app:ActionTest7' and error = false

Using setPrefix

import { ActionStandard, setPrefix } from 'flux-action-class'

// Add it only once and it changes default prefix for all actions
setPrefix('app:')

class ActionTest7 extends ActionStandard {}
// Creates action with no payload, no meta, type 'app:ActionTest7' and error = false

Usage in reducers

Be aware, if you're using switch-case based reducers, TS compiler is no longer capable of automatic union discrimination, in other words the compiler can no longer match action's type by its field type.

Consider going with selecting a reducer from a map by key (relevant article (go to Tip 3: Switch away from switch), Redux's official documentation) or using ngrx-actions instead.

You can take a look at the discussion here

Usage in production. Minification.

flux-action-class relies on class names and Terser (Uglify) by default doesn't preserve class names during minification. Here's what you can do to change that.

terser-webpack-plugin

Add keep_classnames: true (keep_fnames: true for ES5 targets) to terserOptions.

module.exports = {
  optimization: {
    minimizer: [
      new TerserPlugin({
        terserOptions: {
          // add this line
          keep_classnames: true,
        },
      }),
    ],
  },
}

create-react-app

CRA doesn't expose Terser config by default (please, upvote this issue to change it!). Rour options are:

After that you can follow instructions for tuning terser-webpack-plugin listed above.

Angular

Unfortunately, Angular team doesn't allow us to tune Terser with their default builder (please, upvote this issue to change it!). You could use this custom builder, which is just a subclass of their default builder with Terser-tuning added.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].