-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce the 'onlyIfChanged' operator
- Loading branch information
Showing
5 changed files
with
88 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.fλ.Stateful<T, T> => { | ||
|
||
const { areEqual = same } = params ?? {}; | ||
|
||
const op: Operator.fλ.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; | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
|