This repository has been archived by the owner on Oct 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.event.js
205 lines (204 loc) · 5.95 KB
/
app.event.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
(function() {
'use strict';
/*******************************************************************************
* Copyright 2016 Pawel Psztyc, The ARC team
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
******************************************************************************/
/**
* EventTarget is an interface implemented by objects that can receive events and may have
* listeners for them.
*/
class ArcEventTarget {
/**
*
* @constructor
*/
constructor() {
/**
* A list of events registered by this {@link ArcEventTarget}
*/
this.events = new Map();
}
/**
* Register an event handler of a specific event type on the {@link ArcEventTarget}.
*
* @property {String} type A string representing the event type to listen for.
* @property {Function} listener The object that receives a notification when an event of the
* specified type occurs. This must be a JavaScript function.
*/
addEventListener(type, listener) {
if (typeof type !== 'string') {
throw new TypeError('Type must be a string.');
}
if (typeof listener !== 'function') {
throw new TypeError('listener must be a function.');
}
if (!this.events.has(type)) {
this.events.set(type, new Set());
}
var set = this.events.get(type);
if (set.has(listener)) {
console.warn('Listener for type %s is already registered.', type);
return;
}
set.add(listener);
}
/**
* Removes the event listener previously registered with
* {@link EventTarget.addEventListener()}.
*
* @property {String} type A string representing the event type to remove.
* @property {Function} listener The EventListener function to remove from the event target.
*/
removeEventListener(type, listener) {
if (typeof type !== 'string') {
throw new TypeError('Type must be a string.');
}
if (typeof listener !== 'function') {
throw new TypeError('listener must be a function.');
}
if (!this.events.has(type)) {
console.warn('Type %s is not registered.', type);
return;
}
var set = this.events.get(type);
if (!set.has(listener)) {
console.warn('Listener for type %s wasn\'t registered.', type);
return;
}
set.delete(listener);
}
/**
* Dispatches an Event at the specified EventTarget, invoking the affected EventListeners
* in the appropriate order.
*
* @type {Event} event An event to be dispatched.
*/
dispatchEvent(event) {
var type = event.type;
if (!type) {
throw new TypeError('Argument is not a valid event.');
}
if (!this.events.has(type)) {
return;
}
var set = this.events.get(type);
for (let listener of set) {
let canceled = listener(event);
if (event.cancelable && canceled) {
break;
}
}
}
}
class ArcEventSource extends ArcEventTarget {
constructor() {
super();
/**
* A DOMString representing the URL of the source.
*
* @type {String}
*/
this._url = undefined;
/**
* An unsigned short representing the state of the connection.
* Possible values are CONNECTING (0), OPEN (1), or CLOSED (2).
*
* @type {Number}
*/
this._readyState = undefined;
}
/**
* A DOMString representing the URL of the source.
*
* @type {String}
*/
get url() {
return this._url;
}
/**
* An unsigned short representing the state of the connection.
*
* @type {Number}
*/
get readyState() {
return this._readyState;
}
/**
* Sets a listener for open event.
*
* @param {Function} listener A function to be called
*/
set onopen(listener) {
this._onopen = listener;
if (this._onopen) {
this.removeEventListener('open', this._onopen);
}
this.addEventListener('open', listener);
}
/**
* Is an EventHandler being called when an open event is received, that is when the
* connection was just opened.
*
* @type {Function}
*/
get onopen() {
return this._onopen;
}
/**
* Sets a listener for message event.
*
* @param {Function} listener A function to be called
*/
set onmessage(listener) {
this._onmessage = listener;
if (this._onmessage) {
this.removeEventListener('message', this._onmessage);
}
this.addEventListener('message', listener);
}
/**
* Is an EventHandler being called when a message event is received, that is when a message
* is coming from the source.
*
* @type {Function}
*/
get onmessage() {
return this._onmessage;
}
/**
* Sets a listener for error event.
*
* @param {Function} listener A function to be called
*/
set onerror(listener) {
this._onerror = listener;
if (this._onerror) {
this.removeEventListener('error', this._onerror);
}
this.addEventListener('error', listener);
}
/**
* Is an EventHandler being called when an error occurs and the error event is dispatched
* on this object.
*
* @type {Function}
*/
get onerror() {
return this._onerror;
}
}
window.ArcEventTarget = ArcEventTarget;
window.ArcEventSource = ArcEventSource;
})();