npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@takram/planck-event

v0.6.0

Published

Provides for phased event dispatching and state binding

Downloads

23

Readme

Planck Event

Provides for phased event dispatching and state binding.

License npm version JavaScript Style Guide Build Status Sauce Test Status

Sauce Test Status

Getting Started

Installing

npm install @takram/planck-event

Usage

Event Dispatching

EventDispatcher manages event listeners and dispatches events in similar ways as browser’s EventTarget or node’s EventEmitter do so.

Example

import { EventDispatcher } from '@takram/planck-event'

class App extends EventDispatcher {
  constructor(name) {
    super()
    this.name = name
  }

  launch() {
    this.dispatchEvent({ type: 'launch' })
  }

  terminate() {
    this.dispatchEvent({ type: 'terminate', on: Date.now() })
  }
}

const app = new App('test')
app.addEventListener('launch', event => {
  console.log(`App (${event.target.name}) launched`)
})
app.addEventListener('terminate', event => {
  console.log(`App (${event.target.name}) terminated on`, event.on)
})

launch()
terminate()

Event Bubbling and Capturing

Example

import { EventTarget, Event } from '@takram/planck-event'

const target1 = new EventTarget()
target1.name = 'target1'
const target2 = new EventTarget()
target2.name = 'target2'
const target3 = new EventTarget()
target3.name = 'target3'

const listener = event => {
  console.log(event.target.name, event.currentTarget.name, event.eventPhase)
}

const propagationPath = [target1, target2, target3]

target1.addEventListener('type', listener, false)
target2.addEventListener('type', listener, false)
target3.addEventListener('type', listener, false)
target1.addEventListener('type', listener, true)
target2.addEventListener('type', listener, true)
target3.addEventListener('type', listener, true)

target1.dispatchEvent(new Event({
  type: 'type',
  bubbles: true,
  captures: true,
}), propagationPath)

// { target: 'target3', currentTarget: 'target1', eventPhase: 'capture' }
// { target: 'target3', currentTarget: 'target2', eventPhase: 'capture' }
// { target: 'target3', currentTarget: 'target3', eventPhase: 'target' }
// { target: 'target3', currentTarget: 'target3', eventPhase: 'target' }
// { target: 'target3', currentTarget: 'target2', eventPhase: 'bubble' }
// { target: 'target3', currentTarget: 'target1', eventPhase: 'bubble' }

target1.dispatchEvent(new Event({
  type: 'type',
  bubbles: false,
  captures: true,
}), propagationPath)

// { target: 'target3', currentTarget: 'target1', eventPhase: 'capture' }
// { target: 'target3', currentTarget: 'target2', eventPhase: 'capture' }
// { target: 'target3', currentTarget: 'target3', eventPhase: 'target' }
// { target: 'target3', currentTarget: 'target3', eventPhase: 'target' }

target1.dispatchEvent(new Event({
  type: 'type',
  bubbles: true,
  captures: false,
}), propagationPath)

// { target: 'target3', currentTarget: 'target3', eventPhase: 'target' }
// { target: 'target3', currentTarget: 'target3', eventPhase: 'target' }
// { target: 'target3', currentTarget: 'target2', eventPhase: 'bubble' }
// { target: 'target3', currentTarget: 'target1', eventPhase: 'bubble' }

State Binding

Example

1 to 1, one-way binding

import { EventDispatcher, StateEvent } from '@takram/planck-event'

const source = new EventDispatcher()
const target = {}

// 1 to 1 binding defaults to both-ways binding
Binding.bind(source, 'state', target, 'state', { oneWay: true })

source.state = 'loading'
source.dispatchEvent(new StateEvent({ name: 'state', value: source.state }))
console.log(source.state)  // 'loading'
console.log(target.state)  // 'loading'

1 to 1, both-ways binding

import { EventDispatcher, StateEvent } from '@takram/planck-event'

const source = new EventDispatcher()
const target = new EventDispatcher()

Binding.bind(source, 'state', target, 'state')

source.state = 'loading'
source.dispatchEvent(new StateEvent({ name: 'state', value: source.state }))
console.log(source.state)  // 'loading'
console.log(target.state)  // 'loading'

target.state = 'processing'
target.dispatchEvent(new StateEvent({ name: 'state', value: target.state }))
console.log(source.state)  // 'processing'
console.log(target.state)  // 'processing'

1 to 1, both-ways binding with transformation

import { EventDispatcher, StateEvent } from '@takram/planck-event'

const source = new EventDispatcher()
const target = new EventDispatcher()

Binding.bind(source, 'state', target, 'state', {
  transform: value => value.toUppercase(),
  inverseTransform: value => value.toLowercase(),
})

source.state = 'loading'
source.dispatchEvent(new StateEvent({ name: 'state', value: source.state }))
console.log(source.state)  // 'loading'
console.log(target.state)  // 'LOADING'

target.state = 'PROCESSING'
target.dispatchEvent(new StateEvent({ name: 'state', value: target.state }))
console.log(source.state)  // 'processing'
console.log(target.state)  // 'PROCESSING'

1 to multiple, one-way binding

import { EventDispatcher, StateEvent } from '@takram/planck-event'

const source = new EventDispatcher()
const target1 = {}
const target2 = {}

// 1 to multiple binding defaults to one-way binding
Binding.bind(source, 'state', [
  target1, 'state',
  target2, 'state',
])

source.state = 'loading'
source.dispatchEvent(new StateEvent({ name: 'state', value: source.state }))
console.log(source.state)  // 'loading'
console.log(target1.state)  // 'loading'
console.log(target2.state)  // 'loading'

1 to multiple, both-ways binding

import { EventDispatcher, StateEvent } from '@takram/planck-event'

const source = new EventDispatcher()
const target1 = new EventDispatcher()
const target2 = new EventDispatcher()

Binding.bind(source, 'state', [
  target1, 'state',
  target2, 'state',
], {
  oneWay: false,
})

source.state = 'loading'
source.dispatchEvent(new StateEvent({ name: 'state', value: source.state }))
console.log(source.state)  // 'loading'
console.log(target1.state)  // 'loading'
console.log(target2.state)  // 'loading'

target1.state = 'finished'
target1.dispatchEvent(new StateEvent({ name: 'state', value: target1.state }))
// This may seem tricky. The state of target1 propagates to the source first,
// and then the source's change propagates to target2, thus all of the states
// become 'finished'.
console.log(source.state)  // 'finished'
console.log(target1.state)  // 'finished'
console.log(target2.state)  // 'finished'

API Reference

EventDispatcher

# eventDispatcher.addEventListener(type, listener [, capture]) # eventDispatcher.on(type, listener [, capture])

# eventDispatcher.removeEventListener(type, listener [, capture]) # eventDispatcher.off(type, listener [, capture])

# eventDispatcher.once(type, listener [, capture])

# eventDispatcher.dispatchEvent(event)

EventTarget

Inherits from Event

# eventTarget.dispatchEvent(event [, propagationPath])

# eventTarget.dispatchImmediateEvent(event)

# eventTarget.determinePropagationPath([target])

# eventTarget.ancestorEventTarget

# eventTarget.descendantEventTarget

Binding

# Binding.bind(source, sourceState, targets) # Binding.bind(source, sourceState, target1, targetState1 [, target2, targetState2, ...])

# Binding.unbind(source, sourceState, targets) # Binding.unbind(source, sourceState, target1, targetState1 [, target2, targetState2, ...])

# Binding.unbindAll(source, sourceState)

Event

# new Event(options) # event.init(options)

  • type
  • captures
  • bubbles
  • cancelable

# event.stopPropagation()

# event.stopImmediatePropagation()

# event.preventDefault()

# event.type

# event.target

# event.currentTarget

# event.eventPhase

# event.captures

# event.bubbles

# event.cancelable

# event.timeStamp

# event.propagationStopped

# event.immediatePropagationStopped

# event.defaultPrevented

StateEvent

Inherits from Event

# new StateEvent(options) # stateEvent.init(options)

  • name
  • value

# stateEvent.name

# stateEvent.value

# StateEvent.type(name)

EventBundle

Inherits from Event

# new EventBundle(options) # eventBundle.init(options)

  • originalEvent

# eventBundle.originalEvent

MouseEvent

Inherits from EventBundle

# new MouseEvent(options) # mouseEvent.init(options)

  • x
  • y
  • movementX
  • movementY

# mouseEvent.x

# mouseEvent.y

# mouseEvent.movementX

# mouseEvent.movementY

# mouseEvent.button

# mouseEvent.ctrlKey

# mouseEvent.shiftKey

# mouseEvent.altKey

# mouseEvent.metaKey

KeyboardEvent

Inherits from EventBundle

# new KeyboardEvent(options) # keyboardEvent.init(options)

# keyboardEvent.key

# keyboardEvent.code

# keyboardEvent.ctrlKey

# keyboardEvent.shiftKey

# keyboardEvent.altKey

# keyboardEvent.metaKey

# keyboardEvent.repeat

# keyboardEvent.location

TouchEvent

Inherits from EventBundle

# new TouchEvent(options) # touchEvent.init(options)

  • touches
  • changedTouches

# touchEvent.touches

# touchEvent.changedTouches

# touchEvent.ctrlKey

# touchEvent.shiftKey

# touchEvent.altKey

# touchEvent.metaKey

WheelEvent

Inherits from EventBundle

# new WheelEvent(options) # wheelEvent.init(options)

# wheelEvent.deltaX

# wheelEvent.deltaY

# wheelEvent.deltaZ

License

The MIT License

Copyright (C) 2016-Present Shota Matsuda

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.