oncemore.js 988 B

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