From 874f39bf7f5f0ac19ff78447aef589d23cd429d0 Mon Sep 17 00:00:00 2001 From: Gabe Levi Date: Fri, 4 Sep 2015 17:00:21 -0700 Subject: [PATCH] [Flow] Clean up react-native for Flow v0.15.0 --- Examples/Movies/getStyleFromScore.js | 4 +- Libraries/Devtools/setupDevtools.js | 4 +- Libraries/Network/NetInfo.js | 98 ++++++++++++------------- Libraries/StyleSheet/StyleSheetTypes.js | 15 ++++ Libraries/StyleSheet/flattenStyle.js | 3 +- 5 files changed, 70 insertions(+), 54 deletions(-) create mode 100644 Libraries/StyleSheet/StyleSheetTypes.js diff --git a/Examples/Movies/getStyleFromScore.js b/Examples/Movies/getStyleFromScore.js index 98feaa50b..1d5b599b6 100644 --- a/Examples/Movies/getStyleFromScore.js +++ b/Examples/Movies/getStyleFromScore.js @@ -22,7 +22,9 @@ var { var MAX_VALUE = 200; -function getStyleFromScore(score: number): {color: string} { +import type { StyleObj } from 'StyleSheetTypes'; + +function getStyleFromScore(score: number): StyleObj { if (score < 0) { return styles.noScore; } diff --git a/Libraries/Devtools/setupDevtools.js b/Libraries/Devtools/setupDevtools.js index 0bd003dca..93ba87555 100644 --- a/Libraries/Devtools/setupDevtools.js +++ b/Libraries/Devtools/setupDevtools.js @@ -72,7 +72,9 @@ function setupDevtools() { } function handleMessage(evt) { - var data; + // It's hard to handle JSON in a safe manner without inspecting it at + // runtime, hence the any + var data: any; try { data = JSON.parse(evt.data); } catch (e) { diff --git a/Libraries/Network/NetInfo.js b/Libraries/Network/NetInfo.js index f51a95529..5c45be617 100644 --- a/Libraries/Network/NetInfo.js +++ b/Libraries/Network/NetInfo.js @@ -138,6 +138,23 @@ type ConnectivityStateAndroid = $Enum<{ var _subscriptions = new Map(); +if (Platform.OS === 'ios') { + var _isConnected = function( + reachability: ReachabilityStateIOS + ): bool { + return reachability !== 'none' && + reachability !== 'unknown'; + }; +} else if (Platform.OS === 'android') { + var _isConnected = function( + connectionType: ConnectivityStateAndroid + ): bool { + return connectionType !== 'NONE' && connectionType !== 'UNKNOWN'; + }; +} + +var _isConnectedSubscriptions = new Map(); + var NetInfo = { addEventListener: function ( eventName: ChangeEventName, @@ -175,60 +192,41 @@ var NetInfo = { }); }, - isConnected: {}, + isConnected: { + addEventListener: function ( + eventName: ChangeEventName, + handler: Function + ): void { + var listener = (connection) => { + handler(_isConnected(connection)); + }; + _isConnectedSubscriptions.set(handler, listener); + NetInfo.addEventListener( + eventName, + listener + ); + }, - isConnectionMetered: {}, -}; + removeEventListener: function( + eventName: ChangeEventName, + handler: Function + ): void { + var listener = _isConnectedSubscriptions.get(handler); + NetInfo.removeEventListener( + eventName, + listener + ); + _isConnectedSubscriptions.delete(handler); + }, -if (Platform.OS === 'ios') { - var _isConnected = function( - reachability: ReachabilityStateIOS - ): bool { - return reachability !== 'none' && - reachability !== 'unknown'; - }; -} else if (Platform.OS === 'android') { - var _isConnected = function( - connectionType: ConnectivityStateAndroid - ): bool { - return connectionType !== 'NONE' && connectionType !== 'UNKNOWN'; - }; -} - -var _isConnectedSubscriptions = new Map(); - -NetInfo.isConnected = { - addEventListener: function ( - eventName: ChangeEventName, - handler: Function - ): void { - var listener = (connection) => { - handler(_isConnected(connection)); - }; - _isConnectedSubscriptions.set(handler, listener); - NetInfo.addEventListener( - eventName, - listener - ); + fetch: function(): Promise { + return NetInfo.fetch().then( + (connection) => _isConnected(connection) + ); + }, }, - removeEventListener: function( - eventName: ChangeEventName, - handler: Function - ): void { - var listener = _isConnectedSubscriptions.get(handler); - NetInfo.removeEventListener( - eventName, - listener - ); - _isConnectedSubscriptions.delete(handler); - }, - - fetch: function(): Promise { - return NetInfo.fetch().then( - (connection) => _isConnected(connection) - ); - }, + isConnectionMetered: ({}: {} | (callback:Function) => void), }; if (Platform.OS === 'android') { diff --git a/Libraries/StyleSheet/StyleSheetTypes.js b/Libraries/StyleSheet/StyleSheetTypes.js new file mode 100644 index 000000000..779e64772 --- /dev/null +++ b/Libraries/StyleSheet/StyleSheetTypes.js @@ -0,0 +1,15 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @providesModule StyleSheetTypes + * @flow + */ +'use strict'; + +type Atom = number | bool | Object | Array; +export type StyleObj = Atom | Array; diff --git a/Libraries/StyleSheet/flattenStyle.js b/Libraries/StyleSheet/flattenStyle.js index 50e061ede..621c614ff 100644 --- a/Libraries/StyleSheet/flattenStyle.js +++ b/Libraries/StyleSheet/flattenStyle.js @@ -14,8 +14,7 @@ var StyleSheetRegistry = require('StyleSheetRegistry'); var invariant = require('invariant'); -type Atom = number | bool | Object | Array -type StyleObj = Atom | Array +import type { StyleObj } from 'StyleSheetTypes'; function getStyle(style) { if (typeof style === 'number') {