Coder Social home page Coder Social logo

Latest operator about wonka HOT 7 CLOSED

0no-co avatar 0no-co commented on August 15, 2024
Latest operator

from wonka.

Comments (7)

hanrelan avatar hanrelan commented on August 15, 2024

Actually I figured out how to do it with scan by using None as the initial value and then extracting the Some(x) in the subscribe.
However it turns out this doesn't quite do what I want as it just delays the latest value by 1000ms. Is there a way to do something like debounce except instead of waiting for a period of N ms of silence, it is just silent for N ms and then emits the latest value? Essentially something like auditTime

from wonka.

kitten avatar kitten commented on August 15, 2024

I assume you’re in a web environment since you’re using delay?

You’re probably looking for the debounce and/or throttle operators :)

from wonka.

hanrelan avatar hanrelan commented on August 15, 2024

Thanks for the quick response!

Yes it's a web environment.

It's similar to throttle but not quite the same. With throttle the emit happens before the throttle period begins. I want it to happen after the throttle period. Similarly debounce is close but waits for silence which isn't what I want.

To illustrate the difference, imagine scraping the html whenever the DOM updates. Let's say you get 10 DOM updates within 1 second.
If you scraped at every update, you would do 10 scrapes. You'd get all the latest data but you'd be using a lot of CPU.
If you use throttle(1000), you would scrape at t = 0 second and that's it. So you would miss the latest data that existed at t = 1 second.
With auditTime(1000), you would only scrape at t = 1 second. This way you're minimizing the amount of CPU while still grabbing the latest data

from wonka.

kitten avatar kitten commented on August 15, 2024

Oh, okay, sorry for the goose chase for the right operator! 😅 Maybe the correct primitive is then sample? https://wonka.kitten.sh/api/operators#sample

You can pass it a notifier source. Every time that notifier emits it takes the latest value from the source that is passed into sample, so in your case you could do sample(interval(1000)).

It seems like that's exactly what Rx is doing internally as well? https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/auditTime.ts#L56

from wonka.

hanrelan avatar hanrelan commented on August 15, 2024

Oh yes, I think sample is what I want. Initially I thought that it would pull the latest item from the source regardless of if there was anything new, but I just tested it and it seems to stop if there's nothing new to pull.
For anyone else comes across this, my final implementation is:

source
|> Wonka.scan((. _acc, x) => Some(x), None)
|> Wonka.sample(Wonka.interval(1000))
|> Wonka.subscribe((. x) => {
     switch (x) {
     | None => ()
     | Some(x) => print_endline(string_of_int(x))
     }
   });

If you can think of a way to get rid of the Option type and the switch statement there I'd love to know how to do that. In the general case we don't necessarily have an identity type (eg. 0 for int) that we could pass to scan.

from wonka.

kitten avatar kitten commented on August 15, 2024

Oh, I see, so you're trying to emit the initial first value of the source and then sample values afterwards? You can probably write your own latest operator by providing a more complex notifier source to sample.

Essentially we'd want the notifier to emit when the first value of the source is emitted, so we could write that like so in TypeScript:

import {
  Operator,
  pipe,
  share,
  concat,
  take,
  interval,
  sample,
} from 'wonka';

const latest = <A>(time: number): Operator<A, A> => source => {
  const shared = share(source);
  const notifier = concat([
    pipe(shared, take(1)),
    interval(time)
  ]);

  return pipe(shared, sample(notifier));
};

So we're creating a latest operator, share the source and create a notifier that emits when the first item is emitted on the source and afterwards start an interval.

In Reason you'd write it like so (haven't tested this so there may be some errors in the following sample)

open Wonka;

let latest = (time: int) => (source: sourceT('a)): sourceT('a) => {
  let shared = share(source);
  let notifier = concat([|
    shared |> take(1),
    interval(time)
  |]);

  shared |> sample(notifier)
};

from wonka.

hanrelan avatar hanrelan commented on August 15, 2024

Cool thanks!

from wonka.

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.