modify-in-emit.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 assert = require('assert');
  22. var events = require('../');
  23. var callbacks_called = [];
  24. var e = new events.EventEmitter();
  25. function callback1() {
  26. callbacks_called.push('callback1');
  27. e.on('foo', callback2);
  28. e.on('foo', callback3);
  29. e.removeListener('foo', callback1);
  30. }
  31. function callback2() {
  32. callbacks_called.push('callback2');
  33. e.removeListener('foo', callback2);
  34. }
  35. function callback3() {
  36. callbacks_called.push('callback3');
  37. e.removeListener('foo', callback3);
  38. }
  39. e.on('foo', callback1);
  40. assert.equal(1, e.listeners('foo').length);
  41. e.emit('foo');
  42. assert.equal(2, e.listeners('foo').length);
  43. assert.deepEqual(['callback1'], callbacks_called);
  44. e.emit('foo');
  45. assert.equal(0, e.listeners('foo').length);
  46. assert.deepEqual(['callback1', 'callback2', 'callback3'], callbacks_called);
  47. e.emit('foo');
  48. assert.equal(0, e.listeners('foo').length);
  49. assert.deepEqual(['callback1', 'callback2', 'callback3'], callbacks_called);
  50. e.on('foo', callback1);
  51. e.on('foo', callback2);
  52. assert.equal(2, e.listeners('foo').length);
  53. e.removeAllListeners('foo');
  54. assert.equal(0, e.listeners('foo').length);
  55. // Verify that removing callbacks while in emit allows emits to propagate to
  56. // all listeners
  57. callbacks_called = [];
  58. e.on('foo', callback2);
  59. e.on('foo', callback3);
  60. assert.equal(2, e.listeners('foo').length);
  61. e.emit('foo');
  62. assert.deepEqual(['callback2', 'callback3'], callbacks_called);
  63. assert.equal(0, e.listeners('foo').length);