UNPKG

1.8 kBJavaScriptView Raw
1'use strict';
2
3/**
4 * Returns a function that establishes the first prototype passed to it
5 * as the "source of truth" and patches its methods on subsequent invocations,
6 * also patching current and previous prototypes to forward calls to it.
7 */
8module.exports = function makeAssimilatePrototype() {
9 var storedPrototype,
10 knownPrototypes = [];
11
12 function wrapMethod(key) {
13 return function () {
14 if (storedPrototype[key]) {
15 return storedPrototype[key].apply(this, arguments);
16 }
17 };
18 }
19
20 function patchProperty(proto, key) {
21 proto[key] = storedPrototype[key];
22
23 if (typeof proto[key] !== 'function' ||
24 key === 'type' ||
25 key === 'constructor') {
26 return;
27 }
28
29 proto[key] = wrapMethod(key);
30
31 if (storedPrototype[key].isReactClassApproved) {
32 proto[key].isReactClassApproved = storedPrototype[key].isReactClassApproved;
33 }
34
35 if (proto.__reactAutoBindMap && proto.__reactAutoBindMap[key]) {
36 proto.__reactAutoBindMap[key] = proto[key];
37 }
38 }
39
40 function updateStoredPrototype(freshPrototype) {
41 storedPrototype = {};
42
43 for (var key in freshPrototype) {
44 if (freshPrototype.hasOwnProperty(key)) {
45 storedPrototype[key] = freshPrototype[key];
46 }
47 }
48 }
49
50 function reconcileWithStoredPrototypes(freshPrototype) {
51 knownPrototypes.push(freshPrototype);
52 knownPrototypes.forEach(function (proto) {
53 for (var key in storedPrototype) {
54 patchProperty(proto, key);
55 }
56 });
57 }
58
59 return function assimilatePrototype(freshPrototype) {
60 if (freshPrototype.__isAssimilatedByReactHotAPI) {
61 return;
62 }
63
64 updateStoredPrototype(freshPrototype);
65 reconcileWithStoredPrototypes(freshPrototype);
66 freshPrototype.__isAssimilatedByReactHotAPI = true;
67 };
68};
\No newline at end of file