@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.
@@ -2067,9 +2067,6 @@ var RpcSessionImpl = class {
2067
2067
  this.options = options;
2068
2068
  this.exports.push({ hook: mainHook, refcount: 1 });
2069
2069
  this.imports.push(new ImportTableEntry(this, 0, false));
2070
- this.cancelReadLoop = (error) => {
2071
- this.activeReadAbort?.reject(error);
2072
- };
2073
2070
  this.readLoop().catch((err) => this.abort(err));
2074
2071
  }
2075
2072
  exports = [];
@@ -2077,7 +2074,6 @@ var RpcSessionImpl = class {
2077
2074
  imports = [];
2078
2075
  abortReason;
2079
2076
  cancelReadLoop;
2080
- activeReadAbort;
2081
2077
  // We assign positive numbers to imports we initiate, and negative numbers to exports we
2082
2078
  // initiate. So the next import ID is just `imports.length`, but the next export ID needs
2083
2079
  // to be tracked explicitly.
@@ -2355,7 +2351,8 @@ var RpcSessionImpl = class {
2355
2351
  }
2356
2352
  abort(error, trySendAbortMessage = true) {
2357
2353
  if (this.abortReason !== void 0) return;
2358
- this.cancelReadLoop(error);
2354
+ this.cancelReadLoop?.(error);
2355
+ this.cancelReadLoop = void 0;
2359
2356
  if (trySendAbortMessage) {
2360
2357
  try {
2361
2358
  this.transport.send(JSON.stringify(["abort", Devaluator.devaluate(error, void 0, this)])).catch((err) => {
@@ -2393,17 +2390,7 @@ var RpcSessionImpl = class {
2393
2390
  }
2394
2391
  async readLoop() {
2395
2392
  while (!this.abortReason) {
2396
- let readAbort = Promise.withResolvers();
2397
- this.activeReadAbort = readAbort;
2398
- let msgText;
2399
- try {
2400
- msgText = await Promise.race([this.transport.receive(), readAbort.promise]);
2401
- } finally {
2402
- if (this.activeReadAbort === readAbort) {
2403
- this.activeReadAbort = void 0;
2404
- }
2405
- }
2406
- let msg = JSON.parse(msgText);
2393
+ let msg = JSON.parse(await this.receiveOrAbort());
2407
2394
  if (this.abortReason) break;
2408
2395
  if (msg instanceof Array) {
2409
2396
  switch (msg[0]) {
@@ -2485,6 +2472,24 @@ var RpcSessionImpl = class {
2485
2472
  throw new Error(`bad RPC message: ${JSON.stringify(msg)}`);
2486
2473
  }
2487
2474
  }
2475
+ // Use a fresh cancellation promise for each read. Reusing one session-long promise here causes
2476
+ // Promise.race() to accumulate reactions until the session is shut down.
2477
+ receiveOrAbort() {
2478
+ let readCanceled = Promise.withResolvers();
2479
+ this.cancelReadLoop = readCanceled.reject;
2480
+ let receivePromise;
2481
+ try {
2482
+ receivePromise = this.transport.receive();
2483
+ } catch (err) {
2484
+ this.cancelReadLoop = void 0;
2485
+ return Promise.reject(err);
2486
+ }
2487
+ return Promise.race([receivePromise, readCanceled.promise]).finally(() => {
2488
+ if (this.cancelReadLoop === readCanceled.reject) {
2489
+ this.cancelReadLoop = void 0;
2490
+ }
2491
+ });
2492
+ }
2488
2493
  async drain() {
2489
2494
  if (this.abortReason) {
2490
2495
  throw this.abortReason;