forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPaperUIManager.js
More file actions
164 lines (146 loc) · 4.94 KB
/
PaperUIManager.js
File metadata and controls
164 lines (146 loc) · 4.94 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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/
'use strict';
const NativeModules = require('../BatchedBridge/NativeModules');
const Platform = require('../Utilities/Platform');
const UIManagerProperties = require('./UIManagerProperties');
const defineLazyObjectProperty = require('../Utilities/defineLazyObjectProperty');
import NativeUIManager from './NativeUIManager';
const viewManagerConfigs = {};
const triedLoadingConfig = new Set();
let NativeUIManagerConstants = {};
let isNativeUIManagerConstantsSet = false;
function getConstants(): Object {
if (!isNativeUIManagerConstantsSet) {
NativeUIManagerConstants = NativeUIManager.getConstants();
isNativeUIManagerConstantsSet = true;
}
return NativeUIManagerConstants;
}
/* $FlowFixMe(>=0.123.0 site=react_native_fb) This comment suppresses an error
* found when Flow v0.123.0 was deployed. To see the error, delete this comment
* and run Flow. */
const UIManagerJS = {
...NativeUIManager,
getConstants(): Object {
return getConstants();
},
getViewManagerConfig: function(viewManagerName: string): any {
if (
viewManagerConfigs[viewManagerName] === undefined &&
NativeUIManager.getConstantsForViewManager
) {
try {
viewManagerConfigs[
viewManagerName
] = NativeUIManager.getConstantsForViewManager(viewManagerName);
} catch (e) {
viewManagerConfigs[viewManagerName] = null;
}
}
const config = viewManagerConfigs[viewManagerName];
if (config) {
return config;
}
// If we're in the Chrome Debugger, let's not even try calling the sync
// method.
if (!global.nativeCallSyncHook) {
return config;
}
if (
NativeUIManager.lazilyLoadView &&
!triedLoadingConfig.has(viewManagerName)
) {
const result = NativeUIManager.lazilyLoadView(viewManagerName);
triedLoadingConfig.add(viewManagerName);
if (result.viewConfig) {
getConstants()[viewManagerName] = result.viewConfig;
lazifyViewManagerConfig(viewManagerName);
}
}
return viewManagerConfigs[viewManagerName];
},
};
// TODO (T45220498): Remove this.
// 3rd party libs may be calling `NativeModules.UIManager.getViewManagerConfig()`
// instead of `UIManager.getViewManagerConfig()` off UIManager.js.
// This is a workaround for now.
// $FlowFixMe
NativeUIManager.getViewManagerConfig = UIManagerJS.getViewManagerConfig;
function lazifyViewManagerConfig(viewName) {
const viewConfig = getConstants()[viewName];
viewManagerConfigs[viewName] = viewConfig;
if (viewConfig.Manager) {
defineLazyObjectProperty(viewConfig, 'Constants', {
get: () => {
const viewManager = NativeModules[viewConfig.Manager];
const constants = {};
viewManager &&
Object.keys(viewManager).forEach(key => {
const value = viewManager[key];
if (typeof value !== 'function') {
constants[key] = value;
}
});
return constants;
},
});
defineLazyObjectProperty(viewConfig, 'Commands', {
get: () => {
const viewManager = NativeModules[viewConfig.Manager];
const commands = {};
let index = 0;
viewManager &&
Object.keys(viewManager).forEach(key => {
const value = viewManager[key];
if (typeof value === 'function') {
commands[key] = index++;
}
});
return commands;
},
});
}
}
/**
* Copies the ViewManager constants and commands into UIManager. This is
* only needed for iOS, which puts the constants in the ViewManager
* namespace instead of UIManager, unlike Android.
*/
if (Platform.OS === 'ios') {
Object.keys(getConstants()).forEach(viewName => {
lazifyViewManagerConfig(viewName);
});
} else if (getConstants().ViewManagerNames) {
NativeUIManager.getConstants().ViewManagerNames.forEach(viewManagerName => {
defineLazyObjectProperty(NativeUIManager, viewManagerName, {
get: () => NativeUIManager.getConstantsForViewManager(viewManagerName),
});
});
}
if (!global.nativeCallSyncHook) {
Object.keys(getConstants()).forEach(viewManagerName => {
if (!UIManagerProperties.includes(viewManagerName)) {
if (!viewManagerConfigs[viewManagerName]) {
viewManagerConfigs[viewManagerName] = getConstants()[viewManagerName];
}
defineLazyObjectProperty(NativeUIManager, viewManagerName, {
get: () => {
console.warn(
`Accessing view manager configs directly off UIManager via UIManager['${viewManagerName}'] ` +
`is no longer supported. Use UIManager.getViewManagerConfig('${viewManagerName}') instead.`,
);
return UIManagerJS.getViewManagerConfig(viewManagerName);
},
});
}
});
}
module.exports = UIManagerJS;