prepend
operator
#165
AlexandrHoroshih
started this conversation in
Ideas
Replies: 3 comments 2 replies
-
Yeah, list($tasks, ({ store: $task, key: $idx }) => {
h("input", {
attr: { value: $task },
handler: {
change: prepend<InputEvent>({
source: $idx,
target: taskChanged,
fn: (idx, evt) => ({ idx, value: evt.target.value })
})
}
})
}) Also, I'd recommend to support a shorthand as well: prepend($idx, taskChanged, (idx, evt) => ({ idx, value: evt.target.value })) |
Beta Was this translation helpful? Give feedback.
0 replies
-
Another possbile use-case - write to stores from multitarget sample sample({
source: ...,
clock: ...,
fn: () => {
...
return {
dataOne: "bla",
dataTwo: "kek"
}
},
target: [
prepend({ fn: (d) => d.dataOne, target: $storeOne }),
prepend({ fn: (d) => d.dataTwo, target: $storeTwo })
]
}) Maybe, even some shorthand overload would be useful prepend($store, (data) => data.dataOne)
// or other unit
prepend(event, (data) => data.dataOne) |
Beta Was this translation helpful? Give feedback.
2 replies
-
Another case: split with multiple cases + data mapping split({
source: filterBy,
match: {
all: msg => msg === 'all',
new: msg => msg === 'new',
},
cases: {
all: prepend($all, (list) => Object.keys(list).map(key => list[key]).flat()),
new: prepend({
source: $user,
fn: (user, list) => Object.keys(list).map(key => list[key]).flat().filter(entity => entity.userId === user.id),
target: $otherStore
})
}
}) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Kudos to @Kelin2025 who is original author of this operator
Idea
This is basically upside-down version of
sample
, which returns callableclock
event, instead of non-callabletarget
It can be very useful in many cases, like (very popular recently)
guard + fn
Or any case, where we might need to attach additional data to target:
also it can serve as a more common version of
attach
operator, like this:UPD: actually, it is not exactly like
attach
🤔, more like event defined together with some data-flow partTry it
Caveats
Due to lack of support for this kind of type inference in TypeScript it will be almost always necessary to set correct payload type via generic, like this:
Which is similar to usage of
event.prepend
Beta Was this translation helpful? Give feedback.
All reactions