Comments (21)
We can then imagine that for all cases we have to (or can) decorate our reactions:
todos.js
import { reaction } from 'k-simple-state'
export const complete = reaction((action, store) => {
const { todos } = store.data
const todo = todos.get(action.payload)
todos.set({ ...todo, completed: true })
})
listeners.js
import { when } from 'k-simple-state'
import { complete } from './todos'
export default [
complete.when('@@ui/CLICKED_COMPLETE'),
// or
when('@@ui/CLICKED_COMPLETE')(complete),
]
from k-ramel.
yes it can:
// todos.js
import { takeWith } from 'k-simple-state'
export const completeWhen = takeWith(action, store) => {
const { todos } = store.data
const todo = todos.get(action.payload)
todos.set({ ...todos, completed: true })
}
// listeners.js
import { completeWhen } from './todos'
export default [
// here it seems odd to me to call a fonction 'complete' passing an action type :)
completeWhen('@@ui/CLICKED_COMPLETE'),
]
?
from k-ramel.
this is more completeWhen(latest('@@ui/CLICKED_COMPLETE'))
?
Because when I read latest(completeWhen('@@ui/CLICKED_COMPLETE'))
I understand this the latest reaction to happen and triggered.
What we want is the latest action
not the reaction (this is to late)
from k-ramel.
It's remind me event-based vocabulary :
take
meanson(event)
handle reactionstakeWith
meanshandle(reactions)
on event
so it will be :
// take
on(filters)(reaction)
// takeWith
handle(reaction)(filters)
And your example can be written like that :
// todos.js
import { handle } from 'k-simple-state'
export const handleComplete = handle((action, store) => {
const { todos } = store.data
const todo = todos.get(action.payload)
todos.set({ ...todos, completed: true })
})
// listeners.js
import { handleComplete } from './todos'
export default [
handleComplete('@@ui/CLICKED_COMPLETE'),
]
And this is an common vocabulary for listeners
from k-ramel.
We can also use when
instead of on
:
// take
when(filters)(reaction)
// takeWith
handle(reaction)(filters)
from k-ramel.
The issue here is more the name of the generated function, not the API.
Here handleComplete
doesn't represent well the fact that it's a reaction waiting for filters. With this name, I expect a function that can handle the Complete
action.
from k-ramel.
Ok, my bad...
So, your looking for something like that :
// todos.js
import { takeWith } from 'k-simple-state'
export const complete = takeWith((action, store) => {
const { todos } = store.data
const todo = todos.get(action.payload)
todos.set({ ...todos, completed: true })
})
// listeners.js
import { complete } from './todos'
export default [
complete.on('@@ui/CLICKED_COMPLETE'),
]
from k-ramel.
Yes, perhaps it's a good way to explore.
For complete.on
, I have the expectation to handle an envent on the complete
object
Perhaps with another word ? complete.if()
for example ?
from k-ramel.
complete.when('@@ui/CLICKED_COMPLETE')
from k-ramel.
complete.match('@@ui/CLICKED_COMPLETE')
from k-ramel.
I prefer the complete.when()
this is more fluent I think
from k-ramel.
At this time, compleWhen
can be ok isn't it ?
from k-ramel.
I am waiting for @bpetetot POV
from k-ramel.
I'm agree with the wording : when
But I'm not really convinced by the naming convention instead of a DSL.
A DSL could be more extensible :
complete.when('@@ui/CLICKED_COMPLETE')
complete.whenLatest('@@ui/CLICKED_COMPLETE')
complete.debounce('@@ui/CLICKED_COMPLETE', 500)
...
But it could be more complicated to implement.
from k-ramel.
I don't think this more complicated to implement.
We can even write both API I think.
complete('@@ui/CLICKED_COMPLETE') // or rename the function completeWhen
complete.when('@@ui/CLICKED_COMPLETE')
from k-ramel.
from k-ramel.
@EmrysMyrddin the problem with this kind of approach is the number of different exports, and maybe collisions
Or maybe we can have:
import { reaction } from 'k-simple-state'
const { latest, take /* or when */, etc } = reaction
// ...
from k-ramel.
So in summary, maybe something more like that ?
import { latest, take, takeWith } from 'k-simple-state/reactions'
// take version
const onComplete = latest(take('@@ui/CLICKED_COMPLETE'))
onComplete(() => /* do something */)
// takeWith version
const completeWhen = takeWith(() => /* do something */)
latest(completeWhen('@@ui/CLICKED_COMPLETE'))
is it easily understandable when you read the code ?
from k-ramel.
Maybe more understandable with composition :
// take version
const onComplete = compose(latest, take)('@@ui/CLICKED_COMPLETE')
onComplete(() => /* do something */)
// takeWith version
const completeWhen = takeWith(() => /* do something */)
compose(completeWhen, latest)('@@ui/CLICKED_COMPLETE')
from k-ramel.
If we use composition of fonctions maybe we have to rename APIs as you suggested before @bpetetot.
- takeWith -> reaction
- take -> when
const complete = (action, store) => { /* do something */}
// debounce events and call `complete`
compose(
when('@@ui/CLICKED_COMPLETE')
debounce,
)(complete)
// same thing different approach
const debouncedComplete = debounce(complete)
when('@@ui/CLICKED_COMPLETE')(debouncedComplete)
when('@@ui/OTHER_CLICKED')(debouncedComplete)
// calls `complete` on every events
when('@@ui/CLICKED_COMPLETE')(complete)
I think we agree BUT take care this is not the concern of this lib, you can find a debounce
on lodash, a log
in other lib, etc. I don't think this is our purpose.
The main purpose of the lib is to respond with ease to 90% of our developpement.
Which will be something like:
when('@@ui/CLICKED_COMPLETE')(complete)
or:
reaction(complete).when('@@ui/CLICKED_COMPLETE')
reaction(complete).when('@@ui/CLICKED_COMPLETE_2')
from k-ramel.
Seems nice !
And I'm agree that is not the purpose of the lib to bring debounce
, it was just to illustrate my thoughts.
from k-ramel.
Related Issues (20)
- An in-range update of babel7 is breaking the build π¨ HOT 1
- An in-range update of jest-environment-jsdom-global is breaking the build π¨ HOT 1
- reset is triggered 2 times on k-redux-factory types HOT 1
- An in-range update of regenerator-runtime is breaking the build π¨ HOT 1
- An in-range update of babel7 is breaking the build π¨ HOT 24
- Corporate CSP preventing the use of "eval()" HOT 2
- connector/react | lifecycle HOT 1
- An in-range update of eslint is breaking the build π¨ HOT 4
- An in-range update of eslint-plugin-react is breaking the build π¨ HOT 1
- An in-range update of eslint-plugin-import is breaking the build π¨ HOT 2
- An in-range update of rollup-plugin-copy is breaking the build π¨ HOT 3
- change orga name everywhere for next publication
- An in-range update of eslint-plugin-react is breaking the build π¨ HOT 5
- An in-range update of babel-eslint is breaking the build π¨ HOT 1
- An in-range update of eslint-plugin-react-hooks is breaking the build π¨ HOT 2
- An in-range update of babel7 is breaking the build π¨ HOT 12
- An in-range update of regenerator-runtime is breaking the build π¨ HOT 1
- An in-range update of eslint-config-airbnb is breaking the build π¨ HOT 1
- An in-range update of jest2 is breaking the build π¨ HOT 14
- An in-range update of eslint-plugin-import is breaking the build π¨ HOT 1
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 k-ramel.