garronej / evt

💧EventEmitter's typesafe replacement
https://evt.land
MIT License
454 stars 6 forks source link

Feature similar to `distinct` in RxJs #21

Closed rhlsthrm closed 3 years ago

rhlsthrm commented 3 years ago

Is your feature request related to a problem? Please describe. I need have my Evt event emitter be idempotent since the source can emit duplicate events.

Describe the solution you'd like I'd love a pipe-like operator that can filter for events that have already been emitted.

Describe alternatives you've considered RxJs has a distinct operator that does exactly what I need. Since I'm already using Evt and I like the library, I'd like to continue using it if possible.

Additional context

garronej commented 3 years ago

Hi,
Thank you for reaching out.
It's a can do, I will do it ASAP, tonight if I can.

garronej commented 3 years ago

Hi @rhlsthrm, I implemented the distinct operator based on the API of the RxJS operator.
Only difference is, to flush the values you can pass a Ctx and call the .done() method when you want to flush.
Both argument are optionals.

I hope it fits your need, let me know you need anything else

Regards.

import { Evt, distinct } from "evt";

const evtStr = Evt.create<string>();

const flushCtx = Evt.newCtx();

evtStr
    .pipe(distinct(str => str.split(" ")[0], flushCtx))
    .attach(str => console.log(str));

evtStr.post("foo 1");
flushCtx.done();
evtStr.post("foo 2");
evtStr.post("bar 3");
evtStr.post("bar 4");
evtStr.post("foo 5");
evtStr.post("baz 6");

Will print

foo 1
foo 2
bar 3
baz 6
rhlsthrm commented 3 years ago

This is amazing! Thank you so much @garronej this looks like exactly what I need! Is this available in a release for me to try?

garronej commented 3 years ago

Thank you! :blush: Yes it's released already, both in v1 and in v2 (beta).

rhlsthrm commented 3 years ago

Will test this out today!