Comments (5)
If I understand your challenge correctly, here's what I'd do:
val requestStream = EventStream.merge(
EventStream.fromValue(()), // emit a Unit event initially
refreshClickStream.mapToUnit // then emit all events from the refreshClickStream stream (mapped to Unit)
).map {
_ =>
println("get url")
val randomOffset = Math.floor(Math.random()*500)
s"$githubUrl?since=$randomOffset"
}
from airstream.
Observables are (kinda) declarative, that is, you need to specify their behaviour when you're creating them.
When you create a DomEventStream, you tell it which element to look at, and what event type ("click") to listen to. You can call .click() on that element to simulate the click event and kinda "fool" the observable into thinking the user actually clicked it, but that is a special method provided by the browser.
So, you can't cause DomEventStream to emit events that aren't clicks, but you can create another stream that will merge your DomEventStream with some other stream. Yurique above showed how to merge your refreshClickStream
with a stream that simply emits one event when it's started (EventStream.fromValue(())
), however you could instead combine it with any other stream.
There are several ways to create a stream that lets you fire events imperatively, whenever you please – they're listed here among some other useful stream types.
For example, you could create a stream from scratch using val (manualStream, callback) = EventStream.withUnitCallback
, and then call callback()
to make manualStream
fire an event. Then, you can merge this manualStream
with refreshClickStream.mapToUnit
, and you get a stream that emits whenever you call callback(), and also whenever the refresh button is called.
That way, instead of faking a click, you're properly declaring your dataflow, creating a stream that depends not only on clicks but also on your other logic that is needed to produce the desired results.
PS I see you're using Airstream without Laminar – this is certainly possible, but is more manual and more cumbersome because you need to manage ownership lifecycles yourself. If you get bogged down with ownership, just know that it's much smoother with Laminar.
from airstream.
Hi,
@raquo thanks, that worked!.
@raquo This isn't real work, it's just a study on FPS. I'm trying to reproduce this using AirStream https://gist.github.com/staltz/868e7e9bc2a7b8c1f754. For real projects I'm already using Laminar.
from airstream.
I wonder if we should convert this into a Discussion, for posterity :)
from airstream.
Of course, it would be great. I'll finish porting the example from RxJS to Scala/AirStream and post the complete code.
By the way, how do I make this code run only once? When I connect requestUrlStream
with FetchStream
, the event gets processed two time. I could use a .drop(1)
to listen to just one. Is there another way?
val refreshButton = document.querySelector(".refresh").asInstanceOf[HTMLButtonElement]
val refreshClickStream = EventStream.merge(
DomEventStream[dom.MouseEvent](refreshButton, "click").mapToUnit,
EventStream.fromValue(())
)
val closeButton = document.querySelector(".close1").asInstanceOf[HTMLButtonElement]
val closeClickStream = EventStream.merge(
DomEventStream[dom.MouseEvent](closeButton, "click").mapToUnit,
EventStream.fromValue(())
)
val requestUrlStream = refreshClickStream.map(_ => githubUrl)
val responseStream = requestUrlStream.flatMap {
url =>
FetchStream
.get(url)
.map(_.length)
}
val combinedStream = closeClickStream.combineWithFn(responseStream) {
(x, y) => s"combined result x=${x}, y=${y}"
}
combinedStream.addObserver(Observer{
s => println(s)
})
this code show combined result x=undefined, y=29925
two time.
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
- 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
- Vars should be splittable HOT 4
- 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
- Add method variant of EvenStream.merge? HOT 5
- Update and get HOT 5
- add flatmap to ReactiveEventProp HOT 2
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.