Giter VIP home page Giter VIP logo

Comments (9)

lilactown avatar lilactown commented on June 27, 2024 1

That's correct! I missed that. Yes, you need to keep a reference to what is returned by .subscribe()

from observe-component.

adrianmcli avatar adrianmcli commented on June 27, 2024 1

Revised example:

import React from 'react';
import { observeComponent, fromComponent } from 'observe-component/rxjs';

// Create the component with the listeners we want
const TextArea = observeComponent('onInput')('textarea');

export default class MyComponent extends React.Component {
  constructor() {
    super();
    this.state = { typing: false };
  }

  componentDidMount() {
    // Create stream for when user is typing
    const inputEvents$ = fromComponent(TextArea, 'onInput');

    // Create stream for when user has been idle for 1 second
    const idle$ = inputEvents$.debounceTime(1000);

    // Subscribe to invoke the streams and save the subscriptions
    this.inputEventsSub = inputEvents$.subscribe(() => this.setState({ typing: true }));
    this.idleSub = idle$.subscribe(() => this.setState({ typing: false }));
  }

  componentWillUnmount() {
    // Unsubscribe from the streams when component unmounts
    this.inputEventsSub.unsubscribe();
    this.idleSub.unsubscribe();
  }

  render() {
    return (
      <div>
        <TextArea cols="30" rows="10" />
        <div>{this.state.typing ? 'User is typing...' : 'User is idle'}</div>
      </div>
    );
  }
}

from observe-component.

lilactown avatar lilactown commented on June 27, 2024

Awesome! Thanks for taking an interesting in the project.

Was there anything you found confusing about the examples currently in the README? Especially re: not being clear that fromComponent needs to be used.

I'll add this as an example to the README soon. Or you can open a pull request of course! :)

from observe-component.

adrianmcli avatar adrianmcli commented on June 27, 2024

Yeah, I can probably put out a PR this weekend. Do you have any comments on my code though? I'm pretty new to RxJS, so I'm not sure if I'm doing things quite the right way yet.

from observe-component.

lilactown avatar lilactown commented on June 27, 2024

From a code correctness perspective, there are two things that I see:

  1. I don't believe that the .share operators are necessary in this case, since we only have one subscriber. Even if we had multiple subscribers, the .share operator should be used judiciously.

  2. Make sure you unsubscribe from your observables on component unmount to help prevent memory leaks

from observe-component.

adrianmcli avatar adrianmcli commented on June 27, 2024
  1. Doesn't the fact that there are two subscription statements (see the bottom of componentDidMount()) mean that the originating stream (i.e. this.inputEvents$) is being subscribed to twice?

  2. Did I not do this in componentWillUnmount()?

from observe-component.

lilactown avatar lilactown commented on June 27, 2024

I edited/updated my comment with more points before I saw your reply. I'll post them later.

Clearly I misread/misremembered your original post, I'm sorry! My second point is totally wrong, you are unsubscribing correctly.

When you create this.idle$, it's a new observable, so the .share on this.inputEvents$ does not act on this. You are still subscribing twice I think (I may be wrong on this).

Subscribing twice, in general, is fine though. Usually I only use .share if I run into problems with subscribing creating some sort of side effect, but in this case it's fine.

from observe-component.

adrianmcli avatar adrianmcli commented on June 27, 2024

It seems to work without the .share() so I suppose we can take it out.

from observe-component.

adrianmcli avatar adrianmcli commented on June 27, 2024

I'm actually also doubting if I'm unsubscribing properly now. I think I might need to save the subscription object from calling .subscribe() and then call .unsubscribe() on that specific subscription object, but I'm not sure.

from observe-component.

Related Issues (7)

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.