actor-releaser.js (1824B)
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 { 8 MESSAGES_ADD, 9 MESSAGES_CLEAR, 10 PRIVATE_MESSAGES_CLEAR, 11 FRONTS_TO_RELEASE_CLEAR, 12 } = require("resource://devtools/client/webconsole/constants.js"); 13 14 /** 15 * This enhancer is responsible for releasing actors on the backend. 16 * When messages with arguments are removed from the store we should also 17 * clean up the backend. 18 */ 19 function enableActorReleaser(webConsoleUI) { 20 return next => (reducer, initialState, enhancer) => { 21 function releaseActorsEnhancer(state, action) { 22 state = reducer(state, action); 23 24 const { type } = action; 25 if ( 26 webConsoleUI && 27 [MESSAGES_ADD, MESSAGES_CLEAR, PRIVATE_MESSAGES_CLEAR].includes(type) 28 ) { 29 const { frontInSidebar } = state.ui; 30 let { frontsToRelease } = state.messages; 31 // Ignore the front for object still displayed in the sidebar, if there is one. 32 frontsToRelease = frontInSidebar 33 ? frontsToRelease.filter( 34 front => frontInSidebar.actorID !== front.actorID 35 ) 36 : state.messages.frontsToRelease; 37 38 webConsoleUI.hud.commands.objectCommand 39 .releaseObjects(frontsToRelease) 40 // Emit an event we can listen to to make sure all the fronts were released. 41 .then(() => webConsoleUI.emitForTests("fronts-released")); 42 43 // Reset `frontsToRelease` in message reducer. 44 state = reducer(state, { 45 type: FRONTS_TO_RELEASE_CLEAR, 46 }); 47 } 48 49 return state; 50 } 51 52 return next(releaseActorsEnhancer, initialState, enhancer); 53 }; 54 } 55 56 module.exports = enableActorReleaser;