@dxos/codec-protobuf 2.33.9-dev.d0ae5f95 → 2.33.9-dev.d0dce35f
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 +39 -0
- package/dist/browser.js +128 -58
- package/dist/src/precompiled-mapping/create-message-mapper.js +43 -2
- package/dist/src/precompiled-mapping/create-message-mapper.js.map +1 -1
- package/dist/src/service.d.ts +2 -2
- package/dist/src/service.d.ts.map +1 -1
- package/dist/src/service.js +8 -14
- package/dist/src/service.js.map +1 -1
- package/dist/src/stream.d.ts +31 -0
- package/dist/src/stream.d.ts.map +1 -1
- package/dist/src/stream.js +45 -0
- package/dist/src/stream.js.map +1 -1
- package/dist/src/stream.test.js +3 -1
- package/dist/src/stream.test.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/precompiled-mapping/create-message-mapper.ts +40 -2
- package/src/service.ts +11 -18
- package/src/stream.test.ts +3 -1
- package/src/stream.ts +71 -0
package/src/stream.test.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import { Stream } from './stream';
|
|
6
6
|
|
|
7
7
|
describe('Stream', () => {
|
|
8
|
-
test('can consume a stream that immediately closes', async () => {
|
|
8
|
+
test.only('can consume a stream that immediately closes', async () => {
|
|
9
9
|
const stream = new Stream(({ next, close }) => {
|
|
10
10
|
next('foo');
|
|
11
11
|
next('bar');
|
|
@@ -14,6 +14,7 @@ describe('Stream', () => {
|
|
|
14
14
|
});
|
|
15
15
|
|
|
16
16
|
expect(await Stream.consume(stream)).toEqual([
|
|
17
|
+
{ ready: true },
|
|
17
18
|
{ data: 'foo' },
|
|
18
19
|
{ data: 'bar' },
|
|
19
20
|
{ data: 'baz' },
|
|
@@ -36,6 +37,7 @@ describe('Stream', () => {
|
|
|
36
37
|
});
|
|
37
38
|
|
|
38
39
|
expect(await Stream.consume(stream)).toEqual([
|
|
40
|
+
{ ready: true },
|
|
39
41
|
{ data: 'foo' },
|
|
40
42
|
{ data: 'bar' },
|
|
41
43
|
{ data: 'baz' },
|
package/src/stream.ts
CHANGED
|
@@ -8,11 +8,26 @@ import assert from 'node:assert';
|
|
|
8
8
|
const log = debug('dxos:codec-protobuf:stream');
|
|
9
9
|
|
|
10
10
|
type Producer<T> = (callbacks: {
|
|
11
|
+
/**
|
|
12
|
+
* Advises that the producer is ready to stream the data.
|
|
13
|
+
* Called automatically with the first call to `next`.
|
|
14
|
+
*/
|
|
15
|
+
ready: () => void
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Sends a message into the stream.
|
|
19
|
+
*/
|
|
11
20
|
next: (message: T) => void
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Closes the stream.
|
|
24
|
+
* Optional error can be provided.
|
|
25
|
+
*/
|
|
12
26
|
close: (error?: Error) => void
|
|
13
27
|
}) => (() => void) | void
|
|
14
28
|
|
|
15
29
|
export type StreamItem<T> =
|
|
30
|
+
| { ready: true }
|
|
16
31
|
| { data: T }
|
|
17
32
|
| { closed: true, error?: Error }
|
|
18
33
|
|
|
@@ -31,6 +46,9 @@ export class Stream<T> {
|
|
|
31
46
|
return new Promise(resolve => {
|
|
32
47
|
const items: StreamItem<T>[] = [];
|
|
33
48
|
|
|
49
|
+
stream.onReady(() => {
|
|
50
|
+
items.push({ ready: true });
|
|
51
|
+
});
|
|
34
52
|
stream.subscribe(
|
|
35
53
|
data => {
|
|
36
54
|
items.push({ data });
|
|
@@ -47,12 +65,28 @@ export class Stream<T> {
|
|
|
47
65
|
});
|
|
48
66
|
}
|
|
49
67
|
|
|
68
|
+
/**
|
|
69
|
+
* Maps all data coming through the stream.
|
|
70
|
+
*/
|
|
71
|
+
static map<T, U> (source: Stream<T>, map: (data: T) => U): Stream<U> {
|
|
72
|
+
return new Stream(({ ready, next, close }) => {
|
|
73
|
+
source.onReady(ready);
|
|
74
|
+
source.subscribe(data => next(map(data)), close);
|
|
75
|
+
|
|
76
|
+
return () => source.close();
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
50
80
|
private _messageHandler?: (msg: T) => void;
|
|
51
81
|
private _closeHandler?: (error?: Error) => void;
|
|
82
|
+
private _readyHandler?: () => void;
|
|
52
83
|
|
|
53
84
|
private _isClosed = false;
|
|
54
85
|
private _closeError: Error | undefined;
|
|
55
86
|
private _dispose: (() => void) | undefined;
|
|
87
|
+
private _readyPromise: Promise<void>;
|
|
88
|
+
private _resolveReadyPromise!: () => void;
|
|
89
|
+
private _isReady = false;
|
|
56
90
|
|
|
57
91
|
/**
|
|
58
92
|
* Buffer messages before subscription. Set to null when buffer is no longer needed.
|
|
@@ -60,13 +94,23 @@ export class Stream<T> {
|
|
|
60
94
|
private _buffer: T[] | null = [];
|
|
61
95
|
|
|
62
96
|
constructor (producer: Producer<T>) {
|
|
97
|
+
this._readyPromise = new Promise(resolve => {
|
|
98
|
+
this._resolveReadyPromise = resolve;
|
|
99
|
+
});
|
|
100
|
+
|
|
63
101
|
const disposeCallback = producer({
|
|
102
|
+
ready: () => {
|
|
103
|
+
this._markAsReady();
|
|
104
|
+
},
|
|
105
|
+
|
|
64
106
|
next: msg => {
|
|
65
107
|
if (this._isClosed) {
|
|
66
108
|
log('Stream is closed, dropping message.');
|
|
67
109
|
return;
|
|
68
110
|
}
|
|
69
111
|
|
|
112
|
+
this._markAsReady();
|
|
113
|
+
|
|
70
114
|
if (this._messageHandler) {
|
|
71
115
|
try {
|
|
72
116
|
this._messageHandler(msg);
|
|
@@ -102,6 +146,14 @@ export class Stream<T> {
|
|
|
102
146
|
}
|
|
103
147
|
}
|
|
104
148
|
|
|
149
|
+
private _markAsReady () {
|
|
150
|
+
if (!this._isReady) {
|
|
151
|
+
this._isReady = true;
|
|
152
|
+
this._readyHandler?.();
|
|
153
|
+
this._resolveReadyPromise();
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
105
157
|
subscribe (onMessage: (msg: T) => void, onClose?: (error?: Error) => void) {
|
|
106
158
|
assert(!this._messageHandler, 'Stream is already subscribed to.');
|
|
107
159
|
assert(!this._closeHandler, 'Stream is already subscribed to.');
|
|
@@ -128,6 +180,25 @@ export class Stream<T> {
|
|
|
128
180
|
this._closeHandler = onClose;
|
|
129
181
|
}
|
|
130
182
|
|
|
183
|
+
/**
|
|
184
|
+
* Resolves when stream is ready.
|
|
185
|
+
*/
|
|
186
|
+
waitUntilReady (): Promise<void> {
|
|
187
|
+
return this._readyPromise;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Registers a callback to be called when stream is ready.
|
|
192
|
+
*/
|
|
193
|
+
onReady (onReady: () => void): void {
|
|
194
|
+
assert(!this._readyHandler, 'Stream already has a handler for the ready event.');
|
|
195
|
+
this._readyHandler = onReady;
|
|
196
|
+
|
|
197
|
+
if (this._isReady) {
|
|
198
|
+
onReady();
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
131
202
|
/**
|
|
132
203
|
* Close the stream and dispose of any resources.
|
|
133
204
|
*/
|