Skip to content

Commit

Permalink
refactor(combine_reducers): add custom payload to `CombineReducerInit…
Browse files Browse the repository at this point in the history
…Action` and `CombineReducerRegisterAction` to allow custom initialization of sub-reducers
  • Loading branch information
sassanh committed Nov 26, 2024
1 parent 7416592 commit 7033c8f
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 106 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/integration_delivery.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ jobs:
- uses: actions/checkout@v4
name: Checkout

- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}

- name: Install uv
uses: astral-sh/setup-uv@v3
with:
Expand All @@ -68,11 +73,6 @@ jobs:
- uses: actions/checkout@v4
name: Checkout

- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}

- name: Install uv
uses: astral-sh/setup-uv@v3
with:
Expand Down
11 changes: 2 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,11 @@ env/
venv/
ENV/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject
# hatch
redux/_version.py

# mkdocs documentation
/site

# mypy
.mypy_cache/

# hatch
redux/_version.py
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Version 0.18.3

- refactor(combine_reducers): add custom payload to `CombineReducerInitAction` and `CombineReducerRegisterAction` to allow custom initialization of sub-reducers

## Version 0.18.2

- chore(pytest): add `project.entry-points.pytest11` section to `pyproject.toml` so that it can be used as a pytest plugin
Expand Down
12 changes: 8 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ name = "python-redux"
dynamic = ["version"]
description = "Redux implementation for Python"
license = { text = "Apache-2.0" }
authors = [{ name = "Sassan Haradji", email = "[email protected]" }]
maintainers = [{ name = "Sassan Haradji", email = "[email protected]" }]
readme = "README.md"
requires-python = ">=3.11"
keywords = ['store', 'redux', 'reactive', 'autorun']
Expand All @@ -20,6 +22,12 @@ dev-dependencies = [
"tenacity >= 8.2.3",
]

[project.urls]
homepage = 'https://github.com/sassanh/python-redux/'
repository = 'https://github.com/sassanh/python-redux/'
documentation = 'https://github.com/sassanh/python-redux/'
changelog = 'https://github.com/sassanh/python-redux/blob/main/CHANGELOG.md'

[build-system]
requires = ["hatchling", "hatch-vcs"]
build-backend = "hatchling.build"
Expand All @@ -39,10 +47,6 @@ packages = ["redux", "redux_pytest"]
[tool.hatch.build.targets.sdist]
packages = ["redux", "redux_pytest"]

[[project.authors]]
name = "Sassan Haradji"
email = "[email protected]"

[project.scripts]
demo = "demo:main"
todo_demo = "todo_demo:main"
Expand Down
4 changes: 2 additions & 2 deletions redux/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@
'ReducerResult',
'ReducerType',
'Scheduler',
'Store',
'ViewDecorator',
'ViewOptions',
'ViewReturnType',
'combine_reducers',
'is_complete_reducer_result',
'is_state_reducer_result',
'combine_reducers',
'Store',
)
7 changes: 5 additions & 2 deletions redux/basic_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class BaseEvent(Immutable): ...
EventHandler = Callable[[Event], Any] | Callable[[], Any]
AutorunArgs = ParamSpec('AutorunArgs')
ViewArgs = ParamSpec('ViewArgs')
Payload = TypeVar('Payload', bound=Any, default=None)


class CompleteReducerResult(Immutable, Generic[State, Action, Event]):
Expand Down Expand Up @@ -302,13 +303,15 @@ class CombineReducerAction(BaseAction):
_id: str


class CombineReducerInitAction(CombineReducerAction, InitAction):
class CombineReducerInitAction(CombineReducerAction, InitAction, Generic[Payload]):
key: str
payload: Payload | None = None


class CombineReducerRegisterAction(CombineReducerAction):
class CombineReducerRegisterAction(CombineReducerAction, Generic[Payload]):
key: str
reducer: ReducerType
payload: Payload | None = None


class CombineReducerUnregisterAction(CombineReducerAction):
Expand Down
14 changes: 8 additions & 6 deletions redux/combine_reducers.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ def combined_reducer(
result_actions = []
result_events = []
nonlocal state_class
if state is not None and isinstance(action, CombineReducerAction):
if isinstance(action, CombineReducerRegisterAction) and action._id == _id: # noqa: SLF001
if (
state is not None
and isinstance(action, CombineReducerAction)
and action._id == _id # noqa: SLF001
):
if isinstance(action, CombineReducerRegisterAction):
key = action.key
reducer = action.reducer
reducers[key] = reducer
Expand All @@ -66,7 +70,7 @@ def combined_reducer(
)
reducer_result = reducer(
None,
CombineReducerInitAction(_id=_id, key=key),
CombineReducerInitAction(_id=_id, key=key, payload=action.payload),
)
state = state_class(
_id=state._id, # noqa: SLF001
Expand All @@ -93,9 +97,7 @@ def combined_reducer(
if is_complete_reducer_result(reducer_result)
else []
)
elif (
isinstance(action, CombineReducerUnregisterAction) and action._id == _id # noqa: SLF001
):
elif isinstance(action, CombineReducerUnregisterAction):
key = action.key

del reducers[key]
Expand Down
2 changes: 1 addition & 1 deletion redux_pytest/fixtures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
'LoopThread',
'StoreMonitor',
'StoreSnapshot',
'Waiter',
'WaitFor',
'Waiter',
'event_loop',
'needs_finish',
'snapshot_prefix',
Expand Down
Loading

0 comments on commit 7033c8f

Please sign in to comment.