Skip to content
This repository has been archived by the owner on Feb 26, 2022. It is now read-only.

Commit

Permalink
bug 1044607 - remove hasListenerFor
Browse files Browse the repository at this point in the history
  • Loading branch information
zombie committed Jul 28, 2014
1 parent f508bdb commit e66cbf3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 49 deletions.
20 changes: 5 additions & 15 deletions lib/sdk/content/content-worker.js
Expand Up @@ -46,15 +46,9 @@ Object.freeze({
}
return results;
}
function hasListenerFor(name) {
if (!(name in listeners))
return false;
return listeners[name].length > 0;
}
return {
eventEmitter: eventEmitter,
emit: onEvent,
hasListenerFor: hasListenerFor
emit: onEvent
};
},

Expand Down Expand Up @@ -83,7 +77,7 @@ Object.freeze({
emitToChrome(str);
}

let { eventEmitter, emit, hasListenerFor } =
let { eventEmitter, emit } =
ContentWorker.createEventEmitter(onEvent);

return {
Expand All @@ -95,8 +89,7 @@ Object.freeze({
// and modules (only used for context-menu API)
let args = typeof array == "string" ? JSON.parse(array) : array;
return emit.apply(null, args);
},
hasListenerFor: hasListenerFor
}
};
},

Expand Down Expand Up @@ -325,7 +318,7 @@ Object.freeze({

inject: function (exports, chromeAPI, emitToChrome, options) {
let ContentWorker = this;
let { pipe, onChromeEvent, hasListenerFor } =
let { pipe, onChromeEvent } =
ContentWorker.createPipe(emitToChrome);

ContentWorker.injectConsole(exports, pipe);
Expand All @@ -337,9 +330,6 @@ Object.freeze({

Object.freeze( exports.self );

return {
emitToContent: onChromeEvent,
hasListenerFor: hasListenerFor
};
return onChromeEvent;
}
});
16 changes: 3 additions & 13 deletions lib/sdk/content/sandbox.js
Expand Up @@ -77,15 +77,6 @@ const WorkerSandbox = Class({
return emitToContent(this, args);
},

/**
* Tells if content script has at least one listener registered for one event,
* through `self.on('xxx', ...)`.
* /!\ Shouldn't be used. Implemented to avoid breaking context-menu API.
*/
hasListenerFor: function hasListenerFor(name) {
return modelFor(this).hasListenerFor(name);
},

/**
* Configures sandbox and loads content scripts into it.
* @param {Worker} worker
Expand Down Expand Up @@ -185,10 +176,9 @@ const WorkerSandbox = Class({
let chromeAPI = createChromeAPI();
let result = Cu.waiveXrays(ContentWorker).inject(content, chromeAPI, onEvent, options);

// Merge `emitToContent` and `hasListenerFor` into our private
// model of the WorkerSandbox so we can communicate with content
// script
merge(model, result);
// Merge `emitToContent` into our private model of the
// WorkerSandbox so we can communicate with content script
model.emitToContent = result;

let console = new PlainTextConsole(null, getInnerId(window));

Expand Down
17 changes: 7 additions & 10 deletions lib/sdk/context-menu.js
Expand Up @@ -352,18 +352,15 @@ let menuRules = mix(labelledItemRules, {
let ContextWorker = Class({
implements: [ Worker ],

//Returns true if any context listeners are defined in the worker's port.
anyContextListeners: function anyContextListeners() {
return this.getSandbox().hasListenerFor("context");
},

// Calls the context workers context listeners and returns the first result
// that is either a string or a value that evaluates to true. If all of the
// listeners returned false then returns false. If there are no listeners
// then returns null.
// listeners returned false then returns false. If there are no listeners,
// returns true (show the menu item by default).
getMatchedContext: function getCurrentContexts(popupNode) {
let results = this.getSandbox().emitSync("context", popupNode);
return results.reduce(function(val, result) val || result, null);
if (!results.length)
return true;
return results.reduce((val, result) => val || result);
},

// Emits a click event in the worker's port. popupNode is the node that was
Expand All @@ -389,7 +386,7 @@ function hasMatchingContext(contexts, popupNode) {
// or no matched context then returns false.
function getCurrentWorkerContext(item, popupNode) {
let worker = getItemWorkerForWindow(item, popupNode.ownerDocument.defaultView);
if (!worker || !worker.anyContextListeners())
if (!worker)
return true;
return worker.getMatchedContext(popupNode);
}
Expand All @@ -399,7 +396,7 @@ function getCurrentWorkerContext(item, popupNode) {
function isItemVisible(item, popupNode, defaultVisibility) {
if (!item.context.length) {
let worker = getItemWorkerForWindow(item, popupNode.ownerDocument.defaultView);
if (!worker || !worker.anyContextListeners())
if (!worker)
return defaultVisibility;
}

Expand Down
12 changes: 1 addition & 11 deletions lib/sdk/deprecated/traits-worker.js
Expand Up @@ -86,15 +86,6 @@ const WorkerSandbox = EventEmitter.compose({
return this._emitToContent(args);
},

/**
* Tells if content script has at least one listener registered for one event,
* through `self.on('xxx', ...)`.
* /!\ Shouldn't be used. Implemented to avoid breaking context-menu API.
*/
hasListenerFor: function hasListenerFor(name) {
return this._hasListenerFor(name);
},

/**
* Method called by the worker sandbox when it needs to send a message
*/
Expand Down Expand Up @@ -217,8 +208,7 @@ const WorkerSandbox = EventEmitter.compose({
};
let onEvent = this._onContentEvent.bind(this);
let result = Cu.waiveXrays(ContentWorker).inject(content, chromeAPI, onEvent, options);
this._emitToContent = result.emitToContent;
this._hasListenerFor = result.hasListenerFor;
this._emitToContent = result;

// Handle messages send by this script:
let self = this;
Expand Down

0 comments on commit e66cbf3

Please sign in to comment.