Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Commit 1fa2fe0

Browse files
Hedger WangHedger Wang
authored andcommitted
add flow types
1 parent 185a398 commit 1fa2fe0

7 files changed

Lines changed: 144 additions & 24 deletions

plugins/BananaSlug/BananaSlugAbstractNodeMeasurer.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,22 @@
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

1113
const requestAnimationFrame = require('fbjs/lib/requestAnimationFrame');
1214
const immutable = require('immutable');
1315

16+
import type {Measurement} from './BananaSlugTypes';
17+
1418
// How long the measurement can be cached in ms.
1519
const DURATION = 800;
1620

1721
const {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({
3236
var _id = 100;
3337

3438
class 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++),

plugins/BananaSlug/BananaSlugAbstractNodePresenter.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@
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
*/
9-
1011
'use strict';
1112

1213
const immutable = require('immutable');
1314
const requestAnimationFrame = require('fbjs/lib/requestAnimationFrame');
1415

16+
import type {
17+
Measurement,
18+
} from './BananaSlugTypes';
19+
1520
// How long the measurement should be presented for.
1621
const DURATION = 250;
1722

@@ -23,6 +28,13 @@ const MetaData = Record({
2328
});
2429

2530
class BananaSlugAbstractNodePresenter {
31+
_clearTimer: number;
32+
_draw: () => void;
33+
_drawing: boolean;
34+
_enabled: boolean;
35+
_pool: Map<Measurement, MetaData>;
36+
_redraw: () => void;
37+
2638
constructor() {
2739
this._pool = new Map();
2840
this._drawing = false;
@@ -33,7 +45,7 @@ class BananaSlugAbstractNodePresenter {
3345
this._redraw = this._redraw.bind(this);
3446
}
3547

36-
present(measurement: Object): void {
48+
present(measurement: Measurement): void {
3749
if (!this._enabled) {
3850
return;
3951
}
@@ -73,15 +85,15 @@ class BananaSlugAbstractNodePresenter {
7385

7486
if (this._clearTimer) {
7587
clearTimeout(this._clearTimer);
76-
this._clearTimer = null;
88+
this._clearTimer = 0;
7789
}
7890

7991
this._pool = this._pool.clear();
8092
this._drawing = false;
8193
this.clearImpl();
8294
}
8395

84-
drawImpl(info: Object): void {
96+
drawImpl(measurements: Map<Measurement, MetaData>): void {
8597
// sub-class should implement this.
8698
}
8799

@@ -90,7 +102,7 @@ class BananaSlugAbstractNodePresenter {
90102
}
91103

92104
_redraw(): void {
93-
this._clearTimer = null;
105+
this._clearTimer = 0;
94106
if (!this._drawing && this._pool.size > 0) {
95107
this._drawing = true;
96108
this._draw();

plugins/BananaSlug/BananaSlugBackendManager.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
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

1012
'use strict';
@@ -14,11 +16,21 @@ const BananaSlugAbstractNodePresenter = require('./BananaSlugAbstractNodePresent
1416
const BananaSlugWebNodeMeasurer = require('./BananaSlugWebNodeMeasurer');
1517
const BananaSlugWebNodePresenter = require('./BananaSlugWebNodePresenter');
1618

17-
const NODE_TYPE_COMPOSITE = 'Composite';
19+
import type {
20+
Agent,
21+
Measurement,
22+
Measurer,
23+
Presenter,
24+
} from './BananaSlugTypes';
1825

19-
type Agent = any;
26+
const NODE_TYPE_COMPOSITE = 'Composite';
2027

2128
class BananaSlugBackendManager {
29+
_onMeasureNode: () => void;
30+
_measurer: Measurer;
31+
_presenter: Presenter;
32+
_isActive: boolean;
33+
2234
constructor(agent: Agent) {
2335
this._onMeasureNode = this._onMeasureNode.bind(this);
2436

@@ -55,7 +67,7 @@ class BananaSlugBackendManager {
5567
this._measurer.request(node, this._onMeasureNode);
5668
}
5769

58-
_onMeasureNode(measurement: Object): void {
70+
_onMeasureNode(measurement: Measurement): void {
5971
this._presenter.present(measurement);
6072
}
6173

plugins/BananaSlug/BananaSlugFrontendControl.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,40 @@
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

1012
'use strict';
1113

1214
const React = require('react');
13-
1415
const immutable = require('immutable');
1516

16-
const State = immutable.Record({
17+
import type {
18+
ControlState,
19+
} from './BananaSlugTypes';
20+
21+
type Props = {
22+
onChange: (v: ControlState) => void,
23+
};
24+
25+
type State = {};
26+
27+
type DefaultProps = {};
28+
29+
const StateRecord = immutable.Record({
1730
enabled: false,
1831
});
1932

20-
class BananaSlugFrontendControl extends React.Component {
21-
constructor(props) {
33+
class BananaSlugFrontendControl extends
34+
React.Component<DefaultProps, Props, State> {
35+
_defaultState: ControlState;
36+
_toogle: (b: boolean) => void;
37+
38+
constructor(props: Props) {
2239
super(props);
2340
this._toogle = this._toogle.bind(this);
24-
this._defaultState = new State();
41+
this._defaultState = new StateRecord();
2542
}
2643

2744
componentDidMount(): void {
@@ -30,7 +47,7 @@ class BananaSlugFrontendControl extends React.Component {
3047
}
3148
}
3249

33-
render() {
50+
render(): ReactElement {
3451
var state = this.props.state || this._defaultState;
3552
return (
3653
<div style={styles.container} onClick={this._toogle} tabIndex={0}>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @flow
10+
*/
11+
'use strict';
12+
13+
export type Agent = any;
14+
15+
export type Node = any;
16+
17+
export type Measurement = {
18+
bottom: number,
19+
expiration: number,
20+
height: number,
21+
id: string,
22+
left: number,
23+
right: number,
24+
scrollX: number,
25+
scrollY: number,
26+
top: number,
27+
width: number,
28+
};
29+
30+
export type onMeasureNode = (m: Measurement) => void;
31+
32+
export type Measurer = {
33+
request:(n: Node, c: onMeasureNode) => string,
34+
};
35+
36+
export type Presenter = {
37+
present: (m: Measurement) => void,
38+
setEnabled: (b: boolean) => void,
39+
};
40+
41+
export type ControlState = {
42+
enabled: boolean,
43+
};

plugins/BananaSlug/BananaSlugWebNodeMeasurer.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,23 @@
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

1012
'use strict';
1113

1214
const BananaSlugAbstractNodeMeasurer = require('./BananaSlugAbstractNodeMeasurer');
1315

16+
import type {
17+
Measurement,
18+
} from './BananaSlugTypes';
19+
1420
const DUMMY = {
1521
bottom: 0,
22+
expiration: 0,
1623
height: 0,
24+
id: '',
1725
left: 0,
1826
right: 0,
1927
scrollX: 0,
@@ -23,7 +31,7 @@ const DUMMY = {
2331
};
2432

2533
class BananaSlugWebNodeMeasurer extends BananaSlugAbstractNodeMeasurer {
26-
measureImpl(node): Object {
34+
measureImpl(node: any): Measurement {
2735
if (!node || typeof node.getBoundingClientRect !== 'function') {
2836
return DUMMY;
2937
}
@@ -45,13 +53,15 @@ class BananaSlugWebNodeMeasurer extends BananaSlugAbstractNodeMeasurer {
4553

4654
return {
4755
bottom: rect.bottom,
56+
expiration: 0,
4857
height: rect.height,
58+
id: '',
4959
left: rect.left,
5060
right: rect.right,
61+
scrollX,
62+
scrollY,
5163
top: rect.top,
5264
width: rect.width,
53-
scrollY,
54-
scrollX,
5565
};
5666
}
5767
}

plugins/BananaSlug/BananaSlugWebNodePresenter.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
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

1012
'use strict';
@@ -72,6 +74,8 @@ function drawBorder(ctx, measurement, borderWidth, borderColor) {
7274
const CANVAS_NODE_ID = 'BananaSlugWebNodePresenter';
7375

7476
class BananaSlugWebNodePresenter extends BananaSlugAbstractNodePresenter {
77+
_canvas: any;
78+
7579
constructor() {
7680
super();
7781
this._canvas = null;

0 commit comments

Comments
 (0)