rxjs icon indicating copy to clipboard operation
rxjs copied to clipboard

Add `set` alias for `next` on `BehaviorSubject`

Open benlesh opened this issue 6 years ago • 14 comments

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.

benlesh avatar Apr 26 '19 16:04 benlesh

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.

kwonoj avatar Apr 26 '19 17:04 kwonoj

Here's a little playground with set aliased to next for the curious:

https://svelte.dev/repl?version=3.1.0&gist=90c5eb4c3e9754ebafb1ed0f828866a2

johnlindquist avatar Apr 26 '19 17:04 johnlindquist

...and a very slightly modified version illustrating assignments directly to a BehaviourSubject:

https://svelte.dev/repl?version=3.1.0&gist=542cdd9735cb7964a5c618374178a5c2

Rich-Harris avatar Apr 26 '19 17:04 Rich-Harris

This would be extremely helpful in Angular too.

[(value)]="fooBehaviorSubject.value"

AlexAegis avatar Apr 26 '19 20:04 AlexAegis

🤷‍♂ 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.

cartant avatar Apr 27 '19 07:04 cartant

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)

benlesh avatar May 08 '19 18:05 benlesh

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?

evdama avatar May 28 '19 18:05 evdama

Hey, sorry — just seeing this. Sure, would be glad to host it in the Svelte org, thank you.

Rich-Harris avatar May 28 '19 22:05 Rich-Harris

Hi @BenLesh.

As i can see from the discussion this issue can be closed.

BioPhoton avatar Aug 12 '19 01:08 BioPhoton

Started a small project with the "SvelteSubject" and implemented the svelte stores as RxJS Observables: https://github.com/spierala/svelte-store-rxjs

  • writable() returns a Writable instance. Writable extends RxJS BehaviorSubject.
  • readable() and derived() return a RxJS Observable

spierala avatar Mar 06 '21 22:03 spierala

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;
	}
}

@benlesh thank you for this! I have two questions if you ever come back to this thread and have some time:

  1. Given the deprecation warnings around lift do you see any issues with this implementation in the future?

  2. 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.

AlexWarnes avatar Mar 24 '22 22:03 AlexWarnes

The lift method would be optional, honestly.

benlesh avatar Mar 25 '22 00:03 benlesh

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 avatar Apr 06 '23 00:04 DallasHoff

@DallasHoff The package @AlexWarnes wrote and maintains is svelte-fuse-rx. I don't think there's official support for RxJS inside Svelte yet.

m1nhtu99-hoan9 avatar Apr 23 '23 07:04 m1nhtu99-hoan9