Giter Site home page Giter Site logo

Comments (5)

theweiweiway avatar theweiweiway commented on May 27, 2024 1

Hey Felix,

This might be over my head, but I'll look through the code and try to give it a shot!

from bloc.js.

felangel avatar felangel commented on May 27, 2024 1

Hey Felix,

This might be over my head, but I'll look through the code and try to give it a shot!

No worries at all! If you have any questions don't hesitate to ask and again if you prefer to wait that's totally fine as well 👍

from bloc.js.

felangel avatar felangel commented on May 27, 2024

Hi @theweiweiway 👋
Thanks for opening an issue!

I agree that your proposal is more "react-like" and would be a lot more familiar to the existing react community while still preserving the core concepts/behavior of the bloc library.

Unfortunately, I'm focusing on migrating the dart bloc ecosystem to null safety right now but if you're willing to give this a shot, I'm always happy to review the code 👍

If you're busy/prefer not to work on the implementation, no worries! I'll try to get to this in the coming weeks 😄

from bloc.js.

theweiweiway avatar theweiweiway commented on May 27, 2024

Hey @felangel, I've got something that kind of looks like a useBlocBuilder hook - but I think I'm missing a few things which is preventing it from working properly. I've been modelling this hook off of your <BlocBuilder /> component - I have a few questions though.

Here's your <BlocBuilder /> component. I've added comments in to explain what I think the code is doing. Can you let me know if my understanding is correct? For now, I'm omitting previous state and condition and only focusing on the core streaming functionality. I've also added some questions in the code

  private subscribe(): void {
    this.subscription = this.bloc.listen((state: S) => {
      let rebuild: boolean =
        this.condition !== null ? this.condition.call(this, this.previousState, state) : true

      if (rebuild) {
        // this setState updates the state that is being emitted.
        // this also causes componentDidUpdate to fire 
        // which will update ensure we are listening to
        // the most updated version of the bloc
        this.setState({ blocState: state })
      }
      this.previousState = state
    })
  }

  private unsubscribe(): void {
    this.subscription.unsubscribe()
  }

  componentDidUpdate(prevProps: BlocBuilderProps<B, S>): void {
    // every time there is a `blocState` change, we need to update
    // this.bloc so that it has the most recent version of the bloc.
    // The most recent version of the bloc is always `props.bloc`! (is 
    // this assumption actually correct?)
    if (prevProps.bloc !== this.props.bloc) {
      this.unsubscribe()
      // update this.bloc with most updated version of the bloc
      this.bloc = this.props.bloc
      this.previousState = this.bloc.state

      // not 100% sure why we are setStating here. Didn't the
      // setState in the subscribe function already bring us to most
      // updated state? 
      this.setState({ blocState: this.bloc.state })

      // now we resubscribe, which will listen to the most
      // updated version of the bloc
      this.subscribe()
    }
  }

OK - Thanks Felix!

from bloc.js.

theweiweiway avatar theweiweiway commented on May 27, 2024

Closing due to PR #40

from bloc.js.

Related Issues (12)

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.