Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

modify-in-emit.js 3.1KB

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