Skip to content

Commit

Permalink
flowify Libraries/ReactIOS
Browse files Browse the repository at this point in the history
  • Loading branch information
mroch committed Mar 26, 2015
1 parent f124c32 commit 7ffa794
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 66 deletions.
3 changes: 2 additions & 1 deletion .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
# Ignore react-tools where there are overlaps, but don't ignore anything that
# react-native relies on
.*/node_modules/react-tools/src/vendor/core/ExecutionEnvironment.js
.*/node_modules/react-tools/src/browser/.*
.*/node_modules/react-tools/src/browser/eventPlugins/ResponderEventPlugin.js
.*/node_modules/react-tools/src/browser/ui/React.js
.*/node_modules/react-tools/src/core/ReactInstanceHandles.js
.*/node_modules/react-tools/src/event/EventPropagators.js

Expand Down
1 change: 1 addition & 0 deletions Libraries/ReactIOS/IOSDefaultEventPluginOrder.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule IOSDefaultEventPluginOrder
* @flow
*/
'use strict';

Expand Down
10 changes: 6 additions & 4 deletions Libraries/ReactIOS/IOSNativeBridgeEventPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule IOSNativeBridgeEventPlugin
* @flow
*/

"use strict";
Expand Down Expand Up @@ -51,10 +52,11 @@ var IOSNativeBridgeEventPlugin = {
* @see {EventPluginHub.extractEvents}
*/
extractEvents: function(
topLevelType,
topLevelTarget,
topLevelTargetID,
nativeEvent) {
topLevelType: string,
topLevelTarget: EventTarget,
topLevelTargetID: string,
nativeEvent: Event
): ?Object {
var bubbleDispatchConfig = customBubblingEventTypes[topLevelType];
var directDispatchConfig = customDirectEventTypes[topLevelType];
var event = SyntheticEvent.getPooled(
Expand Down
49 changes: 38 additions & 11 deletions Libraries/ReactIOS/NativeMethodsMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule NativeMethodsMixin
* @flow
*/
'use strict';

var NativeModules = require('NativeModules');
var NativeModules = require('NativeModules');
var RCTPOPAnimationManager = NativeModules.POPAnimationManager;
var RCTUIManager = NativeModules.UIManager;
Expand All @@ -20,7 +20,26 @@ var flattenStyle = require('flattenStyle');
var invariant = require('invariant');
var mergeFast = require('mergeFast');

var animationIDInvariant = function(funcName, anim) {
type MeasureOnSuccessCallback = (
x: number,
y: number,
width: number,
height: number,
pageX: number,
pageY: number
) => void

type MeasureLayoutOnSuccessCallback = (
left: number,
top: number,
width: number,
height: number
) => void

var animationIDInvariant = function(
funcName: string,
anim: number
) {
invariant(
anim,
funcName + ' must be called with a valid animation ID returned from' +
Expand All @@ -29,21 +48,25 @@ var animationIDInvariant = function(funcName, anim) {
};

var NativeMethodsMixin = {
addAnimation: function(anim, callback) {
addAnimation: function(anim: number, callback?: (finished: bool) => void) {
animationIDInvariant('addAnimation', anim);
RCTPOPAnimationManager.addAnimation(this.getNodeHandle(), anim, callback);
},

removeAnimation: function(anim) {
removeAnimation: function(anim: number) {
animationIDInvariant('removeAnimation', anim);
RCTPOPAnimationManager.removeAnimation(this.getNodeHandle(), anim);
},

measure: function(callback) {
measure: function(callback: MeasureOnSuccessCallback) {
RCTUIManager.measure(this.getNodeHandle(), callback);
},

measureLayout: function(relativeToNativeNode, onSuccess, onFail) {
measureLayout: function(
relativeToNativeNode: number,
onSuccess: MeasureLayoutOnSuccessCallback,
onFail: () => void /* currently unused */
) {
RCTUIManager.measureLayout(
this.getNodeHandle(),
relativeToNativeNode,
Expand All @@ -57,7 +80,7 @@ var NativeMethodsMixin = {
* in future diff process, this means that if you do not include them in the
* next render, they will remain active.
*/
setNativeProps: function(nativeProps) {
setNativeProps: function(nativeProps: Object) {
// nativeProps contains a style attribute that's going to be flattened
// and all the attributes expanded in place. In order to make this
// process do as few allocations and copies as possible, we return
Expand Down Expand Up @@ -111,15 +134,19 @@ function throwOnStylesProp(component, props) {
}
}
if (__DEV__) {
// hide this from Flow since we can't define these properties outside of
// __DEV__ without actually implementing them (setting them to undefined
// isn't allowed by ReactClass)
var NativeMethodsMixin_DEV = (NativeMethodsMixin: any);
invariant(
!NativeMethodsMixin.componentWillMount &&
!NativeMethodsMixin.componentWillReceiveProps,
!NativeMethodsMixin_DEV.componentWillMount &&
!NativeMethodsMixin_DEV.componentWillReceiveProps,
'Do not override existing functions.'
);
NativeMethodsMixin.componentWillMount = function () {
NativeMethodsMixin_DEV.componentWillMount = function () {
throwOnStylesProp(this, this.props);
};
NativeMethodsMixin.componentWillReceiveProps = function (newProps) {
NativeMethodsMixin_DEV.componentWillReceiveProps = function (newProps) {
throwOnStylesProp(this, newProps);
};
}
Expand Down
1 change: 1 addition & 0 deletions Libraries/ReactIOS/React.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule React
* @flow
*/

"use strict";
Expand Down
6 changes: 3 additions & 3 deletions Libraries/ReactIOS/ReactIOS.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule ReactIOS
* @flow
*/

"use strict";

var ReactChildren = require('ReactChildren');
Expand Down Expand Up @@ -51,7 +51,7 @@ var resolveDefaultProps = function(element) {
};

// Experimental optimized element creation
var augmentElement = function(element) {
var augmentElement = function(element: ReactElement) {
if (__DEV__) {
invariant(
false,
Expand All @@ -67,7 +67,7 @@ var augmentElement = function(element) {
return element;
};

var render = function(component, mountInto) {
var render = function(component: ReactComponent, mountInto: number) {
ReactIOSMount.renderComponent(component, mountInto);
};

Expand Down
1 change: 1 addition & 0 deletions Libraries/ReactIOS/ReactIOSComponentEnvironment.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule ReactIOSComponentEnvironment
* @flow
*/
'use strict';

Expand Down
1 change: 1 addition & 0 deletions Libraries/ReactIOS/ReactIOSComponentMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule ReactIOSComponentMixin
* @flow
*/
'use strict';

Expand Down
2 changes: 1 addition & 1 deletion Libraries/ReactIOS/ReactIOSDOMIDOperations.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule ReactIOSDOMIDOperations
* @typechecks static-only
* @flow
*/

"use strict";
Expand Down
1 change: 1 addition & 0 deletions Libraries/ReactIOS/ReactIOSDefaultInjection.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule ReactIOSDefaultInjection
* @flow
*/

"use strict";
Expand Down
11 changes: 10 additions & 1 deletion Libraries/ReactIOS/createReactIOSNativeComponentClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,27 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule createReactIOSNativeComponentClass
* @flow
*/

"use strict";

var ReactElement = require('ReactElement');
var ReactIOSNativeComponent = require('ReactIOSNativeComponent');

// See also ReactIOSNativeComponent
type ReactIOSNativeComponentViewConfig = {
validAttributes: Object;
uiViewClassName: string;
}

/**
* @param {string} config iOS View configuration.
* @private
*/
var createReactIOSNativeComponentClass = function(viewConfig) {
var createReactIOSNativeComponentClass = function(
viewConfig: ReactIOSNativeComponentViewConfig
): Function { // returning Function is lossy :/
var Constructor = function(element) {
this._currentElement = element;

Expand Down
100 changes: 55 additions & 45 deletions Libraries/ReactIOS/diffRawProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule diffRawProperties
* @flow
*/
'use strict';

Expand All @@ -21,66 +22,75 @@
* previous. These properties are as supplied to component construction.
* @return {?object}
*/
function diffRawProperties(updatePayload, prevProps, nextProps, validAttributes) {
function diffRawProperties(
updatePayload: ?Object,
prevProps: ?Object,
nextProps: ?Object,
validAttributes: Object
): ?Object {
var validAttributeConfig;
var nextProp;
var prevProp;
var isScalar;
var shouldUpdate;

for (var propKey in nextProps) {
validAttributeConfig = validAttributes[propKey];
if (!validAttributeConfig) {
continue; // not a valid native prop
}
prevProp = prevProps && prevProps[propKey];
nextProp = nextProps[propKey];
if (prevProp !== nextProp) {
// If you want a property's diff to be detected, you must configure it
// to be so - *or* it must be a scalar property. For now, we'll allow
// creation with any attribute that is not scalar, but we should
// eventually even reject those unless they are properly configured.
isScalar = typeof nextProp !== 'object' || nextProp === null;
shouldUpdate = isScalar ||
!prevProp ||
validAttributeConfig.diff &&
validAttributeConfig.diff(prevProp, nextProp);
if (nextProps) {
for (var propKey in nextProps) {
validAttributeConfig = validAttributes[propKey];
if (!validAttributeConfig) {
continue; // not a valid native prop
}
prevProp = prevProps && prevProps[propKey];
nextProp = nextProps[propKey];
if (prevProp !== nextProp) {
// If you want a property's diff to be detected, you must configure it
// to be so - *or* it must be a scalar property. For now, we'll allow
// creation with any attribute that is not scalar, but we should
// eventually even reject those unless they are properly configured.
isScalar = typeof nextProp !== 'object' || nextProp === null;
shouldUpdate = isScalar ||
!prevProp ||
validAttributeConfig.diff &&
validAttributeConfig.diff(prevProp, nextProp);

if (shouldUpdate) {
updatePayload = updatePayload || {};
updatePayload[propKey] = nextProp;
if (shouldUpdate) {
updatePayload = updatePayload || {};
updatePayload[propKey] = nextProp;
}
}
}
}

// Also iterate through all the previous props to catch any that have been
// removed and make sure native gets the signal so it can reset them to the
// default.
for (var propKey in prevProps) {
validAttributeConfig = validAttributes[propKey];
if (!validAttributeConfig) {
continue; // not a valid native prop
}
if (updatePayload && updatePayload[propKey] !== undefined) {
continue; // Prop already specified
}
prevProp = prevProps[propKey];
nextProp = nextProps && nextProps[propKey];
if (prevProp !== nextProp) {
if (nextProp === undefined) {
nextProp = null; // null is a sentinel we explicitly send to native
if (prevProps) {
for (var propKey in prevProps) {
validAttributeConfig = validAttributes[propKey];
if (!validAttributeConfig) {
continue; // not a valid native prop
}
if (updatePayload && updatePayload[propKey] !== undefined) {
continue; // Prop already specified
}
// If you want a property's diff to be detected, you must configure it
// to be so - *or* it must be a scalar property. For now, we'll allow
// creation with any attribute that is not scalar, but we should
// eventually even reject those unless they are properly configured.
isScalar = typeof nextProp !== 'object' || nextProp === null;
shouldUpdate = isScalar && prevProp !== nextProp ||
validAttributeConfig.diff &&
validAttributeConfig.diff(prevProp, nextProp);
if (shouldUpdate) {
updatePayload = updatePayload || {};
updatePayload[propKey] = nextProp;
prevProp = prevProps[propKey];
nextProp = nextProps && nextProps[propKey];
if (prevProp !== nextProp) {
if (nextProp === undefined) {
nextProp = null; // null is a sentinel we explicitly send to native
}
// If you want a property's diff to be detected, you must configure it
// to be so - *or* it must be a scalar property. For now, we'll allow
// creation with any attribute that is not scalar, but we should
// eventually even reject those unless they are properly configured.
isScalar = typeof nextProp !== 'object' || nextProp === null;
shouldUpdate = isScalar && prevProp !== nextProp ||
validAttributeConfig.diff &&
validAttributeConfig.diff(prevProp, nextProp);
if (shouldUpdate) {
updatePayload = updatePayload || {};
updatePayload[propKey] = nextProp;
}
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions Libraries/react-native/react-native-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@
// see also react-native.js

declare var __DEV__: boolean;

declare var __REACT_DEVTOOLS_GLOBAL_HOOK__: any; /*?{
inject: ?((stuff: Object) => void)
};*/

0 comments on commit 7ffa794

Please sign in to comment.