Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debounce: make it possible to inmediately return the initial value #990

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions ember-resources/src/util/debounce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class TrackedValue<T> {
*
* A utility for debouncing high-frequency updates.
* The returned value will only be updated every `ms` and is
* initially undefined.
* initially undefined (unless you pass `true` as argument for the `initialize` parameter).
*
* This can be useful when a user's typing is updating a tracked
* property and you want to derive data less frequently than on
Expand Down Expand Up @@ -63,17 +63,28 @@ class TrackedValue<T> {
*
* @param {number} ms delay in milliseconds to wait before updating the returned value
* @param {() => Value} thunk function that returns the value to debounce
* @param {boolean} initialize whether the initial value should be returned inmediately, or undefined should be returned
*/
export function debounce<Value = unknown>(ms: number, thunk: () => Value) {
export function debounce<Value = unknown>(ms: number, thunk: () => Value, initialize: boolean = false) {
let lastValue: Value;
let timer: number;
let state = new TrackedValue<Value>();

/**
* Whether the initial value has been returned inmediately or not
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps? https://limber.glimdown.com/edit?c=AYjmCsGcAIBsEsBuBTAUPAtgBwPYCcAXaAYR2xwDtkKiAzPM6AcgAFQEMNk8B6AYzK4qNJgG505QtADe0PMkg4Arnj7IAYgEM%2BBfAE8ANHIXLVyI0sjnoa2LGgBfaPUZNkGAEbcAtPMUq1SDEJXClZAjxtAGtkABNHZwYMZgjouO8PJXhYAm94CiDxTFCiWUoEl2TWdy9eDBxY%2BFp4bmDi-FLobQJ4cqdK5hYa7h4cD3BkHWDUZAAPEucl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Z7aMP1iThwaQKOQLHhpEczGZY2NROoxWO7Y4%2B0o7EnYleGrdG1HtQM7WAdzP1lfs701hB721GTVNdZdKLDdeBOzs8g8WIsw7k89BznwzsPnJTKs3O3LavLL3yr3zxsCvcVnE1WvKkvqdHUDplm2dKkBgCAA&format=glimdown

*/
let wasInitialized = !initialize;

return resource(({ on }) => {
lastValue = thunk();

on.cleanup(() => timer && clearTimeout(timer));
timer = setTimeout(() => (state.value = lastValue), ms);
if (!wasInitialized) {
state.value = lastValue;
wasInitialized = true;
} else {
on.cleanup(() => timer && clearTimeout(timer));
timer = setTimeout(() => (state.value = lastValue), ms);
}

return state.value;
});
Expand Down
61 changes: 56 additions & 5 deletions test-app/tests/utils/debounce/js-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module('Utils | debounce | js', function (hooks) {
module('debounce', function () {
test('works with @use', async function (assert) {
class Test {
@tracked data = '';
@tracked data = 'initial';

@use text = debounce(100, () => this.data);
}
Expand All @@ -23,24 +23,75 @@ module('Utils | debounce | js', function (hooks) {

setOwner(test, this.owner);

assert.strictEqual(test.text, undefined);
assert.strictEqual(test.text, undefined, 'Initial value is undefined');

test.data = 'b';

// 25ms
await someTime();
assert.strictEqual(test.text, undefined, 'Initial value is undefined');
test.data = 'bo';

// another 25ms (= ~50ms < 100ms debounce)
await someTime();
assert.strictEqual(test.text, undefined, 'Initial value is undefined');
test.data = 'boo';

// another 25ms (= ~75ms < 100ms debounce)
await someTime();
assert.strictEqual(test.text, undefined, 'Initial value is undefined');

// 110ms > 100ms debounce, value should be set now
await someTime(110);
assert.strictEqual(test.text, 'boo');

// change the value again, wait 0ms, value is not updated yet
test.data = 'boop';
assert.strictEqual(test.text, 'boo');

// wait at least 100ms, value is now updated
await someTime(110);
assert.strictEqual(test.text, 'boop');
});

test('initialize = true', async function (assert) {
class Test {
@tracked data = 'initial';

@use text = debounce(100, () => this.data, true);
}

let test = new Test();

setOwner(test, this.owner);

assert.strictEqual(test.text, 'initial');

test.data = 'b';

// 25ms
await someTime();
assert.strictEqual(test.text, undefined);
assert.strictEqual(test.text, 'initial');
test.data = 'bo';

// another 25ms (= ~50ms < 100ms debounce)
await someTime();
assert.strictEqual(test.text, undefined);
assert.strictEqual(test.text, 'initial');
test.data = 'boo';

// another 25ms (= ~75ms < 100ms debounce)
await someTime();
assert.strictEqual(test.text, undefined);
assert.strictEqual(test.text, 'initial');

// 110ms > 100ms debounce, value should be set now
await someTime(110);
assert.strictEqual(test.text, 'boo');

// change the value again, wait 0ms, value is not updated yet
test.data = 'boop';
assert.strictEqual(test.text, 'boo');

// wait at least 100ms, value is now updated
await someTime(110);
assert.strictEqual(test.text, 'boop');
});
Expand Down