-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshallowWithContext.js
149 lines (125 loc) · 3.68 KB
/
shallowWithContext.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
import { aop, hookName, createHook } from 'to-aop';
import { createCloneClass } from 'create-clone-class';
const SHALLOW_CONTEXT_FLAG = '__SHALLOW_CONTEXT_FLAG__';
const classHookAround = createHook(
hookName.aroundMethod,
/.*/,
({ object, property, context, args }) => {
const originalContext = context ? context.context : undefined;
if (
originalContext &&
typeof originalContext === 'object' &&
!Array.isArray(originalContext)
) {
context.context = Object.prototype.hasOwnProperty.call(
originalContext,
SHALLOW_CONTEXT_FLAG,
)
? originalContext.value
: originalContext;
}
const originalResult = Reflect.apply(object[property], context, args);
if (context) {
context.context = originalContext;
}
return originalResult;
},
);
export function createContext(value) {
if (
value &&
Object.prototype.hasOwnProperty.call(value, SHALLOW_CONTEXT_FLAG)
) {
return value;
}
let context = {
value,
};
Reflect.defineProperty(context, SHALLOW_CONTEXT_FLAG, {
value: true,
enumerable: true,
writable: false,
});
return context;
}
export function withContext(Component, context) {
if (!context) {
return Component;
}
const isValidMemoComponent =
typeof Component === 'object' &&
Component.type &&
typeof Component.type === 'function' &&
Component['$$typeof'];
const isValidComponent =
typeof Component === 'function' ||
(typeof Component === 'object' &&
Component.type &&
Component.WrappedComponent) ||
isValidMemoComponent;
if (!isValidComponent) {
throw new TypeError(
`The defined Component must be function or specific react object. You give type of ${typeof Component}.`,
);
}
if (typeof context !== 'object') {
throw new TypeError(
`The defined context must be object. You give type of ${typeof Component}.`,
);
}
const legacyContext = Object.keys(context).reduce(
(accumulatedContext, key) => {
accumulatedContext[key] = () => {};
return accumulatedContext;
},
{},
);
const LegacyContextComponent = cloneComponent(
isValidMemoComponent ? Component.type : Component,
);
LegacyContextComponent.contextTypes = legacyContext;
return LegacyContextComponent;
}
export function cloneComponent(Component) {
if (Component && Component.prototype && Component.prototype.render) {
class LegacyContextComponent extends Component {
constructor(props, context) {
super(
props,
Object.prototype.hasOwnProperty.call(context, SHALLOW_CONTEXT_FLAG)
? context.value
: context,
);
}
}
const CloneLegacyContextComponent = createCloneClass(
LegacyContextComponent,
);
const originalConsoleWarn = console.warn;
console.warn = () => {};
aop(CloneLegacyContextComponent, classHookAround);
console.warn = originalConsoleWarn;
return CloneLegacyContextComponent;
} else {
if (Component.__isCloneComponent__) {
Component = Component.__cloneFromComponent__;
}
let LegacyContextComponent = function (props, context, ...rest) {
return Component.apply(this, [
props,
Object.prototype.hasOwnProperty.call(context, SHALLOW_CONTEXT_FLAG)
? context.value
: context,
...rest,
]);
};
for (let key in Component) {
if (Object.prototype.hasOwnProperty.call(Component, key)) {
LegacyContextComponent[key] = Component[key];
}
}
LegacyContextComponent.__isCloneComponent__ = true;
LegacyContextComponent.__cloneFromComponent__ = Component;
return LegacyContextComponent;
}
}