Skip to content

Commit

Permalink
Merge pull request #5811 from CodeByAlex/performance-fix-knob-change-…
Browse files Browse the repository at this point in the history
…debouncing

[Addon: Knobs] - Performance Fix: added debouncing between keystrokes to speed up component rendering
  • Loading branch information
shilman committed Mar 8, 2019
1 parent 62dfbc3 commit 546a6fd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions addons/knobs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"escape-html": "^1.0.3",
"fast-deep-equal": "^2.0.1",
"global": "^4.3.2",
"lodash.debounce": "^4.0.8",
"prop-types": "^15.6.2",
"qs": "^6.5.2",
"react-color": "^2.17.0",
Expand Down
9 changes: 8 additions & 1 deletion addons/knobs/src/registerKnobs.js
Original file line number Diff line number Diff line change
@@ -1,11 +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;

function forceReRender() {
addons.getChannel().emit(FORCE_RE_RENDER);
Expand All @@ -16,7 +18,8 @@ function setPaneKnobs(timestamp = +new Date()) {
channel.emit(SET, { knobs: knobStore.getAll(), timestamp });
}

function knobChanged(change) {
// 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.
Expand All @@ -26,6 +29,10 @@ function knobChanged(change) {
knobStore.markAllUnused();

forceReRender();
}, KNOB_CHANGED_DEBOUNCE_DELAY_MS);

function knobChanged(change) {
debouncedOnKnobChanged(change);
}

function knobClicked(clicked) {
Expand Down

0 comments on commit 546a6fd

Please sign in to comment.