diff options
author | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
---|---|---|
committer | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
commit | 5f8de423f190bbb79a62f804151bc24824fa32d8 (patch) | |
tree | 10027f336435511475e392454359edea8e25895d /services/cloudsync/CloudSyncEventSource.jsm | |
parent | 49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff) | |
download | uxp-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz |
Add m-esr52 at 52.6.0
Diffstat (limited to 'services/cloudsync/CloudSyncEventSource.jsm')
-rw-r--r-- | services/cloudsync/CloudSyncEventSource.jsm | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/services/cloudsync/CloudSyncEventSource.jsm b/services/cloudsync/CloudSyncEventSource.jsm new file mode 100644 index 0000000000..edb9c426b2 --- /dev/null +++ b/services/cloudsync/CloudSyncEventSource.jsm @@ -0,0 +1,65 @@ +/* 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/. */ + +this.EXPORTED_SYMBOLS = ["EventSource"]; + +Components.utils.import("resource://services-common/utils.js"); + +var EventSource = function (types, suspendFunc, resumeFunc) { + this.listeners = new Map(); + for (let type of types) { + this.listeners.set(type, new Set()); + } + + this.suspend = suspendFunc || function () {}; + this.resume = resumeFunc || function () {}; + + this.addEventListener = this.addEventListener.bind(this); + this.removeEventListener = this.removeEventListener.bind(this); +}; + +EventSource.prototype = { + addEventListener: function (type, listener) { + if (!this.listeners.has(type)) { + return; + } + this.listeners.get(type).add(listener); + this.resume(); + }, + + removeEventListener: function (type, listener) { + if (!this.listeners.has(type)) { + return; + } + this.listeners.get(type).delete(listener); + if (!this.hasListeners()) { + this.suspend(); + } + }, + + hasListeners: function () { + for (let l of this.listeners.values()) { + if (l.size > 0) { + return true; + } + } + return false; + }, + + emit: function (type, arg) { + if (!this.listeners.has(type)) { + return; + } + CommonUtils.nextTick( + function () { + for (let listener of this.listeners.get(type)) { + listener.call(undefined, arg); + } + }, + this + ); + }, +}; + +this.EventSource = EventSource; |