Giter VIP home page Giter VIP logo

eventilator's Introduction

Eventilator: but can your event manager do this?

API:

  eventilator(
    once Boolean,
    target EventTarget|String|Array,
    type String|Object<String, Function>,
    =handle Function,
    =options Boolean|Object = false
  )
  • .on/once['any-event-name'] | .on/once(target, event, handle, options) - add listener
  • .on/once(target, { event: handle }) - add listeners
  • .off(target, event, handle) | handle.off - remove listener
  • .emit(target, type, detail) | handle.emit(type, detail) - emit event
  • .curry - extra (used internally but why not share)
  const {on, once} = eventilator

  let count = 0
  const handle = on.anyEvent('div.target', (event, target) => {
    count++
  })

  console.log(handle.target instanceof Element)

  handle.emit('anyEvent')
  handle.off() // same as handle == handle.off
  
  console.log(`
    The handler is currently ${handle.ison ? 'on' : 'off'}.
  `)

  handle.emit('anyEvent')
  handle.on()

  console.log(`
    The handler is currently ${handle.ison ? 'on' : 'off'}.
  `)

  handle.emit('anyEvent')


  setTimeout(() => {
    console.log(`The count should be 2 it is ${count}`)
  }, 5)
  const {on, once} = eventilator

  const handles = once('div.fancy-element', {
    click (event, fancyElement) {},
    keydown (event, fancyElement) {}
  })

  const {
    click: {off: clickOff},
    keydown: {off: keydownOff}
  } = handles

  try {
    await someUnavailableOperation()
  } catch (e) {
    handles.click.off()
  }

Arrays or Selectors finding multiple elements works as well.

const [aHndl, bHndl, cHndl] = on.pointerover([a, b, c], (e, elementX) => {
  // do something
})

aHndl.off()
bHndl.off()
cHndl.off()

// or
const handlers = on.pointerover('main > div.fancy-card', (e, cardDiv) => {
  // do something
})
handlers.off() // <- loops over each and turns'em off

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.