Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TM] Add spec for UIManager #24902

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions Libraries/ReactNative/NativeUIManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use strict-local and update references to Object which is implicit any?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thymikee object is a desired return result according to the issue filed. In codegen, I think it has a different meaning from any.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to use {[key: string]: mixed} instead of Object. I'm not sure what assumptions codegen is making, but I would expect it (sooner or later) to get data from Flow Type AST

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now, let's stick with Object. The internal codegen isn't very smart yet, so we can improve it over time later.

* @format
*/

'use strict';

import type {TurboModule} from 'RCTExport';
import * as TurboModuleRegistry from 'TurboModuleRegistry';

export interface Spec extends TurboModule {
+getConstants: () => Object;
+getConstantsForViewManager: (viewManagerName: string) => Object;
+getDefaultEventTypes: () => Array<string>;
+playTouchSound: () => void;
+lazilyLoadView: (name: string) => Object; // revisit return
+createView: (
reactTag: number,
viewName: string,
rootTag: number,
props: Object,
) => void;
+updateView: (reactTag: number, viewName: string, props: Object) => void;
+focus: (reactTag: ?number) => void;
+blur: (reactTag: ?number) => void;
+findSubviewIn: (
reactTag: ?number,
point: [number, number],
callback: (
nativeViewTag: number,
left: number,
top: number,
width: number,
height: number,
) => void,
) => void;
+dispatchViewManagerCommand: (
reactTag: ?number,
commandID: number,
commandArgs: ?Array<string | number | boolean>, // is this best?
) => void;
+measure: (
reactTag: ?number,
callback: (
left: number,
top: number,
width: number,
height: number,
pageX: number,
pageY: number,
) => void,
) => void;
+measureInWindow: (
reactTag: number,
callback: (result: Array<number>) => void,
) => void;
+viewIsDescendantOf: (
reactTag: number,
ancestorReactTag: number,
callback: (result: Array<boolean>) => void,
) => void;
+measureLayout: (
reactTag: number,
ancestorReactTag: ?number,
errorCallback: (error: Object) => void,
callback: (result: Array<number>) => void,
) => void;
+measureLayoutRelativeToParent: (
reactTag: number,
errorCallback: (error: Object) => void,
callback: (result: Array<number>) => void,
) => void;
+setJSResponder: (reactTag: number, blockNativeResponder: boolean) => void;
+clearJSResponder: () => void;
+configureNextLayoutAnimation: (
config: Object,
callback: () => void, // check what is returned here
errorCallback: (error: Object) => void,
) => void;
+removeSubviewsFromContainerWithID: (containerID: number) => void;
+replaceExistingNonRootView: (reactTag: number, newReactTag: number) => void;
+setChildren: (containerTag: number, reactTags: Array<number>) => void;
+manageChildren: (
containerTag: number,
moveFromIndices: Array<number>,
moveToIndices: Array<number>,
addChildReactTags: Array<number>,
addAtIndices: Array<number>,
removeAtIndices: Array<number>,
) => void;

// android only
+setLayoutAnimationEnabledExperimental: (enabled: boolean) => void;
+sendAccessibilityEvent: (tag: number, eventType: number) => void;
}

export default TurboModuleRegistry.getEnforcing<Spec>('UIManager');
120 changes: 72 additions & 48 deletions Libraries/ReactNative/UIManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @flow
* @format
*/
'use strict';
Expand All @@ -14,58 +14,79 @@ const Platform = require('../Utilities/Platform');
const UIManagerProperties = require('./UIManagerProperties');

const defineLazyObjectProperty = require('../Utilities/defineLazyObjectProperty');
const invariant = require('invariant');

const {UIManager} = NativeModules;
import NativeUIManager from './NativeUIManager';
import type {Spec} from './NativeUIManager';

const viewManagerConfigs = {};

invariant(
UIManager,
'UIManager is undefined. The native module config is probably incorrect.',
);
interface UIManagerJSInterface extends Spec {
+getViewManagerConfig: (viewManagerName: string) => Object;
createView: (
reactTag: number,
viewName: string,
rootTag: number,
props: Object,
) => void;
updateView: (reactTag: number, viewName: string, props: Object) => void;
manageChildren: (
containerTag: number,
moveFromIndices: Array<number>,
moveToIndices: Array<number>,
addChildReactTags: Array<number>,
addAtIndices: Array<number>,
removeAtIndices: Array<number>,
) => void;
}

const triedLoadingConfig = new Set();
UIManager.getViewManagerConfig = function(viewManagerName: string) {
if (
viewManagerConfigs[viewManagerName] === undefined &&
UIManager.getConstantsForViewManager
) {
try {
viewManagerConfigs[
viewManagerName
] = UIManager.getConstantsForViewManager(viewManagerName);
} catch (e) {
viewManagerConfigs[viewManagerName] = null;
ericlewis marked this conversation as resolved.
Show resolved Hide resolved
const UIManagerJS: UIManagerJSInterface = {
...NativeUIManager,
getViewManagerConfig: function(viewManagerName: string) {
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 (__DEV__) {
if (!global.nativeCallSyncHook) {
const config = viewManagerConfigs[viewManagerName];
if (config) {
return config;
}
}

if (UIManager.lazilyLoadView && !triedLoadingConfig.has(viewManagerName)) {
const result = UIManager.lazilyLoadView(viewManagerName);
triedLoadingConfig.add(viewManagerName);
if (result.viewConfig) {
UIManager[viewManagerName] = result.viewConfig;
lazifyViewManagerConfig(viewManagerName);
// If we're in the Chrome Debugger, let's not even try calling the sync
// method.
if (__DEV__) {
if (!global.nativeCallSyncHook) {
return config;
}
}

if (
NativeUIManager.lazilyLoadView &&
!triedLoadingConfig.has(viewManagerName)
) {
const result = NativeUIManager.lazilyLoadView(viewManagerName);
triedLoadingConfig.add(viewManagerName);
if (result.viewConfig) {
NativeUIManager.getConstants()[viewManagerName] = result.viewConfig;
lazifyViewManagerConfig(viewManagerName);
}
}
}

return viewManagerConfigs[viewManagerName];
return viewManagerConfigs[viewManagerName];
},
};

function lazifyViewManagerConfig(viewName) {
const viewConfig = UIManager[viewName];
const viewConfig = NativeUIManager.getConstants()[viewName];
if (viewConfig.Manager) {
viewManagerConfigs[viewName] = viewConfig;
defineLazyObjectProperty(viewConfig, 'Constants', {
Expand Down Expand Up @@ -106,10 +127,10 @@ function lazifyViewManagerConfig(viewName) {
* namespace instead of UIManager, unlike Android.
*/
if (Platform.OS === 'ios') {
Object.keys(UIManager).forEach(viewName => {
Object.keys(NativeUIManager).forEach(viewName => {
lazifyViewManagerConfig(viewName);
});
} else if (UIManager.ViewManagerNames) {
} else if (NativeUIManager.getConstants().ViewManagerNames) {
// 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.
Expand All @@ -120,13 +141,13 @@ if (Platform.OS === 'ios') {
residual(
'void',
(UIManager, defineLazyObjectProperty) => {
UIManager.ViewManagerNames.forEach(viewManagerName => {
UIManager.getConstants().ViewManagerNames.forEach(viewManagerName => {
defineLazyObjectProperty(UIManager, viewManagerName, {
get: () => UIManager.getConstantsForViewManager(viewManagerName),
});
});
},
UIManager,
NativeUIManager,
defineLazyObjectProperty,
);

Expand All @@ -135,27 +156,30 @@ if (Platform.OS === 'ios') {
// so that any accesses to unknown properties along the global code will fail
// when Prepack encounters them.
if (global.__makePartial) {
global.__makePartial(UIManager);
global.__makePartial(NativeUIManager);
}
}

if (__DEV__) {
Object.keys(UIManager).forEach(viewManagerName => {
Object.keys(NativeUIManager.getConstants()).forEach(viewManagerName => {
if (!UIManagerProperties.includes(viewManagerName)) {
if (!viewManagerConfigs[viewManagerName]) {
viewManagerConfigs[viewManagerName] = UIManager[viewManagerName];
viewManagerConfigs[viewManagerName] = NativeUIManager.getConstants()[
viewManagerName
];
}
defineLazyObjectProperty(UIManager, 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 UIManager.getViewManagerConfig(viewManagerName);

return UIManagerJS.getViewManagerConfig(viewManagerName);
},
});
}
});
}

module.exports = UIManager;
module.exports = UIManagerJS;
4 changes: 2 additions & 2 deletions Libraries/ReactNative/UIManagerStatTracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ const UIManagerStatTracker = {
remove,
) {
incStat('manageChildren', 1);
incStat('move', Object.keys(moveFrom || []).length);
incStat('remove', Object.keys(remove || []).length);
incStat('move', moveFrom.length);
incStat('remove', remove.length);
manageChildrenOrig(tag, moveFrom, moveTo, addTags, addIndices, remove);
};
},
Expand Down
7 changes: 4 additions & 3 deletions Libraries/ReactNative/getNativeComponentAttributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,18 @@ function attachDefaultEventTypes(viewConfig: any) {
// This is supported on UIManager platforms (ex: Android),
// as lazy view managers are not implemented for all platforms.
// See [UIManager] for details on constants and implementations.
if (UIManager.ViewManagerNames || UIManager.LazyViewManagersEnabled) {
const constants = UIManager.getConstants();
if (constants.ViewManagerNames || constants.LazyViewManagersEnabled) {
// Lazy view managers enabled.
viewConfig = merge(viewConfig, UIManager.getDefaultEventTypes());
} else {
viewConfig.bubblingEventTypes = merge(
viewConfig.bubblingEventTypes,
UIManager.genericBubblingEventTypes,
constants.genericBubblingEventTypes,
);
viewConfig.directEventTypes = merge(
viewConfig.directEventTypes,
UIManager.genericDirectEventTypes,
constants.genericDirectEventTypes,
);
}
}
Expand Down