Skip to content

Commit

Permalink
feat: debounce studio refresh when multiple files change
Browse files Browse the repository at this point in the history
  • Loading branch information
acaldas committed Nov 6, 2024
1 parent 10ebfa9 commit aa8401d
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 deletions studio/vite-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,33 @@ export function watchLocalFiles(
documentModelsPath?: string,
editorsPath?: string,
) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const debounce = (callback: () => unknown, delay = 100) => {
let timeout: NodeJS.Timeout | undefined;
return function () {
clearTimeout(timeout);
timeout = setTimeout(() => {
callback();
}, delay);
};
};

const refreshModelsWithDebounce = debounce(() => {
console.log(`Local document models changed, reloading Connect...`);
server.ws.send({
type: 'full-reload',
path: '*',
});
});

const refreshEditorsWithDebounce = debounce(() => {
console.log(`Local document editors changed, reloading Connect...`);
server.ws.send({
type: 'full-reload',
path: '*',
});
});

if (documentModelsPath) {
// Use fs to watch the file and trigger a server reload when it changes
console.log(
Expand All @@ -66,13 +93,7 @@ export function watchLocalFiles(
recursive: true,
},
(event, filename) => {
console.log(
`Local document models changed, reloading server...`,
);
server.ws.send({
type: 'full-reload',
path: '*',
});
refreshModelsWithDebounce();
},
);
} catch (e) {
Expand All @@ -89,13 +110,7 @@ export function watchLocalFiles(
recursive: true,
},
(event, filename) => {
console.log(
`Local document models changed, reloading server...`,
);
server.ws.send({
type: 'full-reload',
path: '*',
});
refreshEditorsWithDebounce();
},
);
} catch (e) {
Expand Down

0 comments on commit aa8401d

Please sign in to comment.