Provides for phased event dispatching and state binding.
npm install @takram/planck-event
EventDispatcher
manages event listeners and dispatches events in similar ways as browser’s EventTarget
or node’s EventEmitter
do so.
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()
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' }
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'
#
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)
Inherits from Event
# eventTarget.dispatchEvent(event [, propagationPath])
# eventTarget.dispatchImmediateEvent(event)
# eventTarget.determinePropagationPath([target])
# eventTarget.ancestorEventTarget
# eventTarget.descendantEventTarget
#
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)
#
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
Inherits from Event
#
new StateEvent(options)
#
stateEvent.init(options)
name
value
# stateEvent.name
# stateEvent.value
# StateEvent.type(name)
Inherits from Event
#
new EventBundle(options)
#
eventBundle.init(options)
originalEvent
# eventBundle.originalEvent
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
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
Inherits from EventBundle
#
new TouchEvent(options)
#
touchEvent.init(options)
touches
changedTouches
# touchEvent.touches
# touchEvent.changedTouches
# touchEvent.ctrlKey
# touchEvent.shiftKey
# touchEvent.altKey
# touchEvent.metaKey
Inherits from EventBundle
#
new WheelEvent(options)
#
wheelEvent.init(options)
# wheelEvent.deltaX
# wheelEvent.deltaY
# wheelEvent.deltaZ
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.