@effectionx/stream-helpers 0.1.1 → 0.3.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.
package/README.md CHANGED
@@ -144,6 +144,54 @@ function* example() {
144
144
  }
145
145
  ```
146
146
 
147
+ ### ForEach
148
+
149
+ The `forEach` helper invokes a function for each item passing through a stream.
150
+ This is useful when you need to perform side effects or operations on each item
151
+ without transforming the stream itself. Unlike other stream helpers that return
152
+ transformed streams, `forEach` consumes the entire stream and returns the
153
+ stream's close value.
154
+
155
+ ```typescript
156
+ import { forEach } from "@effectionx/stream-helpers";
157
+ import { createSignal, spawn } from "effection";
158
+
159
+ function* example() {
160
+ const stream = createSignal<number, void>();
161
+
162
+ // Process each item in the stream
163
+ yield* spawn(() =>
164
+ forEach(function* (item) {
165
+ console.log(`Processing: ${item}`);
166
+ // Perform any side effects here
167
+ }, stream)
168
+ );
169
+
170
+ stream.send(1);
171
+ stream.send(2);
172
+ stream.send(3);
173
+ stream.close();
174
+ }
175
+
176
+ // Example: Handling stream close value
177
+ function* exampleWithCloseValue() {
178
+ const stream = createSignal<string, number>();
179
+
180
+ const result = yield* spawn(() =>
181
+ forEach(function* (item) {
182
+ console.log(`Item: ${item}`);
183
+ }, stream)
184
+ );
185
+
186
+ stream.send("hello");
187
+ stream.send("world");
188
+ stream.close(42); // Close with value 42
189
+
190
+ const closeValue = yield* result;
191
+ console.log(`Stream closed with: ${closeValue}`); // 42
192
+ }
193
+ ```
194
+
147
195
  ### Passthrough Tracker
148
196
 
149
197
  Passthrough Tracker stream helper provides a way to know if all items that
@@ -183,8 +231,7 @@ You can use a simple `pipe()` to compose a series of stream helpers together. In
183
231
  this example, we use one from [remeda](https://remedajs.com/docs/#pipe),
184
232
 
185
233
  ```typescript
186
- import { batch, filter, map, valve } from "@effectionx/stream-helpers";
187
- import { each } from "effection";
234
+ import { batch, filter, forEach, map, valve } from "@effectionx/stream-helpers";
188
235
  // any standard pipe function should work
189
236
  import { pipe } from "remeda";
190
237
 
@@ -202,10 +249,10 @@ function* example(source: Stream<number, unknown>) {
202
249
  batch({ maxSize: 50 }),
203
250
  );
204
251
 
205
- for (const value of yield* each(stream)) {
252
+ // Use forEach to process each value in the composed stream
253
+ yield* forEach(function* (value) {
206
254
  console.log(value);
207
- yield* each.next();
208
- }
255
+ }, stream);
209
256
  }
210
257
  ```
211
258
 
@@ -0,0 +1,27 @@
1
+ import type { Operation, Stream } from "effection";
2
+ /**
3
+ * Invoke a function for each item passing through the stream.
4
+ *
5
+ * @template T - The type of items in the stream
6
+ * @template TClose - The type of the close value returned when the stream ends
7
+ * @param fn - A function that processes each item from the stream.
8
+ * @param stream: A stream to process
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * import { forEach } from "./for-each.ts";
13
+ * import { createSignal } from "effection";
14
+ *
15
+ * // Process items from a stream
16
+ * const stream = createSignal<number, void>();
17
+ *
18
+ * yield* spawn(() => forEach(function*(item) {
19
+ * console.log(`Processing: ${item}`);
20
+ * }, stream));
21
+ *
22
+ * yield* stream.send(1);
23
+ * yield* stream.send(2);
24
+ * ```
25
+ */
26
+ export declare function forEach<T, TClose>(fn: (item: T) => Operation<void>, stream: Stream<T, TClose>): Operation<TClose>;
27
+ //# sourceMappingURL=for-each.d.ts.map
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Invoke a function for each item passing through the stream.
3
+ *
4
+ * @template T - The type of items in the stream
5
+ * @template TClose - The type of the close value returned when the stream ends
6
+ * @param fn - A function that processes each item from the stream.
7
+ * @param stream: A stream to process
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * import { forEach } from "./for-each.ts";
12
+ * import { createSignal } from "effection";
13
+ *
14
+ * // Process items from a stream
15
+ * const stream = createSignal<number, void>();
16
+ *
17
+ * yield* spawn(() => forEach(function*(item) {
18
+ * console.log(`Processing: ${item}`);
19
+ * }, stream));
20
+ *
21
+ * yield* stream.send(1);
22
+ * yield* stream.send(2);
23
+ * ```
24
+ */
25
+ export function* forEach(fn, stream) {
26
+ let subscription = yield* stream;
27
+ let next = yield* subscription.next();
28
+ while (!next.done) {
29
+ yield* fn(next.value);
30
+ next = yield* subscription.next();
31
+ }
32
+ return next.value;
33
+ }
package/esm/mod.d.ts CHANGED
@@ -3,4 +3,5 @@ export * from "./valve.js";
3
3
  export * from "./map.js";
4
4
  export * from "./filter.js";
5
5
  export * from "./tracker.js";
6
+ export * from "./for-each.js";
6
7
  //# sourceMappingURL=mod.d.ts.map
package/esm/mod.d.ts.map CHANGED
@@ -1 +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"}
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/mod.js CHANGED
@@ -3,3 +3,4 @@ export * from "./valve.js";
3
3
  export * from "./map.js";
4
4
  export * from "./filter.js";
5
5
  export * from "./tracker.js";
6
+ export * from "./for-each.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@effectionx/stream-helpers",
3
- "version": "0.1.1",
3
+ "version": "0.3.0",
4
4
  "author": "engineering@frontside.com",
5
5
  "repository": {
6
6
  "type": "git",
@@ -22,9 +22,14 @@
22
22
  "require": "./script/test-helpers.js"
23
23
  }
24
24
  },
25
+ "scripts": {},
25
26
  "engines": {
26
27
  "node": ">= 16"
27
28
  },
28
29
  "sideEffects": false,
30
+ "dependencies": {
31
+ "@effectionx/signals": "0.3.0",
32
+ "effection": "^3"
33
+ },
29
34
  "_generatedBy": "dnt@dev"
30
35
  }
@@ -0,0 +1,27 @@
1
+ import type { Operation, Stream } from "effection";
2
+ /**
3
+ * Invoke a function for each item passing through the stream.
4
+ *
5
+ * @template T - The type of items in the stream
6
+ * @template TClose - The type of the close value returned when the stream ends
7
+ * @param fn - A function that processes each item from the stream.
8
+ * @param stream: A stream to process
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * import { forEach } from "./for-each.ts";
13
+ * import { createSignal } from "effection";
14
+ *
15
+ * // Process items from a stream
16
+ * const stream = createSignal<number, void>();
17
+ *
18
+ * yield* spawn(() => forEach(function*(item) {
19
+ * console.log(`Processing: ${item}`);
20
+ * }, stream));
21
+ *
22
+ * yield* stream.send(1);
23
+ * yield* stream.send(2);
24
+ * ```
25
+ */
26
+ export declare function forEach<T, TClose>(fn: (item: T) => Operation<void>, stream: Stream<T, TClose>): Operation<TClose>;
27
+ //# sourceMappingURL=for-each.d.ts.map
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,36 @@
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/mod.d.ts CHANGED
@@ -3,4 +3,5 @@ export * from "./valve.js";
3
3
  export * from "./map.js";
4
4
  export * from "./filter.js";
5
5
  export * from "./tracker.js";
6
+ export * from "./for-each.js";
6
7
  //# sourceMappingURL=mod.d.ts.map
@@ -1 +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"}
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 CHANGED
@@ -19,3 +19,4 @@ __exportStar(require("./valve.js"), exports);
19
19
  __exportStar(require("./map.js"), exports);
20
20
  __exportStar(require("./filter.js"), exports);
21
21
  __exportStar(require("./tracker.js"), exports);
22
+ __exportStar(require("./for-each.js"), exports);