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