1 | 'use strict';
|
2 |
|
3 | exports.__esModule = true;
|
4 | exports.inject = exports.checkLifeCycleMethods = exports.mergeComponents = undefined;
|
5 |
|
6 | var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
|
7 |
|
8 | var _utils = require('./utils');
|
9 |
|
10 | var _constants = require('./constants');
|
11 |
|
12 | var _config = require('./config');
|
13 |
|
14 | var _config2 = _interopRequireDefault(_config);
|
15 |
|
16 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
17 |
|
18 | function mergeComponents(ProxyComponent, NextComponent, InitialComponent, lastInstance, injectedMembers) {
|
19 | var injectedCode = {};
|
20 | try {
|
21 | var nextInstance = (0, _utils.safeReactConstructor)(NextComponent, lastInstance);
|
22 |
|
23 | try {
|
24 |
|
25 | (0, _utils.deepPrototypeUpdate)(InitialComponent, NextComponent);
|
26 | } catch (e) {
|
27 |
|
28 | }
|
29 |
|
30 | var proxyInstance = (0, _utils.safeReactConstructor)(ProxyComponent, lastInstance);
|
31 |
|
32 | if (!nextInstance || !proxyInstance) {
|
33 | return injectedCode;
|
34 | }
|
35 |
|
36 | var mergedAttrs = _extends({}, proxyInstance, nextInstance);
|
37 | var hasRegenerate = proxyInstance[_constants.REGENERATE_METHOD];
|
38 | var ownKeys = (0, _utils.getOwnKeys)(Object.getPrototypeOf(ProxyComponent.prototype));
|
39 | Object.keys(mergedAttrs).forEach(function (key) {
|
40 | if (key.startsWith(_constants.PREFIX)) return;
|
41 | var nextAttr = nextInstance[key];
|
42 | var prevAttr = proxyInstance[key];
|
43 | if (prevAttr && nextAttr) {
|
44 | if ((0, _utils.isNativeFunction)(nextAttr) || (0, _utils.isNativeFunction)(prevAttr)) {
|
45 |
|
46 | var isSameArity = nextAttr.length === prevAttr.length;
|
47 | var existsInPrototype = ownKeys.indexOf(key) >= 0 || ProxyComponent.prototype[key];
|
48 | if (isSameArity && existsInPrototype) {
|
49 | if (hasRegenerate) {
|
50 | injectedCode[key] = 'Object.getPrototypeOf(this)[\'' + key + '\'].bind(this)';
|
51 | } else {
|
52 | _config2.default.logger.warn('React-stand-in:,', 'Non-controlled class', ProxyComponent.name, 'contains a new native or bound function ', key, nextAttr, '. Unable to reproduce');
|
53 | }
|
54 | } else {
|
55 | _config2.default.logger.warn('React-stand-in:', 'Updated class ', ProxyComponent.name, 'contains native or bound function ', key, nextAttr, '. Unable to reproduce, use arrow functions instead.', '(arity: ' + nextAttr.length + '/' + prevAttr.length + ', proto: ' + (existsInPrototype ? 'yes' : 'no'));
|
56 | }
|
57 | return;
|
58 | }
|
59 |
|
60 | var nextString = String(nextAttr);
|
61 | var injectedBefore = injectedMembers[key];
|
62 | if (nextString !== String(prevAttr) || injectedBefore && nextString !== String(injectedBefore)) {
|
63 | if (!hasRegenerate) {
|
64 | if (nextString.indexOf('function') < 0 && nextString.indexOf('=>') < 0) {
|
65 |
|
66 | injectedCode[key] = nextAttr;
|
67 | } else {
|
68 | _config2.default.logger.warn('React-stand-in:', ' Updated class ', ProxyComponent.name, 'had different code for', key, nextAttr, '. Unable to reproduce. Regeneration support needed.');
|
69 | }
|
70 | } else {
|
71 | injectedCode[key] = nextAttr;
|
72 | }
|
73 | }
|
74 | }
|
75 | });
|
76 | } catch (e) {
|
77 | _config2.default.logger.warn('React-stand-in:', e);
|
78 | }
|
79 | return injectedCode;
|
80 | }
|
81 |
|
82 | function checkLifeCycleMethods(ProxyComponent, NextComponent) {
|
83 | try {
|
84 | var p1 = Object.getPrototypeOf(ProxyComponent.prototype);
|
85 | var p2 = NextComponent.prototype;
|
86 | _utils.reactLifeCycleMountMethods.forEach(function (key) {
|
87 | var d1 = Object.getOwnPropertyDescriptor(p1, key) || { value: p1[key] };
|
88 | var d2 = Object.getOwnPropertyDescriptor(p2, key) || { value: p2[key] };
|
89 | if (!(0, _utils.shallowStringsEqual)(d1, d2)) {
|
90 | _config2.default.logger.warn('React-stand-in:', 'You did update', ProxyComponent.name, 's lifecycle method', key, '. Unable to repeat');
|
91 | }
|
92 | });
|
93 | } catch (e) {
|
94 |
|
95 | }
|
96 | }
|
97 |
|
98 | function inject(target, currentGeneration, injectedMembers) {
|
99 | if (target[_constants.GENERATION] !== currentGeneration) {
|
100 | var hasRegenerate = !!target[_constants.REGENERATE_METHOD];
|
101 | Object.keys(injectedMembers).forEach(function (key) {
|
102 | try {
|
103 | if (hasRegenerate) {
|
104 | target[_constants.REGENERATE_METHOD](key, '(function REACT_HOT_LOADER_SANDBOX () {\n var _this = this; // common babel transpile\n var _this2 = this; // common babel transpile \n return ' + injectedMembers[key] + ';\n }).call(this)');
|
105 | } else {
|
106 | target[key] = injectedMembers[key];
|
107 | }
|
108 | } catch (e) {
|
109 | _config2.default.logger.warn('React-stand-in: Failed to regenerate method ', key, ' of class ', target);
|
110 | _config2.default.logger.warn('got error', e);
|
111 | }
|
112 | });
|
113 |
|
114 | target[_constants.GENERATION] = currentGeneration;
|
115 | }
|
116 | }
|
117 |
|
118 | exports.mergeComponents = mergeComponents;
|
119 | exports.checkLifeCycleMethods = checkLifeCycleMethods;
|
120 | exports.inject = inject; |
\ | No newline at end of file |