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