What's the idiomatic way to create a property in Kefir that changes in response to multiple event types?
In my project, I started off using rxjs
for a FRP-style application. In this application, I wanted to subscribe to state that changed in response to multiple variables. This is how I sort of got it working:
const subject = new BehaviorSubject([]);
addEvents
.withLatestFrom(subject, (newItem, items) => items.concat(newItem))
.subscribe(subject);
removeEvents
.withLatestFrom(subject, (item, items) => {
return items.filter(i => i !== item);
})
.subscribe(subject);
I could tell that this was probably not the best practice; it does not seem idiomatic, and I also just figured out that subscribing an observer to multiple sources isn't really correct.
I decided for many reasons to try a different library than RxJs, and am now evaluating Kefir, which has great documentation and supposedly better performance. But I'm finding it even more difficult to determine how to do what I'd like, short of ugly hacks where I'd have to inspect event types:
kefir
.merge(addEvents, removeEvents)
.scan(
(items, event) => {
switch (event.type) {
case 'add': return items.concat(event.item);
case 'remove': return items.filter(i => i !== event.item);
}
},
[])
.toProperty();
I'd really prefer to not have to use inelegant techniques like big conditional blocks for a bunch of event types in order to create a stream of changes.
I don't plan on using Bacon.js, but I do see it has exactly what I need:
Bacon.update([],
[addEvents], (items, evt) => items.concat(evt.item),
[removeEvents], (items, evt) => items.filter(i => i !== evt.item));
Is there a natural way of doing this sort of thing with Kefir with its standard operators, or is this something I'd end up having to implement myself?