Skip to content

Commit

Permalink
Merge pull request #6022 from CodeByAlex/feature/knobs-performance-de…
Browse files Browse the repository at this point in the history
…bouncing

Modified the Knobs onChange debounce function so that knob storage occurs on keypress but rendering waits until debouncing has completed
  • Loading branch information
shilman committed Mar 15, 2019
1 parent 51d9915 commit 0c9c82a
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions addons/knobs/src/registerKnobs.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import addons from '@storybook/addons';
import { STORY_CHANGED, FORCE_RE_RENDER, REGISTER_SUBSCRIPTION } from '@storybook/core-events';

import debounce from 'lodash.debounce';

import KnobManager from './KnobManager';
import { CHANGE, CLICK, RESET, SET } from './shared';

export const manager = new KnobManager();
const { knobStore } = manager;
const KNOB_CHANGED_DEBOUNCE_DELAY_MS = 150;
const COMPONENT_FORCE_RENDER_DEBOUNCE_DELAY_MS = 325;

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

// Increased performance by reducing the number of times a component is rendered during knob changes
const debouncedOnKnobChanged = debounce(change => {
const { name, value } = change;

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

knobOptions.value = value;
const debouncedOnKnobChanged = debounce(() => {
knobStore.markAllUnused();

forceReRender();
}, KNOB_CHANGED_DEBOUNCE_DELAY_MS);
}, COMPONENT_FORCE_RENDER_DEBOUNCE_DELAY_MS);

function knobChanged(change) {
debouncedOnKnobChanged(change);
const { name } = change;
const { value } = change; // Update the related knob and it's value.
const knobOptions = knobStore.get(name);
knobOptions.value = value;
debouncedOnKnobChanged();
}

function knobClicked(clicked) {
Expand Down

0 comments on commit 0c9c82a

Please sign in to comment.