|
@@ -0,0 +1,630 @@
|
|
1
|
+!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.desktopsharing=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
|
|
2
|
+/* global $, alert, changeLocalVideo, chrome, config, connection, getConferenceHandler, getUserMediaWithConstraints */
|
|
3
|
+/**
|
|
4
|
+ * Indicates that desktop stream is currently in use(for toggle purpose).
|
|
5
|
+ * @type {boolean}
|
|
6
|
+ */
|
|
7
|
+var isUsingScreenStream = false;
|
|
8
|
+/**
|
|
9
|
+ * Indicates that switch stream operation is in progress and prevent from triggering new events.
|
|
10
|
+ * @type {boolean}
|
|
11
|
+ */
|
|
12
|
+var switchInProgress = false;
|
|
13
|
+
|
|
14
|
+/**
|
|
15
|
+ * Method used to get screen sharing stream.
|
|
16
|
+ *
|
|
17
|
+ * @type {function (stream_callback, failure_callback}
|
|
18
|
+ */
|
|
19
|
+var obtainDesktopStream = null;
|
|
20
|
+
|
|
21
|
+/**
|
|
22
|
+ * Flag used to cache desktop sharing enabled state. Do not use directly as it can be <tt>null</tt>.
|
|
23
|
+ * @type {null|boolean}
|
|
24
|
+ */
|
|
25
|
+var _desktopSharingEnabled = null;
|
|
26
|
+
|
|
27
|
+var EventEmitter = require("events");
|
|
28
|
+
|
|
29
|
+var eventEmitter = new EventEmitter();
|
|
30
|
+
|
|
31
|
+/**
|
|
32
|
+ * Method obtains desktop stream from WebRTC 'screen' source.
|
|
33
|
+ * Flag 'chrome://flags/#enable-usermedia-screen-capture' must be enabled.
|
|
34
|
+ */
|
|
35
|
+function obtainWebRTCScreen(streamCallback, failCallback) {
|
|
36
|
+ RTC.getUserMediaWithConstraints(
|
|
37
|
+ ['screen'],
|
|
38
|
+ streamCallback,
|
|
39
|
+ failCallback
|
|
40
|
+ );
|
|
41
|
+}
|
|
42
|
+
|
|
43
|
+/**
|
|
44
|
+ * Constructs inline install URL for Chrome desktop streaming extension.
|
|
45
|
+ * The 'chromeExtensionId' must be defined in config.js.
|
|
46
|
+ * @returns {string}
|
|
47
|
+ */
|
|
48
|
+function getWebStoreInstallUrl()
|
|
49
|
+{
|
|
50
|
+ return "https://chrome.google.com/webstore/detail/" + config.chromeExtensionId;
|
|
51
|
+}
|
|
52
|
+
|
|
53
|
+/**
|
|
54
|
+ * Checks whether extension update is required.
|
|
55
|
+ * @param minVersion minimal required version
|
|
56
|
+ * @param extVersion current extension version
|
|
57
|
+ * @returns {boolean}
|
|
58
|
+ */
|
|
59
|
+function isUpdateRequired(minVersion, extVersion)
|
|
60
|
+{
|
|
61
|
+ try
|
|
62
|
+ {
|
|
63
|
+ var s1 = minVersion.split('.');
|
|
64
|
+ var s2 = extVersion.split('.');
|
|
65
|
+
|
|
66
|
+ var len = Math.max(s1.length, s2.length);
|
|
67
|
+ for (var i = 0; i < len; i++)
|
|
68
|
+ {
|
|
69
|
+ var n1 = 0,
|
|
70
|
+ n2 = 0;
|
|
71
|
+
|
|
72
|
+ if (i < s1.length)
|
|
73
|
+ n1 = parseInt(s1[i]);
|
|
74
|
+ if (i < s2.length)
|
|
75
|
+ n2 = parseInt(s2[i]);
|
|
76
|
+
|
|
77
|
+ if (isNaN(n1) || isNaN(n2))
|
|
78
|
+ {
|
|
79
|
+ return true;
|
|
80
|
+ }
|
|
81
|
+ else if (n1 !== n2)
|
|
82
|
+ {
|
|
83
|
+ return n1 > n2;
|
|
84
|
+ }
|
|
85
|
+ }
|
|
86
|
+
|
|
87
|
+ // will happen if boths version has identical numbers in
|
|
88
|
+ // their components (even if one of them is longer, has more components)
|
|
89
|
+ return false;
|
|
90
|
+ }
|
|
91
|
+ catch (e)
|
|
92
|
+ {
|
|
93
|
+ console.error("Failed to parse extension version", e);
|
|
94
|
+ UI.messageHandler.showError('Error',
|
|
95
|
+ 'Error when trying to detect desktopsharing extension.');
|
|
96
|
+ return true;
|
|
97
|
+ }
|
|
98
|
+}
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+function checkExtInstalled(isInstalledCallback) {
|
|
102
|
+ if (!chrome.runtime) {
|
|
103
|
+ // No API, so no extension for sure
|
|
104
|
+ isInstalledCallback(false);
|
|
105
|
+ return;
|
|
106
|
+ }
|
|
107
|
+ chrome.runtime.sendMessage(
|
|
108
|
+ config.chromeExtensionId,
|
|
109
|
+ { getVersion: true },
|
|
110
|
+ function (response) {
|
|
111
|
+ if (!response || !response.version) {
|
|
112
|
+ // Communication failure - assume that no endpoint exists
|
|
113
|
+ console.warn("Extension not installed?: " + chrome.runtime.lastError);
|
|
114
|
+ isInstalledCallback(false);
|
|
115
|
+ } else {
|
|
116
|
+ // Check installed extension version
|
|
117
|
+ var extVersion = response.version;
|
|
118
|
+ console.log('Extension version is: ' + extVersion);
|
|
119
|
+ var updateRequired = isUpdateRequired(config.minChromeExtVersion, extVersion);
|
|
120
|
+ if (updateRequired) {
|
|
121
|
+ alert(
|
|
122
|
+ 'Jitsi Desktop Streamer requires update. ' +
|
|
123
|
+ 'Changes will take effect after next Chrome restart.');
|
|
124
|
+ }
|
|
125
|
+ isInstalledCallback(!updateRequired);
|
|
126
|
+ }
|
|
127
|
+ }
|
|
128
|
+ );
|
|
129
|
+}
|
|
130
|
+
|
|
131
|
+function doGetStreamFromExtension(streamCallback, failCallback) {
|
|
132
|
+ // Sends 'getStream' msg to the extension. Extension id must be defined in the config.
|
|
133
|
+ chrome.runtime.sendMessage(
|
|
134
|
+ config.chromeExtensionId,
|
|
135
|
+ { getStream: true, sources: config.desktopSharingSources },
|
|
136
|
+ function (response) {
|
|
137
|
+ if (!response) {
|
|
138
|
+ failCallback(chrome.runtime.lastError);
|
|
139
|
+ return;
|
|
140
|
+ }
|
|
141
|
+ console.log("Response from extension: " + response);
|
|
142
|
+ if (response.streamId) {
|
|
143
|
+ RTC.getUserMediaWithConstraints(
|
|
144
|
+ ['desktop'],
|
|
145
|
+ function (stream) {
|
|
146
|
+ streamCallback(stream);
|
|
147
|
+ },
|
|
148
|
+ failCallback,
|
|
149
|
+ null, null, null,
|
|
150
|
+ response.streamId);
|
|
151
|
+ } else {
|
|
152
|
+ failCallback("Extension failed to get the stream");
|
|
153
|
+ }
|
|
154
|
+ }
|
|
155
|
+ );
|
|
156
|
+}
|
|
157
|
+/**
|
|
158
|
+ * Asks Chrome extension to call chooseDesktopMedia and gets chrome 'desktop' stream for returned stream token.
|
|
159
|
+ */
|
|
160
|
+function obtainScreenFromExtension(streamCallback, failCallback) {
|
|
161
|
+ checkExtInstalled(
|
|
162
|
+ function (isInstalled) {
|
|
163
|
+ if (isInstalled) {
|
|
164
|
+ doGetStreamFromExtension(streamCallback, failCallback);
|
|
165
|
+ } else {
|
|
166
|
+ chrome.webstore.install(
|
|
167
|
+ getWebStoreInstallUrl(),
|
|
168
|
+ function (arg) {
|
|
169
|
+ console.log("Extension installed successfully", arg);
|
|
170
|
+ // We need to reload the page in order to get the access to chrome.runtime
|
|
171
|
+ window.location.reload(false);
|
|
172
|
+ },
|
|
173
|
+ function (arg) {
|
|
174
|
+ console.log("Failed to install the extension", arg);
|
|
175
|
+ failCallback(arg);
|
|
176
|
+ UI.messageHandler.showError('Error',
|
|
177
|
+ 'Failed to install desktop sharing extension');
|
|
178
|
+ }
|
|
179
|
+ );
|
|
180
|
+ }
|
|
181
|
+ }
|
|
182
|
+ );
|
|
183
|
+}
|
|
184
|
+
|
|
185
|
+/**
|
|
186
|
+ * Call this method to toggle desktop sharing feature.
|
|
187
|
+ * @param method pass "ext" to use chrome extension for desktop capture(chrome extension required),
|
|
188
|
+ * pass "webrtc" to use WebRTC "screen" desktop source('chrome://flags/#enable-usermedia-screen-capture'
|
|
189
|
+ * must be enabled), pass any other string or nothing in order to disable this feature completely.
|
|
190
|
+ */
|
|
191
|
+function setDesktopSharing(method) {
|
|
192
|
+ // Check if we are running chrome
|
|
193
|
+ if (!navigator.webkitGetUserMedia) {
|
|
194
|
+ obtainDesktopStream = null;
|
|
195
|
+ console.info("Desktop sharing disabled");
|
|
196
|
+ } else if (method == "ext") {
|
|
197
|
+ obtainDesktopStream = obtainScreenFromExtension;
|
|
198
|
+ console.info("Using Chrome extension for desktop sharing");
|
|
199
|
+ } else if (method == "webrtc") {
|
|
200
|
+ obtainDesktopStream = obtainWebRTCScreen;
|
|
201
|
+ console.info("Using Chrome WebRTC for desktop sharing");
|
|
202
|
+ }
|
|
203
|
+
|
|
204
|
+ // Reset enabled cache
|
|
205
|
+ _desktopSharingEnabled = null;
|
|
206
|
+}
|
|
207
|
+
|
|
208
|
+/**
|
|
209
|
+ * Initializes <link rel=chrome-webstore-item /> with extension id set in config.js to support inline installs.
|
|
210
|
+ * Host site must be selected as main website of published extension.
|
|
211
|
+ */
|
|
212
|
+function initInlineInstalls()
|
|
213
|
+{
|
|
214
|
+ $("link[rel=chrome-webstore-item]").attr("href", getWebStoreInstallUrl());
|
|
215
|
+}
|
|
216
|
+
|
|
217
|
+function getSwitchStreamFailed(error) {
|
|
218
|
+ console.error("Failed to obtain the stream to switch to", error);
|
|
219
|
+ switchInProgress = false;
|
|
220
|
+}
|
|
221
|
+
|
|
222
|
+function streamSwitchDone() {
|
|
223
|
+ switchInProgress = false;
|
|
224
|
+ eventEmitter.emit(
|
|
225
|
+ DesktopSharingEventTypes.SWITCHING_DONE,
|
|
226
|
+ isUsingScreenStream);
|
|
227
|
+}
|
|
228
|
+
|
|
229
|
+function newStreamCreated(stream)
|
|
230
|
+{
|
|
231
|
+ eventEmitter.emit(DesktopSharingEventTypes.NEW_STREAM_CREATED,
|
|
232
|
+ stream, isUsingScreenStream, streamSwitchDone);
|
|
233
|
+}
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+module.exports = {
|
|
237
|
+ isUsingScreenStream: function () {
|
|
238
|
+ return isUsingScreenStream;
|
|
239
|
+ },
|
|
240
|
+
|
|
241
|
+ /**
|
|
242
|
+ * @returns {boolean} <tt>true</tt> if desktop sharing feature is available and enabled.
|
|
243
|
+ */
|
|
244
|
+ isDesktopSharingEnabled: function () {
|
|
245
|
+ if (_desktopSharingEnabled === null) {
|
|
246
|
+ if (obtainDesktopStream === obtainScreenFromExtension) {
|
|
247
|
+ // Parse chrome version
|
|
248
|
+ var userAgent = navigator.userAgent.toLowerCase();
|
|
249
|
+ // We can assume that user agent is chrome, because it's enforced when 'ext' streaming method is set
|
|
250
|
+ var ver = parseInt(userAgent.match(/chrome\/(\d+)\./)[1], 10);
|
|
251
|
+ console.log("Chrome version" + userAgent, ver);
|
|
252
|
+ _desktopSharingEnabled = ver >= 34;
|
|
253
|
+ } else {
|
|
254
|
+ _desktopSharingEnabled = obtainDesktopStream === obtainWebRTCScreen;
|
|
255
|
+ }
|
|
256
|
+ }
|
|
257
|
+ return _desktopSharingEnabled;
|
|
258
|
+ },
|
|
259
|
+
|
|
260
|
+ init: function () {
|
|
261
|
+ setDesktopSharing(config.desktopSharing);
|
|
262
|
+
|
|
263
|
+ // Initialize Chrome extension inline installs
|
|
264
|
+ if (config.chromeExtensionId) {
|
|
265
|
+ initInlineInstalls();
|
|
266
|
+ }
|
|
267
|
+
|
|
268
|
+ eventEmitter.emit(DesktopSharingEventTypes.INIT);
|
|
269
|
+ },
|
|
270
|
+
|
|
271
|
+ addListener: function(listener, type)
|
|
272
|
+ {
|
|
273
|
+ eventEmitter.on(type, listener);
|
|
274
|
+ },
|
|
275
|
+
|
|
276
|
+ removeListener: function (listener,type) {
|
|
277
|
+ eventEmitter.removeListener(type, listener);
|
|
278
|
+ },
|
|
279
|
+
|
|
280
|
+ /*
|
|
281
|
+ * Toggles screen sharing.
|
|
282
|
+ */
|
|
283
|
+ toggleScreenSharing: function () {
|
|
284
|
+ if (switchInProgress || !obtainDesktopStream) {
|
|
285
|
+ console.warn("Switch in progress or no method defined");
|
|
286
|
+ return;
|
|
287
|
+ }
|
|
288
|
+ switchInProgress = true;
|
|
289
|
+
|
|
290
|
+ if (!isUsingScreenStream)
|
|
291
|
+ {
|
|
292
|
+ // Switch to desktop stream
|
|
293
|
+ obtainDesktopStream(
|
|
294
|
+ function (stream) {
|
|
295
|
+ // We now use screen stream
|
|
296
|
+ isUsingScreenStream = true;
|
|
297
|
+ // Hook 'ended' event to restore camera when screen stream stops
|
|
298
|
+ stream.addEventListener('ended',
|
|
299
|
+ function (e) {
|
|
300
|
+ if (!switchInProgress && isUsingScreenStream) {
|
|
301
|
+ toggleScreenSharing();
|
|
302
|
+ }
|
|
303
|
+ }
|
|
304
|
+ );
|
|
305
|
+ newStreamCreated(stream);
|
|
306
|
+ },
|
|
307
|
+ getSwitchStreamFailed);
|
|
308
|
+ } else {
|
|
309
|
+ // Disable screen stream
|
|
310
|
+ RTC.getUserMediaWithConstraints(
|
|
311
|
+ ['video'],
|
|
312
|
+ function (stream) {
|
|
313
|
+ // We are now using camera stream
|
|
314
|
+ isUsingScreenStream = false;
|
|
315
|
+ newStreamCreated(stream);
|
|
316
|
+ },
|
|
317
|
+ getSwitchStreamFailed, config.resolution || '360'
|
|
318
|
+ );
|
|
319
|
+ }
|
|
320
|
+ }
|
|
321
|
+};
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+},{"events":2}],2:[function(require,module,exports){
|
|
325
|
+// Copyright Joyent, Inc. and other Node contributors.
|
|
326
|
+//
|
|
327
|
+// Permission is hereby granted, free of charge, to any person obtaining a
|
|
328
|
+// copy of this software and associated documentation files (the
|
|
329
|
+// "Software"), to deal in the Software without restriction, including
|
|
330
|
+// without limitation the rights to use, copy, modify, merge, publish,
|
|
331
|
+// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
332
|
+// persons to whom the Software is furnished to do so, subject to the
|
|
333
|
+// following conditions:
|
|
334
|
+//
|
|
335
|
+// The above copyright notice and this permission notice shall be included
|
|
336
|
+// in all copies or substantial portions of the Software.
|
|
337
|
+//
|
|
338
|
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
339
|
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
340
|
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
341
|
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
342
|
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
343
|
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
344
|
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
345
|
+
|
|
346
|
+function EventEmitter() {
|
|
347
|
+ this._events = this._events || {};
|
|
348
|
+ this._maxListeners = this._maxListeners || undefined;
|
|
349
|
+}
|
|
350
|
+module.exports = EventEmitter;
|
|
351
|
+
|
|
352
|
+// Backwards-compat with node 0.10.x
|
|
353
|
+EventEmitter.EventEmitter = EventEmitter;
|
|
354
|
+
|
|
355
|
+EventEmitter.prototype._events = undefined;
|
|
356
|
+EventEmitter.prototype._maxListeners = undefined;
|
|
357
|
+
|
|
358
|
+// By default EventEmitters will print a warning if more than 10 listeners are
|
|
359
|
+// added to it. This is a useful default which helps finding memory leaks.
|
|
360
|
+EventEmitter.defaultMaxListeners = 10;
|
|
361
|
+
|
|
362
|
+// Obviously not all Emitters should be limited to 10. This function allows
|
|
363
|
+// that to be increased. Set to zero for unlimited.
|
|
364
|
+EventEmitter.prototype.setMaxListeners = function(n) {
|
|
365
|
+ if (!isNumber(n) || n < 0 || isNaN(n))
|
|
366
|
+ throw TypeError('n must be a positive number');
|
|
367
|
+ this._maxListeners = n;
|
|
368
|
+ return this;
|
|
369
|
+};
|
|
370
|
+
|
|
371
|
+EventEmitter.prototype.emit = function(type) {
|
|
372
|
+ var er, handler, len, args, i, listeners;
|
|
373
|
+
|
|
374
|
+ if (!this._events)
|
|
375
|
+ this._events = {};
|
|
376
|
+
|
|
377
|
+ // If there is no 'error' event listener then throw.
|
|
378
|
+ if (type === 'error') {
|
|
379
|
+ if (!this._events.error ||
|
|
380
|
+ (isObject(this._events.error) && !this._events.error.length)) {
|
|
381
|
+ er = arguments[1];
|
|
382
|
+ if (er instanceof Error) {
|
|
383
|
+ throw er; // Unhandled 'error' event
|
|
384
|
+ } else {
|
|
385
|
+ throw TypeError('Uncaught, unspecified "error" event.');
|
|
386
|
+ }
|
|
387
|
+ return false;
|
|
388
|
+ }
|
|
389
|
+ }
|
|
390
|
+
|
|
391
|
+ handler = this._events[type];
|
|
392
|
+
|
|
393
|
+ if (isUndefined(handler))
|
|
394
|
+ return false;
|
|
395
|
+
|
|
396
|
+ if (isFunction(handler)) {
|
|
397
|
+ switch (arguments.length) {
|
|
398
|
+ // fast cases
|
|
399
|
+ case 1:
|
|
400
|
+ handler.call(this);
|
|
401
|
+ break;
|
|
402
|
+ case 2:
|
|
403
|
+ handler.call(this, arguments[1]);
|
|
404
|
+ break;
|
|
405
|
+ case 3:
|
|
406
|
+ handler.call(this, arguments[1], arguments[2]);
|
|
407
|
+ break;
|
|
408
|
+ // slower
|
|
409
|
+ default:
|
|
410
|
+ len = arguments.length;
|
|
411
|
+ args = new Array(len - 1);
|
|
412
|
+ for (i = 1; i < len; i++)
|
|
413
|
+ args[i - 1] = arguments[i];
|
|
414
|
+ handler.apply(this, args);
|
|
415
|
+ }
|
|
416
|
+ } else if (isObject(handler)) {
|
|
417
|
+ len = arguments.length;
|
|
418
|
+ args = new Array(len - 1);
|
|
419
|
+ for (i = 1; i < len; i++)
|
|
420
|
+ args[i - 1] = arguments[i];
|
|
421
|
+
|
|
422
|
+ listeners = handler.slice();
|
|
423
|
+ len = listeners.length;
|
|
424
|
+ for (i = 0; i < len; i++)
|
|
425
|
+ listeners[i].apply(this, args);
|
|
426
|
+ }
|
|
427
|
+
|
|
428
|
+ return true;
|
|
429
|
+};
|
|
430
|
+
|
|
431
|
+EventEmitter.prototype.addListener = function(type, listener) {
|
|
432
|
+ var m;
|
|
433
|
+
|
|
434
|
+ if (!isFunction(listener))
|
|
435
|
+ throw TypeError('listener must be a function');
|
|
436
|
+
|
|
437
|
+ if (!this._events)
|
|
438
|
+ this._events = {};
|
|
439
|
+
|
|
440
|
+ // To avoid recursion in the case that type === "newListener"! Before
|
|
441
|
+ // adding it to the listeners, first emit "newListener".
|
|
442
|
+ if (this._events.newListener)
|
|
443
|
+ this.emit('newListener', type,
|
|
444
|
+ isFunction(listener.listener) ?
|
|
445
|
+ listener.listener : listener);
|
|
446
|
+
|
|
447
|
+ if (!this._events[type])
|
|
448
|
+ // Optimize the case of one listener. Don't need the extra array object.
|
|
449
|
+ this._events[type] = listener;
|
|
450
|
+ else if (isObject(this._events[type]))
|
|
451
|
+ // If we've already got an array, just append.
|
|
452
|
+ this._events[type].push(listener);
|
|
453
|
+ else
|
|
454
|
+ // Adding the second element, need to change to array.
|
|
455
|
+ this._events[type] = [this._events[type], listener];
|
|
456
|
+
|
|
457
|
+ // Check for listener leak
|
|
458
|
+ if (isObject(this._events[type]) && !this._events[type].warned) {
|
|
459
|
+ var m;
|
|
460
|
+ if (!isUndefined(this._maxListeners)) {
|
|
461
|
+ m = this._maxListeners;
|
|
462
|
+ } else {
|
|
463
|
+ m = EventEmitter.defaultMaxListeners;
|
|
464
|
+ }
|
|
465
|
+
|
|
466
|
+ if (m && m > 0 && this._events[type].length > m) {
|
|
467
|
+ this._events[type].warned = true;
|
|
468
|
+ console.error('(node) warning: possible EventEmitter memory ' +
|
|
469
|
+ 'leak detected. %d listeners added. ' +
|
|
470
|
+ 'Use emitter.setMaxListeners() to increase limit.',
|
|
471
|
+ this._events[type].length);
|
|
472
|
+ if (typeof console.trace === 'function') {
|
|
473
|
+ // not supported in IE 10
|
|
474
|
+ console.trace();
|
|
475
|
+ }
|
|
476
|
+ }
|
|
477
|
+ }
|
|
478
|
+
|
|
479
|
+ return this;
|
|
480
|
+};
|
|
481
|
+
|
|
482
|
+EventEmitter.prototype.on = EventEmitter.prototype.addListener;
|
|
483
|
+
|
|
484
|
+EventEmitter.prototype.once = function(type, listener) {
|
|
485
|
+ if (!isFunction(listener))
|
|
486
|
+ throw TypeError('listener must be a function');
|
|
487
|
+
|
|
488
|
+ var fired = false;
|
|
489
|
+
|
|
490
|
+ function g() {
|
|
491
|
+ this.removeListener(type, g);
|
|
492
|
+
|
|
493
|
+ if (!fired) {
|
|
494
|
+ fired = true;
|
|
495
|
+ listener.apply(this, arguments);
|
|
496
|
+ }
|
|
497
|
+ }
|
|
498
|
+
|
|
499
|
+ g.listener = listener;
|
|
500
|
+ this.on(type, g);
|
|
501
|
+
|
|
502
|
+ return this;
|
|
503
|
+};
|
|
504
|
+
|
|
505
|
+// emits a 'removeListener' event iff the listener was removed
|
|
506
|
+EventEmitter.prototype.removeListener = function(type, listener) {
|
|
507
|
+ var list, position, length, i;
|
|
508
|
+
|
|
509
|
+ if (!isFunction(listener))
|
|
510
|
+ throw TypeError('listener must be a function');
|
|
511
|
+
|
|
512
|
+ if (!this._events || !this._events[type])
|
|
513
|
+ return this;
|
|
514
|
+
|
|
515
|
+ list = this._events[type];
|
|
516
|
+ length = list.length;
|
|
517
|
+ position = -1;
|
|
518
|
+
|
|
519
|
+ if (list === listener ||
|
|
520
|
+ (isFunction(list.listener) && list.listener === listener)) {
|
|
521
|
+ delete this._events[type];
|
|
522
|
+ if (this._events.removeListener)
|
|
523
|
+ this.emit('removeListener', type, listener);
|
|
524
|
+
|
|
525
|
+ } else if (isObject(list)) {
|
|
526
|
+ for (i = length; i-- > 0;) {
|
|
527
|
+ if (list[i] === listener ||
|
|
528
|
+ (list[i].listener && list[i].listener === listener)) {
|
|
529
|
+ position = i;
|
|
530
|
+ break;
|
|
531
|
+ }
|
|
532
|
+ }
|
|
533
|
+
|
|
534
|
+ if (position < 0)
|
|
535
|
+ return this;
|
|
536
|
+
|
|
537
|
+ if (list.length === 1) {
|
|
538
|
+ list.length = 0;
|
|
539
|
+ delete this._events[type];
|
|
540
|
+ } else {
|
|
541
|
+ list.splice(position, 1);
|
|
542
|
+ }
|
|
543
|
+
|
|
544
|
+ if (this._events.removeListener)
|
|
545
|
+ this.emit('removeListener', type, listener);
|
|
546
|
+ }
|
|
547
|
+
|
|
548
|
+ return this;
|
|
549
|
+};
|
|
550
|
+
|
|
551
|
+EventEmitter.prototype.removeAllListeners = function(type) {
|
|
552
|
+ var key, listeners;
|
|
553
|
+
|
|
554
|
+ if (!this._events)
|
|
555
|
+ return this;
|
|
556
|
+
|
|
557
|
+ // not listening for removeListener, no need to emit
|
|
558
|
+ if (!this._events.removeListener) {
|
|
559
|
+ if (arguments.length === 0)
|
|
560
|
+ this._events = {};
|
|
561
|
+ else if (this._events[type])
|
|
562
|
+ delete this._events[type];
|
|
563
|
+ return this;
|
|
564
|
+ }
|
|
565
|
+
|
|
566
|
+ // emit removeListener for all listeners on all events
|
|
567
|
+ if (arguments.length === 0) {
|
|
568
|
+ for (key in this._events) {
|
|
569
|
+ if (key === 'removeListener') continue;
|
|
570
|
+ this.removeAllListeners(key);
|
|
571
|
+ }
|
|
572
|
+ this.removeAllListeners('removeListener');
|
|
573
|
+ this._events = {};
|
|
574
|
+ return this;
|
|
575
|
+ }
|
|
576
|
+
|
|
577
|
+ listeners = this._events[type];
|
|
578
|
+
|
|
579
|
+ if (isFunction(listeners)) {
|
|
580
|
+ this.removeListener(type, listeners);
|
|
581
|
+ } else {
|
|
582
|
+ // LIFO order
|
|
583
|
+ while (listeners.length)
|
|
584
|
+ this.removeListener(type, listeners[listeners.length - 1]);
|
|
585
|
+ }
|
|
586
|
+ delete this._events[type];
|
|
587
|
+
|
|
588
|
+ return this;
|
|
589
|
+};
|
|
590
|
+
|
|
591
|
+EventEmitter.prototype.listeners = function(type) {
|
|
592
|
+ var ret;
|
|
593
|
+ if (!this._events || !this._events[type])
|
|
594
|
+ ret = [];
|
|
595
|
+ else if (isFunction(this._events[type]))
|
|
596
|
+ ret = [this._events[type]];
|
|
597
|
+ else
|
|
598
|
+ ret = this._events[type].slice();
|
|
599
|
+ return ret;
|
|
600
|
+};
|
|
601
|
+
|
|
602
|
+EventEmitter.listenerCount = function(emitter, type) {
|
|
603
|
+ var ret;
|
|
604
|
+ if (!emitter._events || !emitter._events[type])
|
|
605
|
+ ret = 0;
|
|
606
|
+ else if (isFunction(emitter._events[type]))
|
|
607
|
+ ret = 1;
|
|
608
|
+ else
|
|
609
|
+ ret = emitter._events[type].length;
|
|
610
|
+ return ret;
|
|
611
|
+};
|
|
612
|
+
|
|
613
|
+function isFunction(arg) {
|
|
614
|
+ return typeof arg === 'function';
|
|
615
|
+}
|
|
616
|
+
|
|
617
|
+function isNumber(arg) {
|
|
618
|
+ return typeof arg === 'number';
|
|
619
|
+}
|
|
620
|
+
|
|
621
|
+function isObject(arg) {
|
|
622
|
+ return typeof arg === 'object' && arg !== null;
|
|
623
|
+}
|
|
624
|
+
|
|
625
|
+function isUndefined(arg) {
|
|
626
|
+ return arg === void 0;
|
|
627
|
+}
|
|
628
|
+
|
|
629
|
+},{}]},{},[1])(1)
|
|
630
|
+});
|