@graffy/stream 0.15.25-alpha.3 → 0.15.25-alpha.5

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 (2) hide show
  1. package/package.json +2 -7
  2. package/index.cjs +0 -84
package/package.json CHANGED
@@ -2,13 +2,8 @@
2
2
  "name": "@graffy/stream",
3
3
  "description": "Utility for creating AsyncIterables (streams) from any callback-based",
4
4
  "author": "aravind (https://github.com/aravindet)",
5
- "version": "0.15.25-alpha.3",
6
- "main": "./index.cjs",
7
- "exports": {
8
- "import": "./index.mjs",
9
- "require": "./index.cjs"
10
- },
11
- "module": "./index.mjs",
5
+ "version": "0.15.25-alpha.5",
6
+ "main": "./index.mjs",
12
7
  "types": "./types/index.d.ts",
13
8
  "repository": {
14
9
  "type": "git",
package/index.cjs DELETED
@@ -1,84 +0,0 @@
1
- "use strict";
2
- Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
3
- const normalCompletion = Promise.resolve({ value: void 0, done: true });
4
- function makeStream(init, options = {}) {
5
- const payloads = [];
6
- const requests = [];
7
- let complete;
8
- let drain;
9
- const { highWatermark = Infinity, lowWatermark = 0, debugId } = options;
10
- const push = (value) => {
11
- if (complete)
12
- return;
13
- const payload = Promise.resolve({ value, done: false });
14
- if (requests.length)
15
- return requests.shift()(payload);
16
- payloads.push(payload);
17
- if (payloads.length >= highWatermark) {
18
- return new Promise((resolve) => {
19
- drain = resolve;
20
- });
21
- }
22
- };
23
- const end = (error) => {
24
- complete = error ? Promise.reject(error) : normalCompletion;
25
- let resolve;
26
- while (resolve = requests.shift())
27
- resolve(complete);
28
- };
29
- let close;
30
- let initialized = false;
31
- return {
32
- debugId,
33
- next: () => {
34
- if (!initialized) {
35
- close = init(push, end);
36
- initialized = true;
37
- }
38
- if (drain && payloads.length <= lowWatermark) {
39
- drain();
40
- drain = null;
41
- }
42
- if (payloads.length)
43
- return payloads.shift();
44
- if (complete)
45
- return complete;
46
- return new Promise((resolve) => requests.push(resolve));
47
- },
48
- return(value) {
49
- complete = Promise.resolve({ value, done: true });
50
- payloads.length = 0;
51
- if (close)
52
- close(null, value);
53
- return complete;
54
- },
55
- throw(error) {
56
- complete = Promise.reject(error);
57
- payloads.length = 0;
58
- if (close)
59
- close(error);
60
- return complete;
61
- },
62
- [Symbol.asyncIterator]() {
63
- return this;
64
- }
65
- };
66
- }
67
- function mapStream(stream, fn) {
68
- return makeStream((push, end) => {
69
- const next = () => {
70
- stream.next().then(({ value, done }) => {
71
- if (done)
72
- return end();
73
- push(fn(value));
74
- next();
75
- }).catch((error) => end(error));
76
- };
77
- next();
78
- return (error, value) => {
79
- error ? stream.throw(error) : stream.return(value);
80
- };
81
- });
82
- }
83
- exports.makeStream = makeStream;
84
- exports.mapStream = mapStream;