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

[Addon: Knobs] - Performance Fix: added debouncing between keystrokes to speed up component rendering #5811

Merged
Merged
Changes from 2 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
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();
}, KNOB_CHANGED_DEBOUNCE_DELAY_MS); // amount of time used to ensure that the user has time to type before re-rendering
}

function knobClicked(clicked) {
Expand Down