-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.js
66 lines (54 loc) · 1.81 KB
/
events.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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;