@akanjs/devkit 3.4.0-rc.3 → 3.4.0-rc.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.
@@ -0,0 +1,78 @@
1
+ import { afterEach, beforeEach, expect, mock, spyOn, test } from "bun:test";
2
+ import fs from "node:fs";
3
+ import path from "node:path";
4
+ import type { Logger } from "akanjs/common";
5
+ import type { ChangeBatch } from "akanjs/server";
6
+ import type { App } from "../commandDecorators";
7
+ import { tempDirs, writeText } from "../test/testHelpers";
8
+
9
+ let root = "";
10
+ mock.module("../frontendBuild/watchRootResolver", () => ({
11
+ WatchRootResolver: class {
12
+ async resolve() {
13
+ return [root];
14
+ }
15
+ },
16
+ }));
17
+
18
+ const { DevIdleSuspend } = await import("./devIdleSuspend");
19
+
20
+ const logger = {
21
+ info: () => undefined,
22
+ verbose: () => undefined,
23
+ warn: () => undefined,
24
+ error: () => undefined,
25
+ } as unknown as Logger;
26
+ const makeRoot = tempDirs("akan-idle-suspend-poll-");
27
+ const until = async (seen: () => boolean, timeoutMs: number) => {
28
+ const deadline = Date.now() + timeoutMs;
29
+ while (!seen() && Date.now() < deadline) await new Promise((resolve) => setTimeout(resolve, 20));
30
+ return seen();
31
+ };
32
+
33
+ let previous: string | undefined;
34
+ beforeEach(() => {
35
+ previous = process.env.AKAN_DEV_IDLE_SUSPEND_MS;
36
+ process.env.AKAN_DEV_IDLE_SUSPEND_MS = "5";
37
+ });
38
+ afterEach(() => {
39
+ if (previous === undefined) delete process.env.AKAN_DEV_IDLE_SUSPEND_MS;
40
+ else process.env.AKAN_DEV_IDLE_SUSPEND_MS = previous;
41
+ });
42
+
43
+ test("a save wakes the suspended host though the file watcher never reports it", async () => {
44
+ root = await makeRoot();
45
+ const source = path.join(root, "webkit/useShell.tsx");
46
+ await writeText(source, "export const x = 0;\n");
47
+ const silentWatcher = { on: () => silentWatcher, close: () => undefined };
48
+ const watch = spyOn(fs, "watch").mockImplementation((() => silentWatcher) as unknown as typeof fs.watch);
49
+ let released = 0;
50
+ const wokenWith: (ChangeBatch | null)[] = [];
51
+ const idle = new DevIdleSuspend(
52
+ {} as App,
53
+ { logger },
54
+ {
55
+ readiness: () => ({ builderReady: true, backendReady: true, buildFailed: false, restartPending: false }),
56
+ release: () => {
57
+ released++;
58
+ },
59
+ wake: async (batch) => {
60
+ wokenWith.push(batch);
61
+ },
62
+ wakeFailed: () => undefined,
63
+ flushHeld: () => undefined,
64
+ awake: () => undefined,
65
+ },
66
+ );
67
+ try {
68
+ idle.arm();
69
+ expect(await until(() => released === 1, 5_000)).toBe(true);
70
+ await writeText(source, "export const x = 10;\n");
71
+ expect(await until(() => wokenWith.length > 0, DevIdleSuspend.pollMs * 3)).toBe(true);
72
+ expect(wokenWith[0]?.files).toEqual([source]);
73
+ expect(watch).toHaveBeenCalled();
74
+ } finally {
75
+ idle.stop();
76
+ watch.mockRestore();
77
+ }
78
+ }, 15_000);
@@ -16,6 +16,8 @@ interface DevIdleSuspendLinks {
16
16
  }
17
17
 
18
18
  export class DevIdleSuspend {
19
+ //? fs.watch events can arrive a minute late or never (a wedged macOS fseventsd), and nothing else wakes the host.
20
+ static readonly pollMs = 2_000;
19
21
  readonly #app: App;
20
22
  readonly #logger: Logger;
21
23
  readonly #links: DevIdleSuspendLinks;
@@ -161,6 +163,7 @@ export class DevIdleSuspend {
161
163
  watcher = new HmrWatcher({
162
164
  roots,
163
165
  logger: this.#logger,
166
+ pollMs: DevIdleSuspend.pollMs,
164
167
  onBatch: (batch) => {
165
168
  this.#recordSuspendedChange(batch);
166
169
  void this.wake(`${batch.files.length} file(s) changed`);
@@ -14,6 +14,7 @@ export interface WatcherOptions {
14
14
  onBatch: (batch: ChangeBatch) => void | Promise<void>;
15
15
  /** Delay (ms, default 250) before the mtime re-check; must outlast Bun's ~200ms fs.watch coalescing window. */
16
16
  verifyDelayMs?: number;
17
+ pollMs?: number;
17
18
  }
18
19
 
19
20
  // Bun delivers at least one fs.watch event per coalescing window but drops the other paths, so events decide
@@ -22,6 +23,7 @@ export class HmrWatcher {
22
23
  readonly #roots: string[];
23
24
  readonly #debounceMs: number;
24
25
  readonly #verifyDelayMs: number;
26
+ readonly #pollMs: number | null;
25
27
  readonly #onBatch: WatcherOptions["onBatch"];
26
28
  readonly #logger: Logger;
27
29
  readonly #watchers: fs.FSWatcher[] = [];
@@ -32,6 +34,7 @@ export class HmrWatcher {
32
34
  readonly #scans = new Set<Promise<void>>();
33
35
  #timer: ReturnType<typeof setTimeout> | null = null;
34
36
  #verifyTimer: ReturnType<typeof setTimeout> | null = null;
37
+ #pollTimer: ReturnType<typeof setTimeout> | null = null;
35
38
  #windowStartedAt: number | null = null;
36
39
  #stopped = false;
37
40
  #flushing = false;
@@ -43,6 +46,7 @@ export class HmrWatcher {
43
46
  this.#roots = [...new Set(opts.roots.map((r) => path.resolve(r)))];
44
47
  this.#debounceMs = opts.debounceMs ?? HmrWatcher.#envDebounceMs() ?? 30;
45
48
  this.#verifyDelayMs = opts.verifyDelayMs ?? 250;
49
+ this.#pollMs = opts.pollMs ?? null;
46
50
  this.#onBatch = opts.onBatch;
47
51
  this.#logger = opts.logger;
48
52
  this.#index = new SourceMtimeIndex({ roots: this.#roots, classifier: this.#classifier });
@@ -79,6 +83,7 @@ export class HmrWatcher {
79
83
  // At boot: edits under an unreadable root never rebuild, so no later save would surface it.
80
84
  this.#reportCoverageGaps();
81
85
  this.#logger.verbose(`[hmr] tracking ${this.#index.trackedFileCount} source files for change verification`);
86
+ this.#schedulePoll();
82
87
  } catch (err) {
83
88
  this.#logger.error(
84
89
  `[hmr] mtime index unavailable; falling back to watcher events alone, which drop concurrent saves: ${(err as Error).message}`,
@@ -90,6 +95,7 @@ export class HmrWatcher {
90
95
  this.#stopped = true;
91
96
  if (this.#timer) clearTimeout(this.#timer);
92
97
  if (this.#verifyTimer) clearTimeout(this.#verifyTimer);
98
+ if (this.#pollTimer) clearTimeout(this.#pollTimer);
93
99
  for (const w of this.#watchers) {
94
100
  try {
95
101
  w.close();
@@ -252,6 +258,15 @@ export class HmrWatcher {
252
258
  }, this.#verifyDelayMs);
253
259
  }
254
260
 
261
+ #schedulePoll(): void {
262
+ if (this.#stopped || this.#pollMs === null) return;
263
+ this.#pollTimer = setTimeout(() => {
264
+ this.#pollTimer = null;
265
+ void this.#verify().finally(() => this.#schedulePoll());
266
+ }, this.#pollMs);
267
+ this.#pollTimer.unref();
268
+ }
269
+
255
270
  // Terminates: build output lands in ignored .akan/ and codegen writes are content-guarded, so a rebuild moves no
256
271
  // tracked mtime; only an unsettled directory schedules another scan.
257
272
  async #verify(): Promise<void> {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@akanjs/devkit",
3
- "version": "3.4.0-rc.3",
3
+ "version": "3.4.0-rc.4",
4
4
  "sourceType": "module",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -52,10 +52,10 @@
52
52
  "./workspace/getRelatedCnsts": "./workspace/getRelatedCnsts.ts"
53
53
  },
54
54
  "dependencies": {
55
- "@akanjs/language": "3.4.0-rc.3",
55
+ "@akanjs/language": "3.4.0-rc.4",
56
56
  "@inquirer/prompts": "^8.7.3",
57
57
  "@tailwindcss/node": "^4.3.3",
58
- "akanjs": "3.4.0-rc.3",
58
+ "akanjs": "3.4.0-rc.4",
59
59
  "chalk": "^6.0.1",
60
60
  "commander": "^15.0.0",
61
61
  "dayjs": "^1.11.23",