forked from mobxjs/mobx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
193 lines (173 loc) · 5.97 KB
/
utils.ts
File metadata and controls
193 lines (173 loc) · 5.97 KB
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
let symbolId = 0
function createSymbol(name: string): symbol | string {
if (typeof Symbol === "function") {
return Symbol(name)
}
const symbol = `__$mobx-react ${name} (${symbolId})`
symbolId++
return symbol
}
const createdSymbols = {}
export function newSymbol(name: string): symbol | string {
if (!createdSymbols[name]) {
createdSymbols[name] = createSymbol(name)
}
return createdSymbols[name]
}
export function shallowEqual(objA: any, objB: any): boolean {
//From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js
if (is(objA, objB)) return true
if (typeof objA !== "object" || objA === null || typeof objB !== "object" || objB === null) {
return false
}
const keysA = Object.keys(objA)
const keysB = Object.keys(objB)
if (keysA.length !== keysB.length) return false
for (let i = 0; i < keysA.length; i++) {
if (!Object.hasOwnProperty.call(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) {
return false
}
}
return true
}
function is(x: any, y: any): boolean {
// From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js
if (x === y) {
return x !== 0 || 1 / x === 1 / y
} else {
return x !== x && y !== y
}
}
// based on https://github.com/mridgway/hoist-non-react-statics/blob/master/src/index.js
const hoistBlackList = {
$$typeof: 1,
render: 1,
compare: 1,
type: 1,
childContextTypes: 1,
contextType: 1,
contextTypes: 1,
defaultProps: 1,
getDefaultProps: 1,
getDerivedStateFromError: 1,
getDerivedStateFromProps: 1,
mixins: 1,
displayName: 1,
propTypes: 1
}
export function copyStaticProperties(base: object, target: object): void {
const protoProps = Object.getOwnPropertyNames(Object.getPrototypeOf(base))
Object.getOwnPropertyNames(base).forEach(key => {
if (!hoistBlackList[key] && protoProps.indexOf(key) === -1) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(base, key)!)
}
})
}
/**
* Helper to set `prop` to `this` as non-enumerable (hidden prop)
* @param target
* @param prop
* @param value
*/
export function setHiddenProp(target: object, prop: any, value: any): void {
if (!Object.hasOwnProperty.call(target, prop)) {
Object.defineProperty(target, prop, {
enumerable: false,
configurable: true,
writable: true,
value
})
} else {
target[prop] = value
}
}
/**
* Utilities for patching componentWillUnmount, to make sure @disposeOnUnmount works correctly icm with user defined hooks
* and the handler provided by mobx-react
*/
const mobxMixins = newSymbol("patchMixins")
const mobxPatchedDefinition = newSymbol("patchedDefinition")
export interface Mixins extends Record<string, any> {
locks: number
methods: Array<Function>
}
function getMixins(target: object, methodName: string): Mixins {
const mixins = (target[mobxMixins] = target[mobxMixins] || {})
const methodMixins = (mixins[methodName] = mixins[methodName] || {})
methodMixins.locks = methodMixins.locks || 0
methodMixins.methods = methodMixins.methods || []
return methodMixins
}
function wrapper(realMethod: Function, mixins: Mixins, ...args: Array<any>) {
// locks are used to ensure that mixins are invoked only once per invocation, even on recursive calls
mixins.locks++
try {
let retVal
if (realMethod !== undefined && realMethod !== null) {
retVal = realMethod.apply(this, args)
}
return retVal
} finally {
mixins.locks--
if (mixins.locks === 0) {
mixins.methods.forEach(mx => {
mx.apply(this, args)
})
}
}
}
function wrapFunction(realMethod: Function, mixins: Mixins): (...args: Array<any>) => any {
const fn = function (...args: Array<any>) {
wrapper.call(this, realMethod, mixins, ...args)
}
return fn
}
export function patch(target: object, methodName: string, mixinMethod: Function): void {
const mixins = getMixins(target, methodName)
if (mixins.methods.indexOf(mixinMethod) < 0) {
mixins.methods.push(mixinMethod)
}
const oldDefinition = Object.getOwnPropertyDescriptor(target, methodName)
if (oldDefinition && oldDefinition[mobxPatchedDefinition]) {
// already patched definition, do not repatch
return
}
const originalMethod = target[methodName]
const newDefinition = createDefinition(
target,
methodName,
oldDefinition ? oldDefinition.enumerable : undefined,
mixins,
originalMethod
)
Object.defineProperty(target, methodName, newDefinition)
}
function createDefinition(
target: object,
methodName: string,
enumerable: any,
mixins: Mixins,
originalMethod: Function
): PropertyDescriptor {
let wrappedFunc = wrapFunction(originalMethod, mixins)
return {
[mobxPatchedDefinition]: true,
get: function () {
return wrappedFunc
},
set: function (value) {
if (this === target) {
wrappedFunc = wrapFunction(value, mixins)
} else {
// when it is an instance of the prototype/a child prototype patch that particular case again separately
// since we need to store separate values depending on wether it is the actual instance, the prototype, etc
// e.g. the method for super might not be the same as the method for the prototype which might be not the same
// as the method for the instance
const newDefinition = createDefinition(this, methodName, enumerable, mixins, value)
Object.defineProperty(this, methodName, newDefinition)
}
},
configurable: true,
enumerable: enumerable
}
}