Browse Source

[flow] Type annotations

master
Lyubomir Marinov 8 years ago
parent
commit
cfa3047330

+ 2
- 0
react/features/base/react/Platform.native.js View File

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

+ 2
- 0
react/features/base/react/Platform.web.js View File

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

+ 28
- 12
react/features/base/react/RouteRegistry.js View File

@@ -1,3 +1,7 @@
1
+/* @flow */
2
+
3
+import { Component } from 'react';
4
+
1 5
 /**
2 6
  * Object describing application route.
3 7
  *
@@ -5,12 +9,18 @@
5 9
  * @property {Component} component - React Component constructor.
6 10
  * @property {string} path - URL route, required for web routing.
7 11
  */
12
+type Route = {
13
+    component: Class<Component<*>>, // eslint-disable-line no-undef
14
+    path: string
15
+};
8 16
 
9 17
 /**
10 18
  * A registry for Navigator routes, allowing features to register themselves
11 19
  * without needing to create additional inter-feature dependencies.
12 20
  */
13 21
 class RouteRegistry {
22
+    _elements: Route[];
23
+
14 24
     /**
15 25
      * Initializes a new RouteRegistry instance.
16 26
      */
@@ -19,8 +29,9 @@ class RouteRegistry {
19 29
          * The set of registered routes.
20 30
          *
21 31
          * @private
32
+         * @type {Route[]}
22 33
          */
23
-        this._elements = new Set();
34
+        this._elements = [];
24 35
     }
25 36
 
26 37
     /**
@@ -32,7 +43,7 @@ class RouteRegistry {
32 43
      * @returns {boolean} True if the specified a and b describe one and the
33 44
      * same abstract route; otherwise, false.
34 45
      */
35
-    areRoutesEqual(a, b) {
46
+    areRoutesEqual(a: Route, b: Route) {
36 47
         if (a === b) { // reflexive
37 48
             return true;
38 49
         }
@@ -60,21 +71,25 @@ class RouteRegistry {
60 71
         // We use the destructuring operator to 'clone' the route object to
61 72
         // prevent modifications from outside (e.g. React Native's Navigator
62 73
         // extends it with additional properties).
63
-        return [ ...this._elements ].map(r => {
74
+        return this._elements.map(r => {
64 75
             return { ...r };
65 76
         });
66 77
     }
67 78
 
79
+/* eslint-disable no-undef */
80
+
68 81
     /**
69 82
      * Returns registered route by name if any.
70 83
      *
71
-     * @param {Object} component - The React Component (class) of the route to
72
-     * retrieve.
84
+     * @param {Component} component - The React Component (class) of the route
85
+     * to retrieve.
73 86
      * @returns {Route|null}
74 87
      */
75
-    getRouteByComponent(component) {
76
-        const route
77
-            = [ ...this._elements ].find(r => r.component === component);
88
+    getRouteByComponent(component: Class<Component<*>>) {
89
+
90
+/* eslint-enable no-undef */
91
+
92
+        const route = this._elements.find(r => r.component === component);
78 93
 
79 94
         // We use destructuring operator to 'clone' route object to prevent
80 95
         // modifications from outside (e.g. React Native's Navigator extends
@@ -88,12 +103,13 @@ class RouteRegistry {
88 103
      * @param {Route} route - Route definition object.
89 104
      * @returns {void}
90 105
      */
91
-    register(route) {
92
-        if (this._elements.has(route)) {
93
-            throw new Error(`Route ${route.component} is registered already!`);
106
+    register(route: Route) {
107
+        if (this._elements.includes(route)) {
108
+            throw new Error(
109
+                    `Route ${String(route.component)} is registered already!`);
94 110
         }
95 111
 
96
-        this._elements.add(route);
112
+        this._elements.push(route);
97 113
     }
98 114
 }
99 115
 

+ 5
- 2
react/features/base/react/functions.js View File

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

+ 16
- 7
react/features/base/redux/MiddlewareRegistry.js View File

@@ -1,29 +1,38 @@
1
+/* @flow */
2
+
1 3
 import { applyMiddleware } from 'redux';
2 4
 
5
+type Middleware = Function;
6
+
3 7
 /**
4 8
  * A registry for Redux middleware, allowing features to register their
5 9
  * middleware without needing to create additional inter-feature dependencies.
6 10
  */
7 11
 class MiddlewareRegistry {
12
+    _elements: Middleware[];
13
+
8 14
     /**
9 15
      * Creates a MiddlewareRegistry instance.
10 16
      */
11 17
     constructor() {
12 18
         /**
13 19
          * The set of registered middleware.
20
+         *
21
+         * @private
22
+         * @type {Route[]}
14 23
          */
15
-        this._elements = new Set();
24
+        this._elements = [];
16 25
     }
17 26
 
18 27
     /**
19 28
      * Applies all registered middleware into a store enhancer.
20 29
      * (@link http://redux.js.org/docs/api/applyMiddleware.html).
21 30
      *
22
-     * @param {Function[]} additional - Any additional middleware that need to
31
+     * @param {Middleware[]} additional - Any additional middleware that need to
23 32
      * be included (such as middleware from third-party modules).
24
-     * @returns {Function}
33
+     * @returns {Middleware}
25 34
      */
26
-    applyMiddleware(...additional) {
35
+    applyMiddleware(...additional: Middleware[]) {
27 36
         return applyMiddleware(
28 37
             ...this._elements,
29 38
             ...additional
@@ -35,11 +44,11 @@ class MiddlewareRegistry {
35 44
      *
36 45
      * The method is to be invoked only before {@link #applyMiddleware()}.
37 46
      *
38
-     * @param {Function} middleware - A Redux middleware.
47
+     * @param {Middleware} middleware - A Redux middleware.
39 48
      * @returns {void}
40 49
      */
41
-    register(middleware) {
42
-        this._elements.add(middleware);
50
+    register(middleware: Middleware) {
51
+        this._elements.push(middleware);
43 52
     }
44 53
 }
45 54
 

+ 1
- 1
react/features/base/util/randomUtil.js View File

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

Loading…
Cancel
Save