From ace3c2dc177345f683ead77146ecbe7667ba924f Mon Sep 17 00:00:00 2001 From: Dawid Date: Tue, 10 Sep 2024 14:50:17 +0200 Subject: [PATCH 1/5] throttle reload command --- .../src/commands/start/attachKeyHandlers.js | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/community-cli-plugin/src/commands/start/attachKeyHandlers.js b/packages/community-cli-plugin/src/commands/start/attachKeyHandlers.js index fcb387691e40dc..1f1c8ad42cc5d2 100644 --- a/packages/community-cli-plugin/src/commands/start/attachKeyHandlers.js +++ b/packages/community-cli-plugin/src/commands/start/attachKeyHandlers.js @@ -19,6 +19,18 @@ import fetch from 'node-fetch'; const CTRL_C = '\u0003'; const CTRL_D = '\u0004'; +const reloadTimeout = 700; + +const throttle = (callback, timeout) => { + let previousCall = 0; + return () => { + const currentCall = new Date().getTime(); + if (currentCall - previousCall > timeout) { + previousCall = currentCall; + callback(); + } + }; +}; export default function attachKeyHandlers({ cliConfig, @@ -41,11 +53,15 @@ export default function attachKeyHandlers({ env: {FORCE_COLOR: chalk.supportsColor ? 'true' : 'false'}, }; + const reload = throttle(() => { + logger.info('Reloading connected app(s)...'); + messageSocket.broadcast('reload', null); + }, reloadTimeout); + const onPress = async (key: string) => { switch (key.toLowerCase()) { case 'r': - logger.info('Reloading connected app(s)...'); - messageSocket.broadcast('reload', null); + reload(); break; case 'd': logger.info('Opening Dev Menu...'); From 54a5e177beb82eea977609dfb510d3e26a672fd6 Mon Sep 17 00:00:00 2001 From: Dawid Date: Wed, 11 Sep 2024 08:45:55 +0200 Subject: [PATCH 2/5] added flow types --- .../src/commands/start/attachKeyHandlers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/community-cli-plugin/src/commands/start/attachKeyHandlers.js b/packages/community-cli-plugin/src/commands/start/attachKeyHandlers.js index 1f1c8ad42cc5d2..b26125374ca577 100644 --- a/packages/community-cli-plugin/src/commands/start/attachKeyHandlers.js +++ b/packages/community-cli-plugin/src/commands/start/attachKeyHandlers.js @@ -21,7 +21,7 @@ const CTRL_C = '\u0003'; const CTRL_D = '\u0004'; const reloadTimeout = 700; -const throttle = (callback, timeout) => { +const throttle = (callback: () => void, timeout: number) => { let previousCall = 0; return () => { const currentCall = new Date().getTime(); From ac95f702be85ee04fad5f126b4208231fbabe7df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Ma=C5=82ecki?= Date: Tue, 17 Sep 2024 16:12:29 +0200 Subject: [PATCH 3/5] Update packages/community-cli-plugin/src/commands/start/attachKeyHandlers.js Co-authored-by: Alex Hunt --- .../src/commands/start/attachKeyHandlers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/community-cli-plugin/src/commands/start/attachKeyHandlers.js b/packages/community-cli-plugin/src/commands/start/attachKeyHandlers.js index b26125374ca577..6c49bde5b3ada1 100644 --- a/packages/community-cli-plugin/src/commands/start/attachKeyHandlers.js +++ b/packages/community-cli-plugin/src/commands/start/attachKeyHandlers.js @@ -19,7 +19,7 @@ import fetch from 'node-fetch'; const CTRL_C = '\u0003'; const CTRL_D = '\u0004'; -const reloadTimeout = 700; +const RELOAD_TIMEOUT = 500; const throttle = (callback: () => void, timeout: number) => { let previousCall = 0; From 46745e956e1fd6ee72542b97ed0aade1d3cdb88f Mon Sep 17 00:00:00 2001 From: Dawid Date: Tue, 17 Sep 2024 16:25:42 +0200 Subject: [PATCH 4/5] change throttle to debounce --- .../src/commands/start/attachKeyHandlers.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/packages/community-cli-plugin/src/commands/start/attachKeyHandlers.js b/packages/community-cli-plugin/src/commands/start/attachKeyHandlers.js index 6c49bde5b3ada1..63f27bde7de274 100644 --- a/packages/community-cli-plugin/src/commands/start/attachKeyHandlers.js +++ b/packages/community-cli-plugin/src/commands/start/attachKeyHandlers.js @@ -21,14 +21,11 @@ const CTRL_C = '\u0003'; const CTRL_D = '\u0004'; const RELOAD_TIMEOUT = 500; -const throttle = (callback: () => void, timeout: number) => { - let previousCall = 0; +const debounce = (callback: () => void, timeout: number) => { + let timeoutId; return () => { - const currentCall = new Date().getTime(); - if (currentCall - previousCall > timeout) { - previousCall = currentCall; - callback(); - } + if (timeoutId != null) clearTimeout(timeoutId); + timeoutId = setTimeout(callback, timeout); }; }; @@ -53,10 +50,10 @@ export default function attachKeyHandlers({ env: {FORCE_COLOR: chalk.supportsColor ? 'true' : 'false'}, }; - const reload = throttle(() => { + const reload = debounce(() => { logger.info('Reloading connected app(s)...'); messageSocket.broadcast('reload', null); - }, reloadTimeout); + }, RELOAD_TIMEOUT); const onPress = async (key: string) => { switch (key.toLowerCase()) { From bcd17e8d3f8d7e8bb77b5a4b273bf45d67db3232 Mon Sep 17 00:00:00 2001 From: Dawid Date: Wed, 18 Sep 2024 15:52:19 +0200 Subject: [PATCH 5/5] debounce to throttle --- .../src/commands/start/attachKeyHandlers.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/community-cli-plugin/src/commands/start/attachKeyHandlers.js b/packages/community-cli-plugin/src/commands/start/attachKeyHandlers.js index 63f27bde7de274..82af11a671207a 100644 --- a/packages/community-cli-plugin/src/commands/start/attachKeyHandlers.js +++ b/packages/community-cli-plugin/src/commands/start/attachKeyHandlers.js @@ -21,11 +21,14 @@ const CTRL_C = '\u0003'; const CTRL_D = '\u0004'; const RELOAD_TIMEOUT = 500; -const debounce = (callback: () => void, timeout: number) => { - let timeoutId; +const throttle = (callback: () => void, timeout: number) => { + let previousCallTimestamp = 0; return () => { - if (timeoutId != null) clearTimeout(timeoutId); - timeoutId = setTimeout(callback, timeout); + const currentCallTimestamp = new Date().getTime(); + if (currentCallTimestamp - previousCallTimestamp > timeout) { + previousCallTimestamp = currentCallTimestamp; + callback(); + } }; }; @@ -50,7 +53,7 @@ export default function attachKeyHandlers({ env: {FORCE_COLOR: chalk.supportsColor ? 'true' : 'false'}, }; - const reload = debounce(() => { + const reload = throttle(() => { logger.info('Reloading connected app(s)...'); messageSocket.broadcast('reload', null); }, RELOAD_TIMEOUT);