-
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rstream): add debounce() sub & tests
- Loading branch information
1 parent
1073735
commit 9c53bb4
Showing
3 changed files
with
55 additions
and
0 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
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,19 @@ | ||
import { fromIterable } from "../from/iterable"; | ||
import { metaStream, MetaStreamOpts } from "../metastream"; | ||
|
||
/** | ||
* Returns a subscription which ignores any intermediate inputs arriving | ||
* faster than given `delay` time period. | ||
* | ||
* @example | ||
* ```ts | ||
* | ||
* ``` | ||
* | ||
* @param delay | ||
*/ | ||
export const debounce = <T>(delay: number, opts?: Partial<MetaStreamOpts>) => | ||
metaStream((x: T) => fromIterable([x], { delay }), { | ||
emitLast: true, | ||
...opts, | ||
}); |
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,35 @@ | ||
import * as assert from "assert"; | ||
import { debounce, fromIterable } from "../src/index"; | ||
import { TIMEOUT } from "./config"; | ||
|
||
describe("debounce", () => { | ||
it("basic", (done) => { | ||
const acc: number[] = []; | ||
fromIterable([1, 2, 3], { delay: TIMEOUT }) | ||
.subscribe(debounce(TIMEOUT * 1.5)) | ||
.subscribe({ | ||
next(x) { | ||
acc.push(x); | ||
}, | ||
}); | ||
setTimeout(() => { | ||
assert.deepEqual(acc, [3]); | ||
done(); | ||
}, TIMEOUT * 5); | ||
}); | ||
|
||
it("no last", (done) => { | ||
const acc: number[] = []; | ||
fromIterable([1, 2, 3], { delay: TIMEOUT }) | ||
.subscribe(debounce(TIMEOUT * 1.5, { emitLast: false })) | ||
.subscribe({ | ||
next(x) { | ||
acc.push(x); | ||
}, | ||
}); | ||
setTimeout(() => { | ||
assert.deepEqual(acc, []); | ||
done(); | ||
}, TIMEOUT * 5); | ||
}); | ||
}); |