Comments (4)
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.
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.
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.
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)
- .split operator is hard to use without Laminar
- Killing subscriptions from observers (?)
- splitByType / splitByEnum HOT 6
- Can `DerivedVar` support `map/update` semantics in addition to `map/contramap` semantics? HOT 2
- Add an AjaxStream upload progress observer
- FetchStream does not emit events if it's initialized outside of a transaction HOT 1
- [Question] Simulate DomEventStream click on page load HOT 5
- idiomatic usage of foreach for EventStream HOT 4
- RFC: Should we import-gate flatMap? HOT 7
- Streams should emit in a shared transaction when starting HOT 2
- Error: code length overflow HOT 2
- Transaction stack overflow caused by sibling transactions HOT 4
- split doesn't memoize on Signal[LazyList[A]] HOT 2
- Zooming into a Var should not require an Owner? HOT 5
- SplitChildSignal does not re-sync when restarted HOT 6
- `composeEither`: fixed-scope `flatten` without transcations HOT 4
- StreamFromSignal with onlyChanges = False may filter out initial value
- Streams with 'EventStream.fromFuture' drops result of futures if timing overlaps. HOT 7
- contrib: can't find dep com.dwijnand:sbt-dynver HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from airstream.