2014-09-04 23:44:00 +04:00
|
|
|
/* 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";
|
|
|
|
|
|
|
|
this.EXPORTED_SYMBOLS = ["Adapters"];
|
|
|
|
|
|
|
|
Components.utils.import("resource://gre/modules/Services.jsm");
|
|
|
|
Components.utils.import("resource://gre/modules/CloudSyncEventSource.jsm");
|
|
|
|
|
2017-01-10 20:09:02 +03:00
|
|
|
this.Adapters = function() {
|
2014-09-04 23:44:00 +04:00
|
|
|
let eventTypes = [
|
|
|
|
"sync",
|
|
|
|
];
|
|
|
|
|
|
|
|
let suspended = true;
|
|
|
|
|
2017-01-10 20:09:02 +03:00
|
|
|
let suspend = function() {
|
2014-09-04 23:44:00 +04:00
|
|
|
if (!suspended) {
|
2017-01-12 00:27:42 +03:00
|
|
|
Services.obs.removeObserver(observer, "cloudsync:user-sync");
|
2014-09-04 23:44:00 +04:00
|
|
|
suspended = true;
|
|
|
|
}
|
2017-01-10 20:09:02 +03:00
|
|
|
};
|
2014-09-04 23:44:00 +04:00
|
|
|
|
2017-01-10 20:09:02 +03:00
|
|
|
let resume = function() {
|
2014-09-04 23:44:00 +04:00
|
|
|
if (suspended) {
|
2017-04-14 22:51:38 +03:00
|
|
|
Services.obs.addObserver(observer, "cloudsync:user-sync");
|
2014-09-04 23:44:00 +04:00
|
|
|
suspended = false;
|
|
|
|
}
|
2017-01-10 20:09:02 +03:00
|
|
|
};
|
2014-09-04 23:44:00 +04:00
|
|
|
|
|
|
|
let eventSource = new EventSource(eventTypes, suspend, resume);
|
|
|
|
let registeredAdapters = new Map();
|
|
|
|
|
2017-01-10 20:09:02 +03:00
|
|
|
function register(name, opts) {
|
2014-09-04 23:44:00 +04:00
|
|
|
opts = opts || {};
|
|
|
|
registeredAdapters.set(name, opts);
|
|
|
|
}
|
|
|
|
|
2017-01-10 20:09:02 +03:00
|
|
|
function unregister(name) {
|
2014-09-04 23:44:00 +04:00
|
|
|
if (!registeredAdapters.has(name)) {
|
|
|
|
throw new Error("adapter is not registered: " + name)
|
|
|
|
}
|
|
|
|
registeredAdapters.delete(name);
|
|
|
|
}
|
|
|
|
|
2017-01-10 20:09:02 +03:00
|
|
|
function getAdapterNames() {
|
2014-09-04 23:44:00 +04:00
|
|
|
let result = [];
|
|
|
|
for (let name of registeredAdapters.keys()) {
|
|
|
|
result.push(name);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-01-10 20:09:02 +03:00
|
|
|
function getAdapter(name) {
|
2014-09-04 23:44:00 +04:00
|
|
|
if (!registeredAdapters.has(name)) {
|
|
|
|
throw new Error("adapter is not registered: " + name)
|
|
|
|
}
|
|
|
|
return registeredAdapters.get(name);
|
|
|
|
}
|
|
|
|
|
2017-01-10 20:09:02 +03:00
|
|
|
function countAdapters() {
|
2014-09-04 23:44:00 +04:00
|
|
|
return registeredAdapters.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
let observer = {
|
2017-01-10 20:09:02 +03:00
|
|
|
observe(subject, topic, data) {
|
2014-09-04 23:44:00 +04:00
|
|
|
switch (topic) {
|
|
|
|
case "cloudsync:user-sync":
|
|
|
|
eventSource.emit("sync");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.addEventListener = eventSource.addEventListener;
|
|
|
|
this.removeEventListener = eventSource.removeEventListener;
|
|
|
|
this.register = register.bind(this);
|
|
|
|
this.get = getAdapter.bind(this);
|
|
|
|
this.unregister = unregister.bind(this);
|
|
|
|
this.__defineGetter__("names", getAdapterNames);
|
|
|
|
this.__defineGetter__("count", countAdapters);
|
|
|
|
};
|
|
|
|
|
|
|
|
Adapters.prototype = {
|
|
|
|
|
|
|
|
};
|