clearall
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

clearall 🧹

Clear all event listeners at once.

npm bundle size license

Install

npm install clearall

Examples

Basic

import add from clearall
 
const clearAll = add(window, 'orientationchange', () => { /* ... */ })
    .add(document, 'mouseup', () => { /* ... */ })
    .add(document, 'mousedown', () => { /* ... */ })
    .add(document, 'mousemove', () => { /* ... */ })
    .add(emitter, 'onMessage', () => { /* ... */ })
 
clearAll()

Disposable Class

class View{
    _init(){
        this._clearEvents = add(window, 'orientationchange', this._onRotate)
            .add(this._el, 'mouseup', this._onMouseUp)
            .add(this._el, 'mousedown', this._onMouseDown)
            .add(this._el, 'mousemove', this._onMouseMove)
            .add(this._emitter, 'onMessage', this._onMessage)
    }
 
    dispose(){
        this._clearEvents()
    }
 
    /* ... */
}

React hook

function App(){
    const ref = useRef()
 
    useEffect(() => {
        const clearAll = add(window, 'orientationchange', () => { /* ... */ })
            .add(ref.current, 'mouseup', () => { /* ... */ })
            .add(ref.current, 'mousedown', () => { /* ... */ })
            .add(ref.current, 'mousemove', () => { /* ... */ })
            .add(emitter, 'onMessage', () => { /* ... */ })
 
        return clearAll
    }, [])
 
    /* ... */
}

API

add(listenable, eventName, listener, ...params)

Add an event listener and return a clearAll() function.

clearAll()

Remove all added event listeners.

clearAll.add(listenable, eventName, listener, ...params)

Add an event listener to the same clearAll context.

Tips

Type Error for window custom event in typescript.

// ❌ error
add(window, 'user_custom_event', () => { /* ... */})

For convenience, there are strong type constraints for some global objects. But sometimes this gets in the way. any can cancel this type constraint.

// ✔️ ok.
add(window as any, 'user_custom_event', () => { /* ... */})

License

MIT

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.1.1
    1
    • latest

Version History

Package Sidebar

Install

npm i clearall

Weekly Downloads

1

Version

1.1.1

License

MIT

Unpacked Size

8.96 kB

Total Files

6

Last publish

Collaborators

  • skt-t1-byungi