@effectionx/stream-helpers 0.4.1 → 0.6.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 (98) hide show
  1. package/README.md +47 -0
  2. package/batch.test.ts +112 -0
  3. package/batch.ts +107 -0
  4. package/dist/batch.d.ts.map +1 -0
  5. package/dist/batch.js +86 -0
  6. package/dist/filter.d.ts.map +1 -0
  7. package/dist/filter.js +43 -0
  8. package/dist/for-each.d.ts.map +1 -0
  9. package/dist/lines.d.ts +29 -0
  10. package/dist/lines.d.ts.map +1 -0
  11. package/dist/lines.js +61 -0
  12. package/dist/map.d.ts.map +1 -0
  13. package/dist/map.js +26 -0
  14. package/dist/mod.d.ts +9 -0
  15. package/dist/mod.d.ts.map +1 -0
  16. package/{esm → dist}/mod.js +2 -0
  17. package/dist/subject.d.ts +38 -0
  18. package/dist/subject.d.ts.map +1 -0
  19. package/dist/subject.js +60 -0
  20. package/dist/test-helpers/faucet.d.ts.map +1 -0
  21. package/dist/test-helpers.d.ts +2 -0
  22. package/dist/test-helpers.d.ts.map +1 -0
  23. package/dist/tracker.d.ts.map +1 -0
  24. package/{esm → dist}/tracker.js +12 -14
  25. package/dist/tsconfig.tsbuildinfo +1 -0
  26. package/dist/valve.d.ts.map +1 -0
  27. package/dist/valve.js +46 -0
  28. package/filter.test.ts +47 -0
  29. package/filter.ts +48 -0
  30. package/for-each.test.ts +42 -0
  31. package/{script/for-each.d.ts → for-each.ts} +13 -2
  32. package/lines.ts +74 -0
  33. package/map.test.ts +50 -0
  34. package/map.ts +33 -0
  35. package/mod.ts +8 -0
  36. package/package.json +29 -23
  37. package/subject.test.ts +93 -0
  38. package/subject.ts +67 -0
  39. package/test-helpers/faucet.test.ts +120 -0
  40. package/{script/test-helpers/faucet.d.ts → test-helpers/faucet.ts} +56 -25
  41. package/test-helpers.ts +1 -0
  42. package/tracker.test.ts +109 -0
  43. package/tracker.ts +57 -0
  44. package/tsconfig.json +20 -0
  45. package/valve.test.ts +52 -0
  46. package/valve.ts +73 -0
  47. package/esm/batch.d.ts.map +0 -1
  48. package/esm/batch.js +0 -89
  49. package/esm/filter.d.ts.map +0 -1
  50. package/esm/filter.js +0 -45
  51. package/esm/for-each.d.ts.map +0 -1
  52. package/esm/map.d.ts.map +0 -1
  53. package/esm/map.js +0 -28
  54. package/esm/mod.d.ts +0 -7
  55. package/esm/mod.d.ts.map +0 -1
  56. package/esm/package.json +0 -3
  57. package/esm/test-helpers/faucet.d.ts.map +0 -1
  58. package/esm/test-helpers.d.ts +0 -2
  59. package/esm/test-helpers.d.ts.map +0 -1
  60. package/esm/tracker.d.ts.map +0 -1
  61. package/esm/valve.d.ts.map +0 -1
  62. package/esm/valve.js +0 -48
  63. package/script/batch.d.ts +0 -21
  64. package/script/batch.d.ts.map +0 -1
  65. package/script/batch.js +0 -92
  66. package/script/filter.d.ts +0 -23
  67. package/script/filter.d.ts.map +0 -1
  68. package/script/filter.js +0 -48
  69. package/script/for-each.d.ts.map +0 -1
  70. package/script/for-each.js +0 -36
  71. package/script/map.d.ts +0 -9
  72. package/script/map.d.ts.map +0 -1
  73. package/script/map.js +0 -31
  74. package/script/mod.d.ts +0 -7
  75. package/script/mod.d.ts.map +0 -1
  76. package/script/mod.js +0 -22
  77. package/script/package.json +0 -3
  78. package/script/test-helpers/faucet.d.ts.map +0 -1
  79. package/script/test-helpers/faucet.js +0 -75
  80. package/script/test-helpers.d.ts +0 -2
  81. package/script/test-helpers.d.ts.map +0 -1
  82. package/script/test-helpers.js +0 -17
  83. package/script/tracker.d.ts +0 -24
  84. package/script/tracker.d.ts.map +0 -1
  85. package/script/tracker.js +0 -42
  86. package/script/valve.d.ts +0 -37
  87. package/script/valve.d.ts.map +0 -1
  88. package/script/valve.js +0 -51
  89. /package/{esm → dist}/batch.d.ts +0 -0
  90. /package/{esm → dist}/filter.d.ts +0 -0
  91. /package/{esm → dist}/for-each.d.ts +0 -0
  92. /package/{esm → dist}/for-each.js +0 -0
  93. /package/{esm → dist}/map.d.ts +0 -0
  94. /package/{esm → dist}/test-helpers/faucet.d.ts +0 -0
  95. /package/{esm → dist}/test-helpers/faucet.js +0 -0
  96. /package/{esm → dist}/test-helpers.js +0 -0
  97. /package/{esm → dist}/tracker.d.ts +0 -0
  98. /package/{esm → dist}/valve.d.ts +0 -0
package/valve.ts ADDED
@@ -0,0 +1,73 @@
1
+ import { each, type Operation, spawn, type Stream } from "effection";
2
+ import { createArraySignal } from "@effectionx/signals";
3
+
4
+ /**
5
+ * Options for the valve.
6
+ */
7
+ export interface ValveOptions {
8
+ /**
9
+ * The buffer size when {@link ValveOptions#open} method is called.
10
+ */
11
+ openAt: number;
12
+ /**
13
+ * The buffer size when {@link ValveOptions#close} method is called.
14
+ */
15
+ closeAt: number;
16
+ /**
17
+ * The operation to resume the upstream.
18
+ */
19
+ open(): Operation<void>;
20
+ /**
21
+ * The operation to pause the upstream.
22
+ */
23
+ close(): Operation<void>;
24
+ }
25
+
26
+ /**
27
+ * This function buffers incoming items, if the upstream is producing faster than the downstream
28
+ * can consume, the buffer will grow. If the buffer size exceeds the `closeAt` threshold, the
29
+ * `close` operation will be called which is expected to pause the upstream. The buffer will
30
+ * drain until the buffer size is less than the `openAt` threshold, at which point the `open`
31
+ * operation will be called to resume the upstream.
32
+ *
33
+ * @param options.open - The operation to resume the upstream.
34
+ * @param options.openAt - The buffer size at which the upstream will be resumed.
35
+ * @param options.close - The operation to pause the upstream.
36
+ * @param options.closeAt - The buffer size at which the upstream will be paused.
37
+ * @returns A stream with backpressure applied.
38
+ */
39
+ export function valve(
40
+ options: ValveOptions,
41
+ ): <T>(stream: Stream<T, never>) => Stream<T, never> {
42
+ return <T>(stream: Stream<T, never>): Stream<T, never> => ({
43
+ *[Symbol.iterator]() {
44
+ const buffer = yield* createArraySignal<T>([]);
45
+ let open = true;
46
+
47
+ yield* spawn(function* () {
48
+ for (const item of yield* each(stream)) {
49
+ buffer.push(item);
50
+ if (open && buffer.length >= options.closeAt) {
51
+ yield* options.close();
52
+ open = false;
53
+ }
54
+ yield* each.next();
55
+ }
56
+ });
57
+
58
+ return {
59
+ *next() {
60
+ if (!open && buffer.length <= options.openAt) {
61
+ yield* options.open();
62
+ open = true;
63
+ }
64
+ const value = yield* buffer.shift();
65
+ return {
66
+ done: false,
67
+ value,
68
+ };
69
+ },
70
+ };
71
+ },
72
+ });
73
+ }
@@ -1 +0,0 @@
1
- {"version":3,"file":"batch.d.ts","sourceRoot":"","sources":["../src/batch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,KAAK,MAAM,EAAa,MAAM,WAAW,CAAC;AAG1D,KAAK,iBAAiB,CAAC,CAAC,EAAE,IAAI,SAAS,MAAM,CAAC,GAAG,MAAM,CAAC,IACpD,IAAI,CACJ,CAAC,EACD,OAAO,CAAC,MAAM,CAAC,EAAE,IAAI,CAAC,CACvB,GACC;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,QAAQ,CAAC,CAAC,EAAE,CAAC,EAAE,KAAK,CAAC,CA8E/D"}
package/esm/batch.js DELETED
@@ -1,89 +0,0 @@
1
- import { spawn } from "effection";
2
- import { timebox } from "@effectionx/timebox";
3
- /**
4
- * Creates batches of items from the source stream. The batches can be created either by
5
- * specifying a maximum time or a maximum size. If both are specified, the batch will be
6
- * created when either condition is met.
7
- *
8
- * @param options - The options for the batch.
9
- * @param options.maxTime - The maximum time to wait for a batch.
10
- * @param options.maxSize - The maximum size of a batch.
11
- * @returns A stream of arrays of items from the source stream.
12
- */
13
- export function batch(options) {
14
- return function (stream) {
15
- return {
16
- *[Symbol.iterator]() {
17
- const subscription = yield* stream;
18
- let lastPull;
19
- return {
20
- *next() {
21
- let start = performance.now();
22
- const batch = [];
23
- let next = {
24
- done: true,
25
- value: undefined,
26
- };
27
- if (lastPull && options.maxTime) {
28
- const timeout = yield* timebox(options.maxTime, () => lastPull);
29
- if (timeout.timeout) {
30
- yield* lastPull.halt();
31
- lastPull = undefined;
32
- }
33
- else {
34
- next = timeout.value;
35
- lastPull = undefined;
36
- }
37
- }
38
- else {
39
- next = yield* subscription.next();
40
- }
41
- // push the next value into the batch
42
- while (!next.done) {
43
- batch.push(next.value);
44
- const now = performance.now();
45
- if (options.maxSize && batch.length >= options.maxSize) {
46
- return {
47
- done: false,
48
- value: batch,
49
- };
50
- }
51
- else if (options.maxTime && start + options.maxTime <= now) {
52
- return {
53
- done: false,
54
- value: batch,
55
- };
56
- }
57
- else if (options.maxTime) {
58
- const task = yield* spawn(() => subscription.next());
59
- const timeout = yield* timebox(start + options.maxTime - performance.now(), () => task);
60
- if (timeout.timeout) {
61
- // produce the batch that we have, save task for next batch
62
- lastPull = task;
63
- return {
64
- done: false,
65
- value: batch,
66
- };
67
- }
68
- else {
69
- next = timeout.value;
70
- }
71
- }
72
- else {
73
- next = yield* subscription.next();
74
- }
75
- }
76
- // Stream is done, return any remaining batch
77
- if (batch.length > 0) {
78
- return {
79
- done: false,
80
- value: batch,
81
- };
82
- }
83
- return next;
84
- },
85
- };
86
- },
87
- };
88
- };
89
- }
@@ -1 +0,0 @@
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"}
package/esm/filter.js DELETED
@@ -1,45 +0,0 @@
1
- /**
2
- * Filters items from the stream based on a predicate function.
3
- *
4
- * @param predicate - The function to test each item
5
- * @returns A stream transformer that only emits items that pass the predicate
6
- *
7
- * @example
8
- * ```typescript
9
- * import { filter } from "@effectionx/stream-helpers";
10
- * import { run, each } from "effection";
11
- *
12
- * await run(function* () {
13
- * const stream = filter((x: number) => x > 5)(sourceStream);
14
- *
15
- * for (const value of yield* each(stream)) {
16
- * console.log(value); // Only values > 5
17
- * }
18
- * });
19
- * ```
20
- */
21
- export function filter(predicate) {
22
- return function (stream) {
23
- return {
24
- *[Symbol.iterator]() {
25
- const subscription = yield* stream;
26
- return {
27
- *next() {
28
- while (true) {
29
- const next = yield* subscription.next();
30
- if (next.done) {
31
- return next;
32
- }
33
- if (yield* predicate(next.value)) {
34
- return {
35
- done: false,
36
- value: next.value,
37
- };
38
- }
39
- }
40
- },
41
- };
42
- },
43
- };
44
- };
45
- }
@@ -1 +0,0 @@
1
- {"version":3,"file":"for-each.d.ts","sourceRoot":"","sources":["../src/for-each.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAEnD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAiB,OAAO,CAAC,CAAC,EAAE,MAAM,EAChC,EAAE,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,SAAS,CAAC,IAAI,CAAC,EAChC,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,GACxB,SAAS,CAAC,MAAM,CAAC,CAQnB"}
package/esm/map.d.ts.map DELETED
@@ -1 +0,0 @@
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,CAwB1D"}
package/esm/map.js DELETED
@@ -1,28 +0,0 @@
1
- /**
2
- * Transforms each item in the stream using the provided function.
3
- *
4
- * @param fn - The function to transform each item
5
- * @returns A stream transformer that applies the function to each item
6
- */
7
- export function map(fn) {
8
- return function (stream) {
9
- return {
10
- *[Symbol.iterator]() {
11
- const subscription = yield* stream;
12
- return {
13
- *next() {
14
- const next = yield* subscription.next();
15
- if (next.done) {
16
- return next;
17
- }
18
- const value = yield* fn(next.value);
19
- return {
20
- done: false,
21
- value,
22
- };
23
- },
24
- };
25
- },
26
- };
27
- };
28
- }
package/esm/mod.d.ts DELETED
@@ -1,7 +0,0 @@
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
- export * from "./for-each.js";
7
- //# sourceMappingURL=mod.d.ts.map
package/esm/mod.d.ts.map DELETED
@@ -1 +0,0 @@
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;AAC7B,cAAc,eAAe,CAAC"}
package/esm/package.json DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "type": "module"
3
- }
@@ -1 +0,0 @@
1
- {"version":3,"file":"faucet.d.ts","sourceRoot":"","sources":["../../src/test-helpers/faucet.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiB,KAAK,SAAS,EAAE,KAAK,MAAM,EAAE,MAAM,WAAW,CAAC;AAGvE;;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"}
@@ -1,2 +0,0 @@
1
- export * from "./test-helpers/faucet.js";
2
- //# sourceMappingURL=test-helpers.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"test-helpers.d.ts","sourceRoot":"","sources":["../src/test-helpers.ts"],"names":[],"mappings":"AAAA,cAAc,0BAA0B,CAAC"}
@@ -1 +0,0 @@
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"}
@@ -1 +0,0 @@
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;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC;IACf;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAChB;;OAEG;IACH,IAAI,IAAI,SAAS,CAAC,IAAI,CAAC,CAAC;IACxB;;OAEG;IACH,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 DELETED
@@ -1,48 +0,0 @@
1
- import { each, spawn } from "effection";
2
- import { createArraySignal } from "@effectionx/signals";
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/script/batch.d.ts DELETED
@@ -1,21 +0,0 @@
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<Readonly<T[]>, never>;
20
- export {};
21
- //# sourceMappingURL=batch.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"batch.d.ts","sourceRoot":"","sources":["../src/batch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,KAAK,MAAM,EAAa,MAAM,WAAW,CAAC;AAG1D,KAAK,iBAAiB,CAAC,CAAC,EAAE,IAAI,SAAS,MAAM,CAAC,GAAG,MAAM,CAAC,IACpD,IAAI,CACJ,CAAC,EACD,OAAO,CAAC,MAAM,CAAC,EAAE,IAAI,CAAC,CACvB,GACC;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,QAAQ,CAAC,CAAC,EAAE,CAAC,EAAE,KAAK,CAAC,CA8E/D"}
package/script/batch.js DELETED
@@ -1,92 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.batch = batch;
4
- const effection_1 = require("effection");
5
- const timebox_1 = require("@effectionx/timebox");
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
- const subscription = yield* stream;
21
- let lastPull;
22
- return {
23
- *next() {
24
- let start = performance.now();
25
- const batch = [];
26
- let next = {
27
- done: true,
28
- value: undefined,
29
- };
30
- if (lastPull && options.maxTime) {
31
- const timeout = yield* (0, timebox_1.timebox)(options.maxTime, () => lastPull);
32
- if (timeout.timeout) {
33
- yield* lastPull.halt();
34
- lastPull = undefined;
35
- }
36
- else {
37
- next = timeout.value;
38
- lastPull = undefined;
39
- }
40
- }
41
- else {
42
- next = yield* subscription.next();
43
- }
44
- // push the next value into the batch
45
- while (!next.done) {
46
- batch.push(next.value);
47
- const now = performance.now();
48
- if (options.maxSize && batch.length >= options.maxSize) {
49
- return {
50
- done: false,
51
- value: batch,
52
- };
53
- }
54
- else if (options.maxTime && start + options.maxTime <= now) {
55
- return {
56
- done: false,
57
- value: batch,
58
- };
59
- }
60
- else if (options.maxTime) {
61
- const task = yield* (0, effection_1.spawn)(() => subscription.next());
62
- const timeout = yield* (0, timebox_1.timebox)(start + options.maxTime - performance.now(), () => task);
63
- if (timeout.timeout) {
64
- // produce the batch that we have, save task for next batch
65
- lastPull = task;
66
- return {
67
- done: false,
68
- value: batch,
69
- };
70
- }
71
- else {
72
- next = timeout.value;
73
- }
74
- }
75
- else {
76
- next = yield* subscription.next();
77
- }
78
- }
79
- // Stream is done, return any remaining batch
80
- if (batch.length > 0) {
81
- return {
82
- done: false,
83
- value: batch,
84
- };
85
- }
86
- return next;
87
- },
88
- };
89
- },
90
- };
91
- };
92
- }
@@ -1,23 +0,0 @@
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
@@ -1 +0,0 @@
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"}
package/script/filter.js DELETED
@@ -1,48 +0,0 @@
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
- }
@@ -1 +0,0 @@
1
- {"version":3,"file":"for-each.d.ts","sourceRoot":"","sources":["../src/for-each.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAEnD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAiB,OAAO,CAAC,CAAC,EAAE,MAAM,EAChC,EAAE,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,SAAS,CAAC,IAAI,CAAC,EAChC,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,GACxB,SAAS,CAAC,MAAM,CAAC,CAQnB"}
@@ -1,36 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.forEach = forEach;
4
- /**
5
- * Invoke a function for each item passing through the stream.
6
- *
7
- * @template T - The type of items in the stream
8
- * @template TClose - The type of the close value returned when the stream ends
9
- * @param fn - A function that processes each item from the stream.
10
- * @param stream: A stream to process
11
- *
12
- * @example
13
- * ```typescript
14
- * import { forEach } from "./for-each.ts";
15
- * import { createSignal } from "effection";
16
- *
17
- * // Process items from a stream
18
- * const stream = createSignal<number, void>();
19
- *
20
- * yield* spawn(() => forEach(function*(item) {
21
- * console.log(`Processing: ${item}`);
22
- * }, stream));
23
- *
24
- * yield* stream.send(1);
25
- * yield* stream.send(2);
26
- * ```
27
- */
28
- function* forEach(fn, stream) {
29
- let subscription = yield* stream;
30
- let next = yield* subscription.next();
31
- while (!next.done) {
32
- yield* fn(next.value);
33
- next = yield* subscription.next();
34
- }
35
- return next.value;
36
- }
package/script/map.d.ts DELETED
@@ -1,9 +0,0 @@
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
@@ -1 +0,0 @@
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,CAwB1D"}
package/script/map.js DELETED
@@ -1,31 +0,0 @@
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
- const value = yield* fn(next.value);
22
- return {
23
- done: false,
24
- value,
25
- };
26
- },
27
- };
28
- },
29
- };
30
- };
31
- }
package/script/mod.d.ts DELETED
@@ -1,7 +0,0 @@
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
- export * from "./for-each.js";
7
- //# sourceMappingURL=mod.d.ts.map
@@ -1 +0,0 @@
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;AAC7B,cAAc,eAAe,CAAC"}
package/script/mod.js DELETED
@@ -1,22 +0,0 @@
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);
22
- __exportStar(require("./for-each.js"), exports);