-
Notifications
You must be signed in to change notification settings - Fork 51
/
shouldupdate.js
319 lines (285 loc) · 9.54 KB
/
shouldupdate.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
'use strict';
var isEqual = require('lodash.isequal');
/**
* Directly fetch `shouldComponentUpdate` mixin to use outside of Omniscient.
* You can do this if you don't want to use Omniscients syntactic sugar.
*
* @param {Object} nextProps Next props. Can be objects of cursors, values or immutable structures
* @param {Object} nextState Next state. Can be objects of values or immutable structures
*
* @property {Function} isCursor Get default isCursor
* @property {Function} isEqualState Get default isEqualState
* @property {Function} isEqualProps Get default isEqualProps
* @property {Function} isEqualCursor Get default isEqualCursor
* @property {Function} isEqualImmutable Get default isEqualImmutable
* @property {Function} isImmutable Get default isImmutable
* @property {Function} isIgnorable Get default isIgnorable
* @property {Function} debug Get default debug
*
* @module shouldComponentUpdate
* @returns {Component}
* @api public
*/
module.exports = factory();
/**
* Create a “local” instance of the shouldComponentUpdate with overriden defaults.
*
* ### Options
* ```js
* {
* isCursor: function (cursor), // check if is props
* isEqualCursor: function (oneCursor, otherCursor), // check cursor
* isEqualImmutable: function (oneImmutableStructure, otherImmutableStructure), // check immutable structures
* isEqualState: function (currentState, nextState), // check state
* isImmutable: function (currentState, nextState), // check if object is immutable
* isEqualProps: function (currentProps, nextProps), // check props
* isIgnorable: function (propertyValue, propertyKey), // check if property item is ignorable
* unCursor: function (cursor) // convert from cursor to object
* }
* ```
*
* @param {Object} [Options] Options with defaults to override
*
* @module shouldComponentUpdate.withDefaults
* @returns {Function} shouldComponentUpdate with overriden defaults
* @api public
*/
module.exports.withDefaults = factory;
function factory(methods) {
var debug;
methods = methods || {};
var _isCursor = methods.isCursor || isCursor,
_isEqualCursor = methods.isEqualCursor || isEqualCursor,
_isEqualImmutable = methods.isEqualImmutable || isEqualImmutable,
_isEqualState = methods.isEqualState || isEqualState,
_isEqualProps = methods.isEqualProps || isEqualProps,
_isImmutable = methods.isImmutable || isImmutable,
_isIgnorable = methods.isIgnorable || isIgnorable,
_unCursor = methods.unCursor || unCursor;
var isNotIgnorable = not(or(_isIgnorable, isChildren));
shouldComponentUpdate.isCursor = _isCursor;
shouldComponentUpdate.isEqualState = _isEqualState;
shouldComponentUpdate.isEqualProps = _isEqualProps;
shouldComponentUpdate.isEqualCursor = _isEqualCursor;
shouldComponentUpdate.isEqualImmutable = _isEqualImmutable;
shouldComponentUpdate.isImmutable = _isImmutable;
shouldComponentUpdate.debug = debugFn;
return shouldComponentUpdate;
function shouldComponentUpdate(nextProps, nextState) {
if (nextProps === this.props && nextState === this.state) {
if (debug)
debug.call(this, 'shouldComponentUpdate => false (equal input)');
return false;
}
if (!_isEqualState(this.state, nextState)) {
if (debug)
debug.call(this, 'shouldComponentUpdate => true (state has changed)');
return true;
}
var filteredNextProps = filter(nextProps, isNotIgnorable),
filteredCurrentProps = filter(this.props, isNotIgnorable);
if (!_isEqualProps(filteredCurrentProps, filteredNextProps)) {
if (debug)
debug.call(this, 'shouldComponentUpdate => true (props have changed)');
return true;
}
if (debug) debug.call(this, 'shouldComponentUpdate => false');
return false;
}
/**
* Predicate to check if state is equal. Checks in the tree for immutable structures
* and if it is, check by reference. Does not support cursors.
*
* Override through `shouldComponentUpdate.withDefaults`.
*
* @param {Object} value
* @param {Object} other
*
* @module shouldComponentUpdate.isEqualState
* @returns {Boolean}
* @api public
*/
function isEqualState(value, other) {
return isEqual(value, other, function(current, next) {
if (current === next) return true;
return compare(current, next, _isImmutable, _isEqualImmutable);
});
}
/**
* Predicate to check if props are equal. Checks in the tree for cursors and immutable structures
* and if it is, check by reference.
*
* Override through `shouldComponentUpdate.withDefaults`.
*
* @param {Object} value
* @param {Object} other
*
* @module shouldComponentUpdate.isEqualProps
* @returns {Boolean}
* @api public
*/
function isEqualProps(value, other) {
if (value === other) return true;
var cursorsEqual = compare(value, other, _isCursor, _isEqualCursor);
if (cursorsEqual !== void 0) return cursorsEqual;
var immutableEqual = compare(value, other, _isImmutable, _isEqualImmutable);
if (immutableEqual !== void 0) return immutableEqual;
return isEqual(value, other, function(current, next) {
if (current === next) return true;
var cursorsEqual = compare(current, next, _isCursor, _isEqualCursor);
if (cursorsEqual !== void 0) return cursorsEqual;
return compare(current, next, _isImmutable, _isEqualImmutable);
});
}
/**
* Predicate to check if cursors are equal through reference checks. Uses `unCursor`.
* Override through `shouldComponentUpdate.withDefaults` to support different cursor
* implementations.
*
* @param {Cursor} a
* @param {Cursor} b
*
* @module shouldComponentUpdate.isEqualCursor
* @returns {Boolean}
* @api public
*/
function isEqualCursor(a, b) {
return _unCursor(a) === _unCursor(b);
}
function debugFn(pattern, logFn) {
if (typeof pattern === 'function') {
logFn = pattern;
pattern = void 0;
}
var logger = logFn;
if (!logger && console.debug) {
logger = console.debug.bind(console);
}
if (!logger && console.info) {
logger = console.info.bind(console);
}
var regex = new RegExp(pattern || '.*');
debug = function(str) {
var element = this._reactInternalFiber
? this._reactInternalFiber
: this._reactInternalInstance
? this._reactInternalInstance._currentElement
: this._currentElement;
var key = element && element.key ? ' key=' + element.key : '';
var name = this.constructor.displayName;
if (!key && !name) {
name = 'Unknown';
}
var tag = name + key;
if (regex.test(tag)) logger('<' + tag + '>: ' + str);
};
return debug;
}
}
// Comparator used internally by isEqual implementation. Returns undefined
// if we should do recursive isEqual.
function compare(current, next, typeCheck, equalCheck) {
var isCurrent = typeCheck(current);
var isNext = typeCheck(next);
if (isCurrent && isNext) {
return equalCheck(current, next);
}
if (isCurrent || isNext) {
return false;
}
return void 0;
}
/**
* Predicate to check if immutable structures are equal through reference checks.
* Override through `shouldComponentUpdate.withDefaults` to customize behaviour.
*
* @param {Immutable} a
* @param {Immutable} b
*
* @module shouldComponentUpdate.isEqualImmutable
* @returns {Boolean}
* @api public
*/
function isEqualImmutable(a, b) {
return a === b;
}
/**
* Predicate to check if a potential is an immutable structure or not.
* Override through `shouldComponentUpdate.withDefaults` to support different cursor
* implementations.
*
* @param {maybeImmutable} value to check if it is immutable.
*
* @module shouldComponentUpdate.isImmutable
* @returns {Boolean}
* @api public
*/
var IS_ITERABLE_SENTINEL = '@@__IMMUTABLE_ITERABLE__@@';
function isImmutable(maybeImmutable) {
return !!(maybeImmutable && maybeImmutable[IS_ITERABLE_SENTINEL]);
}
/**
* Transforming function to take in cursor and return a non-cursor.
* Override through `shouldComponentUpdate.withDefaults` to support different cursor
* implementations.
*
* @param {cursor} cursor to transform
*
* @module shouldComponentUpdate.unCursor
* @returns {Object|Number|String|Boolean}
* @api public
*/
function unCursor(cursor) {
return !isCursor(cursor) ? cursor : cursor.deref();
}
/**
* Predicate to check if `potential` is Immutable cursor or not (defaults to duck testing
* Immutable.js cursors). Can override through `.withDefaults()`.
*
* @param {potential} potential to check if is cursor
*
* @module shouldComponentUpdate.isCursor
* @returns {Boolean}
* @api public
*/
function isCursor(potential) {
return !!(potential && typeof potential.deref === 'function');
}
function not(fn) {
return function() {
return !fn.apply(fn, arguments);
};
}
function filter(obj, predicate) {
return Object.keys(obj).reduce(function(acc, key) {
if (predicate(obj[key], key)) {
acc[key] = obj[key];
}
return acc;
}, {});
}
/**
* Predicate to check if a property on props should be ignored or not.
* For now this defaults to ignore if property key is `statics`, but that
* is deprecated behaviour, and will be removed by the next major release.
*
* Override through `shouldComponentUpdate.withDefaults`.
*
* @param {Object} value
* @param {String} key
*
* @module shouldComponentUpdate.isIgnorable
* @returns {Boolean}
* @api public
*/
function isIgnorable(_, key) {
return false;
}
function isChildren(_, key) {
return key === 'children';
}
function or(fn1, fn2) {
return function() {
return fn1.apply(null, arguments) || fn2.apply(null, arguments);
};
}