2015-11-27 13:39:00 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-11-27 13:39:00 +00:00
|
|
|
*
|
2018-08-09 15:32:04 +00:00
|
|
|
* @flow strict-local
|
2017-09-25 05:57:35 +00:00
|
|
|
* @format
|
2015-11-27 13:39:00 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2016-05-09 15:20:08 +00:00
|
|
|
const NativeModules = require('NativeModules');
|
2016-09-23 18:12:54 +00:00
|
|
|
const Platform = require('Platform');
|
2016-05-09 15:20:08 +00:00
|
|
|
|
2016-09-23 18:12:54 +00:00
|
|
|
const defineLazyObjectProperty = require('defineLazyObjectProperty');
|
2016-08-05 18:19:11 +00:00
|
|
|
const invariant = require('fbjs/lib/invariant');
|
2015-11-27 13:39:00 +00:00
|
|
|
|
2017-09-25 05:57:35 +00:00
|
|
|
const {UIManager} = NativeModules;
|
2016-09-23 18:12:54 +00:00
|
|
|
|
2017-09-25 05:57:35 +00:00
|
|
|
invariant(
|
|
|
|
UIManager,
|
|
|
|
'UIManager is undefined. The native module config is probably incorrect.',
|
|
|
|
);
|
2016-02-23 10:26:11 +00:00
|
|
|
|
2017-03-25 07:37:51 +00:00
|
|
|
// In past versions of ReactNative users called UIManager.takeSnapshot()
|
|
|
|
// However takeSnapshot was moved to ReactNative in order to support flat
|
|
|
|
// bundles and to avoid a cyclic dependency between UIManager and ReactNative.
|
|
|
|
// UIManager.takeSnapshot still exists though. In order to avoid confusion or
|
|
|
|
// accidental usage, mask the method with a deprecation warning.
|
|
|
|
UIManager.__takeSnapshot = UIManager.takeSnapshot;
|
|
|
|
UIManager.takeSnapshot = function() {
|
|
|
|
invariant(
|
|
|
|
false,
|
|
|
|
'UIManager.takeSnapshot should not be called directly. ' +
|
2017-09-25 05:57:35 +00:00
|
|
|
'Use ReactNative.takeSnapshot instead.',
|
2017-03-25 07:37:51 +00:00
|
|
|
);
|
2016-02-23 10:26:11 +00:00
|
|
|
};
|
|
|
|
|
2016-05-09 15:20:08 +00:00
|
|
|
/**
|
|
|
|
* 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(UIManager).forEach(viewName => {
|
|
|
|
const viewConfig = UIManager[viewName];
|
|
|
|
if (viewConfig.Manager) {
|
2016-09-23 18:12:54 +00:00
|
|
|
defineLazyObjectProperty(viewConfig, 'Constants', {
|
2016-05-09 15:20:08 +00:00
|
|
|
get: () => {
|
2016-09-23 18:12:54 +00:00
|
|
|
const viewManager = NativeModules[viewConfig.Manager];
|
2016-09-23 18:12:54 +00:00
|
|
|
const constants = {};
|
2017-09-25 05:57:35 +00:00
|
|
|
viewManager &&
|
|
|
|
Object.keys(viewManager).forEach(key => {
|
|
|
|
const value = viewManager[key];
|
|
|
|
if (typeof value !== 'function') {
|
|
|
|
constants[key] = value;
|
|
|
|
}
|
|
|
|
});
|
2016-05-09 15:20:08 +00:00
|
|
|
return constants;
|
|
|
|
},
|
|
|
|
});
|
2016-09-23 18:12:54 +00:00
|
|
|
defineLazyObjectProperty(viewConfig, 'Commands', {
|
2016-05-09 15:20:08 +00:00
|
|
|
get: () => {
|
2016-09-23 18:12:54 +00:00
|
|
|
const viewManager = NativeModules[viewConfig.Manager];
|
2016-09-23 18:12:54 +00:00
|
|
|
const commands = {};
|
2016-05-09 15:20:08 +00:00
|
|
|
let index = 0;
|
2017-09-25 05:57:35 +00:00
|
|
|
viewManager &&
|
|
|
|
Object.keys(viewManager).forEach(key => {
|
|
|
|
const value = viewManager[key];
|
|
|
|
if (typeof value === 'function') {
|
|
|
|
commands[key] = index++;
|
|
|
|
}
|
|
|
|
});
|
2016-05-09 15:20:08 +00:00
|
|
|
return commands;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2018-03-12 15:40:14 +00:00
|
|
|
} else if (UIManager.ViewManagerNames) {
|
2018-04-28 02:09:34 +00:00
|
|
|
// We want to add all the view managers to the UIManager.
|
|
|
|
// However, the way things are set up, the list of view managers is not known at compile time.
|
|
|
|
// As Prepack runs at compile it, it cannot process this loop.
|
|
|
|
// So we wrap it in a special __residual call, which basically tells Prepack to ignore it.
|
2018-05-11 02:06:46 +00:00
|
|
|
let residual = global.__residual
|
|
|
|
? global.__residual
|
|
|
|
: (_, f, ...args) => f.apply(undefined, args);
|
|
|
|
residual(
|
|
|
|
'void',
|
|
|
|
(UIManager, defineLazyObjectProperty) => {
|
|
|
|
UIManager.ViewManagerNames.forEach(viewManagerName => {
|
|
|
|
defineLazyObjectProperty(UIManager, viewManagerName, {
|
|
|
|
get: () => UIManager.getConstantsForViewManager(viewManagerName),
|
|
|
|
});
|
2018-04-28 02:09:34 +00:00
|
|
|
});
|
2018-05-11 02:06:46 +00:00
|
|
|
},
|
|
|
|
UIManager,
|
|
|
|
defineLazyObjectProperty,
|
|
|
|
);
|
2018-04-28 02:09:34 +00:00
|
|
|
|
|
|
|
// As Prepack now no longer knows which properties exactly the UIManager has,
|
|
|
|
// we also tell Prepack that it has only partial knowledge of the UIManager,
|
|
|
|
// so that any accesses to unknown properties along the global code will fail
|
|
|
|
// when Prepack encounters them.
|
|
|
|
if (global.__makePartial) global.__makePartial(UIManager);
|
2016-05-09 15:20:08 +00:00
|
|
|
}
|
|
|
|
|
2015-11-27 13:39:00 +00:00
|
|
|
module.exports = UIManager;
|