[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 // Re-export react-native's Platform because we want to provide a minimal
// equivalent on Web. // equivalent on Web.
import { Platform } from 'react-native'; import { Platform } from 'react-native';

View File

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

View File

@ -1,3 +1,7 @@
/* @flow */
import { Component } from 'react';
/** /**
* Object describing application route. * Object describing application route.
* *
@ -5,12 +9,18 @@
* @property {Component} component - React Component constructor. * @property {Component} component - React Component constructor.
* @property {string} path - URL route, required for web routing. * @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 * A registry for Navigator routes, allowing features to register themselves
* without needing to create additional inter-feature dependencies. * without needing to create additional inter-feature dependencies.
*/ */
class RouteRegistry { class RouteRegistry {
_elements: Route[];
/** /**
* Initializes a new RouteRegistry instance. * Initializes a new RouteRegistry instance.
*/ */
@ -19,8 +29,9 @@ class RouteRegistry {
* The set of registered routes. * The set of registered routes.
* *
* @private * @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 * @returns {boolean} True if the specified a and b describe one and the
* same abstract route; otherwise, false. * same abstract route; otherwise, false.
*/ */
areRoutesEqual(a, b) { areRoutesEqual(a: Route, b: Route) {
if (a === b) { // reflexive if (a === b) { // reflexive
return true; return true;
} }
@ -60,21 +71,25 @@ class RouteRegistry {
// We use the destructuring operator to 'clone' the route object to // We use the destructuring operator to 'clone' the route object to
// prevent modifications from outside (e.g. React Native's Navigator // prevent modifications from outside (e.g. React Native's Navigator
// extends it with additional properties). // extends it with additional properties).
return [ ...this._elements ].map(r => { return this._elements.map(r => {
return { ...r }; return { ...r };
}); });
} }
/* eslint-disable no-undef */
/** /**
* Returns registered route by name if any. * Returns registered route by name if any.
* *
* @param {Object} component - The React Component (class) of the route to * @param {Component} component - The React Component (class) of the route
* retrieve. * to retrieve.
* @returns {Route|null} * @returns {Route|null}
*/ */
getRouteByComponent(component) { getRouteByComponent(component: Class<Component<*>>) {
const route
= [ ...this._elements ].find(r => r.component === component); /* eslint-enable no-undef */
const route = this._elements.find(r => r.component === component);
// We use destructuring operator to 'clone' route object to prevent // We use destructuring operator to 'clone' route object to prevent
// modifications from outside (e.g. React Native's Navigator extends // modifications from outside (e.g. React Native's Navigator extends
@ -88,12 +103,13 @@ class RouteRegistry {
* @param {Route} route - Route definition object. * @param {Route} route - Route definition object.
* @returns {void} * @returns {void}
*/ */
register(route) { register(route: Route) {
if (this._elements.has(route)) { if (this._elements.includes(route)) {
throw new Error(`Route ${route.component} is registered already!`); 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 * Prevents further propagation of the events to be handler by a specific event
* handler/listener in the capturing and bubbling phases. * 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 * @returns {Function} An event handler/listener to be used in place of the
* specified eventHandler in order to stop the events from propagating. * specified eventHandler in order to stop the events from propagating.
*/ */
export function stopEventPropagation(eventHandler) { export function stopEventPropagation<T>(eventHandler: (ev: Event) => T)
return ev => { : (ev: Event) => T {
return (ev: Event) => {
const r = eventHandler(ev); const r = eventHandler(ev);
// React Native does not propagate the press event so, for the sake of // React Native does not propagate the press event so, for the sake of

View File

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

View File

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