Skip to content

Commit

Permalink
added debouncing at 155ms between each keypress before the forceReRen…
Browse files Browse the repository at this point in the history
…der function is called
  • Loading branch information
CodeByAlex committed Mar 1, 2019
1 parent 52a0e77 commit 429752a
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions addons/knobs/src/registerKnobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { CHANGE, CLICK, RESET, SET } from './shared';

export const manager = new KnobManager();
const { knobStore } = manager;
const KNOB_CHANGED_DEBOUNCE_DELAY_MS = 155; // approximate average time between keypresses
let timeoutId = null;

function forceReRender() {
addons.getChannel().emit(FORCE_RE_RENDER);
Expand All @@ -17,15 +19,19 @@ function setPaneKnobs(timestamp = +new Date()) {
}

function knobChanged(change) {
const { name, value } = change;
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
// debouncing occurs here to increase performance
const { name, value } = change;

// Update the related knob and it's value.
const knobOptions = knobStore.get(name);
// Update the related knob and it's value.
const knobOptions = knobStore.get(name);

knobOptions.value = value;
knobStore.markAllUnused();
knobOptions.value = value;
knobStore.markAllUnused();

forceReRender();
forceReRender();
}, DEBOUNCE_DELAY_MS); // amount of time used to ensure that the user has time to type before re-rendering
}

function knobClicked(clicked) {
Expand Down

0 comments on commit 429752a

Please sign in to comment.