@celhive/tool.js 0.0.7 → 0.0.9
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/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/stable-streaming.d.ts +18 -0
- package/lib/stable-streaming.js +51 -0
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -18,4 +18,5 @@ __exportStar(require("./formatCitation"), exports);
|
|
|
18
18
|
__exportStar(require("./get-pdf-viewer-urll"), exports);
|
|
19
19
|
__exportStar(require("./markdown-patch"), exports);
|
|
20
20
|
__exportStar(require("./mock-stream"), exports);
|
|
21
|
+
__exportStar(require("./stable-streaming"), exports);
|
|
21
22
|
__exportStar(require("./token-exceeded"), exports);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
interface stableStreamingProps {
|
|
2
|
+
/** Number of characters output per millisecond */
|
|
3
|
+
ratePerMs: number;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* StableStreaming class to stream characters at a stable rate.
|
|
7
|
+
* It ensures that characters are output at the specified rate per millisecond,
|
|
8
|
+
*/
|
|
9
|
+
export declare class StableStreaming {
|
|
10
|
+
private options;
|
|
11
|
+
private queue;
|
|
12
|
+
private timer;
|
|
13
|
+
private pendingResolvers;
|
|
14
|
+
constructor(options: stableStreamingProps);
|
|
15
|
+
streaming(chars: string, callback: (char: string) => void): Promise<void>;
|
|
16
|
+
private start;
|
|
17
|
+
}
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StableStreaming = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* StableStreaming class to stream characters at a stable rate.
|
|
6
|
+
* It ensures that characters are output at the specified rate per millisecond,
|
|
7
|
+
*/
|
|
8
|
+
class StableStreaming {
|
|
9
|
+
constructor(options) {
|
|
10
|
+
this.queue = [];
|
|
11
|
+
this.timer = null;
|
|
12
|
+
this.pendingResolvers = [];
|
|
13
|
+
if (!options || options.ratePerMs <= 0) {
|
|
14
|
+
throw new Error('ratePerMs must be greater than 0');
|
|
15
|
+
}
|
|
16
|
+
this.options = options;
|
|
17
|
+
}
|
|
18
|
+
streaming(chars, callback) {
|
|
19
|
+
if (!chars)
|
|
20
|
+
return Promise.resolve();
|
|
21
|
+
this.queue.push(...chars.split(''));
|
|
22
|
+
return new Promise((resolve) => {
|
|
23
|
+
this.pendingResolvers.push(resolve);
|
|
24
|
+
if (!this.timer) {
|
|
25
|
+
this.start(callback);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
start(callback) {
|
|
30
|
+
const charsPerTick = Math.max(1, Math.floor(this.options.ratePerMs));
|
|
31
|
+
this.timer = setInterval(() => {
|
|
32
|
+
let remaining = charsPerTick;
|
|
33
|
+
while (remaining > 0 && this.queue.length) {
|
|
34
|
+
const char = this.queue.shift();
|
|
35
|
+
if (char !== undefined) {
|
|
36
|
+
callback(char);
|
|
37
|
+
}
|
|
38
|
+
remaining -= 1;
|
|
39
|
+
}
|
|
40
|
+
if (!this.queue.length) {
|
|
41
|
+
if (this.timer) {
|
|
42
|
+
clearInterval(this.timer);
|
|
43
|
+
this.timer = null;
|
|
44
|
+
}
|
|
45
|
+
this.pendingResolvers.forEach((resolver) => resolver());
|
|
46
|
+
this.pendingResolvers = [];
|
|
47
|
+
}
|
|
48
|
+
}, 1);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
exports.StableStreaming = StableStreaming;
|