Skip to content

Commit 28d3778

Browse files
yungstersfacebook-github-bot
authored andcommittedMay 7, 2018
RN: Optimize View Attribute Initialization
Reviewed By: sahrens Differential Revision: D7893593 fbshipit-source-id: a425e841c9c86b063803878c7b07704c8a90a83a
1 parent 1c90a2b commit 28d3778

File tree

1 file changed

+46
-39
lines changed

1 file changed

+46
-39
lines changed
 

‎Libraries/ReactNative/requireNativeComponent.js

+46-39
Original file line numberDiff line numberDiff line change
@@ -141,22 +141,12 @@ function requireNativeComponent(
141141
viewConfig.directEventTypes = directEventTypes;
142142

143143
for (const key in nativeProps) {
144-
let useAttribute = false;
145-
const attribute = {};
144+
const typeName = nativeProps[key];
145+
const diff = getDifferForType(typeName);
146+
const process = getProcessorForType(typeName);
146147

147-
const differ = TypeToDifferMap[nativeProps[key]];
148-
if (differ) {
149-
attribute.diff = differ;
150-
useAttribute = true;
151-
}
152-
153-
const processor = TypeToProcessorMap[nativeProps[key]];
154-
if (processor) {
155-
attribute.process = processor;
156-
useAttribute = true;
157-
}
158-
159-
viewConfig.validAttributes[key] = useAttribute ? attribute : true;
148+
viewConfig.validAttributes[key] =
149+
diff == null && process == null ? true : {diff, process};
160150
}
161151

162152
// Unfortunately, the current set up puts the style properties on the top
@@ -186,32 +176,49 @@ function requireNativeComponent(
186176
return createReactNativeComponentClass(viewName, getViewConfig);
187177
}
188178

189-
const TypeToDifferMap = {
190-
// iOS Types
191-
CATransform3D: matricesDiffer,
192-
CGPoint: pointsDiffer,
193-
CGSize: sizesDiffer,
194-
UIEdgeInsets: insetsDiffer,
195-
// Android Types
196-
// (not yet implemented)
197-
};
179+
function getDifferForType(
180+
typeName: string,
181+
): ?(prevProp: any, nextProp: any) => boolean {
182+
switch (typeName) {
183+
// iOS Types
184+
case 'CATransform3D':
185+
return matricesDiffer;
186+
case 'CGPoint':
187+
return pointsDiffer;
188+
case 'CGSize':
189+
return sizesDiffer;
190+
case 'UIEdgeInsets':
191+
return insetsDiffer;
192+
// Android Types
193+
// (not yet implemented)
194+
}
195+
return null;
196+
}
197+
198+
function getProcessorForType(typeName: string): ?(nextProp: any) => any {
199+
switch (typeName) {
200+
// iOS Types
201+
case 'CGColor':
202+
case 'UIColor':
203+
return processColor;
204+
case 'CGColorArray':
205+
case 'UIColorArray':
206+
return processColorArray;
207+
case 'CGImage':
208+
case 'UIImage':
209+
case 'RCTImageSource':
210+
return resolveAssetSource;
211+
// Android Types
212+
case 'Color':
213+
return processColor;
214+
case 'ColorArray':
215+
return processColorArray;
216+
}
217+
return null;
218+
}
198219

199220
function processColorArray(colors: ?Array<any>): ?Array<?number> {
200-
return colors && colors.map(processColor);
221+
return colors == null ? null : colors.map(processColor);
201222
}
202223

203-
const TypeToProcessorMap = {
204-
// iOS Types
205-
CGColor: processColor,
206-
CGColorArray: processColorArray,
207-
UIColor: processColor,
208-
UIColorArray: processColorArray,
209-
CGImage: resolveAssetSource,
210-
UIImage: resolveAssetSource,
211-
RCTImageSource: resolveAssetSource,
212-
// Android Types
213-
Color: processColor,
214-
ColorArray: processColorArray,
215-
};
216-
217224
module.exports = requireNativeComponent;

0 commit comments

Comments
 (0)
Please sign in to comment.