-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Example APIs that could leverage Observables #72
Comments
Rather |
The native observers are probably good candidates for this too: |
All constructors take a callback which recieves the respective records, but in the case of an Observable they'd be the
The callbacks always return an Array of entries, batched to intervals. Maybe it makes sense for observables to only recieve one at a time? Lastly, I think a big concern with these observers is that |
I like the latter two options you list here (static method, possibly named |
I think it might work well. Unlike promises, where subclassing is a huge mess because we made their internals so exposed, subclassing for Someone would need to work out the details though. Probably worth a separate issue. |
The DOM Observer style APIs were specifically designed to allow the browser to efficiently queue and filter records. I don't think Observable makes sense there because it it encourages a single observer for every element (or observation) and doing userland filtering instead of browser internal filtering. |
Also the intent was for setTimeout, setInterval and requestAnimationFrame APIs to all be replaced by https://developer.mozilla.org/en-US/docs/Web/API/Scheduler/postTask which can handle all those situations and supports abort signal and dynamic priorities. I don't think another set of APIs should be added that duplicates the scheduler API, though the scheduler API could be evolved (ex. to have a version that doesn't use a callback). |
Sort of a shame, In any case, it probably couldn't replace For example, this code will slowly drift off of emitting once a second because of how long the work takes between scheduling... Even if you move the function recursiveInterval(callback, ms) {
let id = 0;
const run = () => {
id = setTimeout(() => {
callback();
run();
}, ms);
}
return () => clearTimeout(id);
}
recursiveInterval(() => {
// do some work.
for (let i = 0; i < 1e7; i++) {}
console.log(new Date().toString())
}, 1000) |
For const startTime = performance.now();
Scheduler.animationFrames
.map((now) => now - startTime)
.subscribe((elapsedTime) => {
// move something based on elapsed time.
}); For const thirtyMinutes = 30 * 60 * 1000;
const pollingInterval = Scheduler.interval(thirtyMinutes);
pollingInterval.flatMap(async function* () {
const response = await fetch('/get/data');
if (response.ok) {
yield response.json()
} else {
console.error('Error polling for data');
}
})
.subscribe(results => {
updateSomeList(results);
}); |
In your example you're not immediately scheduling the next iteration. setTimeout is async, call it immediately upon entering run instead. That's all the browser does inside setInterval too, it immediately schedules the next task (with offset adjustment) before running the callback. function recursiveInterval(callback, ms) {
let id = 0;
const run = () => {
id = setTimeout(() => {
run();
callback();
}, ms);
}
return () => clearTimeout(id);
} That'll reduce the drift considerably. Note that browsers already do firing alignment on timers to conserve power, and timers will also skew because of event loop business, but it is true that browsers try to align the to the requested repeat interval: you could do that yourself and get identical behavior to the browser, but I wouldn't expect folks to figure that out. You gave that feedback over here: WICG/scheduling-apis#8 (comment) Hopefully Scott will add |
@domenic suggested it might be good to track platform APIs for which observables would work well.
A few that come to the top of mind for me:
interval(number): Observable<number>
- A setInterval, and you'd get either a counter or a high-precision timestamp in it (similar to whatrequestAnimationFrame
does).animationFrames(): Observable<number>
- basically an animation looptimer(number): Observable<void>
- justsetTimeout
. Useful fortakeUntil
, creating timeouts, and many other things. Arguablyinterval(number).take(1)
is the same though. RxJS's implementation also allows aDate
to be passed to fire the timer at a specific date/time (although it has some limitations and I don't think it's used very often).The text was updated successfully, but these errors were encountered: