@effectionx/stream-helpers 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/README.md +260 -0
  2. package/esm/batch.d.ts +21 -0
  3. package/esm/batch.d.ts.map +1 -0
  4. package/esm/batch.js +54 -0
  5. package/esm/filter.d.ts +23 -0
  6. package/esm/filter.d.ts.map +1 -0
  7. package/esm/filter.js +45 -0
  8. package/esm/map.d.ts +9 -0
  9. package/esm/map.d.ts.map +1 -0
  10. package/esm/map.js +27 -0
  11. package/esm/mod.d.ts +6 -0
  12. package/esm/mod.d.ts.map +1 -0
  13. package/esm/mod.js +5 -0
  14. package/esm/package.json +3 -0
  15. package/esm/signals.d.ts +23 -0
  16. package/esm/signals.d.ts.map +1 -0
  17. package/esm/signals.js +119 -0
  18. package/esm/test-helpers/faucet.d.ts +78 -0
  19. package/esm/test-helpers/faucet.d.ts.map +1 -0
  20. package/esm/test-helpers/faucet.js +72 -0
  21. package/esm/test-helpers.d.ts +2 -0
  22. package/esm/test-helpers.d.ts.map +1 -0
  23. package/esm/test-helpers.js +1 -0
  24. package/esm/tracker.d.ts +24 -0
  25. package/esm/tracker.d.ts.map +1 -0
  26. package/esm/tracker.js +39 -0
  27. package/esm/valve.d.ts +22 -0
  28. package/esm/valve.d.ts.map +1 -0
  29. package/esm/valve.js +48 -0
  30. package/package.json +30 -0
  31. package/script/batch.d.ts +21 -0
  32. package/script/batch.d.ts.map +1 -0
  33. package/script/batch.js +57 -0
  34. package/script/filter.d.ts +23 -0
  35. package/script/filter.d.ts.map +1 -0
  36. package/script/filter.js +48 -0
  37. package/script/map.d.ts +9 -0
  38. package/script/map.d.ts.map +1 -0
  39. package/script/map.js +30 -0
  40. package/script/mod.d.ts +6 -0
  41. package/script/mod.d.ts.map +1 -0
  42. package/script/mod.js +21 -0
  43. package/script/package.json +3 -0
  44. package/script/signals.d.ts +23 -0
  45. package/script/signals.d.ts.map +1 -0
  46. package/script/signals.js +125 -0
  47. package/script/test-helpers/faucet.d.ts +78 -0
  48. package/script/test-helpers/faucet.d.ts.map +1 -0
  49. package/script/test-helpers/faucet.js +75 -0
  50. package/script/test-helpers.d.ts +2 -0
  51. package/script/test-helpers.d.ts.map +1 -0
  52. package/script/test-helpers.js +17 -0
  53. package/script/tracker.d.ts +24 -0
  54. package/script/tracker.d.ts.map +1 -0
  55. package/script/tracker.js +42 -0
  56. package/script/valve.d.ts +22 -0
  57. package/script/valve.d.ts.map +1 -0
  58. package/script/valve.js +51 -0
@@ -0,0 +1,78 @@
1
+ import { type Operation, type Stream } from "effection";
2
+ /**
3
+ * Interface of the stream returned by `useFaucet`.
4
+ */
5
+ export interface Faucet<T> extends Stream<T, never> {
6
+ /**
7
+ * Pour items to the stream synchronously.
8
+ * @param items - The items to pour to the stream.
9
+ */
10
+ pour(items: T[]): Operation<void>;
11
+ /**
12
+ * Pour items to the stream using an operation that can be asynchronous.
13
+ * @param op - The generator function to pour items to the stream.
14
+ */
15
+ pour(op: (send: (item: T) => Operation<void>) => Operation<void>): Operation<void>;
16
+ /**
17
+ * Open the stream to allow items to be sent to the stream.
18
+ */
19
+ open(): void;
20
+ /**
21
+ * Close the stream to prevent items from being sent to the stream.
22
+ */
23
+ close(): void;
24
+ }
25
+ /**
26
+ * Options for the faucet.
27
+ */
28
+ export interface FaucetOptions {
29
+ /**
30
+ * Whether the faucet is open when created.
31
+ */
32
+ open?: boolean;
33
+ }
34
+ /**
35
+ * Creates a stream that can be used to test the behavior of streams that use backpressure.
36
+ * It's useful in tests where it can be used as a source stream. This function is used to create
37
+ * the stream.
38
+ *
39
+ * The returned stream has `pour` method that can be used to send items to the stream.
40
+ * It can accept an array of items or a generator function that will be called with a function
41
+ * to send items to the stream.
42
+ *
43
+ * ```typescript
44
+ * import { useFaucet } from "@effectionx/stream-helpers/test-helpers";
45
+ * import { run, each, spawn } from "effection";
46
+ *
47
+ * await run(function* () {
48
+ * const faucet = yield* useFaucet({ open: true });
49
+ *
50
+ * // Remember to spawn the stream subscription before sending items to the stream
51
+ * yield* spawn(function* () {
52
+ * for (let i of yield* each(faucet)) {
53
+ * console.log(i);
54
+ * yield* each.next();
55
+ * }
56
+ * });
57
+ *
58
+ * // Pass an array of items to send items to the stream one at a time synchronously
59
+ * yield* faucet.pour([1, 2, 3]);
60
+ *
61
+ * // Pass an operation to control the rate at which items are sent to the stream
62
+ * yield* faucet.pour(function* (send) {
63
+ * yield* sleep(10);
64
+ * send(5);
65
+ * yield* sleep(30);
66
+ * send(6);
67
+ * yield* sleep(10);
68
+ * send(7);
69
+ * });
70
+ * });
71
+ *
72
+ * ```
73
+ * @param options - The options for the faucet.
74
+ * @param options.open - Whether the faucet is open.
75
+ * @returns stream of items coming from the faucet
76
+ */
77
+ export declare function useFaucet<T>(options: FaucetOptions): Operation<Faucet<T>>;
78
+ //# sourceMappingURL=faucet.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"faucet.d.ts","sourceRoot":"","sources":["../../src/test-helpers/faucet.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,KAAK,SAAS,EAAE,KAAK,MAAM,EAAE,MAAM,WAAW,CAAC;AAGtE;;GAEG;AACH,MAAM,WAAW,MAAM,CAAC,CAAC,CAAE,SAAQ,MAAM,CAAC,CAAC,EAAE,KAAK,CAAC;IACjD;;;OAGG;IACH,IAAI,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC;IAClC;;;OAGG;IACH,IAAI,CACF,EAAE,EAAE,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,SAAS,CAAC,IAAI,CAAC,KAAK,SAAS,CAAC,IAAI,CAAC,GAC1D,SAAS,CAAC,IAAI,CAAC,CAAC;IACnB;;OAEG;IACH,IAAI,IAAI,IAAI,CAAC;IACb;;OAEG;IACH,KAAK,IAAI,IAAI,CAAC;CACf;AAED;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B;;OAEG;IACH,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,wBAAiB,SAAS,CAAC,CAAC,EAAE,OAAO,EAAE,aAAa,GAAG,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CA0B1E"}
@@ -0,0 +1,72 @@
1
+ import { createSignal } from "effection";
2
+ import { createBoolean, is } from "../signals.js";
3
+ /**
4
+ * Creates a stream that can be used to test the behavior of streams that use backpressure.
5
+ * It's useful in tests where it can be used as a source stream. This function is used to create
6
+ * the stream.
7
+ *
8
+ * The returned stream has `pour` method that can be used to send items to the stream.
9
+ * It can accept an array of items or a generator function that will be called with a function
10
+ * to send items to the stream.
11
+ *
12
+ * ```typescript
13
+ * import { useFaucet } from "@effectionx/stream-helpers/test-helpers";
14
+ * import { run, each, spawn } from "effection";
15
+ *
16
+ * await run(function* () {
17
+ * const faucet = yield* useFaucet({ open: true });
18
+ *
19
+ * // Remember to spawn the stream subscription before sending items to the stream
20
+ * yield* spawn(function* () {
21
+ * for (let i of yield* each(faucet)) {
22
+ * console.log(i);
23
+ * yield* each.next();
24
+ * }
25
+ * });
26
+ *
27
+ * // Pass an array of items to send items to the stream one at a time synchronously
28
+ * yield* faucet.pour([1, 2, 3]);
29
+ *
30
+ * // Pass an operation to control the rate at which items are sent to the stream
31
+ * yield* faucet.pour(function* (send) {
32
+ * yield* sleep(10);
33
+ * send(5);
34
+ * yield* sleep(30);
35
+ * send(6);
36
+ * yield* sleep(10);
37
+ * send(7);
38
+ * });
39
+ * });
40
+ *
41
+ * ```
42
+ * @param options - The options for the faucet.
43
+ * @param options.open - Whether the faucet is open.
44
+ * @returns stream of items coming from the faucet
45
+ */
46
+ export function* useFaucet(options) {
47
+ let signal = createSignal();
48
+ let open = yield* createBoolean(options.open);
49
+ return {
50
+ [Symbol.iterator]: signal[Symbol.iterator],
51
+ *pour(items) {
52
+ if (Array.isArray(items)) {
53
+ for (let i of items) {
54
+ yield* is(open, (open) => open);
55
+ signal.send(i);
56
+ }
57
+ }
58
+ else {
59
+ yield* items(function* (item) {
60
+ yield* is(open, (open) => open);
61
+ signal.send(item);
62
+ });
63
+ }
64
+ },
65
+ close() {
66
+ open.set(false);
67
+ },
68
+ open() {
69
+ open.set(true);
70
+ },
71
+ };
72
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./test-helpers/faucet.js";
2
+ //# sourceMappingURL=test-helpers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"test-helpers.d.ts","sourceRoot":"","sources":["../src/test-helpers.ts"],"names":[],"mappings":"AAAA,cAAc,0BAA0B,CAAC"}
@@ -0,0 +1 @@
1
+ export * from "./test-helpers/faucet.js";
@@ -0,0 +1,24 @@
1
+ import { type Operation, type Stream } from "effection";
2
+ export interface Tracker extends Operation<void> {
3
+ /**
4
+ * Returns a stream helper that doesn't modify the items passing through the stream,
5
+ * but will capture a reference to the item. Call the `markOne` or `markMany` methods
6
+ * with the item to indicate that it has exited the stream.
7
+ */
8
+ passthrough(): <T>(stream: Stream<T, never>) => Stream<T, never>;
9
+ /**
10
+ * Call this method with an item that has passed through the stream to indicate that it has exited the stream.
11
+ */
12
+ markOne(item: unknown): void;
13
+ /**
14
+ * Call this method with an iterable of items that have passed through the stream to indicate that they have exited the stream.
15
+ */
16
+ markMany(items: Iterable<unknown>): void;
17
+ }
18
+ /**
19
+ * Creates a tracker that can be used to verify that all items that entered the stream
20
+ * eventually exit the stream. This is helpful when you want to ensure that all items
21
+ * were processed before terminating the operation that created the stream.
22
+ */
23
+ export declare function createTracker(): Operation<Tracker>;
24
+ //# sourceMappingURL=tracker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tracker.d.ts","sourceRoot":"","sources":["../src/tracker.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,SAAS,EAAY,KAAK,MAAM,EAAE,MAAM,WAAW,CAAC;AAGlE,MAAM,WAAW,OAAQ,SAAQ,SAAS,CAAC,IAAI,CAAC;IAC9C;;;;OAIG;IACH,WAAW,IAAI,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,KAAK,CAAC,KAAK,MAAM,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;IACjE;;OAEG;IACH,OAAO,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI,CAAC;IAC7B;;OAEG;IACH,QAAQ,CAAC,KAAK,EAAE,QAAQ,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC;CAC1C;AAED;;;;GAIG;AACH,wBAAgB,aAAa,IAAI,SAAS,CAAC,OAAO,CAAC,CAiClD"}
package/esm/tracker.js ADDED
@@ -0,0 +1,39 @@
1
+ import { resource } from "effection";
2
+ import { createSetSignal, is } from "./signals.js";
3
+ /**
4
+ * Creates a tracker that can be used to verify that all items that entered the stream
5
+ * eventually exit the stream. This is helpful when you want to ensure that all items
6
+ * were processed before terminating the operation that created the stream.
7
+ */
8
+ export function createTracker() {
9
+ return resource(function* (provide) {
10
+ const tracked = yield* createSetSignal();
11
+ yield* provide({
12
+ *[Symbol.iterator]() {
13
+ yield* is(tracked, (set) => set.size === 0);
14
+ },
15
+ passthrough() {
16
+ return function (stream) {
17
+ return {
18
+ *[Symbol.iterator]() {
19
+ const subscription = yield* stream;
20
+ return {
21
+ *next() {
22
+ const next = yield* subscription.next();
23
+ tracked.add(next.value);
24
+ return next;
25
+ },
26
+ };
27
+ },
28
+ };
29
+ };
30
+ },
31
+ markOne(item) {
32
+ tracked.delete(item);
33
+ },
34
+ markMany(items) {
35
+ tracked.difference(items);
36
+ },
37
+ });
38
+ });
39
+ }
package/esm/valve.d.ts ADDED
@@ -0,0 +1,22 @@
1
+ import { type Operation, type Stream } from "effection";
2
+ export interface ValveOptions {
3
+ openAt: number;
4
+ closeAt: number;
5
+ open(): Operation<void>;
6
+ close(): Operation<void>;
7
+ }
8
+ /**
9
+ * This function buffers incoming items, if the upstream is producing faster than the downstream
10
+ * can consume, the buffer will grow. If the buffer size exceeds the `closeAt` threshold, the
11
+ * `close` operation will be called which is expected to pause the upstream. The buffer will
12
+ * drain until the buffer size is less than the `openAt` threshold, at which point the `open`
13
+ * operation will be called to resume the upstream.
14
+ *
15
+ * @param options.open - The operation to resume the upstream.
16
+ * @param options.openAt - The buffer size at which the upstream will be resumed.
17
+ * @param options.close - The operation to pause the upstream.
18
+ * @param options.closeAt - The buffer size at which the upstream will be paused.
19
+ * @returns A stream with backpressure applied.
20
+ */
21
+ export declare function valve(options: ValveOptions): <T>(stream: Stream<T, never>) => Stream<T, never>;
22
+ //# sourceMappingURL=valve.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"valve.d.ts","sourceRoot":"","sources":["../src/valve.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,SAAS,EAAS,KAAK,MAAM,EAAE,MAAM,WAAW,CAAC;AAGrE,MAAM,WAAW,YAAY;IAC3B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,IAAI,SAAS,CAAC,IAAI,CAAC,CAAC;IACxB,KAAK,IAAI,SAAS,CAAC,IAAI,CAAC,CAAC;CAC1B;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,KAAK,CACnB,OAAO,EAAE,YAAY,GACpB,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,KAAK,CAAC,KAAK,MAAM,CAAC,CAAC,EAAE,KAAK,CAAC,CAkCnD"}
package/esm/valve.js ADDED
@@ -0,0 +1,48 @@
1
+ import { each, spawn } from "effection";
2
+ import { createArraySignal } from "./signals.js";
3
+ /**
4
+ * This function buffers incoming items, if the upstream is producing faster than the downstream
5
+ * can consume, the buffer will grow. If the buffer size exceeds the `closeAt` threshold, the
6
+ * `close` operation will be called which is expected to pause the upstream. The buffer will
7
+ * drain until the buffer size is less than the `openAt` threshold, at which point the `open`
8
+ * operation will be called to resume the upstream.
9
+ *
10
+ * @param options.open - The operation to resume the upstream.
11
+ * @param options.openAt - The buffer size at which the upstream will be resumed.
12
+ * @param options.close - The operation to pause the upstream.
13
+ * @param options.closeAt - The buffer size at which the upstream will be paused.
14
+ * @returns A stream with backpressure applied.
15
+ */
16
+ export function valve(options) {
17
+ return function (stream) {
18
+ return {
19
+ *[Symbol.iterator]() {
20
+ const buffer = yield* createArraySignal([]);
21
+ let open = true;
22
+ yield* spawn(function* () {
23
+ for (const item of yield* each(stream)) {
24
+ buffer.push(item);
25
+ if (open && buffer.length >= options.closeAt) {
26
+ yield* options.close();
27
+ open = false;
28
+ }
29
+ yield* each.next();
30
+ }
31
+ });
32
+ return {
33
+ *next() {
34
+ if (!open && buffer.length <= options.openAt) {
35
+ yield* options.open();
36
+ open = true;
37
+ }
38
+ const value = yield* buffer.shift();
39
+ return {
40
+ done: false,
41
+ value,
42
+ };
43
+ },
44
+ };
45
+ },
46
+ };
47
+ };
48
+ }
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@effectionx/stream-helpers",
3
+ "version": "0.1.0",
4
+ "author": "engineering@frontside.com",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/thefrontside/effectionx.git"
8
+ },
9
+ "license": "MIT",
10
+ "bugs": {
11
+ "url": "https://github.com/thefrontside/effectionx/issues"
12
+ },
13
+ "main": "./script/mod.js",
14
+ "module": "./esm/mod.js",
15
+ "exports": {
16
+ ".": {
17
+ "import": "./esm/mod.js",
18
+ "require": "./script/mod.js"
19
+ },
20
+ "./test-helpers": {
21
+ "import": "./esm/test-helpers.js",
22
+ "require": "./script/test-helpers.js"
23
+ }
24
+ },
25
+ "engines": {
26
+ "node": ">= 16"
27
+ },
28
+ "sideEffects": false,
29
+ "_generatedBy": "dnt@dev"
30
+ }
@@ -0,0 +1,21 @@
1
+ import { type Stream } from "effection";
2
+ type RequireAtLeastOne<T, Keys extends keyof T = keyof T> = Pick<T, Exclude<keyof T, Keys>> & {
3
+ [K in Keys]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<Keys, K>>>;
4
+ }[Keys];
5
+ export interface BatchOptions {
6
+ readonly maxTime: number;
7
+ readonly maxSize: number;
8
+ }
9
+ /**
10
+ * Creates batches of items from the source stream. The batches can be created either by
11
+ * specifying a maximum time or a maximum size. If both are specified, the batch will be
12
+ * created when either condition is met.
13
+ *
14
+ * @param options - The options for the batch.
15
+ * @param options.maxTime - The maximum time to wait for a batch.
16
+ * @param options.maxSize - The maximum size of a batch.
17
+ * @returns A stream of arrays of items from the source stream.
18
+ */
19
+ export declare function batch(options: RequireAtLeastOne<BatchOptions>): <T>(stream: Stream<T, never>) => Stream<T[], never>;
20
+ export {};
21
+ //# sourceMappingURL=batch.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"batch.d.ts","sourceRoot":"","sources":["../src/batch.ts"],"names":[],"mappings":"AAAA,OAAO,EAA4B,KAAK,MAAM,EAAE,MAAM,WAAW,CAAC;AAGlE,KAAK,iBAAiB,CAAC,CAAC,EAAE,IAAI,SAAS,MAAM,CAAC,GAAG,MAAM,CAAC,IACpD,IAAI,CAAC,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC,EAAE,IAAI,CAAC,CAAC,GAC/B;KACC,CAAC,IAAI,IAAI,CAAC,CAAC,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,EAAE,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC;CACzE,CAAC,IAAI,CAAC,CAAC;AAEV,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B;AAED;;;;;;;;;GASG;AACH,wBAAgB,KAAK,CACnB,OAAO,EAAE,iBAAiB,CAAC,YAAY,CAAC,GACvC,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,KAAK,CAAC,KAAK,MAAM,CAAC,CAAC,EAAE,EAAE,KAAK,CAAC,CA4CrD"}
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.batch = batch;
4
+ const effection_1 = require("effection");
5
+ const signals_js_1 = require("./signals.js");
6
+ /**
7
+ * Creates batches of items from the source stream. The batches can be created either by
8
+ * specifying a maximum time or a maximum size. If both are specified, the batch will be
9
+ * created when either condition is met.
10
+ *
11
+ * @param options - The options for the batch.
12
+ * @param options.maxTime - The maximum time to wait for a batch.
13
+ * @param options.maxSize - The maximum size of a batch.
14
+ * @returns A stream of arrays of items from the source stream.
15
+ */
16
+ function batch(options) {
17
+ return function (stream) {
18
+ return {
19
+ *[Symbol.iterator]() {
20
+ let batch = yield* (0, signals_js_1.createArraySignal)([]);
21
+ yield* (0, effection_1.spawn)(function* () {
22
+ for (let item of yield* (0, effection_1.each)(stream)) {
23
+ batch.push(item);
24
+ if (options.maxSize && batch.length >= options.maxSize) {
25
+ // wait until it's drained
26
+ yield* (0, signals_js_1.is)(batch, (batch) => batch.length === 0);
27
+ }
28
+ yield* effection_1.each.next();
29
+ }
30
+ });
31
+ function drain() {
32
+ let value = batch.valueOf();
33
+ batch.set([]);
34
+ return value;
35
+ }
36
+ return {
37
+ *next() {
38
+ yield* (0, signals_js_1.is)(batch, (batch) => batch.length >= 1);
39
+ if (options.maxTime && options.maxSize) {
40
+ yield* (0, effection_1.race)([
41
+ (0, signals_js_1.is)(batch, (batch) => batch.length === options.maxSize),
42
+ (0, effection_1.sleep)(options.maxTime),
43
+ ]);
44
+ }
45
+ else if (options.maxTime) {
46
+ yield* (0, effection_1.sleep)(options.maxTime);
47
+ }
48
+ else if (options.maxSize) {
49
+ yield* (0, signals_js_1.is)(batch, (batch) => batch.length === options.maxSize);
50
+ }
51
+ return { done: false, value: drain() };
52
+ },
53
+ };
54
+ },
55
+ };
56
+ };
57
+ }
@@ -0,0 +1,23 @@
1
+ import type { Operation, Stream } from "effection";
2
+ /**
3
+ * Filters items from the stream based on a predicate function.
4
+ *
5
+ * @param predicate - The function to test each item
6
+ * @returns A stream transformer that only emits items that pass the predicate
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * import { filter } from "@effectionx/stream-helpers";
11
+ * import { run, each } from "effection";
12
+ *
13
+ * await run(function* () {
14
+ * const stream = filter((x: number) => x > 5)(sourceStream);
15
+ *
16
+ * for (const value of yield* each(stream)) {
17
+ * console.log(value); // Only values > 5
18
+ * }
19
+ * });
20
+ * ```
21
+ */
22
+ export declare function filter<T>(predicate: (value: T) => Operation<boolean>): <TDone>(stream: Stream<T, TDone>) => Stream<T, TDone>;
23
+ //# sourceMappingURL=filter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"filter.d.ts","sourceRoot":"","sources":["../src/filter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAEnD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,MAAM,CAAC,CAAC,EACtB,SAAS,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,SAAS,CAAC,OAAO,CAAC,GAC1C,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,KAAK,CAAC,KAAK,MAAM,CAAC,CAAC,EAAE,KAAK,CAAC,CAyBvD"}
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.filter = filter;
4
+ /**
5
+ * Filters items from the stream based on a predicate function.
6
+ *
7
+ * @param predicate - The function to test each item
8
+ * @returns A stream transformer that only emits items that pass the predicate
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * import { filter } from "@effectionx/stream-helpers";
13
+ * import { run, each } from "effection";
14
+ *
15
+ * await run(function* () {
16
+ * const stream = filter((x: number) => x > 5)(sourceStream);
17
+ *
18
+ * for (const value of yield* each(stream)) {
19
+ * console.log(value); // Only values > 5
20
+ * }
21
+ * });
22
+ * ```
23
+ */
24
+ function filter(predicate) {
25
+ return function (stream) {
26
+ return {
27
+ *[Symbol.iterator]() {
28
+ const subscription = yield* stream;
29
+ return {
30
+ *next() {
31
+ while (true) {
32
+ const next = yield* subscription.next();
33
+ if (next.done) {
34
+ return next;
35
+ }
36
+ if (yield* predicate(next.value)) {
37
+ return {
38
+ done: false,
39
+ value: next.value,
40
+ };
41
+ }
42
+ }
43
+ },
44
+ };
45
+ },
46
+ };
47
+ };
48
+ }
@@ -0,0 +1,9 @@
1
+ import type { Operation, Stream } from "effection";
2
+ /**
3
+ * Transforms each item in the stream using the provided function.
4
+ *
5
+ * @param fn - The function to transform each item
6
+ * @returns A stream transformer that applies the function to each item
7
+ */
8
+ export declare function map<A, B>(fn: (value: A) => Operation<B>): <TClose>(stream: Stream<A, TClose>) => Stream<B, TClose>;
9
+ //# sourceMappingURL=map.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"map.d.ts","sourceRoot":"","sources":["../src/map.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAEnD;;;;;GAKG;AACH,wBAAgB,GAAG,CAAC,CAAC,EAAE,CAAC,EACtB,EAAE,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,SAAS,CAAC,CAAC,CAAC,GAC7B,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,KAAK,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,CAsB1D"}
package/script/map.js ADDED
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.map = map;
4
+ /**
5
+ * Transforms each item in the stream using the provided function.
6
+ *
7
+ * @param fn - The function to transform each item
8
+ * @returns A stream transformer that applies the function to each item
9
+ */
10
+ function map(fn) {
11
+ return function (stream) {
12
+ return {
13
+ *[Symbol.iterator]() {
14
+ const subscription = yield* stream;
15
+ return {
16
+ *next() {
17
+ const next = yield* subscription.next();
18
+ if (next.done) {
19
+ return next;
20
+ }
21
+ return {
22
+ done: false,
23
+ value: yield* fn(next.value),
24
+ };
25
+ },
26
+ };
27
+ },
28
+ };
29
+ };
30
+ }
@@ -0,0 +1,6 @@
1
+ export * from "./batch.js";
2
+ export * from "./valve.js";
3
+ export * from "./map.js";
4
+ export * from "./filter.js";
5
+ export * from "./tracker.js";
6
+ //# sourceMappingURL=mod.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC"}
package/script/mod.js ADDED
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./batch.js"), exports);
18
+ __exportStar(require("./valve.js"), exports);
19
+ __exportStar(require("./map.js"), exports);
20
+ __exportStar(require("./filter.js"), exports);
21
+ __exportStar(require("./tracker.js"), exports);
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,23 @@
1
+ import { type Operation, type Stream } from "effection";
2
+ interface ValueStream<T> extends Stream<T, void> {
3
+ valueOf(): T;
4
+ }
5
+ interface Settable<T> {
6
+ set(value: T): T;
7
+ }
8
+ export interface SettableValue<T> extends Settable<T>, ValueStream<T> {
9
+ }
10
+ interface ArraySignal<T> extends SettableValue<T[]> {
11
+ push(item: T): number;
12
+ shift(): Operation<T>;
13
+ valueOf(): T[];
14
+ get length(): number;
15
+ }
16
+ export declare function createArraySignal<T>(initial: Iterable<T>): Operation<ArraySignal<T>>;
17
+ export declare function is<T>(array: ValueStream<T>, predicate: (item: T) => boolean): Generator<any, void, any>;
18
+ export interface BooleanSignal extends SettableValue<boolean> {
19
+ }
20
+ export declare function createBoolean(initial?: boolean): any;
21
+ export declare function createSetSignal<T>(initial?: Array<T>): any;
22
+ export {};
23
+ //# sourceMappingURL=signals.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signals.d.ts","sourceRoot":"","sources":["../src/signals.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,SAAS,EAEd,KAAK,MAAM,EACZ,MAAM,WAAW,CAAC;AAGnB,UAAU,WAAW,CAAC,CAAC,CAAE,SAAQ,MAAM,CAAC,CAAC,EAAE,IAAI,CAAC;IAC9C,OAAO,IAAI,CAAC,CAAC;CACd;AAED,UAAU,QAAQ,CAAC,CAAC;IAClB,GAAG,CAAC,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC;CAClB;AAED,MAAM,WAAW,aAAa,CAAC,CAAC,CAAE,SAAQ,QAAQ,CAAC,CAAC,CAAC,EAAE,WAAW,CAAC,CAAC,CAAC;CAAG;AAExE,UAAU,WAAW,CAAC,CAAC,CAAE,SAAQ,aAAa,CAAC,CAAC,EAAE,CAAC;IACjD,IAAI,CAAC,IAAI,EAAE,CAAC,GAAG,MAAM,CAAC;IACtB,KAAK,IAAI,SAAS,CAAC,CAAC,CAAC,CAAC;IACtB,OAAO,IAAI,CAAC,EAAE,CAAC;IACf,IAAI,MAAM,IAAI,MAAM,CAAC;CACtB;AAED,wBAAgB,iBAAiB,CAAC,CAAC,EACjC,OAAO,EAAE,QAAQ,CAAC,CAAC,CAAC,GACnB,SAAS,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAwC3B;AAED,wBAAiB,EAAE,CAAC,CAAC,EACnB,KAAK,EAAE,WAAW,CAAC,CAAC,CAAC,EACrB,SAAS,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,6BAahC;AAED,MAAM,WAAW,aAAc,SAAQ,aAAa,CAAC,OAAO,CAAC;CAC5D;AAED,wBAAgB,aAAa,CAAC,OAAO,GAAE,OAAe,OA0BrD;AASD,wBAAgB,eAAe,CAAC,CAAC,EAAE,OAAO,GAAE,KAAK,CAAC,CAAC,CAAM,OAwCxD"}