-
Notifications
You must be signed in to change notification settings - Fork 100
/
react-lite.common.js
2508 lines (2278 loc) · 68 KB
/
react-lite.common.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* react-lite.js v0.15.40
* (c) 2019 Jade Gu
* Released under the MIT License.
*/
'use strict';
var HTML_KEY = 'dangerouslySetInnerHTML';
var SVGNamespaceURI = 'http://www.w3.org/2000/svg';
var COMPONENT_ID = 'liteid';
var VELEMENT = 2;
var VSTATELESS = 3;
var VCOMPONENT = 4;
var VCOMMENT = 5;
var ELEMENT_NODE_TYPE = 1;
var DOC_NODE_TYPE = 9;
var DOCUMENT_FRAGMENT_NODE_TYPE = 11;
/**
* current stateful component's refs property
* will attach to every vnode created by calling component.render method
*/
var refs = null;
function createVnode(vtype, type, props, key, ref) {
var vnode = {
vtype: vtype,
type: type,
props: props,
refs: refs,
key: key,
ref: ref
};
if (vtype === VSTATELESS || vtype === VCOMPONENT) {
vnode.uid = getUid();
}
return vnode;
}
function initVnode(vnode, parentContext, namespaceURI) {
var vtype = vnode.vtype;
var node = null;
if (!vtype) {
// init text
node = document.createTextNode(vnode);
} else if (vtype === VELEMENT) {
// init element
node = initVelem(vnode, parentContext, namespaceURI);
} else if (vtype === VCOMPONENT) {
// init stateful component
node = initVcomponent(vnode, parentContext, namespaceURI);
} else if (vtype === VSTATELESS) {
// init stateless component
node = initVstateless(vnode, parentContext, namespaceURI);
} else if (vtype === VCOMMENT) {
// init comment
node = document.createComment('react-text: ' + (vnode.uid || getUid()));
}
return node;
}
function updateVnode(vnode, newVnode, node, parentContext) {
var vtype = vnode.vtype;
if (vtype === VCOMPONENT) {
return updateVcomponent(vnode, newVnode, node, parentContext);
}
if (vtype === VSTATELESS) {
return updateVstateless(vnode, newVnode, node, parentContext);
}
// ignore VCOMMENT and other vtypes
if (vtype !== VELEMENT) {
return node;
}
var oldHtml = vnode.props[HTML_KEY] && vnode.props[HTML_KEY].__html;
if (oldHtml != null) {
updateVelem(vnode, newVnode, node, parentContext);
initVchildren(newVnode, node, parentContext);
} else {
updateVChildren(vnode, newVnode, node, parentContext);
updateVelem(vnode, newVnode, node, parentContext);
}
return node;
}
function updateVChildren(vnode, newVnode, node, parentContext) {
var patches = {
removes: [],
updates: [],
creates: []
};
diffVchildren(patches, vnode, newVnode, node, parentContext);
flatEach(patches.removes, applyDestroy);
flatEach(patches.updates, applyUpdate);
flatEach(patches.creates, applyCreate);
}
function applyUpdate(data) {
if (!data) {
return;
}
var vnode = data.vnode;
var newNode = data.node;
// update
if (!data.shouldIgnore) {
if (!vnode.vtype) {
newNode.replaceData(0, newNode.length, data.newVnode);
} else if (vnode.vtype === VELEMENT) {
updateVelem(vnode, data.newVnode, newNode, data.parentContext);
} else if (vnode.vtype === VSTATELESS) {
newNode = updateVstateless(vnode, data.newVnode, newNode, data.parentContext);
} else if (vnode.vtype === VCOMPONENT) {
newNode = updateVcomponent(vnode, data.newVnode, newNode, data.parentContext);
}
}
// re-order
var currentNode = newNode.parentNode.childNodes[data.index];
if (currentNode !== newNode) {
newNode.parentNode.insertBefore(newNode, currentNode);
}
return newNode;
}
function applyDestroy(data) {
destroyVnode(data.vnode, data.node);
data.node.parentNode.removeChild(data.node);
}
function applyCreate(data) {
var node = initVnode(data.vnode, data.parentContext, data.parentNode.namespaceURI);
data.parentNode.insertBefore(node, data.parentNode.childNodes[data.index]);
}
/**
* Only vnode which has props.children need to call destroy function
* to check whether subTree has component that need to call lify-cycle method and release cache.
*/
function destroyVnode(vnode, node) {
var vtype = vnode.vtype;
if (vtype === VELEMENT) {
// destroy element
destroyVelem(vnode, node);
} else if (vtype === VCOMPONENT) {
// destroy state component
destroyVcomponent(vnode, node);
} else if (vtype === VSTATELESS) {
// destroy stateless component
destroyVstateless(vnode, node);
}
}
function initVelem(velem, parentContext, namespaceURI) {
var type = velem.type;
var props = velem.props;
var node = null;
if (type === 'svg' || namespaceURI === SVGNamespaceURI) {
node = document.createElementNS(SVGNamespaceURI, type);
namespaceURI = SVGNamespaceURI;
} else {
node = document.createElement(type);
}
initVchildren(velem, node, parentContext);
var isCustomComponent = type.indexOf('-') >= 0 || props.is != null;
setProps(node, props, isCustomComponent);
if (velem.ref != null) {
addItem(pendingRefs, velem);
addItem(pendingRefs, node);
}
return node;
}
function initVchildren(velem, node, parentContext) {
var vchildren = node.vchildren = getFlattenChildren(velem);
var namespaceURI = node.namespaceURI;
for (var i = 0, len = vchildren.length; i < len; i++) {
node.appendChild(initVnode(vchildren[i], parentContext, namespaceURI));
}
}
function getFlattenChildren(vnode) {
var children = vnode.props.children;
var vchildren = [];
if (isArr(children)) {
flatEach(children, collectChild, vchildren);
} else {
collectChild(children, vchildren);
}
return vchildren;
}
function collectChild(child, children) {
if (child != null && typeof child !== 'boolean') {
if (!child.vtype) {
// convert immutablejs data
if (child.toJS) {
child = child.toJS();
if (isArr(child)) {
flatEach(child, collectChild, children);
} else {
collectChild(child, children);
}
return;
}
child = '' + child;
}
children[children.length] = child;
}
}
function diffVchildren(patches, vnode, newVnode, node, parentContext) {
if (!node.vchildren) return; // react-lite hasn't seen this DOM node before
var childNodes = node.childNodes;
var vchildren = node.vchildren;
var newVchildren = node.vchildren = getFlattenChildren(newVnode);
var vchildrenLen = vchildren.length;
var newVchildrenLen = newVchildren.length;
if (vchildrenLen === 0) {
if (newVchildrenLen > 0) {
for (var i = 0; i < newVchildrenLen; i++) {
addItem(patches.creates, {
vnode: newVchildren[i],
parentNode: node,
parentContext: parentContext,
index: i
});
}
}
return;
} else if (newVchildrenLen === 0) {
for (var i = 0; i < vchildrenLen; i++) {
addItem(patches.removes, {
vnode: vchildren[i],
node: childNodes[i]
});
}
return;
}
var updates = Array(newVchildrenLen);
var removes = null;
var creates = null;
// isEqual
for (var i = 0; i < vchildrenLen; i++) {
var _vnode = vchildren[i];
for (var j = 0; j < newVchildrenLen; j++) {
if (updates[j]) {
continue;
}
var _newVnode = newVchildren[j];
if (_vnode === _newVnode) {
updates[j] = {
shouldIgnore: shouldIgnoreUpdate(node),
vnode: _vnode,
newVnode: _newVnode,
node: childNodes[i],
parentContext: parentContext,
index: j
};
vchildren[i] = null;
break;
}
}
}
// isSimilar
for (var i = 0; i < vchildrenLen; i++) {
var _vnode2 = vchildren[i];
if (_vnode2 === null) {
continue;
}
var shouldRemove = true;
for (var j = 0; j < newVchildrenLen; j++) {
if (updates[j]) {
continue;
}
var _newVnode2 = newVchildren[j];
if (_newVnode2.type === _vnode2.type && _newVnode2.key === _vnode2.key && _newVnode2.refs === _vnode2.refs) {
updates[j] = {
vnode: _vnode2,
newVnode: _newVnode2,
node: childNodes[i],
parentContext: parentContext,
index: j
};
shouldRemove = false;
break;
}
}
if (shouldRemove) {
if (!removes) {
removes = [];
}
addItem(removes, {
vnode: _vnode2,
node: childNodes[i]
});
}
}
for (var i = 0; i < newVchildrenLen; i++) {
var item = updates[i];
if (!item) {
if (!creates) {
creates = [];
}
addItem(creates, {
vnode: newVchildren[i],
parentNode: node,
parentContext: parentContext,
index: i
});
} else if (item.vnode.vtype === VELEMENT) {
diffVchildren(patches, item.vnode, item.newVnode, item.node, item.parentContext);
}
}
if (removes) {
addItem(patches.removes, removes);
}
if (creates) {
addItem(patches.creates, creates);
}
addItem(patches.updates, updates);
}
function updateVelem(velem, newVelem, node) {
var isCustomComponent = velem.type.indexOf('-') >= 0 || velem.props.is != null;
patchProps(node, velem.props, newVelem.props, isCustomComponent);
if (velem.ref !== newVelem.ref) {
detachRef(velem.refs, velem.ref, node);
attachRef(newVelem.refs, newVelem.ref, node);
}
return node;
}
function destroyVelem(velem, node) {
var props = velem.props;
var vchildren = node.vchildren;
var childNodes = node.childNodes;
if (vchildren) {
for (var i = 0, len = vchildren.length; i < len; i++) {
destroyVnode(vchildren[i], childNodes[i]);
}
}
detachRef(velem.refs, velem.ref, node);
node.eventStore = node.vchildren = null;
}
function initVstateless(vstateless, parentContext, namespaceURI) {
var vnode = renderVstateless(vstateless, parentContext);
var node = initVnode(vnode, parentContext, namespaceURI);
node.cache = node.cache || {};
node.cache[vstateless.uid] = vnode;
return node;
}
function updateVstateless(vstateless, newVstateless, node, parentContext) {
var uid = vstateless.uid;
var vnode = node.cache[uid];
delete node.cache[uid];
var newVnode = renderVstateless(newVstateless, parentContext);
var newNode = compareTwoVnodes(vnode, newVnode, node, parentContext);
newNode.cache = newNode.cache || {};
newNode.cache[newVstateless.uid] = newVnode;
if (newNode !== node) {
syncCache(newNode.cache, node.cache, newNode);
}
return newNode;
}
function destroyVstateless(vstateless, node) {
var uid = vstateless.uid;
var vnode = node.cache[uid];
delete node.cache[uid];
destroyVnode(vnode, node);
}
function renderVstateless(vstateless, parentContext) {
var factory = vstateless.type;
var props = vstateless.props;
var componentContext = getContextByTypes(parentContext, factory.contextTypes);
var vnode = factory(props, componentContext);
if (vnode && vnode.render) {
vnode = vnode.render();
}
if (vnode === null || vnode === false) {
vnode = createVnode(VCOMMENT);
} else if (!vnode || !vnode.vtype) {
throw new Error('@' + factory.name + '#render:You may have returned undefined, an array or some other invalid object');
}
return vnode;
}
function initVcomponent(vcomponent, parentContext, namespaceURI) {
var Component = vcomponent.type;
var props = vcomponent.props;
var uid = vcomponent.uid;
var componentContext = getContextByTypes(parentContext, Component.contextTypes);
var component = new Component(props, componentContext);
var updater = component.$updater;
var cache = component.$cache;
cache.parentContext = parentContext;
updater.isPending = true;
component.props = component.props || props;
component.context = component.context || componentContext;
if (component.componentWillMount) {
component.componentWillMount();
component.state = updater.getState();
}
var vnode = renderComponent(component);
var node = initVnode(vnode, getChildContext(component, parentContext), namespaceURI);
node.cache = node.cache || {};
node.cache[uid] = component;
cache.vnode = vnode;
cache.node = node;
cache.isMounted = true;
addItem(pendingComponents, component);
if (vcomponent.ref != null) {
addItem(pendingRefs, vcomponent);
addItem(pendingRefs, component);
}
return node;
}
function updateVcomponent(vcomponent, newVcomponent, node, parentContext) {
var uid = vcomponent.uid;
var component = node.cache[uid];
var updater = component.$updater;
var cache = component.$cache;
var Component = newVcomponent.type;
var nextProps = newVcomponent.props;
var componentContext = getContextByTypes(parentContext, Component.contextTypes);
delete node.cache[uid];
node.cache[newVcomponent.uid] = component;
cache.parentContext = parentContext;
if (component.componentWillReceiveProps) {
var needToggleIsPending = !updater.isPending;
if (needToggleIsPending) updater.isPending = true;
component.componentWillReceiveProps(nextProps, componentContext);
if (needToggleIsPending) updater.isPending = false;
}
if (vcomponent.ref !== newVcomponent.ref) {
detachRef(vcomponent.refs, vcomponent.ref, component);
attachRef(newVcomponent.refs, newVcomponent.ref, component);
}
updater.emitUpdate(nextProps, componentContext);
return cache.node;
}
function destroyVcomponent(vcomponent, node) {
var uid = vcomponent.uid;
var component = node.cache[uid];
var cache = component.$cache;
delete node.cache[uid];
detachRef(vcomponent.refs, vcomponent.ref, component);
component.setState = component.forceUpdate = noop;
if (component.componentWillUnmount) {
component.componentWillUnmount();
}
destroyVnode(cache.vnode, node);
delete component.setState;
cache.isMounted = false;
cache.node = cache.parentContext = cache.vnode = component.refs = component.context = null;
}
function getContextByTypes(curContext, contextTypes) {
var context = {};
if (!contextTypes || !curContext) {
return context;
}
for (var key in contextTypes) {
if (contextTypes.hasOwnProperty(key)) {
context[key] = curContext[key];
}
}
return context;
}
function renderComponent(component, parentContext) {
refs = component.refs;
var vnode = component.render();
if (vnode === null || vnode === false) {
vnode = createVnode(VCOMMENT);
} else if (!vnode || !vnode.vtype) {
throw new Error('@' + component.constructor.name + '#render:You may have returned undefined, an array or some other invalid object');
}
refs = null;
return vnode;
}
function getChildContext(component, parentContext) {
if (component.getChildContext) {
var curContext = component.getChildContext();
if (curContext) {
parentContext = extend(extend({}, parentContext), curContext);
}
}
return parentContext;
}
var pendingComponents = [];
function clearPendingComponents() {
var len = pendingComponents.length;
if (!len) {
return;
}
var components = pendingComponents;
pendingComponents = [];
var i = -1;
while (len--) {
var component = components[++i];
var updater = component.$updater;
if (component.componentDidMount) {
component.componentDidMount();
}
updater.isPending = false;
updater.emitUpdate();
}
}
var pendingRefs = [];
function clearPendingRefs() {
var len = pendingRefs.length;
if (!len) {
return;
}
var list = pendingRefs;
pendingRefs = [];
for (var i = 0; i < len; i += 2) {
var vnode = list[i];
var refValue = list[i + 1];
attachRef(vnode.refs, vnode.ref, refValue);
}
}
function clearPending() {
clearPendingRefs();
clearPendingComponents();
}
function compareTwoVnodes(vnode, newVnode, node, parentContext) {
var newNode = node;
if (newVnode == null) {
// remove
destroyVnode(vnode, node);
node.parentNode.removeChild(node);
} else if (vnode.type !== newVnode.type || vnode.key !== newVnode.key) {
// replace
destroyVnode(vnode, node);
newNode = initVnode(newVnode, parentContext, node.namespaceURI);
node.parentNode.replaceChild(newNode, node);
} else if (vnode !== newVnode || parentContext) {
// same type and same key -> update
newNode = updateVnode(vnode, newVnode, node, parentContext);
}
return newNode;
}
function getDOMNode() {
return this;
}
function attachRef(refs, refKey, refValue) {
if (refKey == null || !refValue) {
return;
}
if (refValue.nodeName && !refValue.getDOMNode) {
// support react v0.13 style: this.refs.myInput.getDOMNode()
refValue.getDOMNode = getDOMNode;
}
if (isFn(refKey)) {
refKey(refValue);
} else if (refs) {
refs[refKey] = refValue;
}
}
function detachRef(refs, refKey, refValue) {
if (refKey == null) {
return;
}
if (isFn(refKey)) {
refKey(null);
} else if (refs && refs[refKey] === refValue) {
delete refs[refKey];
}
}
function syncCache(cache, oldCache, node) {
for (var key in oldCache) {
if (!oldCache.hasOwnProperty(key)) {
continue;
}
var value = oldCache[key];
cache[key] = value;
// is component, update component.$cache.node
if (value.forceUpdate) {
value.$cache.node = node;
}
}
}
function shouldIgnoreUpdate(node) {
var vchildren = node.vchildren;
var children = node.children;
if (vchildren) {
for (var i = 0; i < vchildren.length; i++) {
var vchild = vchildren[i];
if (vchild.vtype === VCOMPONENT || vchild.vtype === VSTATELESS) {
if (vchild.type.contextTypes) {
return false;
}
}
}
}
if (children) {
for (var i = 0; i < children.length; i++) {
if (!shouldIgnoreUpdate(children[i])) {
return false;
}
}
}
return true;
}
var updateQueue = {
updaters: [],
isPending: false,
add: function add(updater) {
addItem(this.updaters, updater);
},
batchUpdate: function batchUpdate() {
if (this.isPending) {
return;
}
this.isPending = true;
/*
each updater.update may add new updater to updateQueue
clear them with a loop
event bubbles from bottom-level to top-level
reverse the updater order can merge some props and state and reduce the refresh times
see Updater.update method below to know why
*/
var updaters = this.updaters;
var updater = undefined;
while (updater = updaters.pop()) {
updater.updateComponent();
}
this.isPending = false;
}
};
function Updater(instance) {
this.instance = instance;
this.pendingStates = [];
this.pendingCallbacks = [];
this.isPending = false;
this.nextProps = this.nextContext = null;
this.clearCallbacks = this.clearCallbacks.bind(this);
}
Updater.prototype = {
emitUpdate: function emitUpdate(nextProps, nextContext) {
this.nextProps = nextProps;
this.nextContext = nextContext;
// receive nextProps!! should update immediately
nextProps || !updateQueue.isPending ? this.updateComponent() : updateQueue.add(this);
},
updateComponent: function updateComponent() {
var instance = this.instance;
var pendingStates = this.pendingStates;
var nextProps = this.nextProps;
var nextContext = this.nextContext;
if (nextProps || pendingStates.length > 0) {
nextProps = nextProps || instance.props;
nextContext = nextContext || instance.context;
this.nextProps = this.nextContext = null;
// merge the nextProps and nextState and update by one time
shouldUpdate(instance, nextProps, this.getState(), nextContext, this.clearCallbacks);
}
},
addState: function addState(nextState) {
if (nextState) {
addItem(this.pendingStates, nextState);
if (!this.isPending) {
this.emitUpdate();
}
}
},
replaceState: function replaceState(nextState) {
var pendingStates = this.pendingStates;
pendingStates.pop();
// push special params to point out should replace state
addItem(pendingStates, [nextState]);
},
getState: function getState() {
var instance = this.instance;
var pendingStates = this.pendingStates;
var state = instance.state;
var props = instance.props;
if (pendingStates.length) {
state = extend({}, state);
pendingStates.forEach(function (nextState) {
var isReplace = isArr(nextState);
if (isReplace) {
nextState = nextState[0];
}
if (isFn(nextState)) {
nextState = nextState.call(instance, state, props);
}
// replace state
if (isReplace) {
state = extend({}, nextState);
} else {
extend(state, nextState);
}
});
pendingStates.length = 0;
}
return state;
},
clearCallbacks: function clearCallbacks() {
var pendingCallbacks = this.pendingCallbacks;
var instance = this.instance;
if (pendingCallbacks.length > 0) {
this.pendingCallbacks = [];
pendingCallbacks.forEach(function (callback) {
return callback.call(instance);
});
}
},
addCallback: function addCallback(callback) {
if (isFn(callback)) {
addItem(this.pendingCallbacks, callback);
}
}
};
function Component(props, context) {
this.$updater = new Updater(this);
this.$cache = { isMounted: false };
this.props = props;
this.state = {};
this.refs = {};
this.context = context;
}
var ReactComponentSymbol = {};
Component.prototype = {
constructor: Component,
isReactComponent: ReactComponentSymbol,
// getChildContext: _.noop,
// componentWillUpdate: _.noop,
// componentDidUpdate: _.noop,
// componentWillReceiveProps: _.noop,
// componentWillMount: _.noop,
// componentDidMount: _.noop,
// componentWillUnmount: _.noop,
// shouldComponentUpdate(nextProps, nextState) {
// return true
// },
forceUpdate: function forceUpdate(callback) {
var $updater = this.$updater;
var $cache = this.$cache;
var props = this.props;
var state = this.state;
var context = this.context;
if (!$cache.isMounted) {
return;
}
// if updater is pending, add state to trigger nexttick update
if ($updater.isPending) {
$updater.addState(state);
return;
}
var nextProps = $cache.props || props;
var nextState = $cache.state || state;
var nextContext = $cache.context || context;
var parentContext = $cache.parentContext;
var node = $cache.node;
var vnode = $cache.vnode;
$cache.props = $cache.state = $cache.context = null;
$updater.isPending = true;
if (this.componentWillUpdate) {
this.componentWillUpdate(nextProps, nextState, nextContext);
}
this.state = nextState;
this.props = nextProps;
this.context = nextContext;
var newVnode = renderComponent(this);
var newNode = compareTwoVnodes(vnode, newVnode, node, getChildContext(this, parentContext));
if (newNode !== node) {
newNode.cache = newNode.cache || {};
syncCache(newNode.cache, node.cache, newNode);
}
$cache.vnode = newVnode;
$cache.node = newNode;
clearPending();
if (this.componentDidUpdate) {
this.componentDidUpdate(props, state, context);
}
if (callback) {
callback.call(this);
}
$updater.isPending = false;
$updater.emitUpdate();
},
setState: function setState(nextState, callback) {
var $updater = this.$updater;
$updater.addCallback(callback);
$updater.addState(nextState);
},
replaceState: function replaceState(nextState, callback) {
var $updater = this.$updater;
$updater.addCallback(callback);
$updater.replaceState(nextState);
},
getDOMNode: function getDOMNode() {
var node = this.$cache.node;
return node && node.nodeName === '#comment' ? null : node;
},
isMounted: function isMounted() {
return this.$cache.isMounted;
}
};
function shouldUpdate(component, nextProps, nextState, nextContext, callback) {
var shouldComponentUpdate = true;
if (component.shouldComponentUpdate) {
shouldComponentUpdate = component.shouldComponentUpdate(nextProps, nextState, nextContext);
}
if (shouldComponentUpdate === false) {
component.props = nextProps;
component.state = nextState;
component.context = nextContext || {};
if (callback) {
callback.call(component);
}
return;
}
var cache = component.$cache;
cache.props = nextProps;
cache.state = nextState;
cache.context = nextContext || {};
component.forceUpdate(callback);
}
// event config
var unbubbleEvents = {
/**
* should not bind mousemove in document scope
* even though mousemove event can bubble
*/
onmousemove: 1,
ontouchmove: 1,
onmouseleave: 1,
onmouseenter: 1,
onload: 1,
onunload: 1,
onscroll: 1,
onfocus: 1,
onblur: 1,
onrowexit: 1,
onbeforeunload: 1,
onstop: 1,
ondragdrop: 1,
ondragenter: 1,
ondragexit: 1,
ondraggesture: 1,
ondragover: 1,
oncontextmenu: 1,
onerror: 1,
// media event
onabort: 1,
oncanplay: 1,
oncanplaythrough: 1,
ondurationchange: 1,
onemptied: 1,
onended: 1,
onloadeddata: 1,
onloadedmetadata: 1,
onloadstart: 1,
onencrypted: 1,
onpause: 1,
onplay: 1,
onplaying: 1,
onprogress: 1,
onratechange: 1,
onseeking: 1,
onseeked: 1,
onstalled: 1,
onsuspend: 1,
ontimeupdate: 1,
onvolumechange: 1,
onwaiting: 1
};
function getEventName(key) {
if (key === 'onDoubleClick') {
key = 'ondblclick';
} else if (key === 'onTouchTap') {
key = 'onclick';
}
return key.toLowerCase();
}
// Mobile Safari does not fire properly bubble click events on
// non-interactive elements, which means delegated click listeners do not
// fire. The workaround for this bug involves attaching an empty click
// listener on the target node.
var inMobile = ('ontouchstart' in document);
var emptyFunction = function emptyFunction() {};
var ON_CLICK_KEY = 'onclick';
var eventTypes = {};
function addEvent(elem, eventType, listener) {
eventType = getEventName(eventType);
var eventStore = elem.eventStore || (elem.eventStore = {});
eventStore[eventType] = listener;
if (unbubbleEvents[eventType] === 1) {
elem[eventType] = dispatchUnbubbleEvent;
return;
} else if (!eventTypes[eventType]) {
// onclick -> click
document.addEventListener(eventType.substr(2), dispatchEvent, false);
eventTypes[eventType] = true;
}
if (inMobile && eventType === ON_CLICK_KEY) {
elem.addEventListener('click', emptyFunction, false);
return;
}
var nodeName = elem.nodeName;
if (eventType === 'onchange' && supportInputEvent(elem)) {
addEvent(elem, 'oninput', listener);
}
}
function removeEvent(elem, eventType) {
eventType = getEventName(eventType);
var eventStore = elem.eventStore || (elem.eventStore = {});
delete eventStore[eventType];
if (unbubbleEvents[eventType] === 1) {
elem[eventType] = null;
return;
} else if (inMobile && eventType === ON_CLICK_KEY) {
elem.removeEventListener('click', emptyFunction, false);
return;
}