Skip to content
This repository was archived by the owner on Mar 1, 2026. It is now read-only.

Commit 87a95f0

Browse files
committed
chore(cli): use single-debounce for debouncing
1 parent f969ec4 commit 87a95f0

1 file changed

Lines changed: 28 additions & 42 deletions

File tree

packages/cli/src/actions/generate.ts

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { invariant } from '@zenstackhq/common-helpers';
1+
import { invariant, singleDebounce } from '@zenstackhq/common-helpers';
22
import { ZModelLanguageMetaData } from '@zenstackhq/language';
33
import { type AbstractDeclaration, isPlugin, LiteralExpr, Plugin, type Model } from '@zenstackhq/language/ast';
44
import { getLiteral, getLiteralArray } from '@zenstackhq/language/utils';
@@ -50,8 +50,6 @@ export async function run(options: Options) {
5050
);
5151

5252
const watchedPaths = getRootModelWatchPaths(model);
53-
let reGenerateSchemaTimeout: ReturnType<typeof setTimeout> | undefined;
54-
let generationInProgress = false;
5553

5654
if (logsEnabled) {
5755
const logPaths = [...watchedPaths].map((at) => `- ${at}`).join('\n');
@@ -65,53 +63,41 @@ export async function run(options: Options) {
6563
ignored: (at) => !schemaExtensions.some((ext) => at.endsWith(ext)),
6664
});
6765

68-
const reGenerateSchema = () => {
69-
clearTimeout(reGenerateSchemaTimeout);
66+
// prevent save multiple files and run multiple times
67+
const reGenerateSchema = singleDebounce(async () => {
68+
if (logsEnabled) {
69+
console.log('Got changes, run generation!');
70+
}
7071

71-
// prevent save multiple files and run multiple times
72-
reGenerateSchemaTimeout = setTimeout(async () => {
73-
if (generationInProgress) {
74-
return;
75-
}
72+
try {
73+
const newModel = await pureGenerate(options, true);
74+
const allModelsPaths = getRootModelWatchPaths(newModel);
75+
const newModelPaths = [...allModelsPaths].filter((at) => !watchedPaths.has(at));
76+
const removeModelPaths = [...watchedPaths].filter((at) => !allModelsPaths.has(at));
7677

77-
generationInProgress = true;
78+
if (newModelPaths.length) {
79+
if (logsEnabled) {
80+
const logPaths = newModelPaths.map((at) => `- ${at}`).join('\n');
81+
console.log(`Added file(s) to watch:\n${logPaths}`);
82+
}
7883

79-
if (logsEnabled) {
80-
console.log('Got changes, run generation!');
84+
newModelPaths.forEach((at) => watchedPaths.add(at));
85+
watcher.add(newModelPaths);
8186
}
8287

83-
try {
84-
const newModel = await pureGenerate(options, true);
85-
const allModelsPaths = getRootModelWatchPaths(newModel);
86-
const newModelPaths = [...allModelsPaths].filter((at) => !watchedPaths.has(at));
87-
const removeModelPaths = [...watchedPaths].filter((at) => !allModelsPaths.has(at));
88-
89-
if (newModelPaths.length) {
90-
if (logsEnabled) {
91-
const logPaths = newModelPaths.map((at) => `- ${at}`).join('\n');
92-
console.log(`Added file(s) to watch:\n${logPaths}`);
93-
}
94-
95-
newModelPaths.forEach((at) => watchedPaths.add(at));
96-
watcher.add(newModelPaths);
88+
if (removeModelPaths.length) {
89+
if (logsEnabled) {
90+
const logPaths = removeModelPaths.map((at) => `- ${at}`).join('\n');
91+
console.log(`Removed file(s) from watch:\n${logPaths}`);
9792
}
9893

99-
if (removeModelPaths.length) {
100-
if (logsEnabled) {
101-
const logPaths = removeModelPaths.map((at) => `- ${at}`).join('\n');
102-
console.log(`Removed file(s) from watch:\n${logPaths}`);
103-
}
104-
105-
removeModelPaths.forEach((at) => watchedPaths.delete(at));
106-
watcher.unwatch(removeModelPaths);
107-
}
108-
} catch (e) {
109-
console.error(e);
94+
removeModelPaths.forEach((at) => watchedPaths.delete(at));
95+
watcher.unwatch(removeModelPaths);
11096
}
111-
112-
generationInProgress = false;
113-
}, 500);
114-
};
97+
} catch (e) {
98+
console.error(e);
99+
}
100+
}, 500, true);
115101

116102
watcher.on('unlink', (pathAt) => {
117103
if (logsEnabled) {

0 commit comments

Comments
 (0)