Replies: 7 comments 8 replies
-
Just return undefined if you don’t need to update store const move = createEvent()
const position = createStore({x: 0, y: 0})
.on(move, (position, {x, y}) => {
if (position.x !== x && position.y !== y) {
return {x, y}
}
}) |
Beta Was this translation helpful? Give feedback.
-
The problem is that |
Beta Was this translation helpful? Give feedback.
-
I was going to suggest you to merge events to apply them at once, but maybe your suggestion really make sense. Thus we need to understand how it should looks like. The term "store comparator" looks like it something about comparisons between stores, but it more an "update filter". Then it might look like that: const position = createStore({x: 0, y: 0}, {
updateFilter: (update, current) => update.x !== current.x
})
position.on(move, (_, upd) => upd)
move({x: 0, y: 0})
// no reaction
move({x: 1, y: 2})
// position → {x: 1, y: 2}
|
Beta Was this translation helpful? Give feedback.
-
The another case:
|
Beta Was this translation helpful? Give feedback.
-
Store values should be immutable, mutations are not supported. Even more, in this case |
Beta Was this translation helpful? Give feedback.
-
Doesn't this solved by mapped store?
const move = createEvent()
const $prepoint = createStore({ x: 0, y: 0 })
.on(move, (_, curr) => curr)
const $point = $prepoint.map(
(curr, prev) =>
!prev || curr.x !== prev.x && curr.y !== prev.y
? curr
: prev
)
$point.watch(_ => console.log('$point.watch:', _))
move({ x: 0, y: 1 })
move({ x: 1, y: 0 })
move({ x: 1, y: 1 }) will ouptut
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Proposal
Add option to createStore method, that will change comparision logic of the store value. Docs says:
But sometimes it not convinient to use only reference comparision, but there is need to add some deep comparision.
Use case
I have a simple struct { x, y }. I need to operate with these two values simulatenously, i.e. set bot values simulatenously, and notify listeners when boths values updated. Ofcourse, I can create 3 store - $x, $y, and combine it to $point. But I do not need to process x and y separately and do not need to notify separately for changes of x, and changes of y, so creating 3 stores instead of one is syntax overhead (I will need to create also 2x dublicting events). That's why I preffer create single store $point with complex object { x, y }, but then, I got need to write code like this to prevent :
Or also code like this:
setPointEvent(...point);
It's quite awfull.
Beta Was this translation helpful? Give feedback.
All reactions