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