Code Monkey home page Code Monkey logo

Comments (6)

nex3 avatar nex3 commented on June 1, 2024

Can you provide some details about why/how this would be useful? I'm not very familiar with the RxJS/Observable context.

from async.

matanlurey avatar matanlurey commented on June 1, 2024

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.

nex3 avatar nex3 commented on June 1, 2024

Is this something that we expect to be widely-used enough to put in async?

from async.

matanlurey avatar matanlurey commented on June 1, 2024

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.

nex3 avatar nex3 commented on June 1, 2024

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.

lrhn avatar lrhn commented on June 1, 2024

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)

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.