Bug 1218817 - Implement ServiceWorkerRegistrationActorList;r=janx

This commit is contained in:
Eddy Bruel 2015-11-27 13:44:14 +01:00
Родитель ef0ba18ad3
Коммит 6feb8201a8
1 изменённых файлов: 88 добавлений и 0 удалений

Просмотреть файл

@ -243,3 +243,91 @@ ServiceWorkerRegistrationActor.prototype = {
};
}
};
function ServiceWorkerRegistrationActorList() {
this._actors = new Map();
this._onListChanged = null;
this._mustNotify = false;
this.onRegister = this.onRegister.bind(this);
this.onUnregister = this.onUnregister.bind(this);
};
ServiceWorkerRegistrationActorList.prototype = {
getList: function () {
// Create a set of registrations.
let registrations = new Set();
let array = swm.getAllRegistrations();
for (let index = 0; index < array.length; ++index) {
registrations.add(
array.queryElementAt(index, Ci.nsIServiceWorkerRegistrationInfo));
}
// Delete each actor for which we don't have a registration.
for (let [registration, ] of this._actors) {
if (!registrations.has(registration)) {
this._actors.delete(registration);
}
}
// Create an actor for each registration for which we don't have one.
for (let registration of registrations) {
if (!this._actors.has(registration)) {
this._actors.set(registration,
new ServiceWorkerRegistrationActor(registration));
}
}
if (!this._mustNotify) {
if (this._onListChanged !== null) {
swm.addListener(this);
}
this._mustNotify = true;
}
let actors = [];
for (let [, actor] of this._actors) {
actors.push(actor);
}
return Promise.resolve(actors);
},
get onListchanged() {
return this._onListchanged;
},
set onListChanged(onListChanged) {
if (typeof onListChanged !== "function" && onListChanged !== null) {
throw new Error("onListChanged must be either a function or null.");
}
if (this._mustNotify) {
if (this._onListChanged === null && onListChanged !== null) {
swm.addListener(this);
}
if (this._onListChanged !== null && onListChanged === null) {
swm.removeListener(this);
}
}
this._onListChanged = onListChanged;
},
_notifyListChanged: function () {
this._onListChanged();
if (this._onListChanged !== null) {
swm.removeListener(this);
}
this._mustNotify = false;
},
onRegister: function (registration) {
this._notifyListChanged();
},
onUnregister: function (registration) {
this._notifyListChanged();
}
};
exports.ServiceWorkerRegistrationActorList = ServiceWorkerRegistrationActorList;