-
-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transducers): add partitionTime() transducer
- Loading branch information
1 parent
38d511b
commit efafd0b
Showing
4 changed files
with
56 additions
and
2 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
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,51 @@ | ||
import { iterator } from "../iterator"; | ||
import { partitionBy } from "./partition-by"; | ||
import type { Transducer } from "../api"; | ||
|
||
/** | ||
* Transducer. Yields tumbling, non-overlapping windows/partitions of | ||
* input values, with the window size defined by given realtime `period` | ||
* (in milliseconds). | ||
* | ||
* @remarks | ||
* Only to be used in async contexts, NOT with {@link (transduce:1)} | ||
* directly. | ||
* | ||
* Also see: | ||
* - {@link @thi.ng/rstream# | @thi.ng/rstream} | ||
* - {@link @thi.ng/csp# | @thi.ng/csp}. | ||
* | ||
* @example | ||
* ```ts | ||
* import { fromInterval, trace } from "@thi.ng/rstream"; | ||
* | ||
* // stream emits | ||
* fromInterval(250) | ||
* .transform(partitionTime(1000)) | ||
* .subscribe(trace()) | ||
* // [ 0, 1, 2, 3 ] | ||
* // [ 4, 5, 6, 7 ] | ||
* // [ 8, 9, 10, 11 ] | ||
* // [ 12, 13, 14, 15 ] | ||
* // ... | ||
* ``` | ||
* | ||
* @param period - window size (in ms) | ||
*/ | ||
export function partitionTime<T>(period: number): Transducer<T, T[]>; | ||
export function partitionTime<T>( | ||
period: number, | ||
src: Iterable<T> | ||
): IterableIterator<T[]>; | ||
export function partitionTime<T>(period: number, src?: Iterable<T>): any { | ||
return src | ||
? iterator(partitionTime(period), src) | ||
: partitionBy(() => { | ||
let last = 0; | ||
return () => { | ||
const t = Date.now(); | ||
t - last >= period && (last = t); | ||
return last; | ||
}; | ||
}, true); | ||
} |