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