From cfa3047330af3b9b8bf0695baea8ac8b44ec1a51 Mon Sep 17 00:00:00 2001 From: Lyubomir Marinov Date: Thu, 2 Feb 2017 09:45:34 -0600 Subject: [PATCH] [flow] Type annotations --- react/features/base/react/Platform.native.js | 2 + react/features/base/react/Platform.web.js | 2 + react/features/base/react/RouteRegistry.js | 40 +++++++++++++------ react/features/base/react/functions.js | 7 +++- .../features/base/redux/MiddlewareRegistry.js | 23 +++++++---- react/features/base/util/randomUtil.js | 2 +- 6 files changed, 54 insertions(+), 22 deletions(-) diff --git a/react/features/base/react/Platform.native.js b/react/features/base/react/Platform.native.js index 1710b20d3..2c5130848 100644 --- a/react/features/base/react/Platform.native.js +++ b/react/features/base/react/Platform.native.js @@ -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'; diff --git a/react/features/base/react/Platform.web.js b/react/features/base/react/Platform.web.js index e09dda133..6d03b9a4e 100644 --- a/react/features/base/react/Platform.web.js +++ b/react/features/base/react/Platform.web.js @@ -1,3 +1,5 @@ +/* @flow */ + const userAgent = navigator.userAgent; let OS; diff --git a/react/features/base/react/RouteRegistry.js b/react/features/base/react/RouteRegistry.js index 86403af97..b2734801a 100644 --- a/react/features/base/react/RouteRegistry.js +++ b/react/features/base/react/RouteRegistry.js @@ -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>, // 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>) { + +/* 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); } } diff --git a/react/features/base/react/functions.js b/react/features/base/react/functions.js index 2c27bce85..457418d6e 100644 --- a/react/features/base/react/functions.js +++ b/react/features/base/react/functions.js @@ -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(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 diff --git a/react/features/base/redux/MiddlewareRegistry.js b/react/features/base/redux/MiddlewareRegistry.js index 5cfdd44e5..6ca51e09d 100644 --- a/react/features/base/redux/MiddlewareRegistry.js +++ b/react/features/base/redux/MiddlewareRegistry.js @@ -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); } } diff --git a/react/features/base/util/randomUtil.js b/react/features/base/util/randomUtil.js index dc9106e24..e9f177136 100644 --- a/react/features/base/util/randomUtil.js +++ b/react/features/base/util/randomUtil.js @@ -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 | string) { +export function randomElement(arr: [any] | string) { return arr[randomInt(0, arr.length - 1)]; }