Skip to content

subversivo58/Emitter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Emitter

Browser EventEmitter from EventTarget (constructor)

Motivation:

Since I know Node I use events, the search for libraries for the front end has always been a terrible saga.

When I "stumbled" on this issue in StackOverflow I decided to try to create something useful myself ... whether or not it is "useful" (for you) you can draw your own conclusions.

API:

Instance:

// import module ... yep, ES6 module sintax
import Emitter from './your-path-to/Emitter.mjs'

/**
 * or global for non-module support ... property of `window`
 * <script src="./your-path-to/Emitter.js"></script>
 * <script>
 *     const CustomEmitter = new Emitter() // const CustomEmitter = new window.Emitter()
 * </script>
 */

const CustomEmitter = new Emitter()

Listener:

.on(event, Fn [,once])

CustomEmitter.on('xyz', data => {
    console.log(data)
})

Listener to an or more event(s) ... properties of .on():

  • event: String event target name
  • Fn: Function callback for get data
  • once: Boolean optional alias to .once() method ... default: false

.once(event, Fn)

CustomEmitter.once('abc', data => {
    console.log(data)
})

Watch an event only once ... properties of .once():

  • event: String event target name
  • Fn: Function callback for get data

Fire:

.emit(event, data)

CustomEmitter.emit('abc', 'hello world')
  • event: String event target name
  • data: Object|Array|String arbitrary data to pass an event

Stop:

.off(event [,Fn])

CustomEmitter.off('xyz')
  • event: String event target name
  • Fn: Function optional ... reference to remove especific callback

Wildcard:

.on('*', Fn)

CustomEmitter.on('*', data => {
    console.log(data)
})

Propeties of .on('*'):

  • '*': String wildacard for all events
  • Fn: Function callback for get data

Use '*' for listener all events ... see #3 for more details

Examples:

See exemple directory

To do:

  • allow multiple "callbacks" references to .off() method
  • return the content without having to go through the CustomEvent.detail property. See #6
  • add "wildcard" (*) to listen all events. See #3

Supported Browsers:

In this date (October 7, 2018) Chrome, Firefox, Opera and WebView (Android) according to the documentation in Mozilla

  • desktop:
    • Chrome 64
    • Firefox 59
    • Opera 51
  • mobile:
    • WebView 64
    • Chrome Android 64
    • Firefox Android 59
    • Opera Android 51

License

MIT License

Copyright (c) 2018 - 2020 Lauro Moraes https://github.com/subversivo58

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

About

Browser EventEmitter from EventTarget (constructor)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published