PaperUIManager.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * Copyright (c) Facebook, Inc. and its 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. * @format
  9. */
  10. 'use strict';
  11. const NativeModules = require('../BatchedBridge/NativeModules');
  12. const Platform = require('../Utilities/Platform');
  13. const UIManagerProperties = require('./UIManagerProperties');
  14. const defineLazyObjectProperty = require('../Utilities/defineLazyObjectProperty');
  15. import NativeUIManager from './NativeUIManager';
  16. const viewManagerConfigs = {};
  17. const triedLoadingConfig = new Set();
  18. let NativeUIManagerConstants = {};
  19. let isNativeUIManagerConstantsSet = false;
  20. function getConstants(): Object {
  21. if (!isNativeUIManagerConstantsSet) {
  22. NativeUIManagerConstants = NativeUIManager.getConstants();
  23. isNativeUIManagerConstantsSet = true;
  24. }
  25. return NativeUIManagerConstants;
  26. }
  27. const UIManagerJS = {
  28. /* $FlowFixMe(>=0.111.0 site=react_native_fb) This comment suppresses an
  29. * error found when Flow v0.111 was deployed. To see the error, delete this
  30. * comment and run Flow. */
  31. ...NativeUIManager,
  32. getConstants(): Object {
  33. return getConstants();
  34. },
  35. getViewManagerConfig: function(viewManagerName: string): any {
  36. if (
  37. viewManagerConfigs[viewManagerName] === undefined &&
  38. NativeUIManager.getConstantsForViewManager
  39. ) {
  40. try {
  41. viewManagerConfigs[
  42. viewManagerName
  43. ] = NativeUIManager.getConstantsForViewManager(viewManagerName);
  44. } catch (e) {
  45. viewManagerConfigs[viewManagerName] = null;
  46. }
  47. }
  48. const config = viewManagerConfigs[viewManagerName];
  49. if (config) {
  50. return config;
  51. }
  52. // If we're in the Chrome Debugger, let's not even try calling the sync
  53. // method.
  54. if (!global.nativeCallSyncHook) {
  55. return config;
  56. }
  57. if (
  58. NativeUIManager.lazilyLoadView &&
  59. !triedLoadingConfig.has(viewManagerName)
  60. ) {
  61. const result = NativeUIManager.lazilyLoadView(viewManagerName);
  62. triedLoadingConfig.add(viewManagerName);
  63. if (result.viewConfig) {
  64. getConstants()[viewManagerName] = result.viewConfig;
  65. lazifyViewManagerConfig(viewManagerName);
  66. }
  67. }
  68. return viewManagerConfigs[viewManagerName];
  69. },
  70. };
  71. // TODO (T45220498): Remove this.
  72. // 3rd party libs may be calling `NativeModules.UIManager.getViewManagerConfig()`
  73. // instead of `UIManager.getViewManagerConfig()` off UIManager.js.
  74. // This is a workaround for now.
  75. // $FlowFixMe
  76. NativeUIManager.getViewManagerConfig = UIManagerJS.getViewManagerConfig;
  77. function lazifyViewManagerConfig(viewName) {
  78. const viewConfig = getConstants()[viewName];
  79. viewManagerConfigs[viewName] = viewConfig;
  80. if (viewConfig.Manager) {
  81. defineLazyObjectProperty(viewConfig, 'Constants', {
  82. get: () => {
  83. const viewManager = NativeModules[viewConfig.Manager];
  84. const constants = {};
  85. viewManager &&
  86. Object.keys(viewManager).forEach(key => {
  87. const value = viewManager[key];
  88. if (typeof value !== 'function') {
  89. constants[key] = value;
  90. }
  91. });
  92. return constants;
  93. },
  94. });
  95. defineLazyObjectProperty(viewConfig, 'Commands', {
  96. get: () => {
  97. const viewManager = NativeModules[viewConfig.Manager];
  98. const commands = {};
  99. let index = 0;
  100. viewManager &&
  101. Object.keys(viewManager).forEach(key => {
  102. const value = viewManager[key];
  103. if (typeof value === 'function') {
  104. commands[key] = index++;
  105. }
  106. });
  107. return commands;
  108. },
  109. });
  110. }
  111. }
  112. /**
  113. * Copies the ViewManager constants and commands into UIManager. This is
  114. * only needed for iOS, which puts the constants in the ViewManager
  115. * namespace instead of UIManager, unlike Android.
  116. */
  117. if (Platform.OS === 'ios') {
  118. Object.keys(getConstants()).forEach(viewName => {
  119. lazifyViewManagerConfig(viewName);
  120. });
  121. } else if (getConstants().ViewManagerNames) {
  122. NativeUIManager.getConstants().ViewManagerNames.forEach(viewManagerName => {
  123. defineLazyObjectProperty(NativeUIManager, viewManagerName, {
  124. get: () => NativeUIManager.getConstantsForViewManager(viewManagerName),
  125. });
  126. });
  127. }
  128. if (!global.nativeCallSyncHook) {
  129. Object.keys(getConstants()).forEach(viewManagerName => {
  130. if (!UIManagerProperties.includes(viewManagerName)) {
  131. if (!viewManagerConfigs[viewManagerName]) {
  132. viewManagerConfigs[viewManagerName] = getConstants()[viewManagerName];
  133. }
  134. defineLazyObjectProperty(NativeUIManager, viewManagerName, {
  135. get: () => {
  136. console.warn(
  137. `Accessing view manager configs directly off UIManager via UIManager['${viewManagerName}'] ` +
  138. `is no longer supported. Use UIManager.getViewManagerConfig('${viewManagerName}') instead.`,
  139. );
  140. return UIManagerJS.getViewManagerConfig(viewManagerName);
  141. },
  142. });
  143. }
  144. });
  145. }
  146. module.exports = UIManagerJS;