@fluidframework/driver-utils 2.0.0-internal.3.0.5 → 2.0.0-internal.3.1.1
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/.eslintrc.js +17 -19
- package/.mocharc.js +2 -2
- package/api-extractor.json +2 -2
- package/dist/blobAggregationStorage.d.ts +2 -2
- package/dist/blobAggregationStorage.d.ts.map +1 -1
- package/dist/blobAggregationStorage.js +11 -6
- package/dist/blobAggregationStorage.js.map +1 -1
- package/dist/blobCacheStorageService.d.ts.map +1 -1
- package/dist/blobCacheStorageService.js.map +1 -1
- package/dist/buildSnapshotTree.d.ts.map +1 -1
- package/dist/buildSnapshotTree.js.map +1 -1
- package/dist/documentStorageServiceProxy.d.ts.map +1 -1
- package/dist/documentStorageServiceProxy.js.map +1 -1
- package/dist/emptyDocumentDeltaStorageService.d.ts.map +1 -1
- package/dist/emptyDocumentDeltaStorageService.js.map +1 -1
- package/dist/error.d.ts.map +1 -1
- package/dist/error.js.map +1 -1
- package/dist/fluidResolvedUrl.d.ts.map +1 -1
- package/dist/fluidResolvedUrl.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/insecureUrlResolver.d.ts.map +1 -1
- package/dist/insecureUrlResolver.js +2 -1
- package/dist/insecureUrlResolver.js.map +1 -1
- package/dist/mapWithExpiration.d.ts.map +1 -1
- package/dist/mapWithExpiration.js +5 -3
- package/dist/mapWithExpiration.js.map +1 -1
- package/dist/messageRecognition.d.ts.map +1 -1
- package/dist/messageRecognition.js.map +1 -1
- package/dist/multiDocumentServiceFactory.d.ts.map +1 -1
- package/dist/multiDocumentServiceFactory.js.map +1 -1
- package/dist/multiUrlResolver.d.ts.map +1 -1
- package/dist/multiUrlResolver.js.map +1 -1
- package/dist/network.d.ts +1 -1
- package/dist/network.d.ts.map +1 -1
- package/dist/network.js +4 -3
- package/dist/network.js.map +1 -1
- package/dist/networkUtils.d.ts.map +1 -1
- package/dist/networkUtils.js +2 -3
- package/dist/networkUtils.js.map +1 -1
- package/dist/packageVersion.d.ts +1 -1
- package/dist/packageVersion.js +1 -1
- package/dist/packageVersion.js.map +1 -1
- package/dist/parallelRequests.d.ts.map +1 -1
- package/dist/parallelRequests.js +19 -9
- package/dist/parallelRequests.js.map +1 -1
- package/dist/prefetchDocumentStorageService.d.ts.map +1 -1
- package/dist/prefetchDocumentStorageService.js.map +1 -1
- package/dist/rateLimiter.d.ts.map +1 -1
- package/dist/rateLimiter.js.map +1 -1
- package/dist/readAndParse.d.ts.map +1 -1
- package/dist/readAndParse.js.map +1 -1
- package/dist/runWithRetry.d.ts.map +1 -1
- package/dist/runWithRetry.js.map +1 -1
- package/dist/summaryForCreateNew.d.ts.map +1 -1
- package/dist/summaryForCreateNew.js.map +1 -1
- package/dist/treeConversions.d.ts.map +1 -1
- package/dist/treeConversions.js +1 -4
- package/dist/treeConversions.js.map +1 -1
- package/dist/treeUtils.d.ts +10 -10
- package/dist/treeUtils.d.ts.map +1 -1
- package/dist/treeUtils.js +10 -10
- package/dist/treeUtils.js.map +1 -1
- package/lib/blobAggregationStorage.d.ts +2 -2
- package/lib/blobAggregationStorage.d.ts.map +1 -1
- package/lib/blobAggregationStorage.js +11 -6
- package/lib/blobAggregationStorage.js.map +1 -1
- package/lib/blobCacheStorageService.d.ts.map +1 -1
- package/lib/blobCacheStorageService.js.map +1 -1
- package/lib/buildSnapshotTree.d.ts.map +1 -1
- package/lib/buildSnapshotTree.js.map +1 -1
- package/lib/documentStorageServiceProxy.d.ts.map +1 -1
- package/lib/documentStorageServiceProxy.js.map +1 -1
- package/lib/emptyDocumentDeltaStorageService.d.ts.map +1 -1
- package/lib/emptyDocumentDeltaStorageService.js.map +1 -1
- package/lib/error.d.ts.map +1 -1
- package/lib/error.js.map +1 -1
- package/lib/fluidResolvedUrl.d.ts.map +1 -1
- package/lib/fluidResolvedUrl.js.map +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -1
- package/lib/index.js.map +1 -1
- package/lib/insecureUrlResolver.d.ts.map +1 -1
- package/lib/insecureUrlResolver.js +2 -1
- package/lib/insecureUrlResolver.js.map +1 -1
- package/lib/mapWithExpiration.d.ts.map +1 -1
- package/lib/mapWithExpiration.js +5 -3
- package/lib/mapWithExpiration.js.map +1 -1
- package/lib/messageRecognition.d.ts.map +1 -1
- package/lib/messageRecognition.js +1 -1
- package/lib/messageRecognition.js.map +1 -1
- package/lib/multiDocumentServiceFactory.d.ts.map +1 -1
- package/lib/multiDocumentServiceFactory.js.map +1 -1
- package/lib/multiUrlResolver.d.ts.map +1 -1
- package/lib/multiUrlResolver.js.map +1 -1
- package/lib/network.d.ts +1 -1
- package/lib/network.d.ts.map +1 -1
- package/lib/network.js +4 -3
- package/lib/network.js.map +1 -1
- package/lib/networkUtils.d.ts.map +1 -1
- package/lib/networkUtils.js +2 -3
- package/lib/networkUtils.js.map +1 -1
- package/lib/packageVersion.d.ts +1 -1
- package/lib/packageVersion.js +1 -1
- package/lib/packageVersion.js.map +1 -1
- package/lib/parallelRequests.d.ts.map +1 -1
- package/lib/parallelRequests.js +19 -9
- package/lib/parallelRequests.js.map +1 -1
- package/lib/prefetchDocumentStorageService.d.ts.map +1 -1
- package/lib/prefetchDocumentStorageService.js.map +1 -1
- package/lib/rateLimiter.d.ts.map +1 -1
- package/lib/rateLimiter.js.map +1 -1
- package/lib/readAndParse.d.ts.map +1 -1
- package/lib/readAndParse.js.map +1 -1
- package/lib/runWithRetry.d.ts.map +1 -1
- package/lib/runWithRetry.js.map +1 -1
- package/lib/summaryForCreateNew.d.ts.map +1 -1
- package/lib/summaryForCreateNew.js.map +1 -1
- package/lib/treeConversions.d.ts.map +1 -1
- package/lib/treeConversions.js +4 -7
- package/lib/treeConversions.js.map +1 -1
- package/lib/treeUtils.d.ts +10 -10
- package/lib/treeUtils.d.ts.map +1 -1
- package/lib/treeUtils.js +11 -11
- package/lib/treeUtils.js.map +1 -1
- package/package.json +128 -127
- package/prettier.config.cjs +1 -1
- package/src/blobAggregationStorage.ts +374 -322
- package/src/blobCacheStorageService.ts +20 -17
- package/src/buildSnapshotTree.ts +54 -51
- package/src/documentStorageServiceProxy.ts +49 -43
- package/src/emptyDocumentDeltaStorageService.ts +11 -10
- package/src/error.ts +5 -7
- package/src/fluidResolvedUrl.ts +9 -6
- package/src/index.ts +5 -1
- package/src/insecureUrlResolver.ts +127 -116
- package/src/mapWithExpiration.ts +111 -104
- package/src/messageRecognition.ts +25 -19
- package/src/multiDocumentServiceFactory.ts +73 -62
- package/src/multiUrlResolver.ts +26 -29
- package/src/network.ts +114 -112
- package/src/networkUtils.ts +37 -34
- package/src/packageVersion.ts +1 -1
- package/src/parallelRequests.ts +571 -509
- package/src/prefetchDocumentStorageService.ts +76 -74
- package/src/rateLimiter.ts +29 -29
- package/src/readAndParse.ts +7 -4
- package/src/runWithRetry.ts +105 -94
- package/src/summaryForCreateNew.ts +27 -24
- package/src/treeConversions.ts +48 -70
- package/src/treeUtils.ts +70 -74
- package/tsconfig.esnext.json +6 -6
- package/tsconfig.json +8 -12
package/src/parallelRequests.ts
CHANGED
|
@@ -30,274 +30,307 @@ type WorkingState = "working" | "done" | "canceled";
|
|
|
30
30
|
* @returns - Queue that can be used to retrieve data
|
|
31
31
|
*/
|
|
32
32
|
export class ParallelRequests<T> {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
33
|
+
private latestRequested: number;
|
|
34
|
+
private nextToDeliver: number;
|
|
35
|
+
private readonly results: Map<number, T[]> = new Map();
|
|
36
|
+
private workingState: WorkingState = "working";
|
|
37
|
+
private requestsInFlight = 0;
|
|
38
|
+
private readonly endEvent = new Deferred<void>();
|
|
39
|
+
private requests = 0;
|
|
40
|
+
private readonly knewTo: boolean;
|
|
41
|
+
|
|
42
|
+
private get working() {
|
|
43
|
+
return this.workingState === "working";
|
|
44
|
+
}
|
|
45
|
+
public get canceled() {
|
|
46
|
+
return this.workingState === "canceled";
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
constructor(
|
|
50
|
+
from: number,
|
|
51
|
+
private to: number | undefined,
|
|
52
|
+
private readonly payloadSize: number,
|
|
53
|
+
private readonly logger: ITelemetryLogger,
|
|
54
|
+
private readonly requestCallback: (
|
|
55
|
+
request: number,
|
|
56
|
+
from: number,
|
|
57
|
+
to: number,
|
|
58
|
+
strongTo: boolean,
|
|
59
|
+
props: ITelemetryProperties,
|
|
60
|
+
) => Promise<{ partial: boolean; cancel: boolean; payload: T[] }>,
|
|
61
|
+
private readonly responseCallback: (payload: T[]) => void,
|
|
62
|
+
) {
|
|
63
|
+
this.latestRequested = from;
|
|
64
|
+
this.nextToDeliver = from;
|
|
65
|
+
this.knewTo = to !== undefined;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
public cancel() {
|
|
69
|
+
if (this.working) {
|
|
70
|
+
this.workingState = "canceled";
|
|
71
|
+
this.endEvent.resolve();
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
public async run(concurrency: number) {
|
|
76
|
+
assert(concurrency > 0, 0x102 /* "invalid level of concurrency" */);
|
|
77
|
+
assert(this.working, 0x103 /* "trying to parallel run while not working" */);
|
|
78
|
+
|
|
79
|
+
let c = concurrency;
|
|
80
|
+
while (c > 0) {
|
|
81
|
+
c--;
|
|
82
|
+
this.addRequest();
|
|
83
|
+
}
|
|
84
|
+
this.dispatch(); // will recalculate and trigger this.endEvent if needed
|
|
85
|
+
return this.endEvent.promise;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
private done() {
|
|
89
|
+
// We should satisfy request fully.
|
|
90
|
+
assert(this.to !== undefined, 0x104 /* "undefined end point for parallel fetch" */);
|
|
91
|
+
assert(
|
|
92
|
+
this.nextToDeliver >= this.to,
|
|
93
|
+
0x105 /* "unexpected end point for parallel fetch" */,
|
|
94
|
+
);
|
|
95
|
+
if (this.working) {
|
|
96
|
+
this.workingState = "done";
|
|
97
|
+
this.endEvent.resolve();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
private fail(error) {
|
|
102
|
+
if (this.working) {
|
|
103
|
+
this.workingState = "done";
|
|
104
|
+
this.endEvent.reject(error);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
private dispatch() {
|
|
109
|
+
while (this.working) {
|
|
110
|
+
const value = this.results.get(this.nextToDeliver);
|
|
111
|
+
if (value === undefined) {
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
this.results.delete(this.nextToDeliver);
|
|
115
|
+
assert(
|
|
116
|
+
value.length <= this.payloadSize,
|
|
117
|
+
0x1d9 /* "addRequestCore() should break into smaller chunks" */,
|
|
118
|
+
);
|
|
119
|
+
this.nextToDeliver += value.length;
|
|
120
|
+
this.responseCallback(value);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Account for cancellation - state might be not in consistent state on cancelling operation
|
|
124
|
+
if (this.working) {
|
|
125
|
+
if (this.requestsInFlight === 0) {
|
|
126
|
+
// we should have dispatched everything, no matter whether we knew about the end or not.
|
|
127
|
+
// see comment in addRequestCore() around throwing away chunk if it's above this.to
|
|
128
|
+
assert(
|
|
129
|
+
this.results.size === 0,
|
|
130
|
+
0x107 /* "ending dispatch with remaining results to be sent" */,
|
|
131
|
+
);
|
|
132
|
+
this.done();
|
|
133
|
+
} else if (this.to !== undefined && this.nextToDeliver >= this.to) {
|
|
134
|
+
// Learned about the end and dispatched all the ops up to it.
|
|
135
|
+
// Ignore all the in-flight requests above boundary - unblock caller sooner.
|
|
136
|
+
assert(
|
|
137
|
+
!this.knewTo,
|
|
138
|
+
0x108 /* "ending results dispatch but knew in advance about more requests" */,
|
|
139
|
+
);
|
|
140
|
+
this.done();
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
private getNextChunk() {
|
|
146
|
+
if (!this.working) {
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const from = this.latestRequested;
|
|
151
|
+
if (this.to !== undefined) {
|
|
152
|
+
if (this.to <= from) {
|
|
153
|
+
return undefined;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// this.latestRequested
|
|
158
|
+
// inclusive on the right side! Exclusive on the left.
|
|
159
|
+
this.latestRequested += this.payloadSize;
|
|
160
|
+
|
|
161
|
+
if (this.to !== undefined) {
|
|
162
|
+
this.latestRequested = Math.min(this.to, this.latestRequested);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
assert(from < this.latestRequested, 0x109 /* "unexpected next chunk position" */);
|
|
166
|
+
|
|
167
|
+
return { from, to: this.latestRequested };
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
private addRequest() {
|
|
171
|
+
const chunk = this.getNextChunk();
|
|
172
|
+
if (chunk === undefined) {
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
this.addRequestCore(chunk.from, chunk.to).catch(this.fail.bind(this));
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
private async addRequestCore(fromArg: number, toArg: number) {
|
|
179
|
+
assert(this.working, 0x10a /* "cannot add parallel request while not working" */);
|
|
180
|
+
|
|
181
|
+
let from = fromArg;
|
|
182
|
+
let to = toArg;
|
|
183
|
+
|
|
184
|
+
// to & from are exclusive
|
|
185
|
+
this.requestsInFlight++;
|
|
186
|
+
while (this.working) {
|
|
187
|
+
const requestedLength = to - from;
|
|
188
|
+
assert(requestedLength > 0, 0x10b /* "invalid parallel request range" */);
|
|
189
|
+
|
|
190
|
+
// We should not be wasting time asking for something useless.
|
|
191
|
+
if (this.to !== undefined) {
|
|
192
|
+
assert(from < this.to, 0x10c /* "invalid parallel request start point" */);
|
|
193
|
+
assert(to <= this.to, 0x10d /* "invalid parallel request end point" */);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
this.requests++;
|
|
197
|
+
|
|
198
|
+
const promise = this.requestCallback(
|
|
199
|
+
this.requests,
|
|
200
|
+
from,
|
|
201
|
+
to,
|
|
202
|
+
this.to !== undefined,
|
|
203
|
+
{},
|
|
204
|
+
);
|
|
205
|
+
|
|
206
|
+
// dispatch any prior received data
|
|
207
|
+
this.dispatch();
|
|
208
|
+
|
|
209
|
+
const { payload, cancel, partial } = await promise;
|
|
210
|
+
|
|
211
|
+
if (cancel) {
|
|
212
|
+
this.cancel();
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (this.to !== undefined && from >= this.to) {
|
|
216
|
+
// while we were waiting for response, we learned on what is the boundary
|
|
217
|
+
// We can get here (with actual result!) if situation changed while this request was in
|
|
218
|
+
// flight, i.e. the end was extended over what we learn in some other request
|
|
219
|
+
// While it's useful not to throw this result, this is very corner cases and makes logic
|
|
220
|
+
// (including consistency checks) much harder to write correctly.
|
|
221
|
+
// So for now, we are throwing this result out the window.
|
|
222
|
+
assert(
|
|
223
|
+
!this.knewTo,
|
|
224
|
+
0x10e /* "should not throw result if we knew about boundary in advance" */,
|
|
225
|
+
);
|
|
226
|
+
// Learn how often it happens and if it's too wasteful to throw these chunks.
|
|
227
|
+
// If it pops into our view a lot, we would need to reconsider how we approach it.
|
|
228
|
+
// Note that this is not visible to user other than potentially not hitting 100% of
|
|
229
|
+
// what we can in perf domain.
|
|
230
|
+
if (payload.length !== 0) {
|
|
231
|
+
this.logger.sendErrorEvent({
|
|
232
|
+
eventName: "ParallelRequests_GotExtra",
|
|
233
|
+
from,
|
|
234
|
+
to,
|
|
235
|
+
end: this.to,
|
|
236
|
+
length: payload.length,
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
break;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (this.working) {
|
|
244
|
+
const fromOrig = from;
|
|
245
|
+
const length = payload.length;
|
|
246
|
+
let fullChunk = requestedLength <= length; // we can possible get more than we asked.
|
|
247
|
+
|
|
248
|
+
if (length !== 0) {
|
|
249
|
+
// We can get more than we asked for!
|
|
250
|
+
// This can screw up logic in dispatch!
|
|
251
|
+
// So push only batch size, and keep the rest for later - if conditions are favorable, we
|
|
252
|
+
// will be able to use it. If not (parallel request overlapping these ops), it's easier to
|
|
253
|
+
// discard them and wait for another (overlapping) request to come in later.
|
|
254
|
+
if (requestedLength < length) {
|
|
255
|
+
// This is error in a sense that it's not expected and likely points bug in other layer.
|
|
256
|
+
// This layer copes with this situation just fine.
|
|
257
|
+
this.logger.sendTelemetryEvent({
|
|
258
|
+
eventName: "ParallelRequests_Over",
|
|
259
|
+
from,
|
|
260
|
+
to,
|
|
261
|
+
length,
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
const data = payload.splice(0, requestedLength);
|
|
265
|
+
this.results.set(from, data);
|
|
266
|
+
from += data.length;
|
|
267
|
+
} else {
|
|
268
|
+
// 1. empty (partial) chunks should not be returned by various caching / adapter layers -
|
|
269
|
+
// they should fall back to next layer. This might be important invariant to hold to ensure
|
|
270
|
+
// that we are less likely have bugs where such layer would keep returning empty partial
|
|
271
|
+
// result on each call.
|
|
272
|
+
// 2. Current invariant is that callback does retries until it gets something,
|
|
273
|
+
// with the goal of failing if zero data is retrieved in given amount of time.
|
|
274
|
+
// This is very specific property of storage / ops, so this logic is not here, but given only
|
|
275
|
+
// one user of this class, we assert that to catch issues earlier.
|
|
276
|
+
// These invariant can be relaxed if needed.
|
|
277
|
+
assert(
|
|
278
|
+
!partial,
|
|
279
|
+
0x10f /* "empty/partial chunks should not be returned by caching" */,
|
|
280
|
+
);
|
|
281
|
+
assert(
|
|
282
|
+
!this.knewTo,
|
|
283
|
+
0x110 /* "callback should retry until valid fetch before it learns new boundary" */,
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
if (!partial && !fullChunk) {
|
|
288
|
+
if (!this.knewTo) {
|
|
289
|
+
if (this.to === undefined || this.to > from) {
|
|
290
|
+
// The END
|
|
291
|
+
this.to = from;
|
|
292
|
+
}
|
|
293
|
+
break;
|
|
294
|
+
}
|
|
295
|
+
// We know that there are more items to be retrieved
|
|
296
|
+
// Can we get partial chunk? Ideally storage indicates that's not a full chunk
|
|
297
|
+
// Note that it's possible that not all ops hit storage yet.
|
|
298
|
+
// We will come back to request more, and if we can't get any more ops soon, it's
|
|
299
|
+
// catastrophic failure (see comment above on responsibility of callback to return something)
|
|
300
|
+
// This layer will just keep trying until it gets full set.
|
|
301
|
+
this.logger.sendPerformanceEvent({
|
|
302
|
+
eventName: "ParallelRequests_Partial",
|
|
303
|
+
from: fromOrig,
|
|
304
|
+
to,
|
|
305
|
+
length,
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
if (to === this.latestRequested) {
|
|
310
|
+
// we can go after full chunk at the end if we received partial chunk, or more than asked
|
|
311
|
+
// Also if we got more than we asked to, we can actually use those ops!
|
|
312
|
+
if (payload.length !== 0) {
|
|
313
|
+
this.results.set(from, payload);
|
|
314
|
+
from += payload.length;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
this.latestRequested = from;
|
|
318
|
+
fullChunk = true;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
if (fullChunk) {
|
|
322
|
+
const chunk = this.getNextChunk();
|
|
323
|
+
if (chunk === undefined) {
|
|
324
|
+
break;
|
|
325
|
+
}
|
|
326
|
+
from = chunk.from;
|
|
327
|
+
to = chunk.to;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
this.requestsInFlight--;
|
|
332
|
+
this.dispatch();
|
|
333
|
+
}
|
|
301
334
|
}
|
|
302
335
|
|
|
303
336
|
/**
|
|
@@ -305,45 +338,45 @@ export class ParallelRequests<T> {
|
|
|
305
338
|
* It's essentially a pipe allowing multiple writers, and single reader
|
|
306
339
|
*/
|
|
307
340
|
export class Queue<T> implements IStream<T> {
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
341
|
+
private readonly queue: Promise<IStreamResult<T>>[] = [];
|
|
342
|
+
private deferred: Deferred<IStreamResult<T>> | undefined;
|
|
343
|
+
private done = false;
|
|
344
|
+
|
|
345
|
+
public pushValue(value: T) {
|
|
346
|
+
this.pushCore(Promise.resolve({ done: false, value }));
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
public pushError(error: any) {
|
|
350
|
+
this.pushCore(Promise.reject(error));
|
|
351
|
+
this.done = true;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
public pushDone() {
|
|
355
|
+
this.pushCore(Promise.resolve({ done: true }));
|
|
356
|
+
this.done = true;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
protected pushCore(value: Promise<IStreamResult<T>>) {
|
|
360
|
+
assert(!this.done, 0x112 /* "cannot push onto queue if done" */);
|
|
361
|
+
if (this.deferred) {
|
|
362
|
+
assert(this.queue.length === 0, 0x113 /* "deferred queue should be empty" */);
|
|
363
|
+
this.deferred.resolve(value);
|
|
364
|
+
this.deferred = undefined;
|
|
365
|
+
} else {
|
|
366
|
+
this.queue.push(value);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
public async read(): Promise<IStreamResult<T>> {
|
|
371
|
+
assert(this.deferred === undefined, 0x114 /* "cannot pop if deferred" */);
|
|
372
|
+
const value = this.queue.shift();
|
|
373
|
+
if (value !== undefined) {
|
|
374
|
+
return value;
|
|
375
|
+
}
|
|
376
|
+
assert(!this.done, 0x115 /* "queue should not be done during pop" */);
|
|
377
|
+
this.deferred = new Deferred<IStreamResult<T>>();
|
|
378
|
+
return this.deferred.promise;
|
|
379
|
+
}
|
|
347
380
|
}
|
|
348
381
|
|
|
349
382
|
/**
|
|
@@ -360,93 +393,94 @@ export class Queue<T> implements IStream<T> {
|
|
|
360
393
|
* @returns - an object with resulting ops and cancellation / partial result flags
|
|
361
394
|
*/
|
|
362
395
|
async function getSingleOpBatch(
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
396
|
+
get: (telemetryProps: ITelemetryProperties) => Promise<IDeltasFetchResult>,
|
|
397
|
+
props: ITelemetryProperties,
|
|
398
|
+
strongTo: boolean,
|
|
399
|
+
logger: ITelemetryLogger,
|
|
400
|
+
signal?: AbortSignal,
|
|
401
|
+
scenarioName?: string,
|
|
402
|
+
): Promise<{ partial: boolean; cancel: boolean; payload: ISequencedDocumentMessage[] }> {
|
|
403
|
+
let lastSuccessTime: number | undefined;
|
|
404
|
+
|
|
405
|
+
let retry: number = 0;
|
|
406
|
+
const deltas: ISequencedDocumentMessage[] = [];
|
|
407
|
+
const nothing = { partial: false, cancel: true, payload: [] };
|
|
408
|
+
|
|
409
|
+
while (signal?.aborted !== true) {
|
|
410
|
+
retry++;
|
|
411
|
+
let delay = Math.min(MaxFetchDelayInMs, MissingFetchDelayInMs * Math.pow(2, retry));
|
|
412
|
+
const startTime = performance.now();
|
|
413
|
+
|
|
414
|
+
try {
|
|
415
|
+
// Issue async request for deltas - limit the number fetched to MaxBatchDeltas
|
|
416
|
+
const deltasP = get({ ...props, retry } /* telemetry props */);
|
|
417
|
+
|
|
418
|
+
const { messages, partialResult } = await deltasP;
|
|
419
|
+
deltas.push(...messages);
|
|
420
|
+
|
|
421
|
+
const deltasRetrievedLast = messages.length;
|
|
422
|
+
|
|
423
|
+
if (deltasRetrievedLast !== 0 || !strongTo) {
|
|
424
|
+
return { payload: deltas, cancel: false, partial: partialResult };
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// Storage does not have ops we need.
|
|
428
|
+
// Attempt to fetch more deltas. If we didn't receive any in the previous call we up our retry
|
|
429
|
+
// count since something prevented us from seeing those deltas
|
|
430
|
+
|
|
431
|
+
if (lastSuccessTime === undefined) {
|
|
432
|
+
lastSuccessTime = performance.now();
|
|
433
|
+
} else if (performance.now() - lastSuccessTime > 30000) {
|
|
434
|
+
// If we are connected and receiving proper responses from server, but can't get any ops back,
|
|
435
|
+
// then give up after some time. This likely indicates the issue with ordering service not flushing
|
|
436
|
+
// ops to storage quick enough, and possibly waiting for summaries, while summarizer can't get
|
|
437
|
+
// current as it can't get ops.
|
|
438
|
+
throw createGenericNetworkError(
|
|
439
|
+
// pre-0.58 error message: failedToRetrieveOpsFromStorage:TooManyRetries
|
|
440
|
+
"Failed to retrieve ops from storage (Too Many Retries)",
|
|
441
|
+
{ canRetry: false },
|
|
442
|
+
{
|
|
443
|
+
retry,
|
|
444
|
+
driverVersion,
|
|
445
|
+
...props,
|
|
446
|
+
},
|
|
447
|
+
);
|
|
448
|
+
}
|
|
449
|
+
} catch (error) {
|
|
450
|
+
const canRetry = canRetryOnError(error);
|
|
451
|
+
|
|
452
|
+
lastSuccessTime = undefined;
|
|
453
|
+
|
|
454
|
+
const retryAfter = getRetryDelayFromError(error);
|
|
455
|
+
|
|
456
|
+
// This will log to error table only if the error is non-retryable
|
|
457
|
+
logNetworkFailure(
|
|
458
|
+
logger,
|
|
459
|
+
{
|
|
460
|
+
eventName: "GetDeltas_Error",
|
|
461
|
+
...props,
|
|
462
|
+
retry,
|
|
463
|
+
duration: performance.now() - startTime,
|
|
464
|
+
retryAfter,
|
|
465
|
+
reason: scenarioName,
|
|
466
|
+
},
|
|
467
|
+
error,
|
|
468
|
+
);
|
|
469
|
+
|
|
470
|
+
if (!canRetry) {
|
|
471
|
+
// It's game over scenario.
|
|
472
|
+
throw error;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
if (retryAfter !== undefined && retryAfter >= 0) {
|
|
476
|
+
delay = retryAfter;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
await waitForConnectedState(delay);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
return nothing;
|
|
450
484
|
}
|
|
451
485
|
|
|
452
486
|
/**
|
|
@@ -462,127 +496,155 @@ async function getSingleOpBatch(
|
|
|
462
496
|
* @returns - Messages fetched
|
|
463
497
|
*/
|
|
464
498
|
export function requestOps(
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
499
|
+
get: (
|
|
500
|
+
from: number,
|
|
501
|
+
to: number,
|
|
502
|
+
telemetryProps: ITelemetryProperties,
|
|
503
|
+
) => Promise<IDeltasFetchResult>,
|
|
504
|
+
concurrency: number,
|
|
505
|
+
fromTotal: number,
|
|
506
|
+
toTotal: number | undefined,
|
|
507
|
+
payloadSize: number,
|
|
508
|
+
logger: ITelemetryLogger,
|
|
509
|
+
signal?: AbortSignal,
|
|
510
|
+
scenarioName?: string,
|
|
473
511
|
): IStream<ISequencedDocumentMessage[]> {
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
512
|
+
let requests = 0;
|
|
513
|
+
let lastFetch: number | undefined;
|
|
514
|
+
let length = 0;
|
|
515
|
+
const queue = new Queue<ISequencedDocumentMessage[]>();
|
|
516
|
+
|
|
517
|
+
const propsTotal: ITelemetryProperties = {
|
|
518
|
+
fromTotal,
|
|
519
|
+
toTotal,
|
|
520
|
+
};
|
|
521
|
+
|
|
522
|
+
const telemetryEvent = PerformanceEvent.start(logger, {
|
|
523
|
+
eventName: "GetDeltas",
|
|
524
|
+
...propsTotal,
|
|
525
|
+
reason: scenarioName,
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
const manager = new ParallelRequests<ISequencedDocumentMessage>(
|
|
529
|
+
fromTotal,
|
|
530
|
+
toTotal,
|
|
531
|
+
payloadSize,
|
|
532
|
+
logger,
|
|
533
|
+
async (
|
|
534
|
+
request: number,
|
|
535
|
+
from: number,
|
|
536
|
+
to: number,
|
|
537
|
+
strongTo: boolean,
|
|
538
|
+
propsPerRequest: ITelemetryProperties,
|
|
539
|
+
) => {
|
|
540
|
+
requests++;
|
|
541
|
+
return getSingleOpBatch(
|
|
542
|
+
async (propsAll) => get(from, to, propsAll),
|
|
543
|
+
{ request, from, to, ...propsTotal, ...propsPerRequest },
|
|
544
|
+
strongTo,
|
|
545
|
+
logger,
|
|
546
|
+
signal,
|
|
547
|
+
scenarioName,
|
|
548
|
+
);
|
|
549
|
+
},
|
|
550
|
+
(deltas: ISequencedDocumentMessage[]) => {
|
|
551
|
+
// Assert continuing and right start.
|
|
552
|
+
if (lastFetch === undefined) {
|
|
553
|
+
assert(deltas[0].sequenceNumber === fromTotal, 0x26d /* "wrong start" */);
|
|
554
|
+
} else {
|
|
555
|
+
assert(deltas[0].sequenceNumber === lastFetch + 1, 0x26e /* "wrong start" */);
|
|
556
|
+
}
|
|
557
|
+
lastFetch = deltas[deltas.length - 1].sequenceNumber;
|
|
558
|
+
assert(
|
|
559
|
+
lastFetch - deltas[0].sequenceNumber + 1 === deltas.length,
|
|
560
|
+
0x26f /* "continuous and no duplicates" */,
|
|
561
|
+
);
|
|
562
|
+
length += deltas.length;
|
|
563
|
+
queue.pushValue(deltas);
|
|
564
|
+
},
|
|
565
|
+
);
|
|
566
|
+
|
|
567
|
+
// Implement faster cancellation. getSingleOpBatch() checks signal, but only in between
|
|
568
|
+
// waits (up to 10 seconds) and fetches (can take infinite amount of time).
|
|
569
|
+
// While every such case should be improved and take into account signal (and thus cancel immediately),
|
|
570
|
+
// it is beneficial to have catch-all
|
|
571
|
+
const listener = (event: Event) => {
|
|
572
|
+
manager.cancel();
|
|
573
|
+
};
|
|
574
|
+
if (signal !== undefined) {
|
|
575
|
+
signal.addEventListener("abort", listener);
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
manager
|
|
579
|
+
.run(concurrency)
|
|
580
|
+
.finally(() => {
|
|
581
|
+
if (signal !== undefined) {
|
|
582
|
+
signal.removeEventListener("abort", listener);
|
|
583
|
+
}
|
|
584
|
+
})
|
|
585
|
+
.then(() => {
|
|
586
|
+
const props = {
|
|
587
|
+
lastFetch,
|
|
588
|
+
length,
|
|
589
|
+
requests,
|
|
590
|
+
};
|
|
591
|
+
if (manager.canceled) {
|
|
592
|
+
telemetryEvent.cancel({ ...props, error: "ops request cancelled by client" });
|
|
593
|
+
} else {
|
|
594
|
+
assert(
|
|
595
|
+
toTotal === undefined || (lastFetch !== undefined && lastFetch >= toTotal - 1),
|
|
596
|
+
0x270 /* "All requested ops fetched" */,
|
|
597
|
+
);
|
|
598
|
+
telemetryEvent.end(props);
|
|
599
|
+
}
|
|
600
|
+
queue.pushDone();
|
|
601
|
+
})
|
|
602
|
+
.catch((error) => {
|
|
603
|
+
telemetryEvent.cancel(
|
|
604
|
+
{
|
|
605
|
+
lastFetch,
|
|
606
|
+
length,
|
|
607
|
+
requests,
|
|
608
|
+
},
|
|
609
|
+
error,
|
|
610
|
+
);
|
|
611
|
+
queue.pushError(error);
|
|
612
|
+
});
|
|
613
|
+
|
|
614
|
+
return queue;
|
|
559
615
|
}
|
|
560
616
|
|
|
561
617
|
export const emptyMessageStream: IStream<ISequencedDocumentMessage[]> = {
|
|
562
|
-
|
|
618
|
+
read: async () => {
|
|
619
|
+
return { done: true };
|
|
620
|
+
},
|
|
563
621
|
};
|
|
564
622
|
|
|
565
|
-
export function streamFromMessages(
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
623
|
+
export function streamFromMessages(
|
|
624
|
+
messagesArg: Promise<ISequencedDocumentMessage[]>,
|
|
625
|
+
): IStream<ISequencedDocumentMessage[]> {
|
|
626
|
+
let messages: Promise<ISequencedDocumentMessage[]> | undefined = messagesArg;
|
|
627
|
+
return {
|
|
628
|
+
read: async () => {
|
|
629
|
+
if (messages === undefined) {
|
|
630
|
+
return { done: true };
|
|
631
|
+
}
|
|
632
|
+
const value = await messages;
|
|
633
|
+
messages = undefined;
|
|
634
|
+
return value.length === 0 ? { done: true } : { done: false, value };
|
|
635
|
+
},
|
|
636
|
+
};
|
|
578
637
|
}
|
|
579
638
|
|
|
580
|
-
export function streamObserver<T>(
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
639
|
+
export function streamObserver<T>(
|
|
640
|
+
stream: IStream<T>,
|
|
641
|
+
handler: (value: IStreamResult<T>) => void,
|
|
642
|
+
): IStream<T> {
|
|
643
|
+
return {
|
|
644
|
+
read: async () => {
|
|
645
|
+
const value = await stream.read();
|
|
646
|
+
handler(value);
|
|
647
|
+
return value;
|
|
648
|
+
},
|
|
649
|
+
};
|
|
588
650
|
}
|