Skip to content

Commit

Permalink
Removed non-lazy createReactNativeComponentClass impl
Browse files Browse the repository at this point in the history
There are only a handful of components with JavaScript-defined view configs. It's probably worth making them use the new lazy/async interface as well to be more consistent.
  • Loading branch information
bvaughn committed Aug 31, 2017
1 parent c481b98 commit 32db4bb
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 110 deletions.

This file was deleted.

3 changes: 1 addition & 2 deletions src/renderers/native/ReactNativeFiberEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ const ReactNativeFiber: ReactNativeType = {
ReactNativeComponentTree: require('ReactNativeComponentTree'), // InspectorUtils, ScrollResponder
ReactNativePropRegistry: require('ReactNativePropRegistry'), // flattenStyle, Stylesheet
TouchHistoryMath: require('TouchHistoryMath'), // PanResponder
createReactNativeComponentClass: require('createReactNativeComponentClass'), // eg RCTText, ReactNativeART
lazilyCreateReactNativeComponentClass: require('lazilyCreateReactNativeComponentClass'), // eg RCTView
createReactNativeComponentClass: require('createReactNativeComponentClass'), // eg RCTText, RCTView, ReactNativeART
takeSnapshot: require('takeSnapshot'), // react-native-implementation
},
};
Expand Down
3 changes: 0 additions & 3 deletions src/renderers/native/ReactNativeTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ export type NativeMethodsMixinType = {
type SecretInternalsType = {
NativeMethodsMixin: NativeMethodsMixinType,
createReactNativeComponentClass(
viewConfig: ReactNativeBaseComponentViewConfig,
): any,
lazilyCreateReactNativeComponentClass(
name: string,
callback: ViewConfigGetter,
): any,
Expand Down
30 changes: 8 additions & 22 deletions src/renderers/native/ReactNativeViewConfigRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,32 @@ import type {
ViewConfigGetter,
} from 'ReactNativeTypes';

const registeredViewNames = new Map();
const viewConfigCallbacks = new Map();
const viewConfigs = new Map();

const ReactNativeViewConfigRegistry = {
/**
* Registers a native view/component.
* This method is intended for views with JavaScript-defined configs.
* If the config is loaded from UIManager, use registerLazy() instead.
*/
register(viewConfig: ReactNativeBaseComponentViewConfig) {
const name = viewConfig.uiViewClassName;
invariant(
!registeredViewNames.has(name),
'Tried to register two views with the same name %s',
name,
);
viewConfigs.set(name, viewConfig);
registeredViewNames.set(name);
return name;
},

/**
* Registers a native view/component by name.
* A callback is provided to load the view config from UIManager.
* The callback is deferred until the view is actually rendered.
* This is done to avoid causing Prepack deopts.
*/
registerLazy(name: string, callback: ViewConfigGetter) {
register(name: string, callback: ViewConfigGetter): string {
invariant(
!registeredViewNames.has(name),
!viewConfigCallbacks.has(name),
'Tried to register two views with the same name %s',
name,
);
viewConfigCallbacks.set(name, callback);
registeredViewNames.set(name);
return name;
},

get(name: string) {
/**
* Retrieves a config for the specified view.
* If this is the first time the view has been used,
* This configuration will be lazy-loaded from UIManager.
*/
get(name: string): ReactNativeBaseComponentViewConfig {
let viewConfig;
if (!viewConfigs.has(name)) {
const callback = viewConfigCallbacks.get(name);
Expand Down
16 changes: 8 additions & 8 deletions src/renderers/native/__tests__/ReactNativeEvents-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ beforeEach(() => {
it('handles events', () => {
expect(RCTEventEmitter.register.mock.calls.length).toBe(1);
var EventEmitter = RCTEventEmitter.register.mock.calls[0][0];
var View = createReactNativeComponentClass({
var View = createReactNativeComponentClass('View', () => ({
validAttributes: {foo: true},
uiViewClassName: 'View',
});
}));

var log = [];
ReactNative.render(
Expand Down Expand Up @@ -94,10 +94,10 @@ it('handles events on text nodes', () => {
expect(RCTEventEmitter.register.mock.calls.length).toBe(1);
var EventEmitter = RCTEventEmitter.register.mock.calls[0][0];

var Text = createReactNativeComponentClass({
var Text = createReactNativeComponentClass('Text', () => ({
validAttributes: {foo: true},
uiViewClassName: 'Text',
});
}));

class ContextHack extends React.Component {
static childContextTypes = {isInAParentText: PropTypes.bool};
Expand Down Expand Up @@ -179,10 +179,10 @@ it('handles events on text nodes', () => {

it('handles when a responder is unmounted while a touch sequence is in progress', () => {
var EventEmitter = RCTEventEmitter.register.mock.calls[0][0];
var View = createReactNativeComponentClass({
var View = createReactNativeComponentClass('View', () => ({
validAttributes: {id: true},
uiViewClassName: 'View',
});
}));

function getViewById(id) {
return UIManager.createView.mock.calls.find(
Expand Down Expand Up @@ -273,10 +273,10 @@ it('handles when a responder is unmounted while a touch sequence is in progress'

it('handles events without target', () => {
var EventEmitter = RCTEventEmitter.register.mock.calls[0][0];
var View = createReactNativeComponentClass({
var View = createReactNativeComponentClass('View', () => ({
validAttributes: {id: true},
uiViewClassName: 'View',
});
}));

function getViewById(id) {
return UIManager.createView.mock.calls.find(
Expand Down
24 changes: 12 additions & 12 deletions src/renderers/native/__tests__/ReactNativeMount-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ describe('ReactNative', () => {
});

it('should be able to create and render a native component', () => {
var View = createReactNativeComponentClass({
var View = createReactNativeComponentClass('View', () => ({
validAttributes: {foo: true},
uiViewClassName: 'View',
});
}));

ReactNative.render(<View foo="test" />, 1);
expect(UIManager.createView).toBeCalled();
Expand All @@ -40,10 +40,10 @@ describe('ReactNative', () => {
});

it('should be able to create and update a native component', () => {
var View = createReactNativeComponentClass({
var View = createReactNativeComponentClass('View', () => ({
validAttributes: {foo: true},
uiViewClassName: 'View',
});
}));

ReactNative.render(<View foo="foo" />, 11);

Expand All @@ -61,10 +61,10 @@ describe('ReactNative', () => {
});

it('should not call UIManager.updateView after render for properties that have not changed', () => {
const Text = createReactNativeComponentClass({
const Text = createReactNativeComponentClass('Text', () => ({
validAttributes: {foo: true},
uiViewClassName: 'Text',
});
}));

ReactNative.render(<Text foo="a">1</Text>, 11);
expect(UIManager.updateView).not.toBeCalled();
Expand All @@ -87,10 +87,10 @@ describe('ReactNative', () => {
});

it('should not call UIManager.updateView from setNativeProps for properties that have not changed', () => {
const View = createReactNativeComponentClass({
const View = createReactNativeComponentClass('View', () => ({
validAttributes: {foo: true},
uiViewClassName: 'View',
});
}));

class Subclass extends ReactNative.NativeComponent {
render() {
Expand Down Expand Up @@ -122,10 +122,10 @@ describe('ReactNative', () => {
});

it('returns the correct instance and calls it in the callback', () => {
var View = createReactNativeComponentClass({
var View = createReactNativeComponentClass('View', () => ({
validAttributes: {foo: true},
uiViewClassName: 'View',
});
}));

var a;
var b;
Expand All @@ -143,10 +143,10 @@ describe('ReactNative', () => {
});

it('renders and reorders children', () => {
var View = createReactNativeComponentClass({
var View = createReactNativeComponentClass('View', () => ({
validAttributes: {title: true},
uiViewClassName: 'View',
});
}));

class Component extends React.Component {
render() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,14 @@ describe('createReactNativeComponentClass', () => {
uiViewClassName: 'View',
};

const Text = createReactNativeComponentClass(textViewConfig);
const View = createReactNativeComponentClass(viewViewConfig);
const Text = createReactNativeComponentClass(
textViewConfig.uiViewClassName,
() => textViewConfig,
);
const View = createReactNativeComponentClass(
viewViewConfig.uiViewClassName,
() => viewViewConfig,
);

expect(Text).not.toBe(View);

Expand All @@ -53,10 +59,16 @@ describe('createReactNativeComponentClass', () => {
uiViewClassName: 'Text', // Same
};

createReactNativeComponentClass(textViewConfig);
createReactNativeComponentClass(
textViewConfig.uiViewClassName,
() => textViewConfig,
);

expect(() => {
createReactNativeComponentClass(altTextViewConfig);
createReactNativeComponentClass(
altTextViewConfig.uiViewClassName,
() => altTextViewConfig,
);
}).toThrow('Tried to register two views with the same name Text');
});
});
11 changes: 6 additions & 5 deletions src/renderers/native/createReactNativeComponentClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,21 @@

const ReactNativeViewConfigRegistry = require('ReactNativeViewConfigRegistry');

import type {ReactNativeBaseComponentViewConfig} from 'ReactNativeTypes';
import type {ViewConfigGetter} from 'ReactNativeTypes';

/**
* Creates a renderable ReactNative host component.
* Use this method when the view config is defined within JavaScript.
* Use lazilyCreateReactNativeComponentClass() for view configs retrieved from UIManager.
* Use this method for view configs that are loaded from UIManager.
* Use createReactNativeComponentClass() for view configs defined within JavaScript.
*
* @param {string} config iOS View configuration.
* @private
*/
const createReactNativeComponentClass = function(
viewConfig: ReactNativeBaseComponentViewConfig,
name: string,
callback: ViewConfigGetter,
): string {
return ReactNativeViewConfigRegistry.register(viewConfig);
return ReactNativeViewConfigRegistry.register(name, callback);
};

module.exports = createReactNativeComponentClass;
34 changes: 0 additions & 34 deletions src/renderers/native/lazilyCreateReactNativeComponentClass.js

This file was deleted.

0 comments on commit 32db4bb

Please sign in to comment.