|
| 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 | +const requestAnimationFrame = require('fbjs/lib/requestAnimationFrame'); |
| 14 | +const immutable = require('immutable'); |
| 15 | + |
| 16 | +import type {Measurement} from './BananaSlugTypes'; |
| 17 | + |
| 18 | +// How long the measurement can be cached in ms. |
| 19 | +const DURATION = 800; |
| 20 | + |
| 21 | +const {Record, Map, Set} = immutable; |
| 22 | + |
| 23 | +const MeasurementRecord = Record({ |
| 24 | + bottom: 0, |
| 25 | + expiration: 0, |
| 26 | + height: 0, |
| 27 | + id: '', |
| 28 | + left: 0, |
| 29 | + right: 0, |
| 30 | + scrollX: 0, |
| 31 | + scrollY: 0, |
| 32 | + top: 0, |
| 33 | + width: 0, |
| 34 | +}); |
| 35 | + |
| 36 | +var _id = 100; |
| 37 | + |
| 38 | +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 | + |
| 46 | + constructor() { |
| 47 | + // pending nodes to measure. |
| 48 | + this._nodes = new Map(); |
| 49 | + |
| 50 | + // ids of pending nodes. |
| 51 | + this._ids = new Map(); |
| 52 | + |
| 53 | + // cached measurements. |
| 54 | + this._measurements = new Map(); |
| 55 | + |
| 56 | + // callbacks for pending nodes. |
| 57 | + this._callbacks = new Map(); |
| 58 | + |
| 59 | + this._isRequesting = false; |
| 60 | + |
| 61 | + // non-auto-binds. |
| 62 | + this._measureNodes = this._measureNodes.bind(this); |
| 63 | + } |
| 64 | + |
| 65 | + request(node: Node, callback: (v: Measurement) => void): string { |
| 66 | + var requestID = this._nodes.has(node) ? |
| 67 | + this._nodes.get(node) : |
| 68 | + String(_id++); |
| 69 | + |
| 70 | + this._nodes = this._nodes.set(node, requestID); |
| 71 | + this._ids = this._ids.set(requestID, node); |
| 72 | + |
| 73 | + var callbacks = this._callbacks.has(node) ? |
| 74 | + this._callbacks.get(node) : |
| 75 | + new Set(); |
| 76 | + |
| 77 | + callbacks = callbacks.add(callback); |
| 78 | + this._callbacks = this._callbacks.set(node, callbacks); |
| 79 | + |
| 80 | + if (this._isRequesting) { |
| 81 | + return requestID; |
| 82 | + } |
| 83 | + |
| 84 | + this._isRequesting = true; |
| 85 | + 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 | + } |
| 96 | + } |
| 97 | + |
| 98 | + measureImpl(node: Node): Measurement { |
| 99 | + // sub-class must overwrite this. |
| 100 | + return new MeasurementRecord(); |
| 101 | + } |
| 102 | + |
| 103 | + _measureNodes(): void { |
| 104 | + var now = Date.now(); |
| 105 | + |
| 106 | + this._measurements = this._measurements.withMutations(_measurements => { |
| 107 | + for (const node of this._nodes.keys()) { |
| 108 | + const measurement = this._measureNode(now, node); |
| 109 | + // cache measurement. |
| 110 | + _measurements.set(node, measurement); |
| 111 | + } |
| 112 | + }); |
| 113 | + |
| 114 | + // execute callbacks. |
| 115 | + for (const node of this._nodes.keys()) { |
| 116 | + const measurement = this._measurements.get(node); |
| 117 | + this._callbacks.get(node).forEach(callback => callback(measurement)); |
| 118 | + } |
| 119 | + |
| 120 | + // clear stale measurement. |
| 121 | + this._measurements = this._measurements.withMutations(_measurements => { |
| 122 | + for (const [node, measurement] of _measurements.entries()) { |
| 123 | + if (measurement.expiration < now) { |
| 124 | + _measurements.delete(node); |
| 125 | + } |
| 126 | + } |
| 127 | + }); |
| 128 | + |
| 129 | + this._ids = this._ids.clear(); |
| 130 | + this._nodes = this._nodes.clear(); |
| 131 | + this._callbacks = this._callbacks.clear(); |
| 132 | + this._isRequesting = false; |
| 133 | + } |
| 134 | + |
| 135 | + _measureNode(timestamp: number, node: Node): Measurement { |
| 136 | + var measurement; |
| 137 | + var data; |
| 138 | + |
| 139 | + if (this._measurements.has(node)) { |
| 140 | + measurement = this._measurements.get(node); |
| 141 | + if (measurement.expiration < timestamp) { |
| 142 | + // measurement expires. measure again. |
| 143 | + data = this.measureImpl(node); |
| 144 | + measurement = measurement.merge({ |
| 145 | + ...data, |
| 146 | + expiration: timestamp + DURATION, |
| 147 | + }); |
| 148 | + } |
| 149 | + } else { |
| 150 | + data = this.measureImpl(node); |
| 151 | + measurement = new MeasurementRecord({ |
| 152 | + ...data, |
| 153 | + expiration: timestamp + DURATION, |
| 154 | + id: 'm_' + String(_id++), |
| 155 | + }); |
| 156 | + } |
| 157 | + return measurement; |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +module.exports = BananaSlugAbstractNodeMeasurer; |
0 commit comments