[flow] Type annotations

This commit is contained in:
Lyubomir Marinov 2017-02-02 09:45:34 -06:00
parent 9e033deb7b
commit cfa3047330
6 changed files with 54 additions and 22 deletions

View File

@ -1,3 +1,5 @@
/* @flow */
// Re-export react-native's Platform because we want to provide a minimal
// equivalent on Web.
import { Platform } from 'react-native';

View File

@ -1,3 +1,5 @@
/* @flow */
const userAgent = navigator.userAgent;
let OS;

View File

@ -1,3 +1,7 @@
/* @flow */
import { Component } from 'react';
/**
* Object describing application route.
*
@ -5,12 +9,18 @@
* @property {Component} component - React Component constructor.
* @property {string} path - URL route, required for web routing.
*/
type Route = {
component: Class<Component<*>>, // eslint-disable-line no-undef
path: string
};
/**
* A registry for Navigator routes, allowing features to register themselves
* without needing to create additional inter-feature dependencies.
*/
class RouteRegistry {
_elements: Route[];
/**
* Initializes a new RouteRegistry instance.
*/
@ -19,8 +29,9 @@ class RouteRegistry {
* The set of registered routes.
*
* @private
* @type {Route[]}
*/
this._elements = new Set();
this._elements = [];
}
/**
@ -32,7 +43,7 @@ class RouteRegistry {
* @returns {boolean} True if the specified a and b describe one and the
* same abstract route; otherwise, false.
*/
areRoutesEqual(a, b) {
areRoutesEqual(a: Route, b: Route) {
if (a === b) { // reflexive
return true;
}
@ -60,21 +71,25 @@ class RouteRegistry {
// We use the destructuring operator to 'clone' the route object to
// prevent modifications from outside (e.g. React Native's Navigator
// extends it with additional properties).
return [ ...this._elements ].map(r => {
return this._elements.map(r => {
return { ...r };
});
}
/* eslint-disable no-undef */
/**
* Returns registered route by name if any.
*
* @param {Object} component - The React Component (class) of the route to
* retrieve.
* @param {Component} component - The React Component (class) of the route
* to retrieve.
* @returns {Route|null}
*/
getRouteByComponent(component) {
const route
= [ ...this._elements ].find(r => r.component === component);
getRouteByComponent(component: Class<Component<*>>) {
/* eslint-enable no-undef */
const route = this._elements.find(r => r.component === component);
// We use destructuring operator to 'clone' route object to prevent
// modifications from outside (e.g. React Native's Navigator extends
@ -88,12 +103,13 @@ class RouteRegistry {
* @param {Route} route - Route definition object.
* @returns {void}
*/
register(route) {
if (this._elements.has(route)) {
throw new Error(`Route ${route.component} is registered already!`);
register(route: Route) {
if (this._elements.includes(route)) {
throw new Error(
`Route ${String(route.component)} is registered already!`);
}
this._elements.add(route);
this._elements.push(route);
}
}

View File

@ -1,3 +1,5 @@
/* @flow */
/**
* Prevents further propagation of the events to be handler by a specific event
* handler/listener in the capturing and bubbling phases.
@ -7,8 +9,9 @@
* @returns {Function} An event handler/listener to be used in place of the
* specified eventHandler in order to stop the events from propagating.
*/
export function stopEventPropagation(eventHandler) {
return ev => {
export function stopEventPropagation<T>(eventHandler: (ev: Event) => T)
: (ev: Event) => T {
return (ev: Event) => {
const r = eventHandler(ev);
// React Native does not propagate the press event so, for the sake of

View File

@ -1,29 +1,38 @@
/* @flow */
import { applyMiddleware } from 'redux';
type Middleware = Function;
/**
* A registry for Redux middleware, allowing features to register their
* middleware without needing to create additional inter-feature dependencies.
*/
class MiddlewareRegistry {
_elements: Middleware[];
/**
* Creates a MiddlewareRegistry instance.
*/
constructor() {
/**
* The set of registered middleware.
*
* @private
* @type {Route[]}
*/
this._elements = new Set();
this._elements = [];
}
/**
* Applies all registered middleware into a store enhancer.
* (@link http://redux.js.org/docs/api/applyMiddleware.html).
*
* @param {Function[]} additional - Any additional middleware that need to
* @param {Middleware[]} additional - Any additional middleware that need to
* be included (such as middleware from third-party modules).
* @returns {Function}
* @returns {Middleware}
*/
applyMiddleware(...additional) {
applyMiddleware(...additional: Middleware[]) {
return applyMiddleware(
...this._elements,
...additional
@ -35,11 +44,11 @@ class MiddlewareRegistry {
*
* The method is to be invoked only before {@link #applyMiddleware()}.
*
* @param {Function} middleware - A Redux middleware.
* @param {Middleware} middleware - A Redux middleware.
* @returns {void}
*/
register(middleware) {
this._elements.add(middleware);
register(middleware: Middleware) {
this._elements.push(middleware);
}
}

View File

@ -30,7 +30,7 @@ export function randomAlphanumString(length: number) {
* @param {Array|string} arr - Source.
* @returns {Array|string} Array element or string character.
*/
export function randomElement(arr: Array<any> | string) {
export function randomElement(arr: [any] | string) {
return arr[randomInt(0, arr.length - 1)];
}