Coder Social home page Coder Social logo

WatchBuilder about context_plus HOT 2 CLOSED

lukepighetti avatar lukepighetti commented on June 2, 2024
WatchBuilder

from context_plus.

Comments (2)

s0nerik avatar s0nerik commented on June 2, 2024

Hey Luke, it would be really easy to add such a widget, but it sort of defeats the purpose of the library, IMO, so I'm quite hesitant about adding it to the lib. Let me explain.

  1. The watch function from the builder parameter would have to both accept and return dynamic, which is going to be annoying to work with. Extensions on observable types don’t have such a problem and allow for defining the whole different method signatures based on the receiver type. This gets even more important with the v2 of this package, as β€˜.watch()’ will now return AsyncSnapshot<T> for Stream<T>/Future<T>, T for ValueListenable<T> and the Listenable instance itself for any Listenable.
  2. I could introduce something like WatchBuilder((context) => …), which would just do this:
class WatchBuilder extends StatelessWidget {
  const WatchBuilder({
    super.key,
    required this.builder,
  });

  final WidgetBuilder builder;

  Widget build(BuildContext context) {
    context.unwatch();
    return builder(context);
  }
}

, but, to me, it looks worse in terms of DX compared to just adding the context.unwatch() by hand where/if needed. Especially since in v2 this call is even cheaper and can be made anywhere inside the build method body, even multiple times - with the same result.
3. Not using the context.unwatch() at_all should_not break anything in terms of app's logic/UI:

  • Observable subscription WILL be canceled when the Widget is unmounted.
  • Observable subscription WILL be canceled immediately when the corresponding .watch() call is conditionally replaced with another .watch() call within the build method.
  • The only possible manifestation of the mentioned Flutter's limitation is some potential extra rebuilds if suddenly ALL of the .watch()/context.unwatch() calls have disappeared from the build method's body.
  1. I intended to replicate the InheritedWidget subscription (Something.of(context)) behavior for every observable type out there + make the package stupidly simple to learn and use. This proposal doesn't align with the mentioned vision, IMO.
  2. I'm almost ready to release another package, which would provide an easier-to-use&read alternative to the InheritedWidget, which will be integrated with context_watch in such a way that everytime you access this "InheritedWidget's alternative" within the build method - context.unwatch() will be triggered automatically, making the potential extra rebuilds even less possible, without any additional work from your side. So, if your use-case involves any parent tree lookup for something, you might want to take a look at that new package. I'll let you know when it's released if you're interested.
  3. context_watch v2 will provide a .watchValue(context, (observable) => observable.property) extension to make a widget rebuild only when the specific property value changes. This might help with getting rid of some conditional .watch() calls.

from context_plus.

s0nerik avatar s0nerik commented on June 2, 2024

context_watch 2.0, along with context_ref and context_plus are out now. Check them out to enjoy even less likely unwanted conditional watch() related rebuilds.

from context_plus.

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.