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 1 commit
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
Next Next commit
[TM] Add spec for UIManager
  • Loading branch information
ericlewis committed May 16, 2019
commit 4bc2748408d22a498e7345400fd25fbb4931a3f2
112 changes: 112 additions & 0 deletions Libraries/ReactNative/NativeUIManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/**
* 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';

interface Constants extends Object {
[key: string]: Object;
ViewManagerNames: Array<string>;
LazyViewManagersEnabled: boolean;
bubblingEventTypes: Array<string>;
genericBubblingEventTypes: Array<string>;
genericDirectEventTypes: Array<string>;
}

export interface Spec extends TurboModule {
// get constants, seems to be droid only i think
// droid only:
+getConstants: () => Constants;
+getConstantsForViewManager: (viewManagerName: string) => Object;
+getDefaultEventTypes: () => Array<string>;
+playTouchSound: () => void;
// common
+lazilyLoadView: (name: string) => Object; // revisit return
getViewManagerConfig: (viewManagerName: string) => Object; // probably should move this out, it's overwritten
createView: (
reactTag: number,
viewName: string,
rootTag: number,
props: Object,
) => void;
updateView: (reactTag: number, viewName: string, props: Object) => void;
// maybe ios only?
+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;
}

export default TurboModuleRegistry.getEnforcing<Spec>('UIManager');
48 changes: 24 additions & 24 deletions Libraries/ReactNative/UIManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,21 @@ const Platform = require('../Utilities/Platform');
const UIManagerProperties = require('./UIManagerProperties');

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

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

invariant(
UIManager,
'UIManager is undefined. The native module config is probably incorrect.',
);
const viewManagerConfigs = {};

const triedLoadingConfig = new Set();
UIManager.getViewManagerConfig = function(viewManagerName: string) {
NativeUIManager.getViewManagerConfig = function(viewManagerName: string) {
if (
viewManagerConfigs[viewManagerName] === undefined &&
UIManager.getConstantsForViewManager
NativeUIManager.getConstantsForViewManager
) {
try {
viewManagerConfigs[
viewManagerName
] = UIManager.getConstantsForViewManager(viewManagerName);
] = NativeUIManager.getConstantsForViewManager(viewManagerName);
} catch (e) {
viewManagerConfigs[viewManagerName] = null;
}
Expand All @@ -52,11 +47,14 @@ UIManager.getViewManagerConfig = function(viewManagerName: string) {
}
}

if (UIManager.lazilyLoadView && !triedLoadingConfig.has(viewManagerName)) {
const result = UIManager.lazilyLoadView(viewManagerName);
if (
NativeUIManager.lazilyLoadView &&
!triedLoadingConfig.has(viewManagerName)
) {
const result = NativeUIManager.lazilyLoadView(viewManagerName);
triedLoadingConfig.add(viewManagerName);
if (result.viewConfig) {
UIManager[viewManagerName] = result.viewConfig;
NativeUIManager.getConstants()[viewManagerName] = result.viewConfig;
lazifyViewManagerConfig(viewManagerName);
}
}
Expand All @@ -65,7 +63,7 @@ UIManager.getViewManagerConfig = function(viewManagerName: string) {
};

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 +104,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 +118,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 +133,29 @@ 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 NativeUIManager.getViewManagerConfig(viewManagerName);
},
});
}
});
}

module.exports = UIManager;
module.exports = NativeUIManager;
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