@vastblast/capnweb 0.7.1 → 0.7.4

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/dist/index.js CHANGED
@@ -2042,9 +2042,6 @@ var RpcSessionImpl = class {
2042
2042
  this.options = options;
2043
2043
  this.exports.push({ hook: mainHook, refcount: 1 });
2044
2044
  this.imports.push(new ImportTableEntry(this, 0, false));
2045
- this.cancelReadLoop = (error) => {
2046
- this.activeReadAbort?.reject(error);
2047
- };
2048
2045
  this.readLoop().catch((err) => this.abort(err));
2049
2046
  }
2050
2047
  exports = [];
@@ -2052,7 +2049,6 @@ var RpcSessionImpl = class {
2052
2049
  imports = [];
2053
2050
  abortReason;
2054
2051
  cancelReadLoop;
2055
- activeReadAbort;
2056
2052
  // We assign positive numbers to imports we initiate, and negative numbers to exports we
2057
2053
  // initiate. So the next import ID is just `imports.length`, but the next export ID needs
2058
2054
  // to be tracked explicitly.
@@ -2330,7 +2326,8 @@ var RpcSessionImpl = class {
2330
2326
  }
2331
2327
  abort(error, trySendAbortMessage = true) {
2332
2328
  if (this.abortReason !== void 0) return;
2333
- this.cancelReadLoop(error);
2329
+ this.cancelReadLoop?.(error);
2330
+ this.cancelReadLoop = void 0;
2334
2331
  if (trySendAbortMessage) {
2335
2332
  try {
2336
2333
  this.transport.send(JSON.stringify(["abort", Devaluator.devaluate(error, void 0, this)])).catch((err) => {
@@ -2368,17 +2365,7 @@ var RpcSessionImpl = class {
2368
2365
  }
2369
2366
  async readLoop() {
2370
2367
  while (!this.abortReason) {
2371
- let readAbort = Promise.withResolvers();
2372
- this.activeReadAbort = readAbort;
2373
- let msgText;
2374
- try {
2375
- msgText = await Promise.race([this.transport.receive(), readAbort.promise]);
2376
- } finally {
2377
- if (this.activeReadAbort === readAbort) {
2378
- this.activeReadAbort = void 0;
2379
- }
2380
- }
2381
- let msg = JSON.parse(msgText);
2368
+ let msg = JSON.parse(await this.receiveOrAbort());
2382
2369
  if (this.abortReason) break;
2383
2370
  if (msg instanceof Array) {
2384
2371
  switch (msg[0]) {
@@ -2460,6 +2447,24 @@ var RpcSessionImpl = class {
2460
2447
  throw new Error(`bad RPC message: ${JSON.stringify(msg)}`);
2461
2448
  }
2462
2449
  }
2450
+ // Use a fresh cancellation promise for each read. Reusing one session-long promise here causes
2451
+ // Promise.race() to accumulate reactions until the session is shut down.
2452
+ receiveOrAbort() {
2453
+ let readCanceled = Promise.withResolvers();
2454
+ this.cancelReadLoop = readCanceled.reject;
2455
+ let receivePromise;
2456
+ try {
2457
+ receivePromise = this.transport.receive();
2458
+ } catch (err) {
2459
+ this.cancelReadLoop = void 0;
2460
+ return Promise.reject(err);
2461
+ }
2462
+ return Promise.race([receivePromise, readCanceled.promise]).finally(() => {
2463
+ if (this.cancelReadLoop === readCanceled.reject) {
2464
+ this.cancelReadLoop = void 0;
2465
+ }
2466
+ });
2467
+ }
2463
2468
  async drain() {
2464
2469
  if (this.abortReason) {
2465
2470
  throw this.abortReason;