Skip to content

Decorator @action_creator #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.pyc
.vscode
.idea
__pycache__
4 changes: 2 additions & 2 deletions python_redux/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .apply_middleware import apply_middleware
from .bind_action_creators import bind_action_creators
from .bind_action_creators import action_creator, bind_action_creators
from .combine_reducers import combine_reducers
from .compose import compose
from .create_store import create_store

__all__ = ['apply_middleware', 'bind_action_creators', 'combine_reducers', 'compose', 'create_store']
__all__ = ['apply_middleware', 'action_creator', 'bind_action_creators', 'combine_reducers', 'compose', 'create_store']
31 changes: 24 additions & 7 deletions python_redux/bind_action_creators.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
def bind_action_creator(action_creator, dispatch):
return lambda *args: dispatch(action_creator(*args))
from functools import wraps


def action_creator(dispatch):
""" Decorator which wraps an action creator with the dispatch callable
:param dispatch: the dispatch function for the redux store
:return: Decorator function
:rtype: Function
"""
def decorator(func):
@wraps(func)
def wrapped(*positional, **named):
dispatch(func(*positional, **named))
return wrapped
return decorator


def bind_action_creator(action_creator_fn, dispatch):
return lambda *positional, **named: dispatch(action_creator_fn(*positional, **named))

def bind_action_creators(action_creators=None, dispatch=None):
"""
Expand All @@ -25,12 +42,12 @@ def bind_action_creators(action_creators=None, dispatch=None):
"""
if hasattr(action_creators, '__call__'):
return bind_action_creator(action_creators, dispatch)
if type(action_creators) != dict or action_creators == None:
raise Exception('bind_action_creators expected an object or a function, instead received {}.'.format('None' if action_creators == None else type(action_creators)))
if type(action_creators) != dict or action_creators is None:
raise Exception('bind_action_creators expected an object or a function, instead received {}.'.format('None' if action_creators is None else type(action_creators)))

bound_action_creators = {}
for key in action_creators:
action_creator = action_creators[key]
if hasattr(action_creator, '__call__'):
bound_action_creators[key] = bind_action_creator(action_creator, dispatch)
action_creator_fn = action_creators[key]
if hasattr(action_creator_fn, '__call__'):
bound_action_creators[key] = bind_action_creator(action_creator_fn, dispatch)
return bound_action_creators