You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.6 KiB

4 years ago
  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. var e = new EventEmitter();
  25. e.once('hello', common.mustCall());
  26. e.emit('hello', 'a', 'b');
  27. e.emit('hello', 'a', 'b');
  28. e.emit('hello', 'a', 'b');
  29. e.emit('hello', 'a', 'b');
  30. function remove() {
  31. assert.fail('once->foo should not be emitted');
  32. }
  33. e.once('foo', remove);
  34. e.removeListener('foo', remove);
  35. e.emit('foo');
  36. e.once('e', common.mustCall(function() {
  37. e.emit('e');
  38. }));
  39. e.once('e', common.mustCall());
  40. e.emit('e');
  41. // Verify that the listener must be a function
  42. assert.throws(function() {
  43. var ee = new EventEmitter();
  44. ee.once('foo', null);
  45. }, /^TypeError: The "listener" argument must be of type Function. Received type object$/);
  46. {
  47. // once() has different code paths based on the number of arguments being
  48. // emitted. Verify that all of the cases are covered.
  49. var maxArgs = 4;
  50. for (var i = 0; i <= maxArgs; ++i) {
  51. var ee = new EventEmitter();
  52. var args = ['foo'];
  53. for (var j = 0; j < i; ++j)
  54. args.push(j);
  55. ee.once('foo', common.mustCall(function() {
  56. var params = Array.prototype.slice.call(arguments);
  57. var restArgs = args.slice(1);
  58. assert.ok(Array.isArray(params));
  59. assert.strictEqual(params.length, restArgs.length);
  60. for (var index = 0; index < params.length; index++) {
  61. var param = params[index];
  62. assert.strictEqual(param, restArgs[index]);
  63. }
  64. }));
  65. EventEmitter.prototype.emit.apply(ee, args);
  66. }
  67. }