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