Comments (6)
Can you provide some details about why/how this would be useful? I'm not very familiar with the RxJS/Observable context.
from async.
Sure! One (naive) reason would be polling a server or file for changes. Imagine the following:
Ignore the fact that HTTP headers could help with this problem or long-polling.
Stream<String> pollTextFile(Uri uri) => ...
main() {
pollTextFile(Uri.parse('/status.txt')).distinct().listen(print);
}
In ~1 line of code (yay Dart!) we can now write something that prints to the console whenever /status.txt
changes. However, if this file is on some-sort of bandwidth constrained server (imagine where you pay $ per KB), you're going to have a bad time. Even though the print
only happens when the file changes, you check every-time for changes.
Backoff, or exponential or eased backoff implies works based on the strategy that if a value T
hasn't change since last check, we should assume it's even less likely to change soon, so we deprioritize checking - in this case, check less often.
i.e, imagine polling for changes every 10ms:
// An example where the file is constantly changing.
0ms: GET /status.txt
0ms: <FIRST VALUE>
10ms: GET /status.txt
10ms: <SECOND VALUE>
20ms: GET /status.txt
20ms: <THIRD VALUE>
// An example where it is not.
0ms: GET /status.txt
0ms: <FIRST VALUE>
10ms: GET /status.txt
10ms: --- Not changed. Use ease factor of *2 when checking next time (20ms) ---
30ms: GET /status.txt
30ms: --- Not changed. Use ease factor of *2 when checking next time (40ms) ---
70ms ...
150ms...
<Until we hit a max eased value, or the maximum number of retries>
from async.
Is this something that we expect to be widely-used enough to put in async
?
from async.
I could see it being useful (I needed a very simplified version of this for a demo, so not ready to commit it anywhere yet), especially for mobile clients (read: Flutter users) that are bandwidth constrained.
You could argue it's complex enough to go in it's own package, but it depends what the mission of this package is - a collection of useful async utilities or just a tightly curated "mass demand" library. Either works for me.
from async.
it depends what the mission of this package is - a collection of useful async utilities or just a tightly curated "mass demand" library
I'd say it's for utilities that are broadly useful and pretty fundamental—things that make good building blocks for more complex use-cases. That's a subjective line, but I think all good API design has a lot of subjectivity 😄.
For backoff()
, I totally buy that it's useful when doing expensive polling. The crux of the question for me is, how often is polling likely to be the best solution, as opposed to a push-based model of some sort? My intuition is that in most Flutter/web cases, the user will have some control over the server, but it's possible I'm wrong.
from async.
I would personally not use a Stream
for what is essentially a timer.
I'd rather do a Timer intervalTimer(Iterable<Duration> intervals, void callback(Timer timer))
and give you a way to create an infinite computed iterable.
You can always use a timer to feed a stream-controller if you need a stream, but the stream doesn't feel like the basic operation here.
from async.
Related Issues (20)
- Change the default of `propagateCancel` argument in CancelableOperation.then HOT 2
- Reset method for AsyncMemoizer HOT 1
- Make it easier to safely hold a reference that can cancel an operation without holding the whole operation HOT 1
- Clarify `StreamQueue.next` will fail just after `hasNext` in API document.
- Consider to make second invocation of `streamQueue.hasNext` be postponed concluding the result until the first invocation of `q.next` , unless the stream is closed. HOT 6
- Deprecate StreamQueue.hasNext and StreamQueue.next
- Future.wait() but with Records HOT 4
- Bad State error while trying to reject a StreamQueueTransaction
- Dart 3 incompatibilty: `DelegatingStream<T> extends StreamView` but `StreamView` is `base class` HOT 5
- Add `whereNotNull` for `Stream`
- CancelableOperation value is not propagating errors, so they cannot be catched and app is crashing HOT 3
- There should be cancellable versions of Stream.firstWhere etc.
- Migrate the Result type to sealed classes HOT 2
- Make `ParallelWaitError` Include Error Details HOT 1
- Async Cache is caching exceptions HOT 5
- AsyncMemoizer is caching exceptions HOT 2
- Add an API wrapping runZonedGuarded that surfaces the first error HOT 4
- Clarify `CancelableOperation` docs HOT 4
- Inconsistent behavior of `Stream.listen` on broadcast streams HOT 1
- [Proposal] Add a CountDownLatch implementation
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 async.