reflow.js (1579B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 const Targets = require("resource://devtools/server/actors/targets/index.js"); 8 9 const { 10 getLayoutChangesObserver, 11 releaseLayoutChangesObserver, 12 } = require("resource://devtools/server/actors/reflow.js"); 13 14 class ReflowWatcher { 15 /** 16 * Start watching for reflows related to a given Target Actor. 17 * 18 * @param TargetActor targetActor 19 * The target actor from which we should observe reflows 20 * @param Object options 21 * Dictionary object with following attributes: 22 * - onAvailable: mandatory function 23 * This will be called for each resource. 24 */ 25 async watch(targetActor, { onAvailable }) { 26 // Only track reflow for non-ParentProcess FRAME targets 27 if ( 28 targetActor.targetType !== Targets.TYPES.FRAME || 29 targetActor.typeName === "parentProcessTarget" 30 ) { 31 return; 32 } 33 34 this._targetActor = targetActor; 35 36 const onReflows = reflows => { 37 onAvailable([ 38 { 39 reflows, 40 }, 41 ]); 42 }; 43 44 this._observer = getLayoutChangesObserver(targetActor); 45 this._offReflows = this._observer.on("reflows", onReflows); 46 this._observer.start(); 47 } 48 49 destroy() { 50 releaseLayoutChangesObserver(this._targetActor); 51 52 if (this._offReflows) { 53 this._offReflows(); 54 this._offReflows = null; 55 } 56 } 57 } 58 59 module.exports = ReflowWatcher;