rxjs
rxjs copied to clipboard
Add `set` alias for `next` on `BehaviorSubject`
Per this discussion on Twitter
Basically, svelte has amazing support for RxJS. Particularly in reading from RxJS observables.
This very small addition to RxJS would enable Svelte users to very conveniently trigger reactive updates through RxJS.
Proposal
Just alias next as set on BehaviorSubject. (I don't see a reason yet to add it to all subjects).
That would enable Svelte users to simply bind to them in their templates.
cc/ @Rich-Harris
Other things
It's probably worth discussion with Svelte experts if this solves a big enough use case for them to be a valid change, and/or what other use cases we might need to consider.
dumb q: why svelte can't use next directly?
at a first sight, I'm bit on opposite path for similar reason to we discussed around zone.js 1. I don't want to some framework specific path (even though this is only setting alias) 2. we tend to block / remove alias as much as it's generally bring confusion from consumer point of view.
Here's a little playground with set aliased to next for the curious:
https://svelte.dev/repl?version=3.1.0&gist=90c5eb4c3e9754ebafb1ed0f828866a2
...and a very slightly modified version illustrating assignments directly to a BehaviourSubject:
https://svelte.dev/repl?version=3.1.0&gist=542cdd9735cb7964a5c618374178a5c2
This would be extremely helpful in Angular too.
[(value)]="fooBehaviorSubject.value"
🤷♂ BehaviorSubject already has a read-only value property and a getValue method. With a set method, it's going to be a shambles, IMO. I can see the appeal - and Svelte looks great - but this class's interface is horrible. Maybe it's already so horrible that it doesn't matter, IDK. Either way I don't feel too strongly about this, but ... yuck.
So after discussion with the core team. It seems more reasonable to create a separate package with a SvelteSubject that would behave in the way you want... it is exactly this:
class SvelteSubject extends BehaviorSubject {
set(value) {
super.next(value);
}
lift(operator) {
const result = new SvelteSubject();
result.operator = operator;
result.source = this;
return result;
}
}
@Rich-Harris what do you think about us adding this to the svelte org? (I'd happily help maintain that for you)
I've been looking into using rxjs with svelte today but didn't find much info apart this issue and https://github.com/sveltejs/svelte/issues/2549
Is there some documentation yet, or maybe tests with some code examples?
Hey, sorry — just seeing this. Sure, would be glad to host it in the Svelte org, thank you.
Hi @BenLesh.
As i can see from the discussion this issue can be closed.
Started a small project with the "SvelteSubject" and implemented the svelte stores as RxJS Observables: https://github.com/spierala/svelte-store-rxjs
writable()returns aWritableinstance.Writableextends RxJS BehaviorSubject.readable()andderived()return a RxJS Observable
So after discussion with the core team. It seems more reasonable to create a separate package with a
SvelteSubjectthat would behave in the way you want... it is exactly this:class SvelteSubject extends BehaviorSubject { set(value) { super.next(value); } lift(operator) { const result = new SvelteSubject(); result.operator = operator; result.source = this; return result; } }
@benlesh thank you for this! I have two questions if you ever come back to this thread and have some time:
-
Given the deprecation warnings around
liftdo you see any issues with this implementation in the future? -
Does
const result = new SvelteSubject()require an initial/current value since it is creating a new bs? (this might be a dumb question)
Side Note: I wrote my first npm package with a handful of rxjs-based svelte actions for ppl who don't want to touch the rxjs side of things, but also expose an rxWritable based on this code for those who need more control with operators, so thank you again because it's a great combination.
The lift method would be optional, honestly.
Hey, sorry — just seeing this. Sure, would be glad to host it in the Svelte org, thank you.
What did the name of this package end up being? I'm not finding it.
@DallasHoff The package @AlexWarnes wrote and maintains is svelte-fuse-rx. I don't think there's official support for RxJS inside Svelte yet.