Skip to content

Suggestions on action creators #2

@mondwan

Description

@mondwan

I suggest action creators should cache and return corresponding namedtuple for the same action name in order to combine best practices from Javscript (JS) and Python (PY).

In Javascript, files for reducers, actions are placed separately while the single dispatch mechanism in python requires the same object reference.

Currently, in order to glue up reducers and actions, I have to do something below

  • reducers/common.py
from pyredux import default_reducer
from pyrsistent import pmap

import xxx.actions.common as Common
import xxx.constants as CONST


DEFAULT_STATE = {
    'isLoading': False,
}


@default_reducer
def main(action, state=pmap(DEFAULT_STATE)):
    return state


@main.register(Common.TYPES[CONST.TYPES['UPDATE_IS_LOADING']])
def updateIsLoading(action, state):
    return state.set('isLoading', action.payload)
  • actions/common.py
import pyredux

import xxx.constants as CONST


TYPES = {
    CONST.TYPES['UPDATE_IS_LOADING']: pyredux.create_action_type(
        CONST.TYPES['UPDATE_IS_LOADING']
    ),
}


def updateIsLoading(isLoading=False):
    return TYPES[CONST.TYPES['UPDATE_IS_LOADING']](isLoading)

It it little bit tedious for exposing those TYPES as they need to be same reference for single dispatch?

I am wondering if the built-in action creator provide a caching mechanism I can simply create an action object every time instead of caching them myself

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions