main
js 48 lines 1.37 KB
Raw
1 /**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @flow
8 */
9
10 import type {InternalInstance} from './renderer';
11
12 export function decorate(object: Object, attr: string, fn: Function): Function {
13 const old = object[attr];
14 // $FlowFixMe[missing-this-annot] webpack config needs to be updated to allow `this` type annotations
15 object[attr] = function (instance: InternalInstance) {
16 return fn.call(this, old, arguments);
17 };
18 return old;
19 }
20
21 export function decorateMany(
22 source: Object,
23 fns: {[attr: string]: Function, ...},
24 ): Object {
25 const olds: {[string]: $FlowFixMe} = {};
26 for (const name in fns) {
27 olds[name] = decorate(source, name, fns[name]);
28 }
29 return olds;
30 }
31
32 export function restoreMany(source: Object, olds: Object): void {
33 for (const name in olds) {
34 source[name] = olds[name];
35 }
36 }
37
38 // $FlowFixMe[missing-this-annot] webpack config needs to be updated to allow `this` type annotations
39 export function forceUpdate(instance: InternalInstance): void {
40 if (typeof instance.forceUpdate === 'function') {
41 instance.forceUpdate();
42 } else if (
43 instance.updater != null &&
44 typeof instance.updater.enqueueForceUpdate === 'function'
45 ) {
46 instance.updater.enqueueForceUpdate(this, () => {}, 'forceUpdate');
47 }
48 }