oncemore.js 1003 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. "use strict";
  2. var util = require('util');
  3. module.exports = oncemore;
  4. // apply oncemore() to an emitter, and enable it to accept multiple events as input
  5. function oncemore(emitter) {
  6. if (!emitter) return emitter;
  7. var once = emitter.once;
  8. if (once && !once._old) {
  9. emitter.once = function(type, listener) {
  10. if (arguments.length <= 2)
  11. return once.apply(this, arguments);
  12. var types = Array.prototype.slice.call(arguments, 0, -1);
  13. var listener = arguments.length ? arguments[arguments.length-1] : undefined;
  14. if (typeof listener !== 'function')
  15. throw TypeError('listener must be a function');
  16. function g() {
  17. types.forEach(function(type) {
  18. this.removeListener(type, g);
  19. }, this);
  20. listener.apply(this, arguments);
  21. }
  22. g.listener = listener;
  23. types.forEach(function(type) {
  24. this.on(type, g);
  25. }, this);
  26. return this;
  27. };
  28. emitter.once._old = once;
  29. }
  30. return emitter;
  31. }