Coder Social home page Coder Social logo

Vars should be splittable about airstream HOT 4 OPEN

raquo avatar raquo commented on August 20, 2024 3
Vars should be splittable

from airstream.

Comments (4)

phfroidmont avatar phfroidmont commented on August 20, 2024 3

I use this pattern of splitting vars quite a bit. Since this only comes up in the context of creating child components, creating a few helpers limited to that scope made things much easier (maybe I should make a PR to Laminext @yurique ?).

extension [A](seqVar: Var[Seq[A]])
  def splitRender[Key](key: A => Key)(project: Var[A] => HtmlElement)(using CanEqual[Key, Key])
    : Mod[HtmlElement] =
    onMountInsert(ctx =>
      given Owner = ctx.owner
      children <-- seqVar.signal.split(key)((_, _, signal) =>
        val zoomedVar = Var(signal.observe.now())
        project(zoomedVar)
          .amend(signal --> zoomedVar, zoomedVar --> seqVar.updaterById(key))
      )
    )

extension [A](optionVar: Var[Option[A]])
  def splitRenderOption(project: Var[A] => HtmlElement, ifEmpty: => HtmlElement)
    : Mod[HtmlElement] =
    onMountInsert(ctx =>
      given Owner = ctx.owner
      child <-- optionVar.signal.splitOption(
        (_, signal) =>
          project(
            optionVar.zoom(_.getOrElse(signal.observe.now()))((_, value) => Some(value))
          ),
        ifEmpty = ifEmpty
      )
    )

As for zooming, I actually implemented something very similar to the LazyDerivedVar you describe (that I called ValueController). Internally it was a wrapper for Signal[State], Observer[State => State] and Lens[State, State2] from Monocle which allows support for an updater method. Zooming further is just a matter of chaining the lenses. I can dig up the actual implementation if you think that's any help.

When the zoom method was added to Laminar, I decided to use it instead. Even if the ergonomics of zoom are not ideal it felt better than maintaining my bespoke abstraction.

from airstream.

filonik-cmu avatar filonik-cmu commented on August 20, 2024 1

Just wanted to voice my support. I feel like this functionality would be extremely useful for working nested hierarchies of data and components. Without it, derived vars currently feel a bit like second class citizens compared to signals in Airstream/Laminar.

from airstream.

rgwilton avatar rgwilton commented on August 20, 2024 1

Just to add a +1 that I was finding it hard to get Laminar to work as I expected. I was trying to follow the similar structure of zooming into subparts of a Var and passing those onto rendering components that would zoom further. This worked, but meant that I couldn't really use split.

So, then I tried to use split instead, but found it hard to then zoom into the writer because I wanted the current context to zoom into.

from airstream.

raquo avatar raquo commented on August 20, 2024 1

You can now preview this feature in Airstream v17.1.0-M1. It can be used with Laminar 17.0.0. The new method names on Var are split and splitMutate – the latter for mutating mutable collections in-place in a Var.

from airstream.

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.