зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1591407 - Remove unused devtools/shared/client/event-source.js r=nchevobbe
Differential Revision: https://phabricator.services.mozilla.com/D50601 --HG-- extra : moz-landing-system : lando
This commit is contained in:
Родитель
6261a77787
Коммит
73f6c47923
|
@ -1,138 +0,0 @@
|
||||||
/* 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 DevToolsUtils = require("devtools/shared/DevToolsUtils");
|
|
||||||
|
|
||||||
/**
|
|
||||||
* TODO: Get rid of this API in favor of EventTarget (bug 1042642)
|
|
||||||
*
|
|
||||||
* Add simple event notification to a prototype object. Any object that has
|
|
||||||
* some use for event notifications or the observer pattern in general can be
|
|
||||||
* augmented with the necessary facilities by passing its prototype to this
|
|
||||||
* function.
|
|
||||||
*
|
|
||||||
* @param proto object
|
|
||||||
* The prototype object that will be modified.
|
|
||||||
*/
|
|
||||||
function eventSource(proto) {
|
|
||||||
/**
|
|
||||||
* Add a listener to the event source for a given event.
|
|
||||||
*
|
|
||||||
* @param name string
|
|
||||||
* The event to listen for.
|
|
||||||
* @param listener function
|
|
||||||
* Called when the event is fired. If the same listener
|
|
||||||
* is added more than once, it will be called once per
|
|
||||||
* addListener call.
|
|
||||||
* @param key function (optional)
|
|
||||||
* Key to use for removeListener, defaults to the listener. Used by helper method
|
|
||||||
* addOneTimeListener, which creates a custom listener. Use the original listener
|
|
||||||
* as key to allow to remove oneTimeListeners.
|
|
||||||
*/
|
|
||||||
proto.addListener = function(name, listener, key = listener) {
|
|
||||||
if (typeof listener != "function") {
|
|
||||||
throw TypeError("Listeners must be functions.");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!this._listeners) {
|
|
||||||
this._listeners = {};
|
|
||||||
}
|
|
||||||
|
|
||||||
this._getListeners(name).push({ key, callback: listener });
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add a listener to the event source for a given event. The
|
|
||||||
* listener will be removed after it is called for the first time.
|
|
||||||
*
|
|
||||||
* @param name string
|
|
||||||
* The event to listen for.
|
|
||||||
* @param listener function
|
|
||||||
* Called when the event is fired.
|
|
||||||
* @returns Promise
|
|
||||||
* Resolved with an array of the arguments of the event.
|
|
||||||
*/
|
|
||||||
proto.addOneTimeListener = function(name, listener) {
|
|
||||||
return new Promise(resolve => {
|
|
||||||
const oneTimeListener = (eventName, ...rest) => {
|
|
||||||
this.removeListener(name, listener);
|
|
||||||
if (listener) {
|
|
||||||
listener(eventName, ...rest);
|
|
||||||
}
|
|
||||||
resolve(rest[0]);
|
|
||||||
};
|
|
||||||
this.addListener(name, oneTimeListener, listener);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove a listener from the event source previously added with
|
|
||||||
* addListener().
|
|
||||||
*
|
|
||||||
* @param name string
|
|
||||||
* The event name used during addListener to add the listener.
|
|
||||||
* @param listener function
|
|
||||||
* The callback to remove. If addListener was called multiple
|
|
||||||
* times, all instances will be removed.
|
|
||||||
*/
|
|
||||||
proto.removeListener = function(name, listener) {
|
|
||||||
if (!this._listeners || (listener && !this._listeners[name])) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!listener) {
|
|
||||||
this._listeners[name] = [];
|
|
||||||
} else {
|
|
||||||
this._listeners[name] = this._listeners[name].filter(
|
|
||||||
l => l.key != listener
|
|
||||||
);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the listeners for the specified event name. If none are defined it
|
|
||||||
* initializes an empty list and returns that.
|
|
||||||
*
|
|
||||||
* @param name string
|
|
||||||
* The event name.
|
|
||||||
*/
|
|
||||||
proto._getListeners = function(name) {
|
|
||||||
if (name in this._listeners) {
|
|
||||||
return this._listeners[name];
|
|
||||||
}
|
|
||||||
this._listeners[name] = [];
|
|
||||||
return this._listeners[name];
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Notify listeners of an event.
|
|
||||||
*
|
|
||||||
* @param name string
|
|
||||||
* The event to fire.
|
|
||||||
* @param arguments
|
|
||||||
* All arguments will be passed along to the listeners,
|
|
||||||
* including the name argument.
|
|
||||||
*/
|
|
||||||
proto.emit = function() {
|
|
||||||
if (!this._listeners) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const name = arguments[0];
|
|
||||||
const listeners = this._getListeners(name).slice(0);
|
|
||||||
|
|
||||||
for (const listener of listeners) {
|
|
||||||
try {
|
|
||||||
listener.callback.apply(null, arguments);
|
|
||||||
} catch (e) {
|
|
||||||
// Prevent a bad listener from interfering with the others.
|
|
||||||
DevToolsUtils.reportException("notify event '" + name + "'", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = eventSource;
|
|
|
@ -9,6 +9,5 @@ DevToolsModules(
|
||||||
'constants.js',
|
'constants.js',
|
||||||
'debugger-client.js',
|
'debugger-client.js',
|
||||||
'deprecated-thread-client.js',
|
'deprecated-thread-client.js',
|
||||||
'event-source.js',
|
|
||||||
'long-string-client.js',
|
'long-string-client.js',
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,66 +0,0 @@
|
||||||
/* Any copyright is dedicated to the Public Domain.
|
|
||||||
http://creativecommons.org/publicdomain/zero/1.0/ */
|
|
||||||
|
|
||||||
"use strict";
|
|
||||||
|
|
||||||
const eventSource = require("devtools/shared/client/event-source");
|
|
||||||
|
|
||||||
// Test basic event-source APIs:
|
|
||||||
// - addListener
|
|
||||||
// - removeListener
|
|
||||||
// - addOneTimeListener
|
|
||||||
|
|
||||||
add_task(function() {
|
|
||||||
// Create a basic test object that can emit events using event-source.js
|
|
||||||
class TestClass {}
|
|
||||||
eventSource(TestClass.prototype);
|
|
||||||
const testObject = new TestClass();
|
|
||||||
|
|
||||||
testBasicAddRemoveListener(testObject);
|
|
||||||
|
|
||||||
info("Check that one time listeners are only triggered once");
|
|
||||||
testOneTimeListener(testObject);
|
|
||||||
|
|
||||||
info("Check that one time listeners can be removed");
|
|
||||||
testRemoveOneTimeListener(testObject);
|
|
||||||
});
|
|
||||||
|
|
||||||
function testBasicAddRemoveListener(testObject) {
|
|
||||||
let eventsReceived = 0;
|
|
||||||
const onTestEvent = () => eventsReceived++;
|
|
||||||
|
|
||||||
testObject.addListener("event-testBasicAddRemoveListener", onTestEvent);
|
|
||||||
testObject.emit("event-testBasicAddRemoveListener");
|
|
||||||
ok(eventsReceived === 1, "Event listener was triggered");
|
|
||||||
|
|
||||||
testObject.emit("event-testBasicAddRemoveListener");
|
|
||||||
ok(eventsReceived === 2, "Event listener was triggered again");
|
|
||||||
|
|
||||||
testObject.removeListener("event-testBasicAddRemoveListener", onTestEvent);
|
|
||||||
testObject.emit("event-testBasicAddRemoveListener");
|
|
||||||
ok(eventsReceived === 2, "Event listener was not triggered anymore");
|
|
||||||
}
|
|
||||||
|
|
||||||
function testOneTimeListener(testObject) {
|
|
||||||
let eventsReceived = 0;
|
|
||||||
const onTestEvent = () => eventsReceived++;
|
|
||||||
|
|
||||||
testObject.addOneTimeListener("event-testOneTimeListener", onTestEvent);
|
|
||||||
testObject.emit("event-testOneTimeListener");
|
|
||||||
ok(eventsReceived === 1, "Event listener was triggered");
|
|
||||||
|
|
||||||
testObject.emit("event-testOneTimeListener");
|
|
||||||
ok(eventsReceived === 1, "Event listener was not triggered again");
|
|
||||||
|
|
||||||
testObject.removeListener("event-testOneTimeListener", onTestEvent);
|
|
||||||
}
|
|
||||||
|
|
||||||
function testRemoveOneTimeListener(testObject) {
|
|
||||||
let eventsReceived = 0;
|
|
||||||
const onTestEvent = () => eventsReceived++;
|
|
||||||
|
|
||||||
testObject.addOneTimeListener("event-testRemoveOneTimeListener", onTestEvent);
|
|
||||||
testObject.removeListener("event-testRemoveOneTimeListener", onTestEvent);
|
|
||||||
testObject.emit("event-testRemoveOneTimeListener");
|
|
||||||
ok(eventsReceived === 0, "Event listener was already removed");
|
|
||||||
}
|
|
|
@ -19,7 +19,6 @@ support-files =
|
||||||
run-if = nightly_build
|
run-if = nightly_build
|
||||||
[test_eventemitter_basic.js]
|
[test_eventemitter_basic.js]
|
||||||
[test_eventemitter_static.js]
|
[test_eventemitter_static.js]
|
||||||
[test_eventsource.js]
|
|
||||||
[test_fetch-bom.js]
|
[test_fetch-bom.js]
|
||||||
[test_fetch-chrome.js]
|
[test_fetch-chrome.js]
|
||||||
[test_fetch-file.js]
|
[test_fetch-file.js]
|
||||||
|
|
Загрузка…
Ссылка в новой задаче