@peter.naydenov/notice 1.0.2 → 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 +22 -1
  2. package/package.json +1 -1
  3. package/src/main.js +17 -1
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}!` ) ) // 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' )
@@ -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.2",
3
+ "version": "1.1.0",
4
4
  "description": "Event emmiter - NOTICE",
5
5
  "main": "src/main.js",
6
6
  "scripts": {
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 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 ()