| @@ 300-404 (lines=105) @@ | ||
| 297 | return self; |
|
| 298 | }; |
|
| 299 | ||
| 300 | EventEmitter.prototype.emit = function() { |
|
| 301 | ||
| 302 | this._events || init.call(this); |
|
| 303 | ||
| 304 | var type = arguments[0]; |
|
| 305 | ||
| 306 | if (type === 'newListener' && !this._newListener) { |
|
| 307 | if (!this._events.newListener) { |
|
| 308 | return false; |
|
| 309 | } |
|
| 310 | } |
|
| 311 | ||
| 312 | var al = arguments.length; |
|
| 313 | var args,l,i,j; |
|
| 314 | var handler; |
|
| 315 | ||
| 316 | if (this._all && this._all.length) { |
|
| 317 | handler = this._all.slice(); |
|
| 318 | if (al > 3) { |
|
| 319 | args = new Array(al); |
|
| 320 | for (j = 0; j < al; j++) args[j] = arguments[j]; |
|
| 321 | } |
|
| 322 | ||
| 323 | for (i = 0, l = handler.length; i < l; i++) { |
|
| 324 | this.event = type; |
|
| 325 | switch (al) { |
|
| 326 | case 1: |
|
| 327 | handler[i].call(this, type); |
|
| 328 | break; |
|
| 329 | case 2: |
|
| 330 | handler[i].call(this, type, arguments[1]); |
|
| 331 | break; |
|
| 332 | case 3: |
|
| 333 | handler[i].call(this, type, arguments[1], arguments[2]); |
|
| 334 | break; |
|
| 335 | default: |
|
| 336 | handler[i].apply(this, args); |
|
| 337 | } |
|
| 338 | } |
|
| 339 | } |
|
| 340 | ||
| 341 | if (this.wildcard) { |
|
| 342 | handler = []; |
|
| 343 | var ns = typeof type === 'string' ? type.split(this.delimiter) : type.slice(); |
|
| 344 | searchListenerTree.call(this, handler, ns, this.listenerTree, 0); |
|
| 345 | } else { |
|
| 346 | handler = this._events[type]; |
|
| 347 | if (typeof handler === 'function') { |
|
| 348 | this.event = type; |
|
| 349 | switch (al) { |
|
| 350 | case 1: |
|
| 351 | handler.call(this); |
|
| 352 | break; |
|
| 353 | case 2: |
|
| 354 | handler.call(this, arguments[1]); |
|
| 355 | break; |
|
| 356 | case 3: |
|
| 357 | handler.call(this, arguments[1], arguments[2]); |
|
| 358 | break; |
|
| 359 | default: |
|
| 360 | args = new Array(al - 1); |
|
| 361 | for (j = 1; j < al; j++) args[j - 1] = arguments[j]; |
|
| 362 | handler.apply(this, args); |
|
| 363 | } |
|
| 364 | return true; |
|
| 365 | } else if (handler) { |
|
| 366 | // need to make copy of handlers because list can change in the middle |
|
| 367 | // of emit call |
|
| 368 | handler = handler.slice(); |
|
| 369 | } |
|
| 370 | } |
|
| 371 | ||
| 372 | if (handler && handler.length) { |
|
| 373 | if (al > 3) { |
|
| 374 | args = new Array(al - 1); |
|
| 375 | for (j = 1; j < al; j++) args[j - 1] = arguments[j]; |
|
| 376 | } |
|
| 377 | for (i = 0, l = handler.length; i < l; i++) { |
|
| 378 | this.event = type; |
|
| 379 | switch (al) { |
|
| 380 | case 1: |
|
| 381 | handler[i].call(this); |
|
| 382 | break; |
|
| 383 | case 2: |
|
| 384 | handler[i].call(this, arguments[1]); |
|
| 385 | break; |
|
| 386 | case 3: |
|
| 387 | handler[i].call(this, arguments[1], arguments[2]); |
|
| 388 | break; |
|
| 389 | default: |
|
| 390 | handler[i].apply(this, args); |
|
| 391 | } |
|
| 392 | } |
|
| 393 | return true; |
|
| 394 | } else if (!this._all && type === 'error') { |
|
| 395 | if (arguments[1] instanceof Error) { |
|
| 396 | throw arguments[1]; // Unhandled 'error' event |
|
| 397 | } else { |
|
| 398 | throw new Error("Uncaught, unspecified 'error' event."); |
|
| 399 | } |
|
| 400 | return false; |
|
| 401 | } |
|
| 402 | ||
| 403 | return !!this._all; |
|
| 404 | }; |
|
| 405 | ||
| 406 | EventEmitter.prototype.emitAsync = function() { |
|
| 407 | ||
| @@ 406-501 (lines=96) @@ | ||
| 403 | return !!this._all; |
|
| 404 | }; |
|
| 405 | ||
| 406 | EventEmitter.prototype.emitAsync = function() { |
|
| 407 | ||
| 408 | this._events || init.call(this); |
|
| 409 | ||
| 410 | var type = arguments[0]; |
|
| 411 | ||
| 412 | if (type === 'newListener' && !this._newListener) { |
|
| 413 | if (!this._events.newListener) { return Promise.resolve([false]); } |
|
| 414 | } |
|
| 415 | ||
| 416 | var promises= []; |
|
| 417 | ||
| 418 | var al = arguments.length; |
|
| 419 | var args,l,i,j; |
|
| 420 | var handler; |
|
| 421 | ||
| 422 | if (this._all) { |
|
| 423 | if (al > 3) { |
|
| 424 | args = new Array(al); |
|
| 425 | for (j = 1; j < al; j++) args[j] = arguments[j]; |
|
| 426 | } |
|
| 427 | for (i = 0, l = this._all.length; i < l; i++) { |
|
| 428 | this.event = type; |
|
| 429 | switch (al) { |
|
| 430 | case 1: |
|
| 431 | promises.push(this._all[i].call(this, type)); |
|
| 432 | break; |
|
| 433 | case 2: |
|
| 434 | promises.push(this._all[i].call(this, type, arguments[1])); |
|
| 435 | break; |
|
| 436 | case 3: |
|
| 437 | promises.push(this._all[i].call(this, type, arguments[1], arguments[2])); |
|
| 438 | break; |
|
| 439 | default: |
|
| 440 | promises.push(this._all[i].apply(this, args)); |
|
| 441 | } |
|
| 442 | } |
|
| 443 | } |
|
| 444 | ||
| 445 | if (this.wildcard) { |
|
| 446 | handler = []; |
|
| 447 | var ns = typeof type === 'string' ? type.split(this.delimiter) : type.slice(); |
|
| 448 | searchListenerTree.call(this, handler, ns, this.listenerTree, 0); |
|
| 449 | } else { |
|
| 450 | handler = this._events[type]; |
|
| 451 | } |
|
| 452 | ||
| 453 | if (typeof handler === 'function') { |
|
| 454 | this.event = type; |
|
| 455 | switch (al) { |
|
| 456 | case 1: |
|
| 457 | promises.push(handler.call(this)); |
|
| 458 | break; |
|
| 459 | case 2: |
|
| 460 | promises.push(handler.call(this, arguments[1])); |
|
| 461 | break; |
|
| 462 | case 3: |
|
| 463 | promises.push(handler.call(this, arguments[1], arguments[2])); |
|
| 464 | break; |
|
| 465 | default: |
|
| 466 | args = new Array(al - 1); |
|
| 467 | for (j = 1; j < al; j++) args[j - 1] = arguments[j]; |
|
| 468 | promises.push(handler.apply(this, args)); |
|
| 469 | } |
|
| 470 | } else if (handler && handler.length) { |
|
| 471 | handler = handler.slice(); |
|
| 472 | if (al > 3) { |
|
| 473 | args = new Array(al - 1); |
|
| 474 | for (j = 1; j < al; j++) args[j - 1] = arguments[j]; |
|
| 475 | } |
|
| 476 | for (i = 0, l = handler.length; i < l; i++) { |
|
| 477 | this.event = type; |
|
| 478 | switch (al) { |
|
| 479 | case 1: |
|
| 480 | promises.push(handler[i].call(this)); |
|
| 481 | break; |
|
| 482 | case 2: |
|
| 483 | promises.push(handler[i].call(this, arguments[1])); |
|
| 484 | break; |
|
| 485 | case 3: |
|
| 486 | promises.push(handler[i].call(this, arguments[1], arguments[2])); |
|
| 487 | break; |
|
| 488 | default: |
|
| 489 | promises.push(handler[i].apply(this, args)); |
|
| 490 | } |
|
| 491 | } |
|
| 492 | } else if (!this._all && type === 'error') { |
|
| 493 | if (arguments[1] instanceof Error) { |
|
| 494 | return Promise.reject(arguments[1]); // Unhandled 'error' event |
|
| 495 | } else { |
|
| 496 | return Promise.reject("Uncaught, unspecified 'error' event."); |
|
| 497 | } |
|
| 498 | } |
|
| 499 | ||
| 500 | return Promise.all(promises); |
|
| 501 | }; |
|
| 502 | ||
| 503 | EventEmitter.prototype.on = function(type, listener) { |
|
| 504 | return this._on(type, listener, false); |
|