@peter.naydenov/notice 1.0.2 → 2.0.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/LICENSE +21 -0
- package/README.md +50 -14
- package/package.json +8 -7
- package/src/main.js +18 -2
- package/test/01-notice.js +9 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Peter Naydenov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -38,6 +38,7 @@ eBus.emit ( 'note' )
|
|
|
38
38
|
, emit : 'Trigger a event'
|
|
39
39
|
, stop : 'Ignore event for a while'
|
|
40
40
|
, start : 'Remove event from ignore list'
|
|
41
|
+
, debug : 'Returns a console message on each triggered event'
|
|
41
42
|
}
|
|
42
43
|
```
|
|
43
44
|
|
|
@@ -64,7 +65,7 @@ Register a single event.
|
|
|
64
65
|
```js
|
|
65
66
|
const eBus = notice ();
|
|
66
67
|
|
|
67
|
-
eBus.once ( 'start', name => console.log ( `Hey, ${name}!` ) )
|
|
68
|
+
eBus.once ( 'start', name => console.log ( `Hey, ${name}!` ) )
|
|
68
69
|
eBus.emit ( 'start', 'Johny' )
|
|
69
70
|
// ---> Hey, Johny!
|
|
70
71
|
eBus.emit ( 'start', 'Vessy' )
|
|
@@ -155,24 +156,59 @@ Enable again specified event.
|
|
|
155
156
|
|
|
156
157
|
```js
|
|
157
158
|
let result = 0;
|
|
158
|
-
const
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
// ---> result == 0
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
159
|
+
const
|
|
160
|
+
eBus = notice ()
|
|
161
|
+
, fn1 = x => result += 1 + x
|
|
162
|
+
, fn2 = x => result += 3 + x
|
|
163
|
+
;
|
|
164
|
+
eBus.on ( 'go', fn1 )
|
|
165
|
+
eBus.on ( 'go', fn2 )
|
|
166
|
+
eBus.stop ( 'go' )
|
|
167
|
+
eBus.emit ( 'go', 2 )
|
|
168
|
+
// ---> result == 0
|
|
169
|
+
eBus.start ( 'go' )
|
|
170
|
+
eBus.emit ( 'go', 1 )
|
|
171
|
+
console.log ( result )
|
|
172
|
+
// ---> result == 6
|
|
171
173
|
```
|
|
172
174
|
|
|
173
175
|
|
|
176
|
+
### Notice.debug ( state, label )
|
|
177
|
+
Provide debug message on each event. By default is debug is 'off'.
|
|
178
|
+
|
|
179
|
+
```js
|
|
180
|
+
// Turn debug "on"
|
|
181
|
+
eBus.debug ( true )
|
|
182
|
+
|
|
183
|
+
// Turn debug "off"
|
|
184
|
+
eBus.debug ( false )
|
|
185
|
+
|
|
186
|
+
// Activate debugger and set a debug message prefix
|
|
187
|
+
eBus.debug ( true, '[eBus]:' )
|
|
188
|
+
eBus.emit ( 'dummy' )
|
|
189
|
+
// --> [eBus]: Event "dummy" was triggered.
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
174
196
|
## Release History
|
|
175
197
|
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
### 2.0.0 ( 2023-06-15)
|
|
201
|
+
- [x] Library was converted to ES6 module;
|
|
202
|
+
- [x] License was changed from ISC to MIT;
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
### 1.1.0 ( 2022-10-21)
|
|
207
|
+
- [x] Method 'debug' was added;
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
|
|
176
212
|
### 1.0.1 ( 2022-08-15)
|
|
177
213
|
- [x] Fix: Event data is coming in Array;
|
|
178
214
|
- [x] Fix: Multiple instances of notice;
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peter.naydenov/notice",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Event emmiter - NOTICE",
|
|
5
5
|
"main": "src/main.js",
|
|
6
|
+
"type": "module",
|
|
6
7
|
"scripts": {
|
|
7
8
|
"test": "mocha test",
|
|
8
|
-
"cover": "
|
|
9
|
+
"cover": "c8 mocha"
|
|
9
10
|
},
|
|
10
11
|
"author": "Peter Naydenov",
|
|
11
12
|
"keywords": [
|
|
@@ -15,13 +16,13 @@
|
|
|
15
16
|
"subscriber",
|
|
16
17
|
"pubsub"
|
|
17
18
|
],
|
|
18
|
-
"license": "
|
|
19
|
+
"license": "MIT",
|
|
19
20
|
"devDependencies": {
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
"
|
|
21
|
+
"c8": "^8.0.0",
|
|
22
|
+
"chai": "4.3.7",
|
|
23
|
+
"mocha": "10.2.0"
|
|
23
24
|
},
|
|
24
|
-
"
|
|
25
|
+
"c8": {
|
|
25
26
|
"include": [
|
|
26
27
|
"src/**/*.js"
|
|
27
28
|
],
|
package/src/main.js
CHANGED
|
@@ -5,6 +5,8 @@ function notice () {
|
|
|
5
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
|
+
, debugFlag = false
|
|
9
|
+
, debugHeader = ''
|
|
8
10
|
;
|
|
9
11
|
function on ( e, fn ) {
|
|
10
12
|
if ( !scroll[e] ) scroll[e] = []
|
|
@@ -15,7 +17,7 @@ function notice () {
|
|
|
15
17
|
scrollOnce[e].push ( fn )
|
|
16
18
|
} // once func.
|
|
17
19
|
function off ( e, fx ) {
|
|
18
|
-
if ( fx ) { // fx is
|
|
20
|
+
if ( fx ) { // fx is optional
|
|
19
21
|
if ( scroll[e] ) scroll[e] = scroll[e].filter ( fn => fn !== fx )
|
|
20
22
|
if ( scrollOnce[e] ) scrollOnce[e] = scrollOnce[e].filter ( fn => fn !== fx )
|
|
21
23
|
if ( scroll[e] && scroll[e].length === 0 ) delete scroll[e]
|
|
@@ -25,8 +27,21 @@ function notice () {
|
|
|
25
27
|
if ( scrollOnce[e] ) delete scrollOnce[e]
|
|
26
28
|
if ( scroll[e] ) delete scroll[e]
|
|
27
29
|
} // off func.
|
|
30
|
+
function debug ( val, header ) {
|
|
31
|
+
debugFlag = val ? true : false
|
|
32
|
+
if ( header && (typeof header === 'string') ) debugHeader = header
|
|
33
|
+
} // debug func.
|
|
28
34
|
function emit () {
|
|
29
35
|
const [ e, ...args ] = arguments
|
|
36
|
+
if ( debugFlag ) {
|
|
37
|
+
console.log ( `${debugHeader} Event "${e}" was triggered.`)
|
|
38
|
+
if ( args.length > 0 ) {
|
|
39
|
+
console.log ( 'Arguments:')
|
|
40
|
+
console.log ( ...args )
|
|
41
|
+
console.log ( '^----' )
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
}
|
|
30
45
|
if ( e == '*' ) { // The wildcard '*' doesn't work for 'once' events
|
|
31
46
|
let evNames = Object.keys ( scroll )
|
|
32
47
|
evNames.forEach ( name => {
|
|
@@ -70,6 +85,7 @@ function notice () {
|
|
|
70
85
|
, emit // Trigger a event
|
|
71
86
|
, stop // Ignore event for a while
|
|
72
87
|
, start // Remove event from ignore list
|
|
88
|
+
, debug
|
|
73
89
|
}
|
|
74
90
|
} // notice fn.
|
|
75
91
|
return new Notice ()
|
|
@@ -77,6 +93,6 @@ function notice () {
|
|
|
77
93
|
|
|
78
94
|
|
|
79
95
|
|
|
80
|
-
|
|
96
|
+
export default notice
|
|
81
97
|
|
|
82
98
|
|
package/test/01-notice.js
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
"use strict"
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
import notice from '../src/main.js'
|
|
4
|
+
import { expect } from 'chai'
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
7
9
|
|
|
8
10
|
describe ( 'Testing Notice', () => {
|
|
9
11
|
|
|
12
|
+
|
|
13
|
+
|
|
10
14
|
it ( 'Standard event', () => {
|
|
11
15
|
const eBus = notice ();
|
|
12
16
|
let result = 0;
|
|
@@ -258,6 +262,7 @@ it ( 'Multiple Notice instances', () => {
|
|
|
258
262
|
}) // it Multiple Notice instances
|
|
259
263
|
|
|
260
264
|
|
|
265
|
+
|
|
261
266
|
}) // define
|
|
262
267
|
|
|
263
268
|
|