From fa9869b5a080506571c91b09a845dcf01b99e71f Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Sun, 28 Aug 2016 10:31:49 -0700 Subject: [PATCH] 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. --- .../stack/reconciler/ReactInstanceType.js | 4 +- .../shared/stack/reconciler/ReactOwner.js | 46 +++++++++++-------- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/renderers/shared/stack/reconciler/ReactInstanceType.js b/src/renderers/shared/stack/reconciler/ReactInstanceType.js index 36b2bd0d5d..b27d3371c7 100644 --- a/src/renderers/shared/stack/reconciler/ReactInstanceType.js +++ b/src/renderers/shared/stack/reconciler/ReactInstanceType.js @@ -24,8 +24,8 @@ export type ReactInstance = { receiveComponent: any, performUpdateIfNecessary: any, updateComponent: any, - attachRef: any, - detachRef: any, + attachRef: (ref: string, component: ReactInstance) => void, + detachRef: (ref: string) => void, getName: () => string, getPublicInstance: any, diff --git a/src/renderers/shared/stack/reconciler/ReactOwner.js b/src/renderers/shared/stack/reconciler/ReactOwner.js index df2bfb8d7b..270f7fb933 100644 --- a/src/renderers/shared/stack/reconciler/ReactOwner.js +++ b/src/renderers/shared/stack/reconciler/ReactOwner.js @@ -7,12 +7,28 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @providesModule ReactOwner + * @flow */ 'use strict'; var invariant = require('invariant'); +import type { ReactInstance } from 'ReactInstanceType'; + +/** + * @param {?object} object + * @return {boolean} True if `object` is a valid owner. + * @final + */ +function isValidOwner(object: any): bool { + return !!( + object && + typeof object.attachRef === 'function' && + typeof object.detachRef === 'function' + ); +} + /** * ReactOwners are capable of storing references to owned components. * @@ -44,20 +60,6 @@ var invariant = require('invariant'); * @class ReactOwner */ var ReactOwner = { - - /** - * @param {?object} object - * @return {boolean} True if `object` is a valid owner. - * @final - */ - isValidOwner: function(object) { - return !!( - object && - typeof object.attachRef === 'function' && - typeof object.detachRef === 'function' - ); - }, - /** * Adds a component by ref to an owner component. * @@ -67,9 +69,13 @@ var ReactOwner = { * @final * @internal */ - addComponentAsRefTo: function(component, ref, owner) { + addComponentAsRefTo: function( + component: ReactInstance, + ref: string, + owner: ReactInstance, + ): void { invariant( - ReactOwner.isValidOwner(owner), + isValidOwner(owner), 'addComponentAsRefTo(...): Only a ReactOwner can have refs. You might ' + 'be adding a ref to a component that was not created inside a component\'s ' + '`render` method, or you have multiple copies of React loaded ' + @@ -87,9 +93,13 @@ var ReactOwner = { * @final * @internal */ - removeComponentAsRefFrom: function(component, ref, owner) { + removeComponentAsRefFrom: function( + component: ReactInstance, + ref: string, + owner: ReactInstance, + ): void { invariant( - ReactOwner.isValidOwner(owner), + isValidOwner(owner), 'removeComponentAsRefFrom(...): Only a ReactOwner can have refs. You might ' + 'be removing a ref to a component that was not created inside a component\'s ' + '`render` method, or you have multiple copies of React loaded ' +