@weapp-vite/hmr 0.1.0 → 0.2.1

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.d.mts CHANGED
@@ -170,11 +170,16 @@ export declare class HmrDeliveryCoordinator {
170
170
  private pending;
171
171
  private running?;
172
172
  private failed;
173
+ private failure;
174
+ private revision;
173
175
  private pendingBytes;
176
+ private readonly disposals;
174
177
  constructor(onError: (error: unknown) => void, requestResynchronization?: (() => void) | undefined, maxPending?: number);
175
178
  enqueue(task: HmrDeliveryTask): void;
176
179
  retry(): void;
177
180
  reset(): void;
181
+ /** 等待已入队交付及等待期间的新代次;失败向调用方传播,不重试或改变队列。 */
182
+ whenSettled(): Promise<void>;
178
183
  close(): Promise<void>;
179
184
  private prepare;
180
185
  private dispose;
package/dist/index.mjs CHANGED
@@ -306,7 +306,10 @@ var HmrDeliveryCoordinator = class {
306
306
  pending = [];
307
307
  running;
308
308
  failed = false;
309
+ failure;
310
+ revision = 0;
309
311
  pendingBytes = 0;
312
+ disposals = /* @__PURE__ */ new Set();
310
313
  constructor(onError, requestResynchronization, maxPending = maxRetainedDeltaCount) {
311
314
  this.onError = onError;
312
315
  this.requestResynchronization = requestResynchronization;
@@ -326,6 +329,7 @@ var HmrDeliveryCoordinator = class {
326
329
  task,
327
330
  generation: this.generation
328
331
  };
332
+ this.revision += 1;
329
333
  this.prepare(item);
330
334
  this.pending.push(item);
331
335
  this.pendingBytes += task.bytes ?? 0;
@@ -333,22 +337,42 @@ var HmrDeliveryCoordinator = class {
333
337
  }
334
338
  retry() {
335
339
  this.failed = false;
340
+ this.failure = void 0;
341
+ this.revision += 1;
336
342
  if (!this.running && !this.closed) this.running = this.drain().finally(() => {
337
343
  this.running = void 0;
338
344
  if (this.pending.length && !this.failed && !this.closed) this.retry();
339
345
  });
340
346
  }
341
347
  reset() {
348
+ this.revision += 1;
342
349
  this.generation += 1;
343
350
  for (const item of this.pending) this.dispose(item);
344
351
  this.pending = [];
345
352
  this.pendingBytes = 0;
346
353
  this.failed = false;
354
+ this.failure = void 0;
355
+ }
356
+ /** 等待已入队交付及等待期间的新代次;失败向调用方传播,不重试或改变队列。 */
357
+ async whenSettled() {
358
+ for (;;) {
359
+ const revision = this.revision;
360
+ try {
361
+ await this.running;
362
+ await Promise.all(this.disposals);
363
+ } catch (error) {
364
+ throw this.failed ? this.failure : error;
365
+ }
366
+ if (this.closed) throw new Error("HMR delivery coordinator closed before settlement");
367
+ if (this.failed) throw this.failure;
368
+ if (revision === this.revision && !this.running && this.pending.length === 0 && this.disposals.size === 0) return;
369
+ }
347
370
  }
348
371
  async close() {
349
372
  this.closed = true;
350
373
  this.reset();
351
374
  await this.running;
375
+ await Promise.all(this.disposals);
352
376
  }
353
377
  prepare(item) {
354
378
  if (!item.preparation) {
@@ -359,12 +383,16 @@ var HmrDeliveryCoordinator = class {
359
383
  }
360
384
  return item.preparation;
361
385
  }
362
- async dispose(item) {
363
- if (item.disposed) return;
364
- item.disposed = true;
365
- try {
366
- await (await item.preparation)?.dispose?.();
367
- } catch {}
386
+ dispose(item) {
387
+ if (item.disposal) return item.disposal;
388
+ item.disposal = (async () => {
389
+ try {
390
+ await (await item.preparation)?.dispose?.();
391
+ } catch {}
392
+ })();
393
+ this.disposals.add(item.disposal);
394
+ item.disposal.then(() => this.disposals.delete(item.disposal));
395
+ return item.disposal;
368
396
  }
369
397
  async drain() {
370
398
  while (!this.closed && this.pending.length) {
@@ -386,6 +414,7 @@ var HmrDeliveryCoordinator = class {
386
414
  } catch (error) {
387
415
  if (item.generation === this.generation && !this.closed) {
388
416
  this.failed = true;
417
+ this.failure = error;
389
418
  this.onError(error);
390
419
  if (item.generation === this.generation && this.pending.length > 1) this.requestResynchronization?.();
391
420
  return;
package/package.json CHANGED
@@ -1,14 +1,24 @@
1
1
  {
2
2
  "name": "@weapp-vite/hmr",
3
3
  "type": "module",
4
- "version": "0.1.0",
4
+ "version": "0.2.1",
5
5
  "description": "小程序宿主共享的编译批次、资产提交与交付协调内核",
6
6
  "license": "MIT",
7
+ "homepage": "https://github.com/weapp-vite/weapp-vite#readme",
7
8
  "repository": {
8
9
  "type": "git",
9
10
  "url": "git+https://github.com/weapp-vite/weapp-vite.git",
10
11
  "directory": "packages/hmr"
11
12
  },
13
+ "bugs": {
14
+ "url": "https://github.com/weapp-vite/weapp-vite/issues"
15
+ },
16
+ "keywords": [
17
+ "weapp-vite",
18
+ "weapp",
19
+ "mini-program",
20
+ "hmr"
21
+ ],
12
22
  "sideEffects": false,
13
23
  "exports": {
14
24
  ".": {
@@ -37,7 +47,8 @@
37
47
  "build": "tsdown",
38
48
  "typecheck": "tsc --noEmit -p tsconfig.json",
39
49
  "test": "vitest run -c vitest.config.ts",
40
- "test:types": "tsd --files \"test-d/**/*.test-d.ts\"",
50
+ "test:types": "pnpm run test:types:check",
51
+ "test:types:check": "tsd --files \"test-d/**/*.test-d.ts\"",
41
52
  "lint": "eslint ."
42
53
  }
43
54
  }