Code Monkey home page Code Monkey logo

Comments (5)

josepot avatar josepot commented on May 25, 2024 1

However worth noting I believe it introduces a higher level of complexity than using BehaviorSubjects. But maybe it's just another way of approaching the problem that I'm less familiar with

This library wants to promote a mental model where we avoid mutating state directly, because the we want to be able to define the dynamic behavior of the state at the time of its declaration. Therefore, if we adhere to this mental model, then we should try to use Subjects only for capturing external events, not as a means for mutating the state. This is indeed a quite radical mental-shift, but based in our experience, this mental model ends-up generating code that's less complex and easier to maintain.

I'm going to close this issue, because as you said, I have already answered your question. However, feel free to keep commenting on it after it's closed. Thanks!

from react-rxjs.

sbitproz avatar sbitproz commented on May 25, 2024

So rethinking this question, it appears quite obvious with streams that I must update the array and update Subject with the next data item.

However a clarification on I also wondered if we could use BehaviorSubject instead of Subject? might be useful.

from react-rxjs.

sbitproz avatar sbitproz commented on May 25, 2024

However I would like to know if we can use BehaviorSubject instead of Subject. The use case might be you have a list of items and want to add a new item to the end of the list. In keeping the business logic in the store files, you might want to call a simple no parameter function to add an new item to the end of the list. With a BehaviorSubject enables you to use the last emitted value like so:

const subject: BehaviorSubject<string> = new BehaviorSubject(["a","b","c"]); 
cons newListItem = () => {
   subject.next([...subject.value, "d"])
}

from react-rxjs.

josepot avatar josepot commented on May 25, 2024

Hi @sbitproz

Thanks for your kind words and for your interest in this library. Also, sorry that it took me a while to get back to you, I'm trying to take a break during these days 🙂

This is a very interesting question. As you already know, there are many different ways to accomplish this and using a BehaviorSubject is one of them, yes. However, generally speaking, I'm not a huge fan of using BehaviourSubjects and I rather instead defining different streams for the different events that take place and then defining streams that derive from the stream-events. Meaning that in the case of a list that has to be updated, I would probably rather using scan instead of using a BehaviourSubject. But that, of course, it's just the way that I like to work with streams, so please don't read too much into it.

One thing to take into account, though, is that one of the purposes of this library is to help at avoiding memory-leaks. Meaning that if the library is used as it's intended, if a stream stops having subscribers then its state gets disposed. This section of the docs tries to explain this a bit better. However, if we were using BehaviourSubject instead of scaning stateless streams, then we could be inadvertently generating memory leaks, because even when there are no subscribers the BehaviourSubject will keep its latest state around. This is to say that it really depends on your use case, but that as a general recommendation I rather not to promote the pattern that you are suggesting.

I hope that this answers your question.

PS: I've seen that you have opened another issue. I don't have time right now to look at it, but I check it out as soon as I have a chance: probably in the next few days or so 🙂

from react-rxjs.

sbitproz avatar sbitproz commented on May 25, 2024

Hi @josepot

Totally grateful for message, on new years day no less...

I like the way you've approached the solution with streams. I've applied your todo example with my state solution - but now I think I might adapt a little more to my use case. However worth noting I believe it introduces a higher level of complexity than using BehaviorSubjects. But maybe it's just another way of approaching the problem that I'm less familiar with.

Although I understand that BehaviorSubject cache's the last emitted value, I'm not sure that will produce a leak. In reality it's just single BehaviorSubject instance representing the state with a cache which will be overwritten.

I freely admit, it maybe be down to my lack of understanding of what's going on under the hood.

You have answered my question and I've read most of the library documentation, just wanted to soundboard the idea (BehaviorSubject). Really appreciate your help.

from react-rxjs.

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.