events-ex 2.0.0-alpha.2 → 2.0.0-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/LICENSE +1 -1
  3. package/README.cn.md +4 -2
  4. package/README.md +4 -2
  5. package/docs/README.md +4 -2
  6. package/docs/classes/event.Event.md +6 -6
  7. package/docs/classes/event_emitter-1.EventEmitter.md +1 -1
  8. package/docs/classes/event_emitter.EventEmitter.md +11 -11
  9. package/docs/modules/all_off.md +1 -1
  10. package/docs/modules/consts.md +2 -1
  11. package/docs/modules/default_methods.md +1 -1
  12. package/docs/modules/has_listeners.md +1 -1
  13. package/docs/modules/pipe.md +1 -1
  14. package/docs/modules/pipe_async.md +1 -1
  15. package/docs/modules/unify.md +1 -1
  16. package/docs/modules/util_array_remove.md +1 -1
  17. package/docs/modules/util_object_for_each.md +1 -1
  18. package/docs/modules/util_string_pad.md +1 -1
  19. package/docs/modules/util_to_int.md +1 -1
  20. package/docs/modules/util_valid_callable.md +1 -1
  21. package/docs/modules/util_valid_object.md +1 -1
  22. package/docs/modules/wrap_event_emitter.md +2 -2
  23. package/lib/consts.d.ts +2 -0
  24. package/lib/consts.js +3 -1
  25. package/package.json +1 -1
  26. package/src/consts.js +2 -0
  27. package/test/add-listeners-test.js +0 -42
  28. package/test/all-off-test.js +0 -51
  29. package/test/check-listener-leaks-test.js +0 -76
  30. package/test/emitter-method-names-test.js +0 -11
  31. package/test/event-emitter-async-test.js +0 -152
  32. package/test/event-emitter-test.js +0 -145
  33. package/test/eventable-test.js +0 -108
  34. package/test/events-listeners-test.js +0 -46
  35. package/test/events-remove-listeners-test.js +0 -89
  36. package/test/has-listeners-test.js +0 -44
  37. package/test/listeners-side-effects-test.js +0 -40
  38. package/test/max-listeners-test.js +0 -33
  39. package/test/modify-in-emit-test.js +0 -121
  40. package/test/no-error-provided-to-error-event-test.js +0 -38
  41. package/test/num-args-test.js +0 -41
  42. package/test/once-test.js +0 -44
  43. package/test/pipe-async-test.js +0 -58
  44. package/test/pipe-test.js +0 -57
  45. package/test/remove-all-listeners-test.js +0 -74
  46. package/test/set-max-listeners-side-effects-test.js +0 -13
  47. package/test/subclass-test.js +0 -49
  48. package/test/unify-async-test.js +0 -122
  49. package/test/unify-test.js +0 -122
package/CHANGELOG.md CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [commit-and-tag-version](https://github.com/absolute-version/commit-and-tag-version) for commit guidelines.
4
4
 
5
+ ## [2.0.0-alpha.3](https://github.com/snowyu/events-ex.js/compare/v2.0.0-alpha.2...v2.0.0-alpha.3) (2023-08-27)
6
+
7
+
8
+ ### Features
9
+
10
+ * add ABORT state constant ([a8b3710](https://github.com/snowyu/events-ex.js/commit/a8b3710ae7f1695263ed0bd9036db31831ed9ae0))
11
+
5
12
  ## [2.0.0-alpha.2](https://github.com/snowyu/events-ex.js/compare/v2.0.0-alpha.1...v2.0.0-alpha.2) (2023-06-13)
6
13
 
7
14
 
package/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (C) 2012-2015 Mariusz Nowak (www.medikoo.com)
1
+ Copyright (C) 2015-2023 Riceball LEE (snowyu.lee@gmail.com)
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  of this software and associated documentation files (the "Software"), to deal
package/README.cn.md CHANGED
@@ -87,6 +87,7 @@ my.emit('event');
87
87
  import {EventEmitter, states} from 'events-ex';
88
88
  import {isObject} from 'util-ex';
89
89
 
90
+ states.ABORT = -1
90
91
  class MyDb extends EventEmitter {
91
92
  get(key) {
92
93
  // Demo the event object bubbling usage:
@@ -108,8 +109,9 @@ db.on('getting', function(key){
108
109
  state: states.DONE,
109
110
  result: result,
110
111
  }
111
- } else if (result === null) {
112
- // abort default get key.
112
+ this.stopped = true // it will skip other listeners if true
113
+ } else {
114
+ // you can abort to get key by default.
113
115
  this.result = {state: states.ABORT};
114
116
  // this.stopped = true // it will skip other listeners if true
115
117
  }
package/README.md CHANGED
@@ -77,6 +77,7 @@ Bubbling event usage:
77
77
  import {EventEmitter, states} from 'events-ex';
78
78
  import {isObject} from 'util-ex';
79
79
 
80
+ states.ABORT = -1
80
81
  class MyDb extends EventEmitter {
81
82
  get(key) {
82
83
  // Demo the event object bubbling usage:
@@ -98,8 +99,9 @@ db.on('getting', function(key){
98
99
  state: states.DONE,
99
100
  result: result,
100
101
  }
101
- } else if (result === null) {
102
- // abort default get key.
102
+ this.stopped = true // it will skip other listeners if true
103
+ } else {
104
+ // you can abort to get key by default.
103
105
  this.result = {state: states.ABORT};
104
106
  // this.stopped = true // it will skip other listeners if true
105
107
  }
package/docs/README.md CHANGED
@@ -77,6 +77,7 @@ Bubbling event usage:
77
77
  import {EventEmitter, states} from 'events-ex';
78
78
  import {isObject} from 'util-ex';
79
79
 
80
+ states.ABORT = -1
80
81
  class MyDb extends EventEmitter {
81
82
  get(key) {
82
83
  // Demo the event object bubbling usage:
@@ -98,8 +99,9 @@ db.on('getting', function(key){
98
99
  state: states.DONE,
99
100
  result: result,
100
101
  }
101
- } else if (result === null) {
102
- // abort default get key.
102
+ this.stopped = true // it will skip other listeners if true
103
+ } else {
104
+ // you can abort to get key by default.
103
105
  this.result = {state: states.ABORT};
104
106
  // this.stopped = true // it will skip other listeners if true
105
107
  }
@@ -41,7 +41,7 @@ Event Object that contains information about the event, such as the target eleme
41
41
 
42
42
  #### Defined in
43
43
 
44
- [src/event.js:8](https://github.com/snowyu/events-ex.js/blob/25f69f3/src/event.js#L8)
44
+ [src/event.js:8](https://github.com/snowyu/events-ex.js/blob/2b0a99d/src/event.js#L8)
45
45
 
46
46
  ## Properties
47
47
 
@@ -53,7 +53,7 @@ Keep your event result here if any.
53
53
 
54
54
  #### Defined in
55
55
 
56
- [src/event.js:38](https://github.com/snowyu/events-ex.js/blob/25f69f3/src/event.js#L38)
56
+ [src/event.js:38](https://github.com/snowyu/events-ex.js/blob/2b0a99d/src/event.js#L38)
57
57
 
58
58
  ___
59
59
 
@@ -65,7 +65,7 @@ Whether stop the bubbling event
65
65
 
66
66
  #### Defined in
67
67
 
68
- [src/event.js:32](https://github.com/snowyu/events-ex.js/blob/25f69f3/src/event.js#L32)
68
+ [src/event.js:32](https://github.com/snowyu/events-ex.js/blob/2b0a99d/src/event.js#L32)
69
69
 
70
70
  ___
71
71
 
@@ -77,7 +77,7 @@ Who trigger the event
77
77
 
78
78
  #### Defined in
79
79
 
80
- [src/event.js:26](https://github.com/snowyu/events-ex.js/blob/25f69f3/src/event.js#L26)
80
+ [src/event.js:26](https://github.com/snowyu/events-ex.js/blob/2b0a99d/src/event.js#L26)
81
81
 
82
82
  ## Methods
83
83
 
@@ -95,7 +95,7 @@ The result of the event.
95
95
 
96
96
  #### Defined in
97
97
 
98
- [src/event.js:45](https://github.com/snowyu/events-ex.js/blob/25f69f3/src/event.js#L45)
98
+ [src/event.js:45](https://github.com/snowyu/events-ex.js/blob/2b0a99d/src/event.js#L45)
99
99
 
100
100
  ___
101
101
 
@@ -117,4 +117,4 @@ Initializes the event with the target object.
117
117
 
118
118
  #### Defined in
119
119
 
120
- [src/event.js:20](https://github.com/snowyu/events-ex.js/blob/25f69f3/src/event.js#L20)
120
+ [src/event.js:20](https://github.com/snowyu/events-ex.js/blob/2b0a99d/src/event.js#L20)
@@ -22,4 +22,4 @@ Class that represents an event emitter.
22
22
 
23
23
  #### Defined in
24
24
 
25
- [src/event-emitter.js:7](https://github.com/snowyu/events-ex.js/blob/25f69f3/src/event-emitter.js#L7)
25
+ [src/event-emitter.js:7](https://github.com/snowyu/events-ex.js/blob/2b0a99d/src/event-emitter.js#L7)
@@ -43,7 +43,7 @@ Class that represents an event emitter.
43
43
 
44
44
  #### Defined in
45
45
 
46
- [src/event-emitter.d.ts:7](https://github.com/snowyu/events-ex.js/blob/25f69f3/src/event-emitter.d.ts#L7)
46
+ [src/event-emitter.d.ts:7](https://github.com/snowyu/events-ex.js/blob/2b0a99d/src/event-emitter.d.ts#L7)
47
47
 
48
48
  ## Methods
49
49
 
@@ -68,7 +68,7 @@ The result of the event.
68
68
 
69
69
  #### Defined in
70
70
 
71
- [src/event-emitter.d.ts:38](https://github.com/snowyu/events-ex.js/blob/25f69f3/src/event-emitter.d.ts#L38)
71
+ [src/event-emitter.d.ts:38](https://github.com/snowyu/events-ex.js/blob/2b0a99d/src/event-emitter.d.ts#L38)
72
72
 
73
73
  ___
74
74
 
@@ -93,7 +93,7 @@ A promise that resolves with the result of the event.
93
93
 
94
94
  #### Defined in
95
95
 
96
- [src/event-emitter.d.ts:44](https://github.com/snowyu/events-ex.js/blob/25f69f3/src/event-emitter.d.ts#L44)
96
+ [src/event-emitter.d.ts:44](https://github.com/snowyu/events-ex.js/blob/2b0a99d/src/event-emitter.d.ts#L44)
97
97
 
98
98
  ___
99
99
 
@@ -117,7 +117,7 @@ Returns the count of listeners that are registered to listen for the specified e
117
117
 
118
118
  #### Defined in
119
119
 
120
- [src/event-emitter.d.ts:66](https://github.com/snowyu/events-ex.js/blob/25f69f3/src/event-emitter.d.ts#L66)
120
+ [src/event-emitter.d.ts:66](https://github.com/snowyu/events-ex.js/blob/2b0a99d/src/event-emitter.d.ts#L66)
121
121
 
122
122
  ___
123
123
 
@@ -141,7 +141,7 @@ Returns an array of functions that are registered to listen for the specified ev
141
141
 
142
142
  #### Defined in
143
143
 
144
- [src/event-emitter.d.ts:59](https://github.com/snowyu/events-ex.js/blob/25f69f3/src/event-emitter.d.ts#L59)
144
+ [src/event-emitter.d.ts:59](https://github.com/snowyu/events-ex.js/blob/2b0a99d/src/event-emitter.d.ts#L59)
145
145
 
146
146
  ___
147
147
 
@@ -170,7 +170,7 @@ The EventEmitter instance to allow chaining.
170
170
 
171
171
  #### Defined in
172
172
 
173
- [src/event-emitter.d.ts:32](https://github.com/snowyu/events-ex.js/blob/25f69f3/src/event-emitter.d.ts#L32)
173
+ [src/event-emitter.d.ts:32](https://github.com/snowyu/events-ex.js/blob/2b0a99d/src/event-emitter.d.ts#L32)
174
174
 
175
175
  ___
176
176
 
@@ -199,7 +199,7 @@ The EventEmitter instance to allow chaining.
199
199
 
200
200
  #### Defined in
201
201
 
202
- [src/event-emitter.d.ts:16](https://github.com/snowyu/events-ex.js/blob/25f69f3/src/event-emitter.d.ts#L16)
202
+ [src/event-emitter.d.ts:16](https://github.com/snowyu/events-ex.js/blob/2b0a99d/src/event-emitter.d.ts#L16)
203
203
 
204
204
  ___
205
205
 
@@ -228,7 +228,7 @@ The EventEmitter instance to allow chaining.
228
228
 
229
229
  #### Defined in
230
230
 
231
- [src/event-emitter.d.ts:24](https://github.com/snowyu/events-ex.js/blob/25f69f3/src/event-emitter.d.ts#L24)
231
+ [src/event-emitter.d.ts:24](https://github.com/snowyu/events-ex.js/blob/2b0a99d/src/event-emitter.d.ts#L24)
232
232
 
233
233
  ___
234
234
 
@@ -252,7 +252,7 @@ Removes all listeners for a specific event or all events from an event emitter.
252
252
 
253
253
  #### Defined in
254
254
 
255
- [src/event-emitter.d.ts:51](https://github.com/snowyu/events-ex.js/blob/25f69f3/src/event-emitter.d.ts#L51)
255
+ [src/event-emitter.d.ts:51](https://github.com/snowyu/events-ex.js/blob/2b0a99d/src/event-emitter.d.ts#L51)
256
256
 
257
257
  ___
258
258
 
@@ -272,7 +272,7 @@ ___
272
272
 
273
273
  #### Defined in
274
274
 
275
- [src/event-emitter.d.ts:52](https://github.com/snowyu/events-ex.js/blob/25f69f3/src/event-emitter.d.ts#L52)
275
+ [src/event-emitter.d.ts:52](https://github.com/snowyu/events-ex.js/blob/2b0a99d/src/event-emitter.d.ts#L52)
276
276
 
277
277
  ___
278
278
 
@@ -293,4 +293,4 @@ ___
293
293
 
294
294
  #### Defined in
295
295
 
296
- [src/event-emitter.d.ts:67](https://github.com/snowyu/events-ex.js/blob/25f69f3/src/event-emitter.d.ts#L67)
296
+ [src/event-emitter.d.ts:67](https://github.com/snowyu/events-ex.js/blob/2b0a99d/src/event-emitter.d.ts#L67)
@@ -41,4 +41,4 @@ Removes all listeners for a specific event or all events from an event emitter.
41
41
 
42
42
  #### Defined in
43
43
 
44
- [src/all-off.js:12](https://github.com/snowyu/events-ex.js/blob/25f69f3/src/all-off.js#L12)
44
+ [src/all-off.js:12](https://github.com/snowyu/events-ex.js/blob/2b0a99d/src/all-off.js#L12)
@@ -18,10 +18,11 @@
18
18
 
19
19
  | Name | Type |
20
20
  | :------ | :------ |
21
+ | `ABORT` | `number` |
21
22
  | `CONTINUE` | `any` |
22
23
  | `DONE` | `number` |
23
24
  | `STOPPED` | `number` |
24
25
 
25
26
  #### Defined in
26
27
 
27
- [src/consts.js:5](https://github.com/snowyu/events-ex.js/blob/25f69f3/src/consts.js#L5)
28
+ [src/consts.js:6](https://github.com/snowyu/events-ex.js/blob/2b0a99d/src/consts.js#L6)
@@ -48,4 +48,4 @@ Renames and re-exports [getEventableMethods](default_methods.md#geteventablemeth
48
48
 
49
49
  #### Defined in
50
50
 
51
- [src/default-methods.js:8](https://github.com/snowyu/events-ex.js/blob/25f69f3/src/default-methods.js#L8)
51
+ [src/default-methods.js:8](https://github.com/snowyu/events-ex.js/blob/2b0a99d/src/default-methods.js#L8)
@@ -37,4 +37,4 @@ Renames and re-exports [hasListeners](has_listeners.md#haslisteners)
37
37
 
38
38
  #### Defined in
39
39
 
40
- [src/has-listeners.js:10](https://github.com/snowyu/events-ex.js/blob/25f69f3/src/has-listeners.js#L10)
40
+ [src/has-listeners.js:10](https://github.com/snowyu/events-ex.js/blob/2b0a99d/src/has-listeners.js#L10)
@@ -46,4 +46,4 @@ Creates a pipeline between two event emitters, so that any events emitted by the
46
46
 
47
47
  #### Defined in
48
48
 
49
- [src/pipe.js:21](https://github.com/snowyu/events-ex.js/blob/25f69f3/src/pipe.js#L21)
49
+ [src/pipe.js:21](https://github.com/snowyu/events-ex.js/blob/2b0a99d/src/pipe.js#L21)
@@ -46,4 +46,4 @@ Creates a pipeline between two event emitters, so that any events emitted by the
46
46
 
47
47
  #### Defined in
48
48
 
49
- [src/pipe-async.js:21](https://github.com/snowyu/events-ex.js/blob/25f69f3/src/pipe-async.js#L21)
49
+ [src/pipe-async.js:21](https://github.com/snowyu/events-ex.js/blob/2b0a99d/src/pipe-async.js#L21)
@@ -43,4 +43,4 @@ Unifies the event listeners of two event emitter objects so that they share the
43
43
 
44
44
  #### Defined in
45
45
 
46
- [src/unify.js:17](https://github.com/snowyu/events-ex.js/blob/25f69f3/src/unify.js#L17)
46
+ [src/unify.js:17](https://github.com/snowyu/events-ex.js/blob/2b0a99d/src/unify.js#L17)
@@ -36,4 +36,4 @@ Renames and re-exports [remove](util_array_remove.md#remove)
36
36
 
37
37
  #### Defined in
38
38
 
39
- [src/util/array-remove.js:5](https://github.com/snowyu/events-ex.js/blob/25f69f3/src/util/array-remove.js#L5)
39
+ [src/util/array-remove.js:5](https://github.com/snowyu/events-ex.js/blob/2b0a99d/src/util/array-remove.js#L5)
@@ -38,4 +38,4 @@ Renames and re-exports [forEach](util_object_for_each.md#foreach)
38
38
 
39
39
  #### Defined in
40
40
 
41
- [src/util/object-for-each.js:6](https://github.com/snowyu/events-ex.js/blob/25f69f3/src/util/object-for-each.js#L6)
41
+ [src/util/object-for-each.js:6](https://github.com/snowyu/events-ex.js/blob/2b0a99d/src/util/object-for-each.js#L6)
@@ -37,4 +37,4 @@ Renames and re-exports [pad](util_string_pad.md#pad)
37
37
 
38
38
  #### Defined in
39
39
 
40
- [src/util/string-pad.js:8](https://github.com/snowyu/events-ex.js/blob/25f69f3/src/util/string-pad.js#L8)
40
+ [src/util/string-pad.js:8](https://github.com/snowyu/events-ex.js/blob/2b0a99d/src/util/string-pad.js#L8)
@@ -36,4 +36,4 @@ Renames and re-exports [toInt](util_to_int.md#toint)
36
36
 
37
37
  #### Defined in
38
38
 
39
- [src/util/to-int.js:2](https://github.com/snowyu/events-ex.js/blob/25f69f3/src/util/to-int.js#L2)
39
+ [src/util/to-int.js:2](https://github.com/snowyu/events-ex.js/blob/2b0a99d/src/util/to-int.js#L2)
@@ -36,4 +36,4 @@ Renames and re-exports [validCallable](util_valid_callable.md#validcallable)
36
36
 
37
37
  #### Defined in
38
38
 
39
- [src/util/valid-callable.js:1](https://github.com/snowyu/events-ex.js/blob/25f69f3/src/util/valid-callable.js#L1)
39
+ [src/util/valid-callable.js:1](https://github.com/snowyu/events-ex.js/blob/2b0a99d/src/util/valid-callable.js#L1)
@@ -36,4 +36,4 @@ Renames and re-exports [validObject](util_valid_object.md#validobject)
36
36
 
37
37
  #### Defined in
38
38
 
39
- [src/util/valid-object.js:3](https://github.com/snowyu/events-ex.js/blob/25f69f3/src/util/valid-object.js#L3)
39
+ [src/util/valid-object.js:3](https://github.com/snowyu/events-ex.js/blob/2b0a99d/src/util/valid-object.js#L3)
@@ -30,7 +30,7 @@ Renames and re-exports [wrapEventEmitter](wrap_event_emitter.md#wrapeventemitter
30
30
 
31
31
  #### Defined in
32
32
 
33
- [src/wrap-event-emitter.js:6](https://github.com/snowyu/events-ex.js/blob/25f69f3/src/wrap-event-emitter.js#L6)
33
+ [src/wrap-event-emitter.js:6](https://github.com/snowyu/events-ex.js/blob/2b0a99d/src/wrap-event-emitter.js#L6)
34
34
 
35
35
  ## Functions
36
36
 
@@ -54,4 +54,4 @@ o or new Event instance
54
54
 
55
55
  #### Defined in
56
56
 
57
- [src/wrap-event-emitter.js:33](https://github.com/snowyu/events-ex.js/blob/25f69f3/src/wrap-event-emitter.js#L33)
57
+ [src/wrap-event-emitter.js:33](https://github.com/snowyu/events-ex.js/blob/2b0a99d/src/wrap-event-emitter.js#L33)
package/lib/consts.d.ts CHANGED
@@ -2,8 +2,10 @@ export namespace states {
2
2
  export { CONTINUE };
3
3
  export { DONE };
4
4
  export { STOPPED };
5
+ export { ABORT };
5
6
  }
6
7
  declare const CONTINUE: any;
7
8
  declare const DONE: 1;
8
9
  declare const STOPPED: 2;
10
+ declare const ABORT: -1;
9
11
  export {};
package/lib/consts.js CHANGED
@@ -7,9 +7,11 @@ exports.states = void 0;
7
7
  const CONTINUE = undefined;
8
8
  const DONE = 1;
9
9
  const STOPPED = 2;
10
+ const ABORT = -1;
10
11
  const states = {
11
12
  CONTINUE,
12
13
  DONE,
13
- STOPPED
14
+ STOPPED,
15
+ ABORT
14
16
  };
15
17
  exports.states = states;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "events-ex",
3
- "version": "2.0.0-alpha.2",
3
+ "version": "2.0.0-alpha.3",
4
4
  "description": "Browser-friendly enhanced events most compatible with standard node.js, it's powerful eventable ability.",
5
5
  "contributors": [
6
6
  {
package/src/consts.js CHANGED
@@ -1,9 +1,11 @@
1
1
  const CONTINUE = undefined
2
2
  const DONE = 1
3
3
  const STOPPED = 2
4
+ const ABORT = -1
4
5
 
5
6
  export const states = {
6
7
  CONTINUE,
7
8
  DONE,
8
9
  STOPPED,
10
+ ABORT,
9
11
  }
@@ -1,42 +0,0 @@
1
- import {assert} from "chai";
2
- import {EventEmitter} from '../src'
3
-
4
- describe('EventEmitter', () => {
5
- it('should add listeners', () => {
6
- var e = new EventEmitter();
7
-
8
- var events_new_listener_emited = [];
9
- var listeners_new_listener_emited = [];
10
- var times_hello_emited = 0;
11
-
12
- // sanity check
13
- assert.equal(e.addListener, e.on);
14
-
15
- e.on('newListener', function(event, listener) {
16
- events_new_listener_emited.push(event);
17
- listeners_new_listener_emited.push(listener);
18
- });
19
-
20
- function hello(a, b) {
21
- times_hello_emited += 1;
22
- assert.equal('a', a);
23
- assert.equal('b', b);
24
- }
25
- e.on('hello', hello);
26
-
27
- var foo = function foo() {};
28
- e.once('foo', foo);
29
-
30
- e.emit('hello', 'a', 'b');
31
-
32
-
33
- // just make sure that this doesn't throw:
34
- var f = new EventEmitter();
35
- f.setMaxListeners(0);
36
-
37
-
38
- assert.deepEqual(['hello', 'foo'], events_new_listener_emited);
39
- assert.deepEqual([hello, foo], listeners_new_listener_emited);
40
- assert.equal(1, times_hello_emited);
41
- });
42
- });
@@ -1,51 +0,0 @@
1
- import {assert} from "chai";
2
-
3
- import ee from '../src/wrap-event-emitter'
4
- import allOff from '../src/all-off'
5
-
6
- describe('all-off', () => {
7
- it('should remove all listeners', () => {
8
- var x, count, count2;
9
-
10
- x = ee();
11
- count = 0;
12
- count2 = 0;
13
- x.on('foo', function () {
14
- ++count;
15
- });
16
- x.on('foo', function () {
17
- ++count;
18
- });
19
- x.on('bar', function () {
20
- ++count2;
21
- });
22
- x.on('bar', function () {
23
- ++count2;
24
- });
25
- allOff(x, 'foo');
26
- x.emit('foo');
27
- x.emit('bar');
28
- assert.equal(count, 0, "foo off: type");
29
- assert.equal(count2, 2, "foo off: ohter type");
30
-
31
- count = 0;
32
- count2 = 0;
33
- x.on('foo', function () {
34
- ++count;
35
- });
36
- x.on('foo', function () {
37
- ++count;
38
- });
39
- x.on('bar', function () {
40
- ++count2;
41
- });
42
- x.on('bar', function () {
43
- ++count2;
44
- });
45
- allOff(x);
46
- x.emit('foo');
47
- x.emit('bar');
48
- assert.equal(count, 0, "All off: type");
49
- assert.equal(count2, 0, "All off: other type");
50
- });
51
- });
@@ -1,76 +0,0 @@
1
- import {assert} from "chai";
2
- import {EventEmitter} from '../src'
3
-
4
- describe('EventEmitter', () => {
5
- it('should check listener leaks', () => {
6
- var oldError = console.error;
7
- var oldTrace = console.trace;
8
- console.error = function(){};
9
- console.trace = function(){};
10
-
11
- var e = new EventEmitter();
12
-
13
- // default
14
- for (var i = 0; i < 10; i++) {
15
- e.on('default', function() {});
16
- }
17
- assert.ok(!e._events['default'].hasOwnProperty('warned'));
18
- e.on('default', function() {});
19
- assert.ok(e._events['default'].warned, 'should warned on default event');
20
-
21
- // specific
22
- e.setMaxListeners(5);
23
- for (var i = 0; i < 5; i++) {
24
- e.on('specific', function() {});
25
- }
26
- assert.ok(!e._events['specific'].hasOwnProperty('warned'));
27
- e.on('specific', function() {});
28
- assert.ok(e._events['specific'].warned, 'should warned on specific event');
29
-
30
- // only one
31
- e.setMaxListeners(1);
32
- e.on('only one', function() {});
33
- assert.ok(!e._events['only one'].hasOwnProperty('warned'));
34
- e.on('only one', function() {});
35
- assert.ok(e._events['only one'].hasOwnProperty('warned'), 'should warned on only one event');
36
-
37
- // unlimited
38
- e.setMaxListeners(0);
39
- for (var i = 0; i < 1000; i++) {
40
- e.on('unlimited', function() {});
41
- }
42
- assert.ok(!e._events['unlimited'].hasOwnProperty('warned'), 'should not warned on unlimited');
43
-
44
- // process-wide
45
- EventEmitter.defaultMaxListeners = 42;
46
- e = new EventEmitter();
47
-
48
- for (var i = 0; i < 42; ++i) {
49
- e.on('fortytwo', function() {});
50
- }
51
- assert.ok(!e._events['fortytwo'].hasOwnProperty('warned'));
52
- e.on('fortytwo', function() {});
53
- assert.ok(e._events['fortytwo'].hasOwnProperty('warned'));
54
- delete e._events['fortytwo'].warned;
55
-
56
- EventEmitter.defaultMaxListeners = 44;
57
- e.on('fortytwo', function() {});
58
- assert.ok(!e._events['fortytwo'].hasOwnProperty('warned'));
59
- e.on('fortytwo', function() {});
60
- assert.ok(e._events['fortytwo'].hasOwnProperty('warned'));
61
-
62
- // but _maxListeners still has precedence over defaultMaxListeners
63
- EventEmitter.defaultMaxListeners = 42;
64
- e = new EventEmitter();
65
- e.setMaxListeners(1);
66
- e.on('uno', function() {});
67
- assert.ok(!e._events['uno'].hasOwnProperty('warned'));
68
- e.on('uno', function() {});
69
- assert.ok(e._events['uno'].hasOwnProperty('warned'), 'should warned on uno event');
70
-
71
- // chainable
72
- assert.strictEqual(e, e.setMaxListeners(1), 'setMaxListeners chainable');
73
- console.error = oldError;
74
- console.trace = oldTrace;
75
- });
76
- });
@@ -1,11 +0,0 @@
1
- import {assert} from "chai";
2
- import {EventEmitter} from '../src'
3
-
4
- describe('EventEmitter', () => {
5
- it('should test method names', () => {
6
- var E = EventEmitter.prototype;
7
- assert.equal(E.constructor.name, 'EventEmitter');
8
- assert.equal(E.on, E.addListener); // Same method.
9
- assert.equal(E.off, E.removeListener); // Same method.
10
- });
11
- })