@jsenv/filesystem 4.10.12 → 4.11.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/filesystem",
3
- "version": "4.10.12",
3
+ "version": "4.11.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -29,7 +29,7 @@
29
29
  "/src/"
30
30
  ],
31
31
  "dependencies": {
32
- "@jsenv/urls": "2.5.3",
32
+ "@jsenv/urls": "2.5.4",
33
33
  "@jsenv/url-meta": "8.5.2",
34
34
  "@jsenv/abort": "4.3.0",
35
35
  "@jsenv/utils": "2.1.2"
@@ -0,0 +1,32 @@
1
+ export const callOnceIdle = (callback, idleMs) => {
2
+ let timeoutId;
3
+ return (...args) => {
4
+ if (timeoutId) {
5
+ clearTimeout(timeoutId);
6
+ }
7
+ timeoutId = setTimeout(() => {
8
+ callback(...args);
9
+ }, idleMs);
10
+ if (timeoutId.unref) {
11
+ timeoutId.unref();
12
+ }
13
+ };
14
+ };
15
+
16
+ export const callOnceIdlePerFile = (callback, idleMs) => {
17
+ const timeoutIdMap = new Map();
18
+ return (fileEvent) => {
19
+ const { relativeUrl } = fileEvent;
20
+ let timeoutId = timeoutIdMap.get(relativeUrl);
21
+ if (timeoutId) {
22
+ clearTimeout(timeoutId);
23
+ }
24
+ timeoutId = setTimeout(() => {
25
+ callback(fileEvent);
26
+ }, idleMs);
27
+ if (timeoutId.unref) {
28
+ timeoutId.unref();
29
+ }
30
+ timeoutIdMap.set(relativeUrl, timeoutId);
31
+ };
32
+ };
@@ -4,6 +4,7 @@ import { readdirSync } from "node:fs";
4
4
  import { assertAndNormalizeDirectoryUrl } from "../path_and_url/directory_url_validation.js";
5
5
  import { readEntryStatSync } from "../read_write/stat/read_entry_stat_sync.js";
6
6
  import { statsToType } from "../read_write/stat/stats_to_type.js";
7
+ import { callOnceIdlePerFile } from "./call_once_idle.js";
7
8
  import { createWatcher } from "./create_watcher.js";
8
9
  import { guardTooFastSecondCallPerFile } from "./guard_second_call.js";
9
10
  import { trackResources } from "./track_resources.js";
@@ -30,6 +31,7 @@ export const registerDirectoryLifecycle = (
30
31
  // For this reason"cooldownBetweenFileEvents" should be reserved to scenarios
31
32
  // like unit tests
32
33
  cooldownBetweenFileEvents = 0,
34
+ idleMs = 50,
33
35
  },
34
36
  ) => {
35
37
  const sourceUrl = assertAndNormalizeDirectoryUrl(source);
@@ -46,6 +48,11 @@ export const registerDirectoryLifecycle = (
46
48
  `removed must be a function or undefined, got ${removed}`,
47
49
  );
48
50
  }
51
+ if (idleMs) {
52
+ if (updated) {
53
+ updated = callOnceIdlePerFile(updated, idleMs);
54
+ }
55
+ }
49
56
  if (cooldownBetweenFileEvents) {
50
57
  if (added) {
51
58
  added = guardTooFastSecondCallPerFile(added, cooldownBetweenFileEvents);
@@ -3,6 +3,7 @@ import { basename, dirname } from "node:path";
3
3
  import { assertAndNormalizeFileUrl } from "../path_and_url/file_url_validation.js";
4
4
  import { readEntryStatSync } from "../read_write/stat/read_entry_stat_sync.js";
5
5
  import { statsToType } from "../read_write/stat/stats_to_type.js";
6
+ import { callOnceIdle } from "./call_once_idle.js";
6
7
  import { createWatcher } from "./create_watcher.js";
7
8
  import { guardTooFastSecondCall } from "./guard_second_call.js";
8
9
  import { trackResources } from "./track_resources.js";
@@ -16,6 +17,7 @@ export const registerFileLifecycle = (
16
17
  notifyExistent = false,
17
18
  keepProcessAlive = true,
18
19
  cooldownBetweenFileEvents = 0,
20
+ idleMs = 50,
19
21
  },
20
22
  ) => {
21
23
  const sourceUrl = assertAndNormalizeFileUrl(source);
@@ -32,6 +34,11 @@ export const registerFileLifecycle = (
32
34
  `removed must be a function or undefined, got ${removed}`,
33
35
  );
34
36
  }
37
+ if (idleMs) {
38
+ if (updated) {
39
+ updated = callOnceIdle(updated, idleMs);
40
+ }
41
+ }
35
42
  if (cooldownBetweenFileEvents) {
36
43
  if (added) {
37
44
  added = guardTooFastSecondCall(added, cooldownBetweenFileEvents);