Mon 21 Jul 22:43:21 CEST 2025

This commit is contained in:
sbosse 2025-07-21 23:34:22 +02:00
parent 9551a8628b
commit b5bec48556

View File

@ -0,0 +1,66 @@
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var helpers = require('cordova-plugin-chrome-apps-common.helpers');
var Event = function(opt_eventName) {
this.name = opt_eventName || '';
this.listeners = [];
};
// Deliberately not filtering functions that are already added.
// I tested on desktop and it will call your callback once for each addListener.
Event.prototype.addListener = function(cb) {
this.listeners.push(cb);
};
Event.prototype.findListener_ = function(cb) {
for(var i = 0; i < this.listeners.length; i++) {
if (this.listeners[i] == cb) {
return i;
}
}
return -1;
};
Event.prototype.removeListener = function(cb) {
var index = this.findListener_(cb);
if (index >= 0) {
this.listeners.splice(index, 1);
}
};
Event.prototype.hasListener = function(cb) {
return this.findListener_(cb) >= 0;
};
Event.prototype.hasListeners = function() {
return this.listeners.length > 0;
};
Event.prototype.fire = function() {
// For Chrome Apps: Never fire events before background page is loaded.
// For vanilla Cordova: fire events after deviceready.
var self = this;
var args = Array.prototype.slice.call(arguments);
helpers.queueLifeCycleEvent(function() {
self._fireInternal.apply(self, args);
});
};
// Used by fire(), will trigger events immediately, rather than queuing to
// happen after startup/initialization
Event.prototype._fireInternal = function() {
for (var i = 0; i < this.listeners.length; i++) {
this.listeners[i].apply(this, arguments);
}
};
// Stubs since we don't support Rules.
Event.prototype.addRules = function() { };
Event.prototype.getRules = function() { };
Event.prototype.removeRules = function() { };
module.exports = Event;