prepend.js 833 B

12345678910111213141516171819202122232425262728293031
  1. 'use strict';
  2. var common = require('./common');
  3. var EventEmitter = require('../');
  4. var assert = require('assert');
  5. var myEE = new EventEmitter();
  6. var m = 0;
  7. // This one comes last.
  8. myEE.on('foo', common.mustCall(function () {
  9. assert.strictEqual(m, 2);
  10. }));
  11. // This one comes second.
  12. myEE.prependListener('foo', common.mustCall(function () {
  13. assert.strictEqual(m++, 1);
  14. }));
  15. // This one comes first.
  16. myEE.prependOnceListener('foo',
  17. common.mustCall(function () {
  18. assert.strictEqual(m++, 0);
  19. }));
  20. myEE.emit('foo');
  21. // Verify that the listener must be a function
  22. assert.throws(function () {
  23. var ee = new EventEmitter();
  24. ee.prependOnceListener('foo', null);
  25. }, 'TypeError: The "listener" argument must be of type Function. Received type object');