@dxos/codec-protobuf 2.33.9-dev.8e92e630 → 2.33.9-dev.9246a07b

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/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
  */