From ea424bb5ee6c8b5853796a811ca78f7437296f34 Mon Sep 17 00:00:00 2001 From: Astrid Sapphire <59789839+SapphicFire@users.noreply.github.com> Date: Sat, 18 Oct 2025 11:32:24 +1100 Subject: [PATCH 1/2] Add the UX Client Script Include --- .../Reusable Debounce/DebounceUtil.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Client-Side Components/UX Client Script Include/Reusable Debounce/DebounceUtil.js diff --git a/Client-Side Components/UX Client Script Include/Reusable Debounce/DebounceUtil.js b/Client-Side Components/UX Client Script Include/Reusable Debounce/DebounceUtil.js new file mode 100644 index 0000000000..508f2fc7c5 --- /dev/null +++ b/Client-Side Components/UX Client Script Include/Reusable Debounce/DebounceUtil.js @@ -0,0 +1,18 @@ +function include() { + class DebounceUtil { + + /** + * @param callbackFunc - callback function following timeout + * @param timeout - debounce timeout + * @param helpers - the helpers object passed from a UX Client Script + */ + static debounce(callbackFunc, timeout = 750, helpers) { + let timer; + return (...args) => { + helpers.timing.clearTimeout(timer); // Clear anything currently in place + timer = helpers.timing.setTimeout(() => { callbackFunc.apply(this, args); }, timeout); + }; + } + } + return DebounceUtil; +} From 65e6e37ca94c00110bb5522094048369949c540e Mon Sep 17 00:00:00 2001 From: Astrid Sapphire <59789839+SapphicFire@users.noreply.github.com> Date: Sat, 18 Oct 2025 12:10:33 +1100 Subject: [PATCH 2/2] Include README and usage --- .../Reusable Debounce/README.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Client-Side Components/UX Client Script Include/Reusable Debounce/README.md diff --git a/Client-Side Components/UX Client Script Include/Reusable Debounce/README.md b/Client-Side Components/UX Client Script Include/Reusable Debounce/README.md new file mode 100644 index 0000000000..1549034179 --- /dev/null +++ b/Client-Side Components/UX Client Script Include/Reusable Debounce/README.md @@ -0,0 +1,32 @@ +## Add a debounce to search fields or other inputs with a client script +Inputs, Typeaheads, and other components that can be used for searching the database, caches, or local storage. However, performing a search for every keypress or other change is often unnecessary and uses more resources than strictly necessary. This UX Client Script Include is a small utility for managing debounces, allowing a 'cool-off' from inputs before performing the activity. + +### Steps +1. Create a new UX Client Script Include (`sys_ux_client_script_include`), using the script from the associated snippet +2. Create a new Client Script in UI Builder, and add the include you created in 1 as a dependency +3. Within the Client Script, import the Script Include as follows, replacing `global.DebounceUtilName` with the scope and UX Client Script Include name: + ```js + const DebounceUtil = imports["global.DebounceUtilName"](); + ``` +4. Within the Client Script, declare a `function` to be called inside the debounce function + +### Example usage +```js +/** +* @param {params} params +* @param {api} params.api +* @param {any} params.event +* @param {any} params.imports +* @param {ApiHelpers} params.helpers +*/ +function handler({api, event, helpers, imports}) { + const DebounceUtil = imports["global.DebounceUtil"](); + var debounceSearch = DebounceUtil.debounce(takeAction, 500, helpers); + debounceSearch(); + + function takeAction(){ + const searchTerm = event.payload.value; + api.setState('fullRefQuery',`nameLIKE${searchTerm}`); + } +} +```