@peter.naydenov/notice 1.0.1 → 1.1.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.
Files changed (3) hide show
  1. package/README.md +29 -8
  2. package/package.json +2 -2
  3. package/src/main.js +22 -6
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
 
@@ -48,7 +49,7 @@ Register a regular event.
48
49
  ```js
49
50
  const eBus = notice ();
50
51
 
51
- eBus.on ( 'start', ([name]) => console.log ( `Hey, ${name}!` ) ) // notice that data params from emit are coming as array
52
+ eBus.on ( 'start', name => console.log ( `Hey, ${name}!` ) )
52
53
  eBus.emit ( 'start', 'Johny' )
53
54
  // ---> Hey, Johny!
54
55
  eBus.emit ( 'start', 'Vessy' )
@@ -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}!` ) ) // notice that data params from emit is coming as array
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' )
@@ -114,8 +115,8 @@ Trigger the event and execute all subscribed functions.
114
115
  let result = 0;
115
116
  const
116
117
  eBus = notice ()
117
- , fn1 = ([x]) => result += 1 + x
118
- , fn2 = ([x]) => result += 3 + x
118
+ , fn1 = x => result += 1 + x
119
+ , fn2 = x => result += 3 + x
119
120
  ;
120
121
  eBus.on ( 'go', fn1 )
121
122
  eBus.on ( 'go', fn2 )
@@ -135,8 +136,8 @@ Disable specified event.
135
136
  let result = 0;
136
137
  const
137
138
  eBus = notice ()
138
- , fn1 = ([x]) => result += 1 + x
139
- , fn2 = ([x]) => result += 3 + x
139
+ , fn1 = x => result += 1 + x
140
+ , fn2 = x => result += 3 + x
140
141
  ;
141
142
  eBus.on ( 'go', fn1 )
142
143
  eBus.on ( 'go', fn2 )
@@ -157,8 +158,8 @@ Enable again specified event.
157
158
  let result = 0;
158
159
  const
159
160
  eBus = notice ()
160
- , fn1 = ([x]) => result += 1 + x
161
- , fn2 = ([x]) => result += 3 + x
161
+ , fn1 = x => result += 1 + x
162
+ , fn2 = x => result += 3 + x
162
163
  ;
163
164
  eBus.on ( 'go', fn1 )
164
165
  eBus.on ( 'go', fn2 )
@@ -171,8 +172,28 @@ const
171
172
  ```
172
173
 
173
174
 
175
+ ### Notice.debug ( state, label )
176
+ Provide debug message on each event. By default is debug is 'off'.
177
+
178
+ ```js
179
+ // Turn debug "on"
180
+ eBus.debug ( true )
181
+
182
+ // Turn debug "off"
183
+ eBus.debug ( false )
184
+
185
+ // Activate debugger and set a debug message prefix
186
+ eBus.debug ( true, '[eBus]:' )
187
+ eBus.emit ( 'dummy' )
188
+ // --> [eBus]: Event "dummy" was triggered.
189
+ ```
190
+
191
+
174
192
  ## Release History
175
193
 
194
+ ### 1.1.0 ( 2022-10-21)
195
+ - [x] Method 'debug' was added;
196
+
176
197
  ### 1.0.1 ( 2022-08-15)
177
198
  - [x] Fix: Event data is coming in Array;
178
199
  - [x] Fix: Multiple instances of notice;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peter.naydenov/notice",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "Event emmiter - NOTICE",
5
5
  "main": "src/main.js",
6
6
  "scripts": {
@@ -18,7 +18,7 @@
18
18
  "license": "ISC",
19
19
  "devDependencies": {
20
20
  "chai": "4.3.6",
21
- "mocha": "9.2.1",
21
+ "mocha": "10.0.0",
22
22
  "nyc": "15.1.0"
23
23
  },
24
24
  "nyc": {
package/src/main.js CHANGED
@@ -1,11 +1,13 @@
1
1
  "use strict"
2
2
  function notice () {
3
- let
4
- scroll = {} // General events with their subscribers
5
- , scrollOnce = {} // Single events with their subscribers
6
- , ignore = [] // Ignore event names ( general and single )
7
- ;
8
3
  function Notice () {
4
+ let
5
+ scroll = {} // General events with their subscribers
6
+ , scrollOnce = {} // Single events with their subscribers
7
+ , ignore = [] // Ignore event names ( general and single )
8
+ , debugFlag = false
9
+ , debugHeader = ''
10
+ ;
9
11
  function on ( e, fn ) {
10
12
  if ( !scroll[e] ) scroll[e] = []
11
13
  scroll[e].push ( fn )
@@ -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 optional
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 ()