@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.
- package/README.md +47 -0
- package/batch.test.ts +112 -0
- package/batch.ts +107 -0
- package/dist/batch.d.ts.map +1 -0
- package/dist/batch.js +86 -0
- package/dist/filter.d.ts.map +1 -0
- package/dist/filter.js +43 -0
- package/dist/for-each.d.ts.map +1 -0
- package/dist/lines.d.ts +29 -0
- package/dist/lines.d.ts.map +1 -0
- package/dist/lines.js +61 -0
- package/dist/map.d.ts.map +1 -0
- package/dist/map.js +26 -0
- package/dist/mod.d.ts +9 -0
- package/dist/mod.d.ts.map +1 -0
- package/{esm → dist}/mod.js +2 -0
- package/dist/subject.d.ts +38 -0
- package/dist/subject.d.ts.map +1 -0
- package/dist/subject.js +60 -0
- package/dist/test-helpers/faucet.d.ts.map +1 -0
- package/dist/test-helpers.d.ts +2 -0
- package/dist/test-helpers.d.ts.map +1 -0
- package/dist/tracker.d.ts.map +1 -0
- package/{esm → dist}/tracker.js +12 -14
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/valve.d.ts.map +1 -0
- package/dist/valve.js +46 -0
- package/filter.test.ts +47 -0
- package/filter.ts +48 -0
- package/for-each.test.ts +42 -0
- package/{script/for-each.d.ts → for-each.ts} +13 -2
- package/lines.ts +74 -0
- package/map.test.ts +50 -0
- package/map.ts +33 -0
- package/mod.ts +8 -0
- package/package.json +29 -23
- package/subject.test.ts +93 -0
- package/subject.ts +67 -0
- package/test-helpers/faucet.test.ts +120 -0
- package/{script/test-helpers/faucet.d.ts → test-helpers/faucet.ts} +56 -25
- package/test-helpers.ts +1 -0
- package/tracker.test.ts +109 -0
- package/tracker.ts +57 -0
- package/tsconfig.json +20 -0
- package/valve.test.ts +52 -0
- package/valve.ts +73 -0
- package/esm/batch.d.ts.map +0 -1
- package/esm/batch.js +0 -89
- package/esm/filter.d.ts.map +0 -1
- package/esm/filter.js +0 -45
- package/esm/for-each.d.ts.map +0 -1
- package/esm/map.d.ts.map +0 -1
- package/esm/map.js +0 -28
- package/esm/mod.d.ts +0 -7
- package/esm/mod.d.ts.map +0 -1
- package/esm/package.json +0 -3
- package/esm/test-helpers/faucet.d.ts.map +0 -1
- package/esm/test-helpers.d.ts +0 -2
- package/esm/test-helpers.d.ts.map +0 -1
- package/esm/tracker.d.ts.map +0 -1
- package/esm/valve.d.ts.map +0 -1
- package/esm/valve.js +0 -48
- package/script/batch.d.ts +0 -21
- package/script/batch.d.ts.map +0 -1
- package/script/batch.js +0 -92
- package/script/filter.d.ts +0 -23
- package/script/filter.d.ts.map +0 -1
- package/script/filter.js +0 -48
- package/script/for-each.d.ts.map +0 -1
- package/script/for-each.js +0 -36
- package/script/map.d.ts +0 -9
- package/script/map.d.ts.map +0 -1
- package/script/map.js +0 -31
- package/script/mod.d.ts +0 -7
- package/script/mod.d.ts.map +0 -1
- package/script/mod.js +0 -22
- package/script/package.json +0 -3
- package/script/test-helpers/faucet.d.ts.map +0 -1
- package/script/test-helpers/faucet.js +0 -75
- package/script/test-helpers.d.ts +0 -2
- package/script/test-helpers.d.ts.map +0 -1
- package/script/test-helpers.js +0 -17
- package/script/tracker.d.ts +0 -24
- package/script/tracker.d.ts.map +0 -1
- package/script/tracker.js +0 -42
- package/script/valve.d.ts +0 -37
- package/script/valve.d.ts.map +0 -1
- package/script/valve.js +0 -51
- /package/{esm → dist}/batch.d.ts +0 -0
- /package/{esm → dist}/filter.d.ts +0 -0
- /package/{esm → dist}/for-each.d.ts +0 -0
- /package/{esm → dist}/for-each.js +0 -0
- /package/{esm → dist}/map.d.ts +0 -0
- /package/{esm → dist}/test-helpers/faucet.d.ts +0 -0
- /package/{esm → dist}/test-helpers/faucet.js +0 -0
- /package/{esm → dist}/test-helpers.js +0 -0
- /package/{esm → dist}/tracker.d.ts +0 -0
- /package/{esm → dist}/valve.d.ts +0 -0
package/script/package.json
DELETED
|
@@ -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,75 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useFaucet = useFaucet;
|
|
4
|
-
const effection_1 = require("effection");
|
|
5
|
-
const signals_1 = require("@effectionx/signals");
|
|
6
|
-
/**
|
|
7
|
-
* Creates a stream that can be used to test the behavior of streams that use backpressure.
|
|
8
|
-
* It's useful in tests where it can be used as a source stream. This function is used to create
|
|
9
|
-
* the stream.
|
|
10
|
-
*
|
|
11
|
-
* The returned stream has `pour` method that can be used to send items to the stream.
|
|
12
|
-
* It can accept an array of items or a generator function that will be called with a function
|
|
13
|
-
* to send items to the stream.
|
|
14
|
-
*
|
|
15
|
-
* ```typescript
|
|
16
|
-
* import { useFaucet } from "@effectionx/stream-helpers/test-helpers";
|
|
17
|
-
* import { run, each, spawn } from "effection";
|
|
18
|
-
*
|
|
19
|
-
* await run(function* () {
|
|
20
|
-
* const faucet = yield* useFaucet({ open: true });
|
|
21
|
-
*
|
|
22
|
-
* // Remember to spawn the stream subscription before sending items to the stream
|
|
23
|
-
* yield* spawn(function* () {
|
|
24
|
-
* for (let i of yield* each(faucet)) {
|
|
25
|
-
* console.log(i);
|
|
26
|
-
* yield* each.next();
|
|
27
|
-
* }
|
|
28
|
-
* });
|
|
29
|
-
*
|
|
30
|
-
* // Pass an array of items to send items to the stream one at a time synchronously
|
|
31
|
-
* yield* faucet.pour([1, 2, 3]);
|
|
32
|
-
*
|
|
33
|
-
* // Pass an operation to control the rate at which items are sent to the stream
|
|
34
|
-
* yield* faucet.pour(function* (send) {
|
|
35
|
-
* yield* sleep(10);
|
|
36
|
-
* send(5);
|
|
37
|
-
* yield* sleep(30);
|
|
38
|
-
* send(6);
|
|
39
|
-
* yield* sleep(10);
|
|
40
|
-
* send(7);
|
|
41
|
-
* });
|
|
42
|
-
* });
|
|
43
|
-
*
|
|
44
|
-
* ```
|
|
45
|
-
* @param options - The options for the faucet.
|
|
46
|
-
* @param options.open - Whether the faucet is open.
|
|
47
|
-
* @returns stream of items coming from the faucet
|
|
48
|
-
*/
|
|
49
|
-
function* useFaucet(options) {
|
|
50
|
-
let signal = (0, effection_1.createChannel)();
|
|
51
|
-
let open = yield* (0, signals_1.createBooleanSignal)(options.open);
|
|
52
|
-
return {
|
|
53
|
-
[Symbol.iterator]: signal[Symbol.iterator],
|
|
54
|
-
*pour(items) {
|
|
55
|
-
if (Array.isArray(items)) {
|
|
56
|
-
for (let i of items) {
|
|
57
|
-
yield* (0, signals_1.is)(open, (open) => open);
|
|
58
|
-
yield* signal.send(i);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
else {
|
|
62
|
-
yield* items(function* (item) {
|
|
63
|
-
yield* (0, signals_1.is)(open, (open) => open);
|
|
64
|
-
yield* signal.send(item);
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
},
|
|
68
|
-
close() {
|
|
69
|
-
open.set(false);
|
|
70
|
-
},
|
|
71
|
-
open() {
|
|
72
|
-
open.set(true);
|
|
73
|
-
},
|
|
74
|
-
};
|
|
75
|
-
}
|
package/script/test-helpers.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"test-helpers.d.ts","sourceRoot":"","sources":["../src/test-helpers.ts"],"names":[],"mappings":"AAAA,cAAc,0BAA0B,CAAC"}
|
package/script/test-helpers.js
DELETED
|
@@ -1,17 +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("./test-helpers/faucet.js"), exports);
|
package/script/tracker.d.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
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
|
package/script/tracker.d.ts.map
DELETED
|
@@ -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"}
|
package/script/tracker.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createTracker = createTracker;
|
|
4
|
-
const effection_1 = require("effection");
|
|
5
|
-
const signals_1 = require("@effectionx/signals");
|
|
6
|
-
/**
|
|
7
|
-
* Creates a tracker that can be used to verify that all items that entered the stream
|
|
8
|
-
* eventually exit the stream. This is helpful when you want to ensure that all items
|
|
9
|
-
* were processed before terminating the operation that created the stream.
|
|
10
|
-
*/
|
|
11
|
-
function createTracker() {
|
|
12
|
-
return (0, effection_1.resource)(function* (provide) {
|
|
13
|
-
const tracked = yield* (0, signals_1.createSetSignal)();
|
|
14
|
-
yield* provide({
|
|
15
|
-
*[Symbol.iterator]() {
|
|
16
|
-
yield* (0, signals_1.is)(tracked, (set) => set.size === 0);
|
|
17
|
-
},
|
|
18
|
-
passthrough() {
|
|
19
|
-
return function (stream) {
|
|
20
|
-
return {
|
|
21
|
-
*[Symbol.iterator]() {
|
|
22
|
-
const subscription = yield* stream;
|
|
23
|
-
return {
|
|
24
|
-
*next() {
|
|
25
|
-
const next = yield* subscription.next();
|
|
26
|
-
tracked.add(next.value);
|
|
27
|
-
return next;
|
|
28
|
-
},
|
|
29
|
-
};
|
|
30
|
-
},
|
|
31
|
-
};
|
|
32
|
-
};
|
|
33
|
-
},
|
|
34
|
-
markOne(item) {
|
|
35
|
-
tracked.delete(item);
|
|
36
|
-
},
|
|
37
|
-
markMany(items) {
|
|
38
|
-
tracked.difference(items);
|
|
39
|
-
},
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
}
|
package/script/valve.d.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { type Operation, type Stream } from "effection";
|
|
2
|
-
/**
|
|
3
|
-
* Options for the valve.
|
|
4
|
-
*/
|
|
5
|
-
export interface ValveOptions {
|
|
6
|
-
/**
|
|
7
|
-
* The buffer size when {@link ValveOptions#open} method is called.
|
|
8
|
-
*/
|
|
9
|
-
openAt: number;
|
|
10
|
-
/**
|
|
11
|
-
* The buffer size when {@link ValveOptions#close} method is called.
|
|
12
|
-
*/
|
|
13
|
-
closeAt: number;
|
|
14
|
-
/**
|
|
15
|
-
* The operation to resume the upstream.
|
|
16
|
-
*/
|
|
17
|
-
open(): Operation<void>;
|
|
18
|
-
/**
|
|
19
|
-
* The operation to pause the upstream.
|
|
20
|
-
*/
|
|
21
|
-
close(): Operation<void>;
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* This function buffers incoming items, if the upstream is producing faster than the downstream
|
|
25
|
-
* can consume, the buffer will grow. If the buffer size exceeds the `closeAt` threshold, the
|
|
26
|
-
* `close` operation will be called which is expected to pause the upstream. The buffer will
|
|
27
|
-
* drain until the buffer size is less than the `openAt` threshold, at which point the `open`
|
|
28
|
-
* operation will be called to resume the upstream.
|
|
29
|
-
*
|
|
30
|
-
* @param options.open - The operation to resume the upstream.
|
|
31
|
-
* @param options.openAt - The buffer size at which the upstream will be resumed.
|
|
32
|
-
* @param options.close - The operation to pause the upstream.
|
|
33
|
-
* @param options.closeAt - The buffer size at which the upstream will be paused.
|
|
34
|
-
* @returns A stream with backpressure applied.
|
|
35
|
-
*/
|
|
36
|
-
export declare function valve(options: ValveOptions): <T>(stream: Stream<T, never>) => Stream<T, never>;
|
|
37
|
-
//# sourceMappingURL=valve.d.ts.map
|
package/script/valve.d.ts.map
DELETED
|
@@ -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/script/valve.js
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.valve = valve;
|
|
4
|
-
const effection_1 = require("effection");
|
|
5
|
-
const signals_1 = require("@effectionx/signals");
|
|
6
|
-
/**
|
|
7
|
-
* This function buffers incoming items, if the upstream is producing faster than the downstream
|
|
8
|
-
* can consume, the buffer will grow. If the buffer size exceeds the `closeAt` threshold, the
|
|
9
|
-
* `close` operation will be called which is expected to pause the upstream. The buffer will
|
|
10
|
-
* drain until the buffer size is less than the `openAt` threshold, at which point the `open`
|
|
11
|
-
* operation will be called to resume the upstream.
|
|
12
|
-
*
|
|
13
|
-
* @param options.open - The operation to resume the upstream.
|
|
14
|
-
* @param options.openAt - The buffer size at which the upstream will be resumed.
|
|
15
|
-
* @param options.close - The operation to pause the upstream.
|
|
16
|
-
* @param options.closeAt - The buffer size at which the upstream will be paused.
|
|
17
|
-
* @returns A stream with backpressure applied.
|
|
18
|
-
*/
|
|
19
|
-
function valve(options) {
|
|
20
|
-
return function (stream) {
|
|
21
|
-
return {
|
|
22
|
-
*[Symbol.iterator]() {
|
|
23
|
-
const buffer = yield* (0, signals_1.createArraySignal)([]);
|
|
24
|
-
let open = true;
|
|
25
|
-
yield* (0, effection_1.spawn)(function* () {
|
|
26
|
-
for (const item of yield* (0, effection_1.each)(stream)) {
|
|
27
|
-
buffer.push(item);
|
|
28
|
-
if (open && buffer.length >= options.closeAt) {
|
|
29
|
-
yield* options.close();
|
|
30
|
-
open = false;
|
|
31
|
-
}
|
|
32
|
-
yield* effection_1.each.next();
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
return {
|
|
36
|
-
*next() {
|
|
37
|
-
if (!open && buffer.length <= options.openAt) {
|
|
38
|
-
yield* options.open();
|
|
39
|
-
open = true;
|
|
40
|
-
}
|
|
41
|
-
const value = yield* buffer.shift();
|
|
42
|
-
return {
|
|
43
|
-
done: false,
|
|
44
|
-
value,
|
|
45
|
-
};
|
|
46
|
-
},
|
|
47
|
-
};
|
|
48
|
-
},
|
|
49
|
-
};
|
|
50
|
-
};
|
|
51
|
-
}
|
/package/{esm → dist}/batch.d.ts
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
/package/{esm → dist}/map.d.ts
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
/package/{esm → dist}/valve.d.ts
RENAMED
|
File without changes
|