Comments (5)
Consider the following models as an example:
{
name: 'count',
state: 0,
reducers: {
addOne: (state) => state + 1,
addBy: (state, payload) => state + payload,
},
/* ...hooks */
}
{
name: 'form',
state: {},
reducers: {
submit: () => {}
}
}
Hooks API Specs
A hooks API should fit all possible combinations of things that someone might want to happen:
-
1.
form/submit
triggersaddOne
, but only the first time, then the subscriber disappears. In "redux-saga" this is calledtake
-
2.
form/submit
triggersaddOne
every time it is fired. In "redux-saga" this is calledtakeEvery
-
3.
form/submit
can triggeraddBy
with the payload 5 -
4.
form/submit
triggers bothaddOne
andaddBy
, one as a "take" and the other as a "takeEvery" -
5.
form/submit
triggersaddOne
, but only ifaddBy
is not triggered, asaddBy
is given precendence -
6. A user should be able to pattern match a hook.
take('*/submit', addOne())
take('form/*', addOne())
In the example above, all submit
actions would be taken.
-
7. A hook should not need to trigger an action in another model
-
8. Someone may want to capture data from an action name, as a possibility
-
9. There probably isn't a situation where both "take" and "takeEvery" are needed for the same action.
-
10. It would be nice if a user were able to add or remove hooks dynamically at later points in time.
There may be other scenarios. @blairbodnar, let me know if you can think of any.
from rematch.
Consider this a kind of summary.
At its simplest form there are two parts. I'll call them an "Action Matcher" and "On Action" for now. But those definitely aren't good names.
Action Matcher
The action you're listening for.
Examples: form/submit
, */submit
, form/*
- The action may come from any model, but we might want to prevent it from coming from the same model to stop potential loops.
On Action
The function called when the action is matched.
-
This could probably be restricted to
effects
orreducers
only. I'm not sure at this point. -
There should be a way to pass a custom payload to a given "effect" or "reducer" call.
-
It may also be interesting to be able to pull data from an action name, and use that in the payload. For example,
form/5
may capture the number "5", and pass that into a reducer payload. Just a thought, maybe off the deep end with this one. -
There should also be a way to differentiate between
take
(first only) andtakeEvery
(every).
from rematch.
My thoughts so far....
{
hooks: {
'model/actionName': /* something here */
'*/actionName': /* something here */
}
}
from rematch.
A possibility:
{
hooks: {
'model/actionName': () => take(this.AddOne()),
'*/actionName': (action) => takeEvery(this.addBy(action.payload))
}
}
from rematch.
How matchers might work:
- 1) look for an exact match
- 2) look for a pattern match
Matchers would have to be performant to make it work well. Pattern matching a lot of hooks may create performance issues on every action triggered.
from rematch.
Related Issues (20)
- Cannot add property loading, object is not extensible HOT 3
- An argument for 'payload' was not provided. HOT 2
- @rematch/select type error. HOT 8
- Arguments Conflict with TS HOT 7
- TS errors on 2.2.0 HOT 13
- What shall we put as the second generic type of RematchStore<TModels, TExtraModels> HOT 2
- Effects in custom middleware HOT 3
- Type errors when calling effects which utilize rootState without needing a payload HOT 4
- Preserving Rematch.js state between server and client side in Nextjs HOT 1
- @rematch/select typing problem with createSelector() function
- [Typescript] Faulty types HOT 2
- `RematchRootState` second argument default of `Record<string, never>` generates useless state type HOT 1
- @rematch/core has incompatible types with react-redux@8
- TS error while using selectors inside model effects (with plugin like loading) HOT 1
- [Question] Effects in separate files HOT 1
- Conditional types omitted in model effect
- Installation step for @rematch/immer installs immer 10 HOT 2
- [Question] Best practice on call this in reducers and get updated state in effects.
- Effect type inference error on typescript 4.9
- Rematch immer compilation error - (imported as 'produce') was not found HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rematch.