gecko-dev/devtools/shared/gcli/command-state.js
Nicolas Chevobbe 1ea09e91a2 Bug 1450195 - Remove old-event-emitter usage from gcli; r=miker.
MozReview-Commit-ID: IYAQJ4KqEet

--HG--
extra : rebase_source : 7b15bbbd0876a14259242724836ab90b93d7bb2b
2018-03-30 09:20:11 +02:00

79 lines
2.4 KiB
JavaScript

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const EventEmitter = require("devtools/shared/event-emitter");
const getTargetId = ({tab}) => tab.linkedBrowser.outerWindowID;
const enabledCommands = new Map();
/**
* The `CommandState` is a singleton that provides utility methods to keep the commands'
* state in sync between the toolbox, the toolbar and the content.
*/
const CommandState = EventEmitter.decorate({
/**
* Returns if a command is enabled on a given target.
*
* @param {Object} target
* The target object must have a tab's reference.
* @param {String} command
* The command's name used in gcli.
* @ returns {Boolean} returns `false` if the command is not enabled for the target
* given, or if the target given hasn't a tab; `true` otherwise.
*/
isEnabledForTarget(target, command) {
if (!target.tab || !enabledCommands.has(command)) {
return false;
}
return enabledCommands.get(command).has(getTargetId(target));
},
/**
* Enables a command on a given target.
* Emits a "changed" event to notify potential observers about the new commands state.
*
* @param {Object} target
* The target object must have a tab's reference.
* @param {String} command
* The command's name used in gcli.
*/
enableForTarget(target, command) {
if (!target.tab) {
return;
}
if (!enabledCommands.has(command)) {
enabledCommands.set(command, new Set());
}
enabledCommands.get(command).add(getTargetId(target));
CommandState.emit("changed", {target, command});
},
/**
* Disabled a command on a given target.
* Emits a "changed" event to notify potential observers about the new commands state.
*
* @param {Object} target
* The target object must have a tab's reference.
* @param {String} command
* The command's name used in gcli.
*/
disableForTarget(target, command) {
if (!target.tab || !enabledCommands.has(command)) {
return;
}
enabledCommands.get(command).delete(getTargetId(target));
CommandState.emit("changed", {target, command});
},
});
exports.CommandState = CommandState;