Skip to content

Commit

Permalink
Introduce the 'onlyIfChanged' operator
Browse files Browse the repository at this point in the history
  • Loading branch information
garronej committed Sep 7, 2023
1 parent 1c60e5e commit 0e6809d
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/lib/util/genericOperators/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { throttleTime } from "./throttleTime";
export { to } from "./to";
export { nonNullable } from "./nonNullable";
export { distinct } from "./distinct";
export { nonNullable } from "./nonNullable";
export { distinct } from "./distinct";
export { onlyIfChanged } from "./onlyIfChanged";
30 changes: 30 additions & 0 deletions src/lib/util/genericOperators/onlyIfChanged.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

import { Operator, NonPostableEvt } from "../../types";
import { StatefulEvt } from "../../StatefulEvt";
import { same } from "../../../tools/inDepth/same";

const initialValue = {};

export const onlyIfChanged= <T>(
params?: {
areEqual?: (a: T, b: T) => boolean;
}
): Operator..Stateful<T, T> => {

const { areEqual = same } = params ?? {};

const op: Operator..Stateful<T, T> = [
function (this: NonPostableEvt<T>, data: T, prev: T) {
return (
this instanceof StatefulEvt ?
areEqual(data, this.state) :
prev === initialValue ? false : areEqual(data, prev)
) ? null : [data];
},
initialValue as T
] ;

return op;

}

2 changes: 1 addition & 1 deletion src/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ if (n) {

await new Promise(resolve => setTimeout(resolve, 2400));

const n = 108;
const n = 110;

console.log({ n });

Expand Down
27 changes: 27 additions & 0 deletions src/test/test109.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Evt } from "../lib";
import { assert } from "tsafe/assert";
import { onlyIfChanged } from "../lib/util/genericOperators/onlyIfChanged";

let alphabet = ""

const evtFoo = Evt.create<{ foo: string; }>();

evtFoo
.pipe(onlyIfChanged())
.attach(({ foo }) => {

alphabet += foo;

});

evtFoo.post({ "foo": "a" });
evtFoo.post({ "foo": "a" });
evtFoo.post({ "foo": "a" });
evtFoo.post({ "foo": "b" });
evtFoo.post({ "foo": "b" });
evtFoo.post({ "foo": "c" });

assert(alphabet === "abc");

console.log("PASS");

27 changes: 27 additions & 0 deletions src/test/test110.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Evt } from "../lib";
import { assert } from "tsafe/assert";
import { onlyIfChanged } from "../lib/util/genericOperators/onlyIfChanged";

let alphabet = ""

const evtFoo = Evt.create<{ foo: string; }>({ "foo": "a"});

evtFoo
.pipe(onlyIfChanged())
.attach(({ foo }) => {

alphabet += foo;

});

evtFoo.post({ "foo": "a" });
evtFoo.post({ "foo": "a" });
evtFoo.post({ "foo": "a" });
evtFoo.post({ "foo": "b" });
evtFoo.post({ "foo": "b" });
evtFoo.post({ "foo": "c" });

assert(alphabet === "abc");

console.log("PASS");

0 comments on commit 0e6809d

Please sign in to comment.