55 * This source code is licensed under the BSD-style license found in the
66 * LICENSE file in the root directory of this source tree. An additional grant
77 * of patent rights can be found in the PATENTS file in the same directory.
8+ *
9+ * @flow
810 */
911'use strict' ;
1012
1113const requestAnimationFrame = require ( 'fbjs/lib/requestAnimationFrame' ) ;
1214const immutable = require ( 'immutable' ) ;
1315
16+ import type { Measurement } from './BananaSlugTypes' ;
17+
1418// How long the measurement can be cached in ms.
1519const DURATION = 800 ;
1620
1721const { Record, Map, Set} = immutable ;
1822
19- const Measurement = Record ( {
23+ const MeasurementRecord = Record ( {
2024 bottom : 0 ,
2125 expiration : 0 ,
2226 height : 0 ,
@@ -32,6 +36,13 @@ const Measurement = Record({
3236var _id = 100 ;
3337
3438class BananaSlugAbstractNodeMeasurer {
39+ _callbacks : Map < Node , ( v : Measurement ) => void > ;
40+ _ids : Map < string , Node > ;
41+ _isRequesting : boolean ;
42+ _measureNodes : ( ) => void ;
43+ _measurements : Map < Node , Measurement > ;
44+ _nodes : Map < string , Node > ;
45+
3546 constructor ( ) {
3647 // pending nodes to measure.
3748 this . _nodes = new Map ( ) ;
@@ -51,7 +62,7 @@ class BananaSlugAbstractNodeMeasurer {
5162 this . _measureNodes = this . _measureNodes . bind ( this ) ;
5263 }
5364
54- request ( node , callback ) : string {
65+ request ( node : Node , callback : ( v : Measurement ) = > void ) : string {
5566 var requestID = this . _nodes . has ( node ) ?
5667 this . _nodes . get ( node ) :
5768 String ( _id ++ ) ;
@@ -67,15 +78,26 @@ class BananaSlugAbstractNodeMeasurer {
6778 this . _callbacks = this . _callbacks . set ( node , callbacks ) ;
6879
6980 if ( this . _isRequesting ) {
70- return ;
81+ return requestID ;
7182 }
7283
7384 this . _isRequesting = true ;
7485 requestAnimationFrame ( this . _measureNodes ) ;
86+ return requestID ;
87+ }
88+
89+ cancel ( requestID : string ) : void {
90+ if ( this . _ids . has ( requestID ) ) {
91+ var node = this . _ids . get ( requestID ) ;
92+ this . _ids = this . _ids . delete ( requestID ) ;
93+ this . _nodes = this . _nodes . delete ( node ) ;
94+ this . _callbacks = this . _callbacks . delete ( node ) ;
95+ }
7596 }
7697
77- measureImpl ( node : any ) : void {
78- // sub-class must implement this.
98+ measureImpl ( node : Node ) : Measurement {
99+ // sub-class must overwrite this.
100+ return new MeasurementRecord ( ) ;
79101 }
80102
81103 _measureNodes ( ) : void {
@@ -110,7 +132,7 @@ class BananaSlugAbstractNodeMeasurer {
110132 this . _isRequesting = false ;
111133 }
112134
113- _measureNode ( timestamp : number , node : any ) : Measurement {
135+ _measureNode ( timestamp : number , node : Node ) : Measurement {
114136 var measurement ;
115137 var data ;
116138
@@ -126,7 +148,7 @@ class BananaSlugAbstractNodeMeasurer {
126148 }
127149 } else {
128150 data = this . measureImpl ( node ) ;
129- measurement = new Measurement ( {
151+ measurement = new MeasurementRecord ( {
130152 ...data ,
131153 expiration : timestamp + DURATION ,
132154 id : 'm_' + String ( _id ++ ) ,
0 commit comments