index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. 'use strict';
  2. module.exports = function (t, a) {
  3. var x = t(), y, count, count2, count3, count4, test, listener1, listener2;
  4. x.emit('none');
  5. test = "Once: ";
  6. count = 0;
  7. x.once('foo', function (a1, a2, a3) {
  8. a(this, x, test + "Context");
  9. a.deep([a1, a2, a3], ['foo', x, 15], test + "Arguments");
  10. ++count;
  11. });
  12. x.emit('foobar');
  13. a(count, 0, test + "Not invoked on other event");
  14. x.emit('foo', 'foo', x, 15);
  15. a(count, 1, test + "Emitted");
  16. x.emit('foo');
  17. a(count, 1, test + "Emitted once");
  18. test = "On & Once: ";
  19. count = 0;
  20. x.on('foo', listener1 = function (a1, a2, a3) {
  21. a(this, x, test + "Context");
  22. a.deep([a1, a2, a3], ['foo', x, 15], test + "Arguments");
  23. ++count;
  24. });
  25. count2 = 0;
  26. x.once('foo', listener2 = function (a1, a2, a3) {
  27. a(this, x, test + "Context");
  28. a.deep([a1, a2, a3], ['foo', x, 15], test + "Arguments");
  29. ++count2;
  30. });
  31. x.emit('foobar');
  32. a(count, 0, test + "Not invoked on other event");
  33. x.emit('foo', 'foo', x, 15);
  34. a(count, 1, test + "Emitted");
  35. x.emit('foo', 'foo', x, 15);
  36. a(count, 2, test + "Emitted twice");
  37. a(count2, 1, test + "Emitted once");
  38. x.off('foo', listener1);
  39. x.emit('foo');
  40. a(count, 2, test + "Not emitter after off");
  41. count = 0;
  42. x.once('foo', listener1 = function () { ++count; });
  43. x.off('foo', listener1);
  44. x.emit('foo');
  45. a(count, 0, "Once Off: Not emitted");
  46. count = 0;
  47. x.on('foo', listener2 = function () {});
  48. x.once('foo', listener1 = function () { ++count; });
  49. x.off('foo', listener1);
  50. x.emit('foo');
  51. a(count, 0, "Once Off (multi): Not emitted");
  52. x.off('foo', listener2);
  53. test = "Prototype Share: ";
  54. y = Object.create(x);
  55. count = 0;
  56. count2 = 0;
  57. count3 = 0;
  58. count4 = 0;
  59. x.on('foo', function () {
  60. ++count;
  61. });
  62. y.on('foo', function () {
  63. ++count2;
  64. });
  65. x.once('foo', function () {
  66. ++count3;
  67. });
  68. y.once('foo', function () {
  69. ++count4;
  70. });
  71. x.emit('foo');
  72. a(count, 1, test + "x on count");
  73. a(count2, 0, test + "y on count");
  74. a(count3, 1, test + "x once count");
  75. a(count4, 0, test + "y once count");
  76. y.emit('foo');
  77. a(count, 1, test + "x on count");
  78. a(count2, 1, test + "y on count");
  79. a(count3, 1, test + "x once count");
  80. a(count4, 1, test + "y once count");
  81. x.emit('foo');
  82. a(count, 2, test + "x on count");
  83. a(count2, 1, test + "y on count");
  84. a(count3, 1, test + "x once count");
  85. a(count4, 1, test + "y once count");
  86. y.emit('foo');
  87. a(count, 2, test + "x on count");
  88. a(count2, 2, test + "y on count");
  89. a(count3, 1, test + "x once count");
  90. a(count4, 1, test + "y once count");
  91. };