This repository has been archived by the owner on Nov 10, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathKnobManager.js
117 lines (88 loc) · 3.71 KB
/
KnobManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends2 = require('babel-runtime/helpers/extends');
var _extends3 = _interopRequireDefault(_extends2);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _WrapStory = require('./components/WrapStory');
var _WrapStory2 = _interopRequireDefault(_WrapStory);
var _KnobStore = require('./KnobStore');
var _KnobStore2 = _interopRequireDefault(_KnobStore);
var _deepEqual = require('deep-equal');
var _deepEqual2 = _interopRequireDefault(_deepEqual);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// This is used by _mayCallChannel to determine how long to wait to before triggering a panel update
var PANEL_UPDATE_INTERVAL = 400;
var KnobManager = function () {
function KnobManager() {
(0, _classCallCheck3.default)(this, KnobManager);
this.knobStore = null;
this.knobStoreMap = {};
}
(0, _createClass3.default)(KnobManager, [{
key: 'knob',
value: function knob(name, options) {
this._mayCallChannel();
var knobStore = this.knobStore;
var existingKnob = knobStore.get(name);
// We need to return the value set by the knob editor via this.
// But, if the user changes the code for the defaultValue we should set
// that value instead.
if (existingKnob && (0, _deepEqual2.default)(options.value, existingKnob.defaultValue)) {
return existingKnob.value;
}
var defaultValue = options.value;
var knobInfo = (0, _extends3.default)({}, options, {
name: name,
defaultValue: defaultValue
});
knobStore.set(name, knobInfo);
return knobStore.get(name).value;
}
}, {
key: 'wrapStory',
value: function wrapStory(channel, storyFn, context) {
this.channel = channel;
var key = context.kind + ':::' + context.story;
var knobStore = this.knobStoreMap[key];
if (!knobStore) {
knobStore = this.knobStoreMap[key] = new _KnobStore2.default();
}
this.knobStore = knobStore;
knobStore.markAllUnused();
var initialContent = storyFn(context);
var props = { context: context, storyFn: storyFn, channel: channel, knobStore: knobStore, initialContent: initialContent };
return _react2.default.createElement(_WrapStory2.default, props);
}
}, {
key: '_mayCallChannel',
value: function _mayCallChannel() {
var _this = this;
// Re rendering of the story may cause changes to the knobStore. Some new knobs maybe added and
// Some knobs may go unused. So we need to update the panel accordingly. For example remove the
// unused knobs from the panel. This function sends the `setKnobs` message to the channel
// triggering a panel re-render.
if (this.calling) {
// If a call to channel has already registered ignore this call.
// Once the previous call is completed all the changes to knobStore including the one that
// triggered this, will be added to the panel.
// This avoids emitting to the channel within very short periods of time.
return;
}
this.calling = true;
setTimeout(function () {
_this.calling = false;
// emit to the channel and trigger a panel re-render
_this.channel.emit('addon:knobs:setKnobs', _this.knobStore.getAll());
}, PANEL_UPDATE_INTERVAL);
}
}]);
return KnobManager;
}();
exports.default = KnobManager;