add-listeners.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright Joyent, Inc. and other Node contributors.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a
  4. // copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to permit
  8. // persons to whom the Software is furnished to do so, subject to the
  9. // following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included
  12. // in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  17. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. var common = require('./common');
  22. var assert = require('assert');
  23. var EventEmitter = require('../');
  24. {
  25. var ee = new EventEmitter();
  26. var events_new_listener_emitted = [];
  27. var listeners_new_listener_emitted = [];
  28. // Sanity check
  29. assert.strictEqual(ee.addListener, ee.on);
  30. ee.on('newListener', function(event, listener) {
  31. // Don't track newListener listeners.
  32. if (event === 'newListener')
  33. return;
  34. events_new_listener_emitted.push(event);
  35. listeners_new_listener_emitted.push(listener);
  36. });
  37. var hello = common.mustCall(function(a, b) {
  38. assert.strictEqual('a', a);
  39. assert.strictEqual('b', b);
  40. });
  41. ee.once('newListener', function(name, listener) {
  42. assert.strictEqual(name, 'hello');
  43. assert.strictEqual(listener, hello);
  44. var listeners = this.listeners('hello');
  45. assert.ok(Array.isArray(listeners));
  46. assert.strictEqual(listeners.length, 0);
  47. });
  48. ee.on('hello', hello);
  49. ee.once('foo', assert.fail);
  50. assert.ok(Array.isArray(events_new_listener_emitted));
  51. assert.strictEqual(events_new_listener_emitted.length, 2);
  52. assert.strictEqual(events_new_listener_emitted[0], 'hello');
  53. assert.strictEqual(events_new_listener_emitted[1], 'foo');
  54. assert.ok(Array.isArray(listeners_new_listener_emitted));
  55. assert.strictEqual(listeners_new_listener_emitted.length, 2);
  56. assert.strictEqual(listeners_new_listener_emitted[0], hello);
  57. assert.strictEqual(listeners_new_listener_emitted[1], assert.fail);
  58. ee.emit('hello', 'a', 'b');
  59. }
  60. // just make sure that this doesn't throw:
  61. {
  62. var f = new EventEmitter();
  63. f.setMaxListeners(0);
  64. }
  65. {
  66. var listen1 = function() {};
  67. var listen2 = function() {};
  68. var ee = new EventEmitter();
  69. ee.once('newListener', function() {
  70. var listeners = ee.listeners('hello');
  71. assert.ok(Array.isArray(listeners));
  72. assert.strictEqual(listeners.length, 0);
  73. ee.once('newListener', function() {
  74. var listeners = ee.listeners('hello');
  75. assert.ok(Array.isArray(listeners));
  76. assert.strictEqual(listeners.length, 0);
  77. });
  78. ee.on('hello', listen2);
  79. });
  80. ee.on('hello', listen1);
  81. // The order of listeners on an event is not always the order in which the
  82. // listeners were added.
  83. var listeners = ee.listeners('hello');
  84. assert.ok(Array.isArray(listeners));
  85. assert.strictEqual(listeners.length, 2);
  86. assert.strictEqual(listeners[0], listen2);
  87. assert.strictEqual(listeners[1], listen1);
  88. }
  89. // Verify that the listener must be a function
  90. assert.throws(function() {
  91. var ee = new EventEmitter();
  92. ee.on('foo', null);
  93. }, /^TypeError: The "listener" argument must be of type Function. Received type object$/);