Code Monkey home page Code Monkey logo

Comments (6)

BigAB avatar BigAB commented on May 23, 2024 2

This is probably a coincidence, but this makes it very pleasant to work with

Not a coincidence at all actually.

People started to talk about it, Rich Harris made an issue to discuss it, Ben Lesh put up a PR to support RxJS as stores (with proper unsubscribing) and there was a healthy discussion in the PR too.

Really, just some effort on the maintainers of 2 fantastic libraries and we get some super power magic.

from timdeschryver.dev.

GavinOvsak avatar GavinOvsak commented on May 23, 2024 1

Another way to integrate RxJS and Svelte variables without mimicking stores would be to pair a $: statement with next like this:

<script>
import { BehaviorSubject } from 'rxjs'
let foo;
let subj = new BehaviorSubject('');
$: subj.next(foo)
</script>
<input type="text" bind:value={foo} />

from timdeschryver.dev.

GiuntaLucas avatar GiuntaLucas commented on May 23, 2024

Verry nice article, i'm coming from angular world, and this is the first think i noticed, Store and BehaviorSubject are pretty the same. Good implementation i have to try like that. Wich interest to use Store if we can do everything with BehaviorSubject ? Thank's.

from timdeschryver.dev.

timdeschryver avatar timdeschryver commented on May 23, 2024

Hi @GiuntaLucas , thanks for the kind words.
The interesting part here is that you don't have to switch to Store if you're already used to using BehaviorSubject. The implementation might be different, but because they hold the same contract (subscribe and next) they can be swapped with each other.

from timdeschryver.dev.

Tommertom avatar Tommertom commented on May 23, 2024

I would think the svelte community would be more prone to adopt rxjs if we can have the operators attached to the svelte writable/readable - maybe a sveltePipe operator instead of SvelteSubject?

Edit- or extending and exposing https://github.com/ReactiveX/rxjs/blob/6fa819beb91ba99dadd6262d6c13f7ddfd9470c5/src/internal/observable/fromSubscribable.ts ? After all, a svelte store is subscribable

from timdeschryver.dev.

Tommertom avatar Tommertom commented on May 23, 2024

This is what I would suggest:

<script lang="ts">
    import { writable, type Writable, type Readable, type Unsubscriber } from 'svelte/store';

    import { Observable, from } from 'rxjs';

    function fromSvelteStore<T>(svelteStore: Writable<T> | Readable<T>): Observable<T> {
        let unsub: Unsubscriber;
        const obs = new Observable<T>((subscriber) => {
            unsub = svelteStore.subscribe((val) => {
                console.log('Next on value');
                subscriber.next(val);
            });

            return () => {
                console.log('Unsubscribing the svelte store');
                unsub();
            };
        });

        return obs;
    }

    import { map } from 'rxjs/operators';
    const w = writable<number>(0);
    const stuff = fromSvelteStore<number>(w).pipe(map((x) => x + 10));
    setTimeout(() => {
        w.set(2);
    }, 3000);
</script>

<h1>The count is {$stuff} or {$w}</h1>
<a href="/other">Go to other page so we can see unsubscribe run</a>

from timdeschryver.dev.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.