@peter.naydenov/notice 2.1.0 → 2.2.0

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peter.naydenov/notice",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "Event emmiter - NOTICE",
5
5
  "main": "src/main.js",
6
6
  "type": "module",
@@ -22,8 +22,8 @@
22
22
  ],
23
23
  "license": "MIT",
24
24
  "devDependencies": {
25
- "c8": "^8.0.0",
26
- "chai": "4.3.7",
25
+ "c8": "^8.0.1",
26
+ "chai": "4.3.8",
27
27
  "mocha": "10.2.0"
28
28
  },
29
29
  "c8": {
package/src/main.js CHANGED
@@ -2,7 +2,7 @@
2
2
  function notice () {
3
3
  function Notice () {
4
4
  let
5
- scroll = {} // General events with their subscribers
5
+ scroll = {'*':[]} // General events with their subscribers
6
6
  , scrollOnce = {} // Single events with their subscribers
7
7
  , ignore = [] // Ignore event names ( general and single )
8
8
  , debugFlag = false
@@ -13,6 +13,7 @@ function notice () {
13
13
  scroll[e].push ( fn )
14
14
  } // on func.
15
15
  function once ( e, fn ) {
16
+ if ( e === '*' ) return // The wildcard '*' doesn't work for 'once' events
16
17
  if ( !scrollOnce[e] ) scrollOnce[e] = []
17
18
  scrollOnce[e].push ( fn )
18
19
  } // once func.
@@ -47,22 +48,27 @@ function notice () {
47
48
  }
48
49
 
49
50
  }
50
- if ( e == '*' ) { // The wildcard '*' doesn't work for 'once' events
51
- let evNames = Object.keys ( scroll )
52
- evNames.forEach ( name => {
53
- if ( ignore.includes(name) ) return
54
- scroll[name].forEach ( fn => fn(...args))
55
- })
51
+
52
+ function exeCallback ( name ) {
53
+ if ( name === '*' ) return
54
+ if ( ignore.includes(name) ) return
55
+ scroll[name].forEach ( fn => fn(...args))
56
+ scroll['*'].forEach ( fn => fn(e,...args) )
57
+ } // exeCallback func.
58
+
59
+ if ( e === '*' ) { // The wildcard '*' doesn't work for 'once' events
60
+ let evNames = Object.keys ( scroll )
61
+ evNames.forEach ( name => exeCallback(name) )
62
+ return
56
63
  }
57
64
  if ( scrollOnce[e] ) {
58
65
  if ( ignore.includes(e) ) return
59
66
  scrollOnce[e].forEach ( fn => fn(...args) )
60
67
  delete scrollOnce[e]
61
68
  }
62
- if ( scroll[e] ) {
63
- if ( ignore.includes(e) ) return
64
- scroll[e].forEach ( fn => fn(...args) )
65
- }
69
+ if ( scroll[e] ) {
70
+ exeCallback ( e )
71
+ }
66
72
  } // emit func.
67
73
  function start ( e ) {
68
74
  if ( e === '*' ) {
package/test/01-notice.js DELETED
@@ -1,282 +0,0 @@
1
- "use strict"
2
-
3
- import notice from '../src/main.js'
4
- import { expect } from 'chai'
5
-
6
-
7
-
8
-
9
-
10
- describe ( 'Testing Notice', () => {
11
-
12
-
13
-
14
- it ( 'Standard event', () => {
15
- const eBus = notice ();
16
- let result = 0;
17
- eBus.on ( 'note', () => result += 1 )
18
- eBus.emit ( 'note' )
19
- eBus.emit ( 'note' )
20
- expect ( result ).to.be.equal ( 2 )
21
- }) // it standard
22
-
23
-
24
-
25
- it ( 'Single event', () => {
26
- const eBus = notice ();
27
- let result = 0;
28
-
29
- eBus.once ( 'note', () => result += 1 )
30
- eBus.emit ( 'note' )
31
- eBus.emit ( 'note' )
32
- expect ( result ).to.be.equal ( 1 )
33
- }) // it single
34
-
35
-
36
- it ( 'Many subscribers for single event', () => {
37
- const eBus = notice ();
38
- let result = 0;
39
-
40
- eBus.once ( 'note', () => result += 1 )
41
- eBus.once ( 'note', () => result += 3 )
42
- eBus.emit ( 'note' )
43
- eBus.emit ( 'note' )
44
- expect ( result ).to.be.equal ( 4 )
45
- }) // it single
46
-
47
-
48
- it ( 'Remove standard event', () => {
49
- const eBus = notice ();
50
- let result = 0;
51
-
52
- eBus.on ( 'note', () => result += 1 )
53
- eBus.off ( 'note' )
54
- eBus.emit ( 'note' )
55
- expect ( result ).to.be.equal ( 0 )
56
- }) // it remove std event
57
-
58
-
59
-
60
- it ( 'Remove single event', () => {
61
- const eBus = notice ();
62
- let result = 0;
63
-
64
- eBus.once ( 'note', () => result += 1 )
65
- eBus.off ( 'note' )
66
- eBus.emit ( 'note' )
67
- expect ( result ).to.be.equal ( 0 )
68
- }) // it remove single event
69
-
70
-
71
-
72
- it ( 'Reset the emitter', () => {
73
- const eBus = notice ();
74
- let result = 0;
75
-
76
- eBus.once ( 'note', () => result += 1 )
77
- eBus.on ( 'note', () => result += 3 )
78
- eBus.on ( 'boom', () => result += 9 )
79
- eBus.reset ()
80
- eBus.emit ( 'note' )
81
- eBus.emit ( 'boom' )
82
- expect ( result ).to.be.equal ( 0 )
83
- }) // it reset the emitter
84
-
85
-
86
- it ( 'Stop and resume standard event', () => {
87
- const eBus = notice ();
88
- let result = 0;
89
-
90
- eBus.on ( 'note', () => result += 1 )
91
- eBus.stop ( 'note' )
92
- eBus.emit ( 'note' )
93
- expect ( result ).to.be.equal ( 0 )
94
- eBus.start ( 'note' )
95
- eBus.emit ( 'note' )
96
- expect ( result ).to.be.equal ( 1 )
97
- }) // it stop and resume std event
98
-
99
-
100
-
101
-
102
- it ( 'Stop and resume single event', () => {
103
- const eBus = notice ();
104
- let result = 0;
105
-
106
- eBus.once ( 'note', () => result += 1 )
107
- eBus.stop ( 'note' )
108
- eBus.emit ( 'note' )
109
- expect ( result ).to.be.equal ( 0 )
110
- eBus.start ( 'note' )
111
- eBus.emit ( 'note' )
112
- eBus.emit ( 'note' )
113
- eBus.emit ( 'note' )
114
- expect ( result ).to.be.equal ( 1 )
115
- }) // it stop and resume single event
116
-
117
-
118
- it ( 'Emit with wildcard', () => {
119
- const eBus = notice ();
120
- let result = 0;
121
-
122
- eBus.on ( 'first' , () => result+=1 )
123
- eBus.on ( 'second', () => result+= 3 )
124
-
125
- eBus.emit ( '*' )
126
- expect ( result ).to.be.equal ( 4 )
127
- }) // it emit with wildcard
128
-
129
-
130
- it ( 'Stop and start with wildcard', () => {
131
- const eBus = notice ();
132
- let result = 0;
133
-
134
- eBus.on ( 'first' , () => result+=1 )
135
- eBus.on ( 'second', () => result+= 3 )
136
-
137
- eBus.stop ( '*' )
138
- eBus.emit ( 'first' )
139
- eBus.emit ( 'second' )
140
- expect ( result ).to.be.equal ( 0 )
141
- eBus.start ( '*' )
142
- eBus.emit ( 'first' )
143
- eBus.emit ( 'second' )
144
- expect ( result ).to.be.equal ( 4 )
145
- }) // it stop with wildcard
146
-
147
-
148
-
149
- it ( 'Wildcard with a stopped event', () => {
150
- const eBus = notice ();
151
- let result = 0;
152
-
153
- eBus.on ( 'first' , () => result+=1 )
154
- eBus.on ( 'second', () => result+= 3 )
155
-
156
- eBus.stop ( 'second' )
157
- eBus.emit ( '*' )
158
- expect ( result ).to.be.equal ( 1 )
159
-
160
- }) // it wildcard with stopped event
161
-
162
-
163
- it ( 'Unsubscribe a function', () => {
164
- const eBus = notice ();
165
- let result = 0;
166
-
167
- const
168
- fn1 = () => result += 1
169
- , fn2 = () => result += 3
170
- , fn3 = () => result += 20
171
- ;
172
-
173
- eBus.on ( 'note' , fn1 )
174
- eBus.on ( 'note' , fn2 )
175
- eBus.once ( 'note' , fn2 )
176
- eBus.once ( 'note' , fn3 )
177
- eBus.off ( 'note' , fn3 )
178
- eBus.emit ( 'note' )
179
- eBus.off ( 'note', fn2 )
180
- eBus.emit ( 'note' )
181
- expect ( result ).to.be.equal ( 8 )
182
- }) // it unsibsribe a function
183
-
184
-
185
-
186
- it ( 'Unsubscribe a single function', () => {
187
- const eBus = notice ();
188
- let result = 0;
189
-
190
- const
191
- fn1 = () => result += 1
192
- , fn2 = () => result += 3
193
- , fn3 = () => result += 20
194
- ;
195
-
196
- eBus.once ( 'note' , fn3 )
197
- eBus.off ( 'note' , fn3 )
198
- eBus.emit ( 'note' )
199
- eBus.emit ( 'note' )
200
- expect ( result ).to.be.equal ( 0 )
201
- }) // it unsibsribe a function
202
-
203
-
204
-
205
- it ( 'Unsubscribe all functions for the event', () => {
206
- const eBus = notice ();
207
- let result = 0;
208
-
209
- const fn3 = () => result += 20;
210
-
211
- eBus.on ( 'note' , fn3 )
212
- eBus.off ( 'note' , fn3 )
213
- eBus.emit ( 'note' )
214
- eBus.emit ( 'note' )
215
- expect ( result ).to.be.equal ( 0 )
216
- }) // it unsibsribe a function
217
-
218
-
219
-
220
- it ( 'Unsubscribe a function from non existing event', () => {
221
- const eBus = notice ();
222
- let result = 0;
223
-
224
- const
225
- fn1 = () => result += 1
226
- , fn2 = () => result += 3
227
- , fn3 = () => result += 20
228
- ;
229
-
230
- eBus.on ( 'note' , fn3 )
231
- eBus.off ( 'non' , fn1 )
232
- eBus.emit ( 'note' )
233
- eBus.emit ( 'note' )
234
- expect ( result ).to.be.equal ( 40 )
235
- }) // it unsibsribe a function
236
-
237
-
238
-
239
- it ( 'Event with primitive data', () => {
240
- const eBus = notice ();
241
- eBus.on ( 'note' , a => expect (a).to.be.equal ( 12 ) )
242
- eBus.emit ( 'note', 12 )
243
- }) // it Event with primitive data
244
-
245
-
246
-
247
- it ( 'Event with object', () => {
248
- const eBus = notice ();
249
- eBus.on ( 'note' , a => expect ( a['val'] ).to.be.equal ( 12 ) )
250
- eBus.emit ( 'note', {val:12} )
251
- }) // it Event with primitive data
252
-
253
-
254
-
255
- it ( 'Event with two values', () => {
256
- const eBus = notice ();
257
- eBus.on ( 'note' , (str,a) => {
258
- expect ( str ).to.be.equal ( 'test' )
259
- expect (a.val).to.be.equal ( 12 )
260
- })
261
- eBus.emit ( 'note', 'test', {val:12} )
262
- }) // it Event with primitive data
263
-
264
-
265
-
266
- it ( 'Multiple Notice instances', () => {
267
- const
268
- eBus1 = notice ()
269
- , eBus2 = notice ()
270
- ;
271
-
272
- eBus1.on ( 'test', x => expect ( x ).to.be.equal ( 12 ) )
273
- eBus2.on ( 'test', x => expect ( x ).to.be.equal ( 73 ) )
274
- eBus1.emit ( 'test', 12 )
275
- eBus2.emit ( 'test', 73 )
276
- }) // it Multiple Notice instances
277
-
278
-
279
-
280
- }) // define
281
-
282
-