UNPKG

2.31 kBJavaScriptView Raw
1var REACT_LIFECYCLE_EXCLUSIONS = [
2 'setState',
3 'render',
4 'componentWillMount',
5 'UNSAFE_componentWillMount',
6 'componentDidMount',
7 'componentWillReceiveProps',
8 'UNSAFE_componentWillReceiveProps',
9 'shouldComponentUpdate',
10 'componentWillUpdate',
11 'getSnapshotBeforeUpdate',
12 'UNSAFE_componentWillUpdate',
13 'componentDidUpdate',
14 'componentWillUnmount',
15];
16/**
17 * Allows you to hoist methods, except those in an exclusion set from a source object into a destination object.
18 *
19 * @public
20 * @param destination - The instance of the object to hoist the methods onto.
21 * @param source - The instance of the object where the methods are hoisted from.
22 * @param exclusions - (Optional) What methods to exclude from being hoisted.
23 * @returns An array of names of methods that were hoisted.
24 */
25export function hoistMethods(
26// eslint-disable-next-line @typescript-eslint/no-explicit-any
27destination,
28// eslint-disable-next-line @typescript-eslint/no-explicit-any
29source, exclusions) {
30 if (exclusions === void 0) { exclusions = REACT_LIFECYCLE_EXCLUSIONS; }
31 var hoisted = [];
32 var _loop_1 = function (methodName) {
33 if (typeof source[methodName] === 'function' &&
34 destination[methodName] === undefined &&
35 (!exclusions || exclusions.indexOf(methodName) === -1)) {
36 hoisted.push(methodName);
37 // eslint-disable-next-line @typescript-eslint/no-explicit-any
38 destination[methodName] = function () {
39 var args = [];
40 for (var _i = 0; _i < arguments.length; _i++) {
41 args[_i] = arguments[_i];
42 }
43 source[methodName].apply(source, args);
44 };
45 }
46 };
47 for (var methodName in source) {
48 _loop_1(methodName);
49 }
50 return hoisted;
51}
52/**
53 * Provides a method for convenience to unhoist hoisted methods.
54 *
55 * @public
56 * @param source - The source object upon which methods were hoisted.
57 * @param methodNames - An array of method names to unhoist.
58 */
59// eslint-disable-next-line @typescript-eslint/no-explicit-any
60export function unhoistMethods(source, methodNames) {
61 methodNames.forEach(function (methodName) { return delete source[methodName]; });
62}
63//# sourceMappingURL=hoist.js.map
\No newline at end of file