@vaadin-component-factory/vcf-pdf-viewer 1.1.1 → 1.2.0

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.
@@ -1,5 +1,5 @@
1
- import { c as assert, l as createPromiseCapability, t as UnknownErrorException, v as UnexpectedResponseException, M as MissingPDFException, A as AbortException } from './util.js';
2
-
1
+ import { c as assert, l as createPromiseCapability, t as UnknownErrorException, v as UnexpectedResponseException, M as MissingPDFException, A as AbortException } from './util.js';
2
+
3
3
  /* Copyright 2018 Mozilla Foundation
4
4
  *
5
5
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,170 +13,170 @@ import { c as assert, l as createPromiseCapability, t as UnknownErrorException,
13
13
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
14
  * See the License for the specific language governing permissions and
15
15
  * limitations under the License.
16
- */
17
- const CallbackKind = {
18
- UNKNOWN: 0,
19
- DATA: 1,
20
- ERROR: 2
21
- };
22
- const StreamKind = {
23
- UNKNOWN: 0,
24
- CANCEL: 1,
25
- CANCEL_COMPLETE: 2,
26
- CLOSE: 3,
27
- ENQUEUE: 4,
28
- ERROR: 5,
29
- PULL: 6,
30
- PULL_COMPLETE: 7,
31
- START_COMPLETE: 8
32
- };
33
-
34
- function wrapReason(reason) {
35
- if (typeof PDFJSDev === "undefined" || PDFJSDev.test("!PRODUCTION || TESTING")) {
36
- assert(reason instanceof Error || typeof reason === "object" && reason !== null, 'wrapReason: Expected "reason" to be a (possibly cloned) Error.');
37
- } else {
38
- if (typeof reason !== "object" || reason === null) {
39
- return reason;
40
- }
41
- }
42
-
43
- switch (reason.name) {
44
- case "AbortException":
45
- return new AbortException(reason.message);
46
-
47
- case "MissingPDFException":
48
- return new MissingPDFException(reason.message);
49
-
50
- case "UnexpectedResponseException":
51
- return new UnexpectedResponseException(reason.message, reason.status);
52
-
53
- case "UnknownErrorException":
54
- return new UnknownErrorException(reason.message, reason.details);
55
-
56
- default:
57
- return new UnknownErrorException(reason.message, reason.toString());
58
- }
59
- }
60
-
61
- class MessageHandler {
62
- constructor(sourceName, targetName, comObj) {
63
- this.sourceName = sourceName;
64
- this.targetName = targetName;
65
- this.comObj = comObj;
66
- this.callbackId = 1;
67
- this.streamId = 1;
68
- this.postMessageTransfers = true;
69
- this.streamSinks = Object.create(null);
70
- this.streamControllers = Object.create(null);
71
- this.callbackCapabilities = Object.create(null);
72
- this.actionHandler = Object.create(null);
73
-
74
- this._onComObjOnMessage = event => {
75
- const data = event.data;
76
-
77
- if (data.targetName !== this.sourceName) {
78
- return;
79
- }
80
-
81
- if (data.stream) {
82
- this._processStreamMessage(data);
83
-
84
- return;
85
- }
86
-
87
- if (data.callback) {
88
- const callbackId = data.callbackId;
89
- const capability = this.callbackCapabilities[callbackId];
90
-
91
- if (!capability) {
92
- throw new Error(`Cannot resolve callback ${callbackId}`);
93
- }
94
-
95
- delete this.callbackCapabilities[callbackId];
96
-
97
- if (data.callback === CallbackKind.DATA) {
98
- capability.resolve(data.data);
99
- } else if (data.callback === CallbackKind.ERROR) {
100
- capability.reject(wrapReason(data.reason));
101
- } else {
102
- throw new Error("Unexpected callback case");
103
- }
104
-
105
- return;
106
- }
107
-
108
- const action = this.actionHandler[data.action];
109
-
110
- if (!action) {
111
- throw new Error(`Unknown action from worker: ${data.action}`);
112
- }
113
-
114
- if (data.callbackId) {
115
- const cbSourceName = this.sourceName;
116
- const cbTargetName = data.sourceName;
117
- new Promise(function (resolve) {
118
- resolve(action(data.data));
119
- }).then(function (result) {
120
- comObj.postMessage({
121
- sourceName: cbSourceName,
122
- targetName: cbTargetName,
123
- callback: CallbackKind.DATA,
124
- callbackId: data.callbackId,
125
- data: result
126
- });
127
- }, function (reason) {
128
- comObj.postMessage({
129
- sourceName: cbSourceName,
130
- targetName: cbTargetName,
131
- callback: CallbackKind.ERROR,
132
- callbackId: data.callbackId,
133
- reason: wrapReason(reason)
134
- });
135
- });
136
- return;
137
- }
138
-
139
- if (data.streamId) {
140
- this._createStreamSink(data);
141
-
142
- return;
143
- }
144
-
145
- action(data.data);
146
- };
147
-
148
- comObj.addEventListener("message", this._onComObjOnMessage);
149
- }
150
-
151
- on(actionName, handler) {
152
- if (typeof PDFJSDev === "undefined" || PDFJSDev.test("!PRODUCTION || TESTING")) {
153
- assert(typeof handler === "function", 'MessageHandler.on: Expected "handler" to be a function.');
154
- }
155
-
156
- const ah = this.actionHandler;
157
-
158
- if (ah[actionName]) {
159
- throw new Error(`There is already an actionName called "${actionName}"`);
160
- }
161
-
162
- ah[actionName] = handler;
163
- }
16
+ */
17
+ const CallbackKind = {
18
+ UNKNOWN: 0,
19
+ DATA: 1,
20
+ ERROR: 2
21
+ };
22
+ const StreamKind = {
23
+ UNKNOWN: 0,
24
+ CANCEL: 1,
25
+ CANCEL_COMPLETE: 2,
26
+ CLOSE: 3,
27
+ ENQUEUE: 4,
28
+ ERROR: 5,
29
+ PULL: 6,
30
+ PULL_COMPLETE: 7,
31
+ START_COMPLETE: 8
32
+ };
33
+
34
+ function wrapReason(reason) {
35
+ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("!PRODUCTION || TESTING")) {
36
+ assert(reason instanceof Error || typeof reason === "object" && reason !== null, 'wrapReason: Expected "reason" to be a (possibly cloned) Error.');
37
+ } else {
38
+ if (typeof reason !== "object" || reason === null) {
39
+ return reason;
40
+ }
41
+ }
42
+
43
+ switch (reason.name) {
44
+ case "AbortException":
45
+ return new AbortException(reason.message);
46
+
47
+ case "MissingPDFException":
48
+ return new MissingPDFException(reason.message);
49
+
50
+ case "UnexpectedResponseException":
51
+ return new UnexpectedResponseException(reason.message, reason.status);
52
+
53
+ case "UnknownErrorException":
54
+ return new UnknownErrorException(reason.message, reason.details);
55
+
56
+ default:
57
+ return new UnknownErrorException(reason.message, reason.toString());
58
+ }
59
+ }
60
+
61
+ class MessageHandler {
62
+ constructor(sourceName, targetName, comObj) {
63
+ this.sourceName = sourceName;
64
+ this.targetName = targetName;
65
+ this.comObj = comObj;
66
+ this.callbackId = 1;
67
+ this.streamId = 1;
68
+ this.postMessageTransfers = true;
69
+ this.streamSinks = Object.create(null);
70
+ this.streamControllers = Object.create(null);
71
+ this.callbackCapabilities = Object.create(null);
72
+ this.actionHandler = Object.create(null);
73
+
74
+ this._onComObjOnMessage = event => {
75
+ const data = event.data;
76
+
77
+ if (data.targetName !== this.sourceName) {
78
+ return;
79
+ }
80
+
81
+ if (data.stream) {
82
+ this._processStreamMessage(data);
83
+
84
+ return;
85
+ }
86
+
87
+ if (data.callback) {
88
+ const callbackId = data.callbackId;
89
+ const capability = this.callbackCapabilities[callbackId];
90
+
91
+ if (!capability) {
92
+ throw new Error(`Cannot resolve callback ${callbackId}`);
93
+ }
94
+
95
+ delete this.callbackCapabilities[callbackId];
96
+
97
+ if (data.callback === CallbackKind.DATA) {
98
+ capability.resolve(data.data);
99
+ } else if (data.callback === CallbackKind.ERROR) {
100
+ capability.reject(wrapReason(data.reason));
101
+ } else {
102
+ throw new Error("Unexpected callback case");
103
+ }
104
+
105
+ return;
106
+ }
107
+
108
+ const action = this.actionHandler[data.action];
109
+
110
+ if (!action) {
111
+ throw new Error(`Unknown action from worker: ${data.action}`);
112
+ }
113
+
114
+ if (data.callbackId) {
115
+ const cbSourceName = this.sourceName;
116
+ const cbTargetName = data.sourceName;
117
+ new Promise(function (resolve) {
118
+ resolve(action(data.data));
119
+ }).then(function (result) {
120
+ comObj.postMessage({
121
+ sourceName: cbSourceName,
122
+ targetName: cbTargetName,
123
+ callback: CallbackKind.DATA,
124
+ callbackId: data.callbackId,
125
+ data: result
126
+ });
127
+ }, function (reason) {
128
+ comObj.postMessage({
129
+ sourceName: cbSourceName,
130
+ targetName: cbTargetName,
131
+ callback: CallbackKind.ERROR,
132
+ callbackId: data.callbackId,
133
+ reason: wrapReason(reason)
134
+ });
135
+ });
136
+ return;
137
+ }
138
+
139
+ if (data.streamId) {
140
+ this._createStreamSink(data);
141
+
142
+ return;
143
+ }
144
+
145
+ action(data.data);
146
+ };
147
+
148
+ comObj.addEventListener("message", this._onComObjOnMessage);
149
+ }
150
+
151
+ on(actionName, handler) {
152
+ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("!PRODUCTION || TESTING")) {
153
+ assert(typeof handler === "function", 'MessageHandler.on: Expected "handler" to be a function.');
154
+ }
155
+
156
+ const ah = this.actionHandler;
157
+
158
+ if (ah[actionName]) {
159
+ throw new Error(`There is already an actionName called "${actionName}"`);
160
+ }
161
+
162
+ ah[actionName] = handler;
163
+ }
164
164
  /**
165
165
  * Sends a message to the comObj to invoke the action with the supplied data.
166
166
  * @param {string} actionName - Action to call.
167
167
  * @param {JSON} data - JSON data to send.
168
168
  * @param {Array} [transfers] - List of transfers/ArrayBuffers.
169
- */
170
-
171
-
172
- send(actionName, data, transfers) {
173
- this._postMessage({
174
- sourceName: this.sourceName,
175
- targetName: this.targetName,
176
- action: actionName,
177
- data
178
- }, transfers);
179
- }
169
+ */
170
+
171
+
172
+ send(actionName, data, transfers) {
173
+ this._postMessage({
174
+ sourceName: this.sourceName,
175
+ targetName: this.targetName,
176
+ action: actionName,
177
+ data
178
+ }, transfers);
179
+ }
180
180
  /**
181
181
  * Sends a message to the comObj to invoke the action with the supplied data.
182
182
  * Expects that the other side will callback with the response.
@@ -184,28 +184,28 @@ class MessageHandler {
184
184
  * @param {JSON} data - JSON data to send.
185
185
  * @param {Array} [transfers] - List of transfers/ArrayBuffers.
186
186
  * @returns {Promise} Promise to be resolved with response data.
187
- */
188
-
189
-
190
- sendWithPromise(actionName, data, transfers) {
191
- const callbackId = this.callbackId++;
192
- const capability = createPromiseCapability();
193
- this.callbackCapabilities[callbackId] = capability;
194
-
195
- try {
196
- this._postMessage({
197
- sourceName: this.sourceName,
198
- targetName: this.targetName,
199
- action: actionName,
200
- callbackId,
201
- data
202
- }, transfers);
203
- } catch (ex) {
204
- capability.reject(ex);
205
- }
206
-
207
- return capability.promise;
208
- }
187
+ */
188
+
189
+
190
+ sendWithPromise(actionName, data, transfers) {
191
+ const callbackId = this.callbackId++;
192
+ const capability = createPromiseCapability();
193
+ this.callbackCapabilities[callbackId] = capability;
194
+
195
+ try {
196
+ this._postMessage({
197
+ sourceName: this.sourceName,
198
+ targetName: this.targetName,
199
+ action: actionName,
200
+ callbackId,
201
+ data
202
+ }, transfers);
203
+ } catch (ex) {
204
+ capability.reject(ex);
205
+ }
206
+
207
+ return capability.promise;
208
+ }
209
209
  /**
210
210
  * Sends a message to the comObj to invoke the action with the supplied data.
211
211
  * Expect that the other side will callback to signal 'start_complete'.
@@ -215,355 +215,355 @@ class MessageHandler {
215
215
  * internal queue.
216
216
  * @param {Array} [transfers] - List of transfers/ArrayBuffers.
217
217
  * @returns {ReadableStream} ReadableStream to read data in chunks.
218
- */
219
-
220
-
221
- sendWithStream(actionName, data, queueingStrategy, transfers) {
222
- const streamId = this.streamId++;
223
- const sourceName = this.sourceName;
224
- const targetName = this.targetName;
225
- const comObj = this.comObj;
226
- return new ReadableStream({
227
- start: controller => {
228
- const startCapability = createPromiseCapability();
229
- this.streamControllers[streamId] = {
230
- controller,
231
- startCall: startCapability,
232
- pullCall: null,
233
- cancelCall: null,
234
- isClosed: false
235
- };
236
-
237
- this._postMessage({
238
- sourceName,
239
- targetName,
240
- action: actionName,
241
- streamId,
242
- data,
243
- desiredSize: controller.desiredSize
244
- }, transfers); // Return Promise for Async process, to signal success/failure.
245
-
246
-
247
- return startCapability.promise;
248
- },
249
- pull: controller => {
250
- const pullCapability = createPromiseCapability();
251
- this.streamControllers[streamId].pullCall = pullCapability;
252
- comObj.postMessage({
253
- sourceName,
254
- targetName,
255
- stream: StreamKind.PULL,
256
- streamId,
257
- desiredSize: controller.desiredSize
258
- }); // Returning Promise will not call "pull"
259
- // again until current pull is resolved.
260
-
261
- return pullCapability.promise;
262
- },
263
- cancel: reason => {
264
- assert(reason instanceof Error, "cancel must have a valid reason");
265
- const cancelCapability = createPromiseCapability();
266
- this.streamControllers[streamId].cancelCall = cancelCapability;
267
- this.streamControllers[streamId].isClosed = true;
268
- comObj.postMessage({
269
- sourceName,
270
- targetName,
271
- stream: StreamKind.CANCEL,
272
- streamId,
273
- reason: wrapReason(reason)
274
- }); // Return Promise to signal success or failure.
275
-
276
- return cancelCapability.promise;
277
- }
278
- }, queueingStrategy);
279
- }
218
+ */
219
+
220
+
221
+ sendWithStream(actionName, data, queueingStrategy, transfers) {
222
+ const streamId = this.streamId++;
223
+ const sourceName = this.sourceName;
224
+ const targetName = this.targetName;
225
+ const comObj = this.comObj;
226
+ return new ReadableStream({
227
+ start: controller => {
228
+ const startCapability = createPromiseCapability();
229
+ this.streamControllers[streamId] = {
230
+ controller,
231
+ startCall: startCapability,
232
+ pullCall: null,
233
+ cancelCall: null,
234
+ isClosed: false
235
+ };
236
+
237
+ this._postMessage({
238
+ sourceName,
239
+ targetName,
240
+ action: actionName,
241
+ streamId,
242
+ data,
243
+ desiredSize: controller.desiredSize
244
+ }, transfers); // Return Promise for Async process, to signal success/failure.
245
+
246
+
247
+ return startCapability.promise;
248
+ },
249
+ pull: controller => {
250
+ const pullCapability = createPromiseCapability();
251
+ this.streamControllers[streamId].pullCall = pullCapability;
252
+ comObj.postMessage({
253
+ sourceName,
254
+ targetName,
255
+ stream: StreamKind.PULL,
256
+ streamId,
257
+ desiredSize: controller.desiredSize
258
+ }); // Returning Promise will not call "pull"
259
+ // again until current pull is resolved.
260
+
261
+ return pullCapability.promise;
262
+ },
263
+ cancel: reason => {
264
+ assert(reason instanceof Error, "cancel must have a valid reason");
265
+ const cancelCapability = createPromiseCapability();
266
+ this.streamControllers[streamId].cancelCall = cancelCapability;
267
+ this.streamControllers[streamId].isClosed = true;
268
+ comObj.postMessage({
269
+ sourceName,
270
+ targetName,
271
+ stream: StreamKind.CANCEL,
272
+ streamId,
273
+ reason: wrapReason(reason)
274
+ }); // Return Promise to signal success or failure.
275
+
276
+ return cancelCapability.promise;
277
+ }
278
+ }, queueingStrategy);
279
+ }
280
280
  /**
281
281
  * @private
282
- */
283
-
284
-
285
- _createStreamSink(data) {
286
- const self = this;
287
- const action = this.actionHandler[data.action];
288
- const streamId = data.streamId;
289
- const sourceName = this.sourceName;
290
- const targetName = data.sourceName;
291
- const comObj = this.comObj;
292
- const streamSink = {
293
- enqueue(chunk, size = 1, transfers) {
294
- if (this.isCancelled) {
295
- return;
296
- }
297
-
298
- const lastDesiredSize = this.desiredSize;
299
- this.desiredSize -= size; // Enqueue decreases the desiredSize property of sink,
300
- // so when it changes from positive to negative,
301
- // set ready as unresolved promise.
302
-
303
- if (lastDesiredSize > 0 && this.desiredSize <= 0) {
304
- this.sinkCapability = createPromiseCapability();
305
- this.ready = this.sinkCapability.promise;
306
- }
307
-
308
- self._postMessage({
309
- sourceName,
310
- targetName,
311
- stream: StreamKind.ENQUEUE,
312
- streamId,
313
- chunk
314
- }, transfers);
315
- },
316
-
317
- close() {
318
- if (this.isCancelled) {
319
- return;
320
- }
321
-
322
- this.isCancelled = true;
323
- comObj.postMessage({
324
- sourceName,
325
- targetName,
326
- stream: StreamKind.CLOSE,
327
- streamId
328
- });
329
- delete self.streamSinks[streamId];
330
- },
331
-
332
- error(reason) {
333
- assert(reason instanceof Error, "error must have a valid reason");
334
-
335
- if (this.isCancelled) {
336
- return;
337
- }
338
-
339
- this.isCancelled = true;
340
- comObj.postMessage({
341
- sourceName,
342
- targetName,
343
- stream: StreamKind.ERROR,
344
- streamId,
345
- reason: wrapReason(reason)
346
- });
347
- },
348
-
349
- sinkCapability: createPromiseCapability(),
350
- onPull: null,
351
- onCancel: null,
352
- isCancelled: false,
353
- desiredSize: data.desiredSize,
354
- ready: null
355
- };
356
- streamSink.sinkCapability.resolve();
357
- streamSink.ready = streamSink.sinkCapability.promise;
358
- this.streamSinks[streamId] = streamSink;
359
- new Promise(function (resolve) {
360
- resolve(action(data.data, streamSink));
361
- }).then(function () {
362
- comObj.postMessage({
363
- sourceName,
364
- targetName,
365
- stream: StreamKind.START_COMPLETE,
366
- streamId,
367
- success: true
368
- });
369
- }, function (reason) {
370
- comObj.postMessage({
371
- sourceName,
372
- targetName,
373
- stream: StreamKind.START_COMPLETE,
374
- streamId,
375
- reason: wrapReason(reason)
376
- });
377
- });
378
- }
282
+ */
283
+
284
+
285
+ _createStreamSink(data) {
286
+ const self = this;
287
+ const action = this.actionHandler[data.action];
288
+ const streamId = data.streamId;
289
+ const sourceName = this.sourceName;
290
+ const targetName = data.sourceName;
291
+ const comObj = this.comObj;
292
+ const streamSink = {
293
+ enqueue(chunk, size = 1, transfers) {
294
+ if (this.isCancelled) {
295
+ return;
296
+ }
297
+
298
+ const lastDesiredSize = this.desiredSize;
299
+ this.desiredSize -= size; // Enqueue decreases the desiredSize property of sink,
300
+ // so when it changes from positive to negative,
301
+ // set ready as unresolved promise.
302
+
303
+ if (lastDesiredSize > 0 && this.desiredSize <= 0) {
304
+ this.sinkCapability = createPromiseCapability();
305
+ this.ready = this.sinkCapability.promise;
306
+ }
307
+
308
+ self._postMessage({
309
+ sourceName,
310
+ targetName,
311
+ stream: StreamKind.ENQUEUE,
312
+ streamId,
313
+ chunk
314
+ }, transfers);
315
+ },
316
+
317
+ close() {
318
+ if (this.isCancelled) {
319
+ return;
320
+ }
321
+
322
+ this.isCancelled = true;
323
+ comObj.postMessage({
324
+ sourceName,
325
+ targetName,
326
+ stream: StreamKind.CLOSE,
327
+ streamId
328
+ });
329
+ delete self.streamSinks[streamId];
330
+ },
331
+
332
+ error(reason) {
333
+ assert(reason instanceof Error, "error must have a valid reason");
334
+
335
+ if (this.isCancelled) {
336
+ return;
337
+ }
338
+
339
+ this.isCancelled = true;
340
+ comObj.postMessage({
341
+ sourceName,
342
+ targetName,
343
+ stream: StreamKind.ERROR,
344
+ streamId,
345
+ reason: wrapReason(reason)
346
+ });
347
+ },
348
+
349
+ sinkCapability: createPromiseCapability(),
350
+ onPull: null,
351
+ onCancel: null,
352
+ isCancelled: false,
353
+ desiredSize: data.desiredSize,
354
+ ready: null
355
+ };
356
+ streamSink.sinkCapability.resolve();
357
+ streamSink.ready = streamSink.sinkCapability.promise;
358
+ this.streamSinks[streamId] = streamSink;
359
+ new Promise(function (resolve) {
360
+ resolve(action(data.data, streamSink));
361
+ }).then(function () {
362
+ comObj.postMessage({
363
+ sourceName,
364
+ targetName,
365
+ stream: StreamKind.START_COMPLETE,
366
+ streamId,
367
+ success: true
368
+ });
369
+ }, function (reason) {
370
+ comObj.postMessage({
371
+ sourceName,
372
+ targetName,
373
+ stream: StreamKind.START_COMPLETE,
374
+ streamId,
375
+ reason: wrapReason(reason)
376
+ });
377
+ });
378
+ }
379
379
  /**
380
380
  * @private
381
- */
382
-
383
-
384
- _processStreamMessage(data) {
385
- const streamId = data.streamId;
386
- const sourceName = this.sourceName;
387
- const targetName = data.sourceName;
388
- const comObj = this.comObj;
389
-
390
- switch (data.stream) {
391
- case StreamKind.START_COMPLETE:
392
- if (data.success) {
393
- this.streamControllers[streamId].startCall.resolve();
394
- } else {
395
- this.streamControllers[streamId].startCall.reject(wrapReason(data.reason));
396
- }
397
-
398
- break;
399
-
400
- case StreamKind.PULL_COMPLETE:
401
- if (data.success) {
402
- this.streamControllers[streamId].pullCall.resolve();
403
- } else {
404
- this.streamControllers[streamId].pullCall.reject(wrapReason(data.reason));
405
- }
406
-
407
- break;
408
-
409
- case StreamKind.PULL:
410
- // Ignore any pull after close is called.
411
- if (!this.streamSinks[streamId]) {
412
- comObj.postMessage({
413
- sourceName,
414
- targetName,
415
- stream: StreamKind.PULL_COMPLETE,
416
- streamId,
417
- success: true
418
- });
419
- break;
420
- } // Pull increases the desiredSize property of sink,
421
- // so when it changes from negative to positive,
422
- // set ready property as resolved promise.
423
-
424
-
425
- if (this.streamSinks[streamId].desiredSize <= 0 && data.desiredSize > 0) {
426
- this.streamSinks[streamId].sinkCapability.resolve();
427
- } // Reset desiredSize property of sink on every pull.
428
-
429
-
430
- this.streamSinks[streamId].desiredSize = data.desiredSize;
431
- const {
432
- onPull
433
- } = this.streamSinks[data.streamId];
434
- new Promise(function (resolve) {
435
- resolve(onPull && onPull());
436
- }).then(function () {
437
- comObj.postMessage({
438
- sourceName,
439
- targetName,
440
- stream: StreamKind.PULL_COMPLETE,
441
- streamId,
442
- success: true
443
- });
444
- }, function (reason) {
445
- comObj.postMessage({
446
- sourceName,
447
- targetName,
448
- stream: StreamKind.PULL_COMPLETE,
449
- streamId,
450
- reason: wrapReason(reason)
451
- });
452
- });
453
- break;
454
-
455
- case StreamKind.ENQUEUE:
456
- assert(this.streamControllers[streamId], "enqueue should have stream controller");
457
-
458
- if (this.streamControllers[streamId].isClosed) {
459
- break;
460
- }
461
-
462
- this.streamControllers[streamId].controller.enqueue(data.chunk);
463
- break;
464
-
465
- case StreamKind.CLOSE:
466
- assert(this.streamControllers[streamId], "close should have stream controller");
467
-
468
- if (this.streamControllers[streamId].isClosed) {
469
- break;
470
- }
471
-
472
- this.streamControllers[streamId].isClosed = true;
473
- this.streamControllers[streamId].controller.close();
474
-
475
- this._deleteStreamController(streamId);
476
-
477
- break;
478
-
479
- case StreamKind.ERROR:
480
- assert(this.streamControllers[streamId], "error should have stream controller");
481
- this.streamControllers[streamId].controller.error(wrapReason(data.reason));
482
-
483
- this._deleteStreamController(streamId);
484
-
485
- break;
486
-
487
- case StreamKind.CANCEL_COMPLETE:
488
- if (data.success) {
489
- this.streamControllers[streamId].cancelCall.resolve();
490
- } else {
491
- this.streamControllers[streamId].cancelCall.reject(wrapReason(data.reason));
492
- }
493
-
494
- this._deleteStreamController(streamId);
495
-
496
- break;
497
-
498
- case StreamKind.CANCEL:
499
- if (!this.streamSinks[streamId]) {
500
- break;
501
- }
502
-
503
- const {
504
- onCancel
505
- } = this.streamSinks[data.streamId];
506
- new Promise(function (resolve) {
507
- resolve(onCancel && onCancel(wrapReason(data.reason)));
508
- }).then(function () {
509
- comObj.postMessage({
510
- sourceName,
511
- targetName,
512
- stream: StreamKind.CANCEL_COMPLETE,
513
- streamId,
514
- success: true
515
- });
516
- }, function (reason) {
517
- comObj.postMessage({
518
- sourceName,
519
- targetName,
520
- stream: StreamKind.CANCEL_COMPLETE,
521
- streamId,
522
- reason: wrapReason(reason)
523
- });
524
- });
525
- this.streamSinks[streamId].sinkCapability.reject(wrapReason(data.reason));
526
- this.streamSinks[streamId].isCancelled = true;
527
- delete this.streamSinks[streamId];
528
- break;
529
-
530
- default:
531
- throw new Error("Unexpected stream case");
532
- }
533
- }
381
+ */
382
+
383
+
384
+ _processStreamMessage(data) {
385
+ const streamId = data.streamId;
386
+ const sourceName = this.sourceName;
387
+ const targetName = data.sourceName;
388
+ const comObj = this.comObj;
389
+
390
+ switch (data.stream) {
391
+ case StreamKind.START_COMPLETE:
392
+ if (data.success) {
393
+ this.streamControllers[streamId].startCall.resolve();
394
+ } else {
395
+ this.streamControllers[streamId].startCall.reject(wrapReason(data.reason));
396
+ }
397
+
398
+ break;
399
+
400
+ case StreamKind.PULL_COMPLETE:
401
+ if (data.success) {
402
+ this.streamControllers[streamId].pullCall.resolve();
403
+ } else {
404
+ this.streamControllers[streamId].pullCall.reject(wrapReason(data.reason));
405
+ }
406
+
407
+ break;
408
+
409
+ case StreamKind.PULL:
410
+ // Ignore any pull after close is called.
411
+ if (!this.streamSinks[streamId]) {
412
+ comObj.postMessage({
413
+ sourceName,
414
+ targetName,
415
+ stream: StreamKind.PULL_COMPLETE,
416
+ streamId,
417
+ success: true
418
+ });
419
+ break;
420
+ } // Pull increases the desiredSize property of sink,
421
+ // so when it changes from negative to positive,
422
+ // set ready property as resolved promise.
423
+
424
+
425
+ if (this.streamSinks[streamId].desiredSize <= 0 && data.desiredSize > 0) {
426
+ this.streamSinks[streamId].sinkCapability.resolve();
427
+ } // Reset desiredSize property of sink on every pull.
428
+
429
+
430
+ this.streamSinks[streamId].desiredSize = data.desiredSize;
431
+ const {
432
+ onPull
433
+ } = this.streamSinks[data.streamId];
434
+ new Promise(function (resolve) {
435
+ resolve(onPull && onPull());
436
+ }).then(function () {
437
+ comObj.postMessage({
438
+ sourceName,
439
+ targetName,
440
+ stream: StreamKind.PULL_COMPLETE,
441
+ streamId,
442
+ success: true
443
+ });
444
+ }, function (reason) {
445
+ comObj.postMessage({
446
+ sourceName,
447
+ targetName,
448
+ stream: StreamKind.PULL_COMPLETE,
449
+ streamId,
450
+ reason: wrapReason(reason)
451
+ });
452
+ });
453
+ break;
454
+
455
+ case StreamKind.ENQUEUE:
456
+ assert(this.streamControllers[streamId], "enqueue should have stream controller");
457
+
458
+ if (this.streamControllers[streamId].isClosed) {
459
+ break;
460
+ }
461
+
462
+ this.streamControllers[streamId].controller.enqueue(data.chunk);
463
+ break;
464
+
465
+ case StreamKind.CLOSE:
466
+ assert(this.streamControllers[streamId], "close should have stream controller");
467
+
468
+ if (this.streamControllers[streamId].isClosed) {
469
+ break;
470
+ }
471
+
472
+ this.streamControllers[streamId].isClosed = true;
473
+ this.streamControllers[streamId].controller.close();
474
+
475
+ this._deleteStreamController(streamId);
476
+
477
+ break;
478
+
479
+ case StreamKind.ERROR:
480
+ assert(this.streamControllers[streamId], "error should have stream controller");
481
+ this.streamControllers[streamId].controller.error(wrapReason(data.reason));
482
+
483
+ this._deleteStreamController(streamId);
484
+
485
+ break;
486
+
487
+ case StreamKind.CANCEL_COMPLETE:
488
+ if (data.success) {
489
+ this.streamControllers[streamId].cancelCall.resolve();
490
+ } else {
491
+ this.streamControllers[streamId].cancelCall.reject(wrapReason(data.reason));
492
+ }
493
+
494
+ this._deleteStreamController(streamId);
495
+
496
+ break;
497
+
498
+ case StreamKind.CANCEL:
499
+ if (!this.streamSinks[streamId]) {
500
+ break;
501
+ }
502
+
503
+ const {
504
+ onCancel
505
+ } = this.streamSinks[data.streamId];
506
+ new Promise(function (resolve) {
507
+ resolve(onCancel && onCancel(wrapReason(data.reason)));
508
+ }).then(function () {
509
+ comObj.postMessage({
510
+ sourceName,
511
+ targetName,
512
+ stream: StreamKind.CANCEL_COMPLETE,
513
+ streamId,
514
+ success: true
515
+ });
516
+ }, function (reason) {
517
+ comObj.postMessage({
518
+ sourceName,
519
+ targetName,
520
+ stream: StreamKind.CANCEL_COMPLETE,
521
+ streamId,
522
+ reason: wrapReason(reason)
523
+ });
524
+ });
525
+ this.streamSinks[streamId].sinkCapability.reject(wrapReason(data.reason));
526
+ this.streamSinks[streamId].isCancelled = true;
527
+ delete this.streamSinks[streamId];
528
+ break;
529
+
530
+ default:
531
+ throw new Error("Unexpected stream case");
532
+ }
533
+ }
534
534
  /**
535
535
  * @private
536
- */
537
-
538
-
539
- async _deleteStreamController(streamId) {
540
- // Delete the `streamController` only when the start, pull, and cancel
541
- // capabilities have settled, to prevent `TypeError`s.
542
- await Promise.allSettled([this.streamControllers[streamId].startCall, this.streamControllers[streamId].pullCall, this.streamControllers[streamId].cancelCall].map(function (capability) {
543
- return capability && capability.promise;
544
- }));
545
- delete this.streamControllers[streamId];
546
- }
536
+ */
537
+
538
+
539
+ async _deleteStreamController(streamId) {
540
+ // Delete the `streamController` only when the start, pull, and cancel
541
+ // capabilities have settled, to prevent `TypeError`s.
542
+ await Promise.allSettled([this.streamControllers[streamId].startCall, this.streamControllers[streamId].pullCall, this.streamControllers[streamId].cancelCall].map(function (capability) {
543
+ return capability && capability.promise;
544
+ }));
545
+ delete this.streamControllers[streamId];
546
+ }
547
547
  /**
548
548
  * Sends raw message to the comObj.
549
549
  * @param {Object} message - Raw message.
550
550
  * @param transfers List of transfers/ArrayBuffers, or undefined.
551
551
  * @private
552
- */
553
-
554
-
555
- _postMessage(message, transfers) {
556
- if (transfers && this.postMessageTransfers) {
557
- this.comObj.postMessage(message, transfers);
558
- } else {
559
- this.comObj.postMessage(message);
560
- }
561
- }
562
-
563
- destroy() {
564
- this.comObj.removeEventListener("message", this._onComObjOnMessage);
565
- }
566
-
567
- }
568
-
569
- export { MessageHandler as M };
552
+ */
553
+
554
+
555
+ _postMessage(message, transfers) {
556
+ if (transfers && this.postMessageTransfers) {
557
+ this.comObj.postMessage(message, transfers);
558
+ } else {
559
+ this.comObj.postMessage(message);
560
+ }
561
+ }
562
+
563
+ destroy() {
564
+ this.comObj.removeEventListener("message", this._onComObjOnMessage);
565
+ }
566
+
567
+ }
568
+
569
+ export { MessageHandler as M };