@samitouri / QOS-React-2 / commits / fa9869b5a0

Type ReactOwner (#7587)

Incrementally type ReactInstance by adding the types of attach/detachRef. I moved isValidOwner as a function inside of the file since it's never used externally.

Christopher Chedeau committed Aug 28, 2016 at 10:31 UTC fa9869b5a080506571c91b09a845dcf01b99e71f
2 files changed +30 -20
src/renderers/shared/stack/reconciler/ReactInstanceType.js
+2 -2
@@ -24,8 +24,8 @@ export type ReactInstance = {
24 receiveComponent: any,
25 performUpdateIfNecessary: any,
26 updateComponent: any,
27 - attachRef: any,
28 - detachRef: any,
27 + attachRef: (ref: string, component: ReactInstance) => void,
28 + detachRef: (ref: string) => void,
29 getName: () => string,
30 getPublicInstance: any,
31
src/renderers/shared/stack/reconciler/ReactOwner.js
+28 -18
@@ -7,12 +7,28 @@
7 * of patent rights can be found in the PATENTS file in the same directory.
8 *
9 * @providesModule ReactOwner
10 + * @flow
11 */
12
13 'use strict';
14
15 var invariant = require('invariant');
16
17 +import type { ReactInstance } from 'ReactInstanceType';
18 +
19 +/**
20 + * @param {?object} object
21 + * @return {boolean} True if `object` is a valid owner.
22 + * @final
23 + */
24 +function isValidOwner(object: any): bool {
25 + return !!(
26 + object &&
27 + typeof object.attachRef === 'function' &&
28 + typeof object.detachRef === 'function'
29 + );
30 +}
31 +
32 /**
33 * ReactOwners are capable of storing references to owned components.
34 *
@@ -44,20 +60,6 @@ var invariant = require('invariant');
60 * @class ReactOwner
61 */
62 var ReactOwner = {
47 -
48 - /**
49 - * @param {?object} object
50 - * @return {boolean} True if `object` is a valid owner.
51 - * @final
52 - */
53 - isValidOwner: function(object) {
54 - return !!(
55 - object &&
56 - typeof object.attachRef === 'function' &&
57 - typeof object.detachRef === 'function'
58 - );
59 - },
60 -
63 /**
64 * Adds a component by ref to an owner component.
65 *
@@ -67,9 +69,13 @@ var ReactOwner = {
69 * @final
70 * @internal
71 */
70 - addComponentAsRefTo: function(component, ref, owner) {
72 + addComponentAsRefTo: function(
73 + component: ReactInstance,
74 + ref: string,
75 + owner: ReactInstance,
76 + ): void {
77 invariant(
72 - ReactOwner.isValidOwner(owner),
78 + isValidOwner(owner),
79 'addComponentAsRefTo(...): Only a ReactOwner can have refs. You might ' +
80 'be adding a ref to a component that was not created inside a component\'s ' +
81 '`render` method, or you have multiple copies of React loaded ' +
@@ -87,9 +93,13 @@ var ReactOwner = {
93 * @final
94 * @internal
95 */
90 - removeComponentAsRefFrom: function(component, ref, owner) {
96 + removeComponentAsRefFrom: function(
97 + component: ReactInstance,
98 + ref: string,
99 + owner: ReactInstance,
100 + ): void {
101 invariant(
92 - ReactOwner.isValidOwner(owner),
102 + isValidOwner(owner),
103 'removeComponentAsRefFrom(...): Only a ReactOwner can have refs. You might ' +
104 'be removing a ref to a component that was not created inside a component\'s ' +
105 '`render` method, or you have multiple copies of React loaded ' +