Skip to content

Commit

Permalink
fix(debounce): respect default delay
Browse files Browse the repository at this point in the history
use 200ms delay for debounce and throttle when no parameter is provided

fixes #1150
  • Loading branch information
avrahamcool committed Apr 6, 2021
1 parent c41e390 commit 420829c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
33 changes: 32 additions & 1 deletion packages/__tests__/3-runtime-html/binding-resources.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('3-runtime-html/binding-resources.spec.ts', function () {
}

describe('debounce', function () {
// if we are to turn the v1 behavior of thottle back on
// if we are to turn the v1 behavior of throttle back on
// the following test should be enabled
// ===================================================
// it('works with toView bindings to elements', async function () {
Expand Down Expand Up @@ -80,6 +80,37 @@ describe('3-runtime-html/binding-resources.spec.ts', function () {
// au.dispose();
// });

it('works without parameters', async function () {
@customElement({
name: 'app',
template: `<input ref="receiver" value.bind="value & debounce">`,
})
class App {
public value: string = '0';
public receiver: HTMLInputElement;
}

const { au, host, ctx } = $createFixture();

const component = new App();
au.app({ component, host });
await au.start();

const receiver = component.receiver;

receiver.value = '1';
receiver.dispatchEvent(new ctx.CustomEvent('change'));

await wait(10);
assert.strictEqual(component.value, '0', 'component value pre 200ms');
await wait(200);
assert.strictEqual(component.value, '1', 'component value post 200ms');

await au.stop();

au.dispose();
});

it('works with toView bindings to elements', async function () {
@customElement({
name: 'app',
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-html/src/binding-behaviors/debounce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const defaultDelay = 200;
//
export class DebounceBindingBehavior extends BindingInterceptor {
private readonly taskQueue: TaskQueue;
private readonly opts: QueueTaskOptions = { delay: 0 };
private readonly opts: QueueTaskOptions = { delay: defaultDelay };
private readonly firstArg: IsAssign | null = null;
private task: ITask | null = null;

Expand Down
4 changes: 2 additions & 2 deletions packages/runtime-html/src/binding-behaviors/throttle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const defaultDelay = 200;
export class ThrottleBindingBehavior extends BindingInterceptor {
private readonly taskQueue: TaskQueue;
private readonly platform: IPlatform;
private readonly opts: QueueTaskOptions = { delay: 0 };
private readonly opts: QueueTaskOptions = { delay: defaultDelay };
private readonly firstArg: IsAssign | null = null;
private task: ITask | null = null;
private lastCall: number = 0;
Expand Down Expand Up @@ -75,7 +75,7 @@ export class ThrottleBindingBehavior extends BindingInterceptor {

public $bind(flags: LifecycleFlags, scope: Scope, hostScope: Scope | null): void {
if (this.firstArg !== null) {
const delay = Number(this.firstArg.evaluate(flags, scope, hostScope, this.locator, null));
const delay = Number(this.firstArg.evaluate(flags, scope, hostScope, this.locator, null));
this.opts.delay = this.delay = isNaN(delay) ? defaultDelay : delay;
}
this.binding.$bind(flags, scope, hostScope);
Expand Down

0 comments on commit 420829c

Please sign in to comment.