Skip to content

Commit

Permalink
Updates from Sun 15 Mar
Browse files Browse the repository at this point in the history
- [ReactNative] Add website to blacklist | Christopher Chedeau
- Ported ART to new UIManager | Nick Lockwood
- [ReactNative] Fix File Watcher test | Christopher Chedeau
- [ReactNative] OSS Interaction Manager | Christopher Chedeau
  • Loading branch information
vjeux committed Mar 16, 2015
1 parent 3b2e61a commit 90164c3
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 159 deletions.
6 changes: 3 additions & 3 deletions Libraries/Image/RCTStaticImageManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ - (UIView *)view

RCT_EXPORT_VIEW_PROPERTY(capInsets)
RCT_REMAP_VIEW_PROPERTY(resizeMode, contentMode)
RCT_CUSTOM_VIEW_PROPERTY(src, RCTStaticImage *)
RCT_CUSTOM_VIEW_PROPERTY(src, RCTStaticImage)
{
if (json) {
if ([[[json description] pathExtension] caseInsensitiveCompare:@"gif"] == NSOrderedSame) {
Expand All @@ -30,7 +30,7 @@ - (UIView *)view
view.image = defaultView.image;
}
}
RCT_CUSTOM_VIEW_PROPERTY(tintColor, RCTStaticImage *)
RCT_CUSTOM_VIEW_PROPERTY(tintColor, RCTStaticImage)
{
if (json) {
view.renderingMode = UIImageRenderingModeAlwaysTemplate;
Expand All @@ -40,7 +40,7 @@ - (UIView *)view
view.tintColor = defaultView.tintColor;
}
}
RCT_CUSTOM_VIEW_PROPERTY(imageTag, RCTStaticImage *)
RCT_CUSTOM_VIEW_PROPERTY(imageTag, RCTStaticImage)
{
if (json) {
[RCTImageLoader loadImageWithTag:[RCTConvert NSString:json] callback:^(NSError *error, UIImage *image) {
Expand Down
64 changes: 33 additions & 31 deletions Libraries/Interaction/InteractionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,29 @@ var invariant = require('invariant');
var keyMirror = require('keyMirror');
var setImmediate = require('setImmediate');

var _emitter = new EventEmitter();
var _interactionSet = new Set();
var _addInteractionSet = new Set();
var _deleteInteractionSet = new Set();
var _nextUpdateHandle = null;
var _queue = [];
var _inc = 0;

/**
* InteractionManager allows long-running work to be scheduled after any
* interactions/animations have completed. In particular, this allows JavaScript
* animations to run smoothly.
*
* Applications can schedule tasks to run after interactions with the following:
*
* InteractionManager.runAfterInteractions(() => {
* // ...long-running synchronous task...
* });
* ```
* InteractionManager.runAfterInteractions(() => {
* // ...long-running synchronous task...
* });
* ```
*
* Compare this to other scheduling alternatives:
*
* - requestAnimationFrame(): for code that animates a view over time.
* - setImmediate/setTimeout(): run code later, note this may delay animations.
* - runAfterInteractions(): run code later, without delaying active animations.
Expand All @@ -37,27 +48,32 @@ var setImmediate = require('setImmediate');
* creating an interaction 'handle' on animation start, and clearing it upon
* completion:
*
* var handle = InteractionManager.createInteractionHandle();
* // run animation... (`runAfterInteractions` tasks are queued)
* // later, on animation completion:
* InteractionManager.clearInteractionHandle(handle);
* // queued tasks run if all handles were cleared
* ```
* var handle = InteractionManager.createInteractionHandle();
* // run animation... (`runAfterInteractions` tasks are queued)
* // later, on animation completion:
* InteractionManager.clearInteractionHandle(handle);
* // queued tasks run if all handles were cleared
* ```
*/

var _emitter = new EventEmitter();
var _interactionSet = new Set();
var _addInteractionSet = new Set();
var _deleteInteractionSet = new Set();
var _nextUpdateHandle = null;
var _queue = [];
var _inc = 0;

var InteractionManager = {
Events: keyMirror({
interactionStart: true,
interactionComplete: true,
}),

/**
* Schedule a function to run after all interactions have completed.
*/
runAfterInteractions(callback) {
invariant(
typeof callback === 'function',
'Must specify a function to schedule.'
);
scheduleUpdate();
_queue.push(callback);
},

/**
* Notify manager that an interaction has started.
*/
Expand All @@ -81,20 +97,6 @@ var InteractionManager = {
_deleteInteractionSet.add(handle);
},

/**
* Schedule a function to run after all interactions have completed.
*
* @param {function} callback
*/
runAfterInteractions(callback) {
invariant(
typeof callback === 'function',
'Must specify a function to schedule.'
);
scheduleUpdate();
_queue.push(callback);
},

addListener: _emitter.addListener.bind(_emitter),
};

Expand Down
10 changes: 5 additions & 5 deletions Libraries/Text/RCTTextManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ - (RCTShadowView *)shadowView
}

RCT_REMAP_VIEW_PROPERTY(containerBackgroundColor, backgroundColor)
RCT_CUSTOM_VIEW_PROPERTY(numberOfLines, RCTText *)
RCT_CUSTOM_VIEW_PROPERTY(numberOfLines, RCTText)
{
NSLineBreakMode truncationMode = NSLineBreakByClipping;
view.numberOfLines = json ? [RCTConvert NSInteger:json] : defaultView.numberOfLines;
Expand All @@ -34,16 +34,16 @@ - (RCTShadowView *)shadowView
view.lineBreakMode = truncationMode;
}

RCT_CUSTOM_SHADOW_PROPERTY(backgroundColor, RCTShadowText *)
RCT_CUSTOM_SHADOW_PROPERTY(backgroundColor, RCTShadowText)
{
view.textBackgroundColor = json ? [RCTConvert UIColor:json] : defaultView.textBackgroundColor;
}
RCT_CUSTOM_SHADOW_PROPERTY(containerBackgroundColor, RCTShadowText *)
RCT_CUSTOM_SHADOW_PROPERTY(containerBackgroundColor, RCTShadowText)
{
view.backgroundColor = json ? [RCTConvert UIColor:json] : defaultView.backgroundColor;
view.isBGColorExplicitlySet = json ? YES : defaultView.isBGColorExplicitlySet;
}
RCT_CUSTOM_SHADOW_PROPERTY(numberOfLines, RCTShadowText *)
RCT_CUSTOM_SHADOW_PROPERTY(numberOfLines, RCTShadowText)
{
NSLineBreakMode truncationMode = NSLineBreakByClipping;
view.maxNumberOfLines = json ? [RCTConvert NSInteger:json] : defaultView.maxNumberOfLines;
Expand All @@ -52,7 +52,7 @@ - (RCTShadowView *)shadowView
}
view.truncationMode = truncationMode;
}
RCT_CUSTOM_SHADOW_PROPERTY(textAlign, RCTShadowText *)
RCT_CUSTOM_SHADOW_PROPERTY(textAlign, RCTShadowText)
{
view.textAlign = json ? [RCTConvert NSTextAlignment:json] : defaultView.textAlign;
}
Expand Down
3 changes: 2 additions & 1 deletion Libraries/react-native/react-native.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

var ReactNative = {
...require('React'),
Animation: require('Animation'),
ActivityIndicatorIOS: require('ActivityIndicatorIOS'),
AlertIOS: require('AlertIOS'),
Animation: require('Animation'),
AppRegistry: require('AppRegistry'),
AppState: require('AppState'),
AppStateIOS: require('AppStateIOS'),
Expand All @@ -18,6 +18,7 @@ var ReactNative = {
DatePickerIOS: require('DatePickerIOS'),
ExpandingText: require('ExpandingText'),
Image: require('Image'),
InteractionManager: require('InteractionManager'),
LayoutAnimation: require('LayoutAnimation'),
ListView: require('ListView'),
ListViewDataSource: require('ListViewDataSource'),
Expand Down
117 changes: 117 additions & 0 deletions ReactKit/Base/RCTConvert.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
+ (CGRect)CGRect:(id)json;
+ (UIEdgeInsets)UIEdgeInsets:(id)json;

+ (CGLineCap)CGLineCap:(id)json;
+ (CGLineJoin)CGLineJoin:(id)json;

+ (CATransform3D)CATransform3D:(id)json;
+ (CGAffineTransform)CGAffineTransform:(id)json;

Expand Down Expand Up @@ -103,3 +106,117 @@ id RCTConvertValue(id target, NSString *keypath, id json);
#ifdef __cplusplus
}
#endif

/**
* This macro is used for creating converter functions with arbitrary logic.
*/
#define RCT_CONVERTER_CUSTOM(type, name, code) \
+ (type)name:(id)json \
{ \
if (json == [NSNull null]) { \
json = nil; \
} \
@try { \
return code; \
} \
@catch (__unused NSException *e) { \
RCTLogError(@"JSON value '%@' of type '%@' cannot be converted to '%s'", \
json, [json class], #type); \
json = nil; \
return code; \
} \
}

/**
* This macro is used for creating simple converter functions that just call
* the specified getter method on the json value.
*/
#define RCT_CONVERTER(type, name, getter) \
RCT_CONVERTER_CUSTOM(type, name, [json getter])

/**
* This macro is used for creating converters for enum types.
*/
#define RCT_ENUM_CONVERTER(type, values, default, getter) \
+ (type)type:(id)json \
{ \
static NSDictionary *mapping; \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
mapping = values; \
}); \
if (!json || json == [NSNull null]) { \
return default; \
} \
if ([json isKindOfClass:[NSNumber class]]) { \
if ([[mapping allValues] containsObject:json] || [json getter] == default) { \
return [json getter]; \
} \
RCTLogError(@"Invalid %s '%@'. should be one of: %@", #type, json, [mapping allValues]); \
return default; \
} \
if (![json isKindOfClass:[NSString class]]) { \
RCTLogError(@"Expected NSNumber or NSString for %s, received %@: %@", #type, [json class], json); \
} \
id value = mapping[json]; \
if(!value && [json description].length > 0) { \
RCTLogError(@"Invalid %s '%@'. should be one of: %@", #type, json, [mapping allKeys]); \
} \
return value ? [value getter] : default; \
}

/**
* This macro is used for creating converter functions for structs that consist
* of a number of CGFloat properties, such as CGPoint, CGRect, etc.
*/
#define RCT_CGSTRUCT_CONVERTER(type, values) \
+ (type)type:(id)json \
{ \
@try { \
static NSArray *fields; \
static NSUInteger count; \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
fields = values; \
count = [fields count]; \
}); \
type result; \
if ([json isKindOfClass:[NSArray class]]) { \
if ([json count] != count) { \
RCTLogError(@"Expected array with count %zd, but count is %zd: %@", count, [json count], json); \
} else { \
for (NSUInteger i = 0; i < count; i++) { \
((CGFloat *)&result)[i] = [json[i] doubleValue]; \
} \
} \
} else if ([json isKindOfClass:[NSDictionary class]]) { \
for (NSUInteger i = 0; i < count; i++) { \
((CGFloat *)&result)[i] = [json[fields[i]] doubleValue]; \
} \
} else if (json && json != [NSNull null]) { \
RCTLogError(@"Expected NSArray or NSDictionary for %s, received %@: %@", #type, [json class], json); \
} \
return result; \
} \
@catch (__unused NSException *e) { \
RCTLogError(@"JSON value '%@' cannot be converted to '%s'", json, #type); \
type result; \
return result; \
} \
}

/**
* This macro is used for creating converter functions for typed arrays.
*/
#define RCT_ARRAY_CONVERTER(type) \
+ (NSArray *)type##Array:(id)json \
{ \
NSMutableArray *values = [[NSMutableArray alloc] init]; \
for (id jsonValue in [self NSArray:json]) { \
id value = [self type:jsonValue]; \
if (value) { \
[values addObject:value]; \
} \
} \
return values; \
}
Loading

0 comments on commit 90164c3

Please sign in to comment.