All files main.js

90.59% Statements 106/117
94.23% Branches 49/52
90.9% Functions 10/11
90.59% Lines 106/117

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 1201x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 32x 32x 24x 24x 9x 9x 9x 24x 24x 7x 5x 5x 5x 5x 5x 5x 7x 7x 24x 24x 1x 1x 1x 24x 24x     24x 24x 40x 40x                 40x 40x 30x 30x 30x 23x 25x 25x 2x 2x 2x 2x   23x 30x 40x 40x 40x 3x 3x 3x 3x 40x 6x 5x 5x 5x 40x 21x 21x 24x 24x 3x 1x 1x 1x 2x 24x 24x 4x 1x 1x 1x 1x 1x 1x 1x 3x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 1x 1x 1x 1x      
"use strict"
function notice () {
    function Notice () {
                    let 
                          scroll     = {'*':[]}  // General events with their subscribers
                        , scrollOnce = {}  // Single events with their subscribers
                        , ignore     = []  // Ignore event names ( general and single )
                        , debugFlag  = false 
                        , debugHeader = ''
                        ;
                    function on ( e, fn ) {
                            if ( !scroll[e] ) scroll[e] = []
                            scroll[e].push ( fn )
                        } // on func.
                    function once ( e, fn ) {
                            if ( e === '*' )   return  // The wildcard '*' doesn't work for 'once' events
                            if ( !scrollOnce[e] )   scrollOnce[e] = []
                            scrollOnce[e].push ( fn )
                        } // once func.
                    function off ( e, fx ) {
                            if ( fx ) {   // fx is optional
                                    if ( scroll[e]     )  scroll[e]     = scroll[e].filter     ( fn => fn !== fx )
                                    if ( scrollOnce[e] )  scrollOnce[e] = scrollOnce[e].filter ( fn => fn !== fx )
                                    if ( scroll[e] && scroll[e].length         === 0 )   delete scroll[e]
                                    if ( scrollOnce[e] && scrollOnce[e].length === 0 )   delete scroll[e]
                                    return
                                }
                            if ( scrollOnce[e] )   delete scrollOnce[e]
                            if ( scroll[e]     )   delete scroll[e]
                        } // off func.
                    function reset () {
                            scroll     = {'*':[]}
                            scrollOnce = {}
                            ignore     = []
                        } // reset func.
                    function debug ( val, header ) {
                            debugFlag =  val ? true : false
                            if ( header && (typeof header === 'string') )   debugHeader = header
                        } // debug func.
                    function emit () {
                            const [ e, ...args ] = arguments
                            if ( debugFlag ) {  
                                        console.log ( `${debugHeader} Event "${e}" was triggered.`)
                                        if ( args.length > 0 ) {
                                            console.log ( 'Arguments:')
                                            console.log ( ...args )
                                            console.log ( '^----' )
                                        }
                                        
                                }
 
                            function exeCallback ( name ) {
                                        let stopped = false;
                                        if ( name === '*' )   return    
                                        if ( ignore.includes(name) )   return
                                        scroll[name].every ( fn => {
                                                            const r = fn ( ...args );
                                                            if ( typeof(r) !== 'string'     )   return true
                                                            if ( r.toUpperCase() === 'STOP' ) {  
                                                                                                stopped = true
                                                                                                return false
                                                                                    }
                                                            return true
                                                        })
                                        if ( !stopped )   scroll['*'].forEach ( fn => fn(e,...args)   )
                                } // exeCallback func.
 
                            if ( e === '*' ) {   // The wildcard '*' doesn't work for 'once' events
                                        let evNames = Object.keys ( scroll )
                                        evNames.forEach ( name => exeCallback(name)   )
                                        return
                                }
                            if ( scrollOnce[e] ) {
                                        if ( ignore.includes(e) )   return
                                        scrollOnce[e].forEach ( fn => fn(...args)   )
                                        delete scrollOnce[e]
                                }
                            if ( scroll[e]     ) { 
                                        exeCallback ( e )
                                }
                        } // emit func.
                    function start ( e ) {
                            if ( e === '*' ) {  
                                        ignore = []
                                        return
                                }
                            ignore = ignore.filter ( type => e != type )
                        } // start func.
                    function stop ( e ) {
                            if ( e === '*' ) {
                                        const 
                                              evNames     = Object.keys ( scroll )
                                            , evOnceNames = Object.keys ( scrollOnce )
                                            ;
                                        ignore = [ ...evOnceNames, ...evNames ]
                                        return
                                }
                            ignore.push ( e )
                        } // stop func.
 
                    return {
                                  on    // Register a event
                                , once  // Register a single event 
                                , off   // Unregister regular and single events
                                , reset // Unregister all events
                                , emit  // Trigger a event
                                , stop  // Ignore event for a while
                                , start // Remove event from ignore list
                                , debug
                        }
        } // notice fn.
    return Notice ()
}
 
 
 
export default notice