events: remove wildcard, use widget's hash as context only when adding/removing events

This commit is contained in:
jean-emmanuel 2018-10-16 10:50:26 +02:00
parent e0558ae14d
commit a394dc6dad
10 changed files with 63 additions and 76 deletions

View file

@ -11,6 +11,7 @@ module.exports = class EventEmitter extends WolfyEventEmitter {
super()
this._customBindings = {}
this._contextEvents = {}
for (var evt in customEvents) {
this._customBindings[evt] = {
@ -34,46 +35,25 @@ module.exports = class EventEmitter extends WolfyEventEmitter {
}
getListeners(evt) {
if (!(evt instanceof RegExp) && evt.indexOf('.*') > -1) {
var events = this._getEvents(),
evtName = evt.substr(0, evt.indexOf('.')),
evtNameCheck = evtName + '.',
response = {}
for (var key in events) {
if (key === evtName || key.indexOf(evtNameCheck) > -1) {
response[key] = events[key]
}
}
return response
} else {
return super.getListeners(evt)
}
}
addListener(evt, listener, options) {
// Custom event setup
var eventName = typeof evt == 'string' ? evt.split('.')[0] : ''
if (
customEvents.hasOwnProperty(eventName) &&
typeof customEvents[eventName].setup === 'function'
customEvents.hasOwnProperty(evt) &&
typeof customEvents[evt].setup === 'function'
) {
if (this._customBindings[eventName].bindings === 0) {
this._customBindings[eventName].options = options
customEvents[eventName].setup.call(this, options)
if (this._customBindings[evt].bindings === 0) {
this._customBindings[evt].options = options
customEvents[evt].setup.call(this, options)
}
this._customBindings[eventName].bindings += 1
this._customBindings[evt].bindings += 1
}
if (options && options.context) {
var hash = options.context.hash
if (!this._contextEvents[hash]) this._contextEvents[hash] = []
this._contextEvents[hash].push([evt, listener])
}
super.addListener(evt, listener)
@ -86,17 +66,15 @@ module.exports = class EventEmitter extends WolfyEventEmitter {
// Custom event teardown
var eventName = typeof evt == 'string' ? evt.split('.')[0] : ''
if (
customEvents.hasOwnProperty(eventName) &&
typeof customEvents[eventName].teardown === 'function' &&
this._customBindings[eventName].bindings !== 0
customEvents.hasOwnProperty(evt) &&
typeof customEvents[evt].teardown === 'function' &&
this._customBindings[evt].bindings !== 0
) {
this._customBindings[eventName].bindings -= 1
if (this._customBindings[eventName].bindings === 0 || !listener) {
var options = this._customBindings[eventName].options
customEvents[eventName].teardown.call(this, options)
this._customBindings[evt].bindings -= 1
if (this._customBindings[evt].bindings === 0 || !listener) {
var options = this._customBindings[evt].options
customEvents[evt].teardown.call(this, options)
}
}
@ -114,6 +92,18 @@ module.exports = class EventEmitter extends WolfyEventEmitter {
return this
}
removeEventContext(context) {
var events = this._contextEvents[context.hash]
if (events) {
for (var i = 0; i < events.length; i++) {
this.removeListener(events[i][0], events[i][1])
}
delete this._contextEvents[context.hash]
}
}

View file

@ -63,7 +63,7 @@ var SessionManager = class SessionManager {
for (var h in widgetManager.widgets) {
if (widgetManager.widgets[h].value !== undefined) {
widgetManager.trigger('change.*', [{
widgetManager.trigger('change', [{
widget: widgetManager.widgets[h],
id: widgetManager.widgets[h].getProp('id'),
linkId: widgetManager.widgets[h].getProp('linkId'),

View file

@ -174,7 +174,7 @@ var WidgetManager = class WidgetManager extends EventEmitter {
hash:hash
})
widget.trigger('widget-removed.*', [{widget: widget}])
widget.trigger('widget-removed', [{widget: widget}])
}
if (id && this.idRoute[id].indexOf(hash) != -1) this.idRoute[id].splice(this.idRoute[id].indexOf(hash), 1)

View file

@ -49,7 +49,7 @@ var Osc = class Osc extends EventEmitter {
}
}
this.trigger(data.address + '.*', [data.args])
this.trigger(data.address, [data.args])
}

View file

@ -8,7 +8,7 @@ class Container extends Widget {
super(options)
this.on(`widget-created.${this.hash}`, (e)=>{
this.on('widget-created', (e)=>{
if (e.widget.parent === this) {
this.children.push(e.widget)
@ -16,7 +16,7 @@ class Container extends Widget {
})
this.on(`widget-removed.${this.hash}`, (e)=>{
this.on('widget-removed', (e)=>{
if (e.widget.parent === this) {
this.children.splice(this.children.indexOf(e.widget), 1)
@ -28,7 +28,6 @@ class Container extends Widget {
onRemove() {
widgetManager.off(`widget-removed.${this.hash}`)
super.onRemove()
}

View file

@ -21,10 +21,10 @@ module.exports = class OscReceiver {
if (this.address !== address) {
if (this.address) osc.off(this.address + '.' + this.parent.hash, this.bindedCallback)
if (this.address) osc.off(this.address, this.bindedCallback)
this.address = address
osc.on(this.address + '.' + this.parent.hash, this.bindedCallback)
osc.on(this.address, this.bindedCallback, {context: this.parent})
}

View file

@ -173,36 +173,36 @@ class Widget extends EventEmitter {
if (Object.keys(this.linkedProps).length) {
widgetManager.on(`widget-created.${this.hash}`, (e)=>{
widgetManager.on('widget-created', (e)=>{
var {id, widget, options} = e
if (widget == this.parent) return
if (widget == this) id = 'this'
if (this.linkedProps[id]) {
this.updateProps(this.linkedProps[id], widget, options)
}
})
}, {context: this})
widgetManager.on(`prop-changed.${this.hash}`, (e)=>{
widgetManager.on('prop-changed', (e)=>{
let {id, widget, options} = e
if (widget == this) id = 'this'
if (widget == this.parent) id = 'parent'
if (this.linkedProps[id]) {
this.updateProps(this.linkedProps[id], widget, options, e.props)
}
})
}, {context: this})
}
if (Object.keys(this.linkedPropsValue).length) {
widgetManager.on(`change.${this.hash}`, (e)=>{
widgetManager.on('change', (e)=>{
var {id, widget, options} = e
if (widget == this) id = 'this'
if (widget == this.parent) id = 'parent'
if (this.linkedPropsValue[id]) {
this.updateProps(this.linkedPropsValue[id], widget, options, ['value'])
}
})
}, {context: this})
}
@ -260,7 +260,7 @@ class Widget extends EventEmitter {
created() {
this.trigger('widget-created.*', [{
this.trigger('widget-created', [{
id: this.getProp('id'),
widget: this,
options: this.reCreateOptions
@ -270,7 +270,7 @@ class Widget extends EventEmitter {
changed(options) {
this.trigger('change.*', [{
this.trigger('change', [{
widget: this,
options: options,
id: this.getProp('id'),
@ -550,7 +550,7 @@ class Widget extends EventEmitter {
this.onPropChanged(changedProps[i].propName, options, changedProps[i].oldPropValue)
}
widgetManager.trigger('prop-changed.*', [{
widgetManager.trigger('prop-changed', [{
id: this.getProp('id'),
props: changedProps,
widget: this,
@ -712,13 +712,13 @@ class Widget extends EventEmitter {
}
onRemove(){
widgetManager.off(`widget-created.${this.hash}`)
widgetManager.off(`prop-changed.${this.hash}`)
widgetManager.off(`change.${this.hash}`)
osc.off(new RegExp('.*\\.' + this.hash))
widgetManager.removeEventContext(this)
osc.removeEventContext(this)
for (var i in this.oscReceivers) {
oscReceiverState[i] = this.oscReceivers[i].value
}
}
}

View file

@ -41,7 +41,7 @@ class Clone extends Container {
if (this.cloneTarget) this.createClone()
// global listenner to catch cloneTarget's creation if no target is locked
widgetManager.on(`widget-created.${this.hash}`, (e)=>{
widgetManager.on('widget-created', (e)=>{
if (this.cloneTarget === null) {
@ -57,7 +57,7 @@ class Clone extends Container {
}
})
}, {context: this})
}
@ -124,8 +124,7 @@ class Clone extends Container {
// clear event listeners on target
if (this.cloneTarget) {
this.cloneTarget.off(`widget-removed.${this.hash}`)
this.cloneTarget.off(`widget-created.${this.hash}`)
this.cloneTarget.removeEventContext(this)
}
}
@ -149,7 +148,7 @@ class Clone extends Container {
var clone = parser.parse([{...clonedProps, ...this.getProp('props')}], this.widget, this)
if (clone.getProp('id') === this.cloneTarget.getProp('id')) {
widgetManager.trigger('change.*', [{
widgetManager.trigger('change', [{
widget: this.cloneTarget,
id: this.cloneTarget.getProp('id'),
linkId: this.cloneTarget.getProp('linkId'),
@ -164,15 +163,15 @@ class Clone extends Container {
// listen for cloneTarget's deletion
// if it is just edited, its recreation will be catched by the global 'widget-created' event handler
this.cloneTarget.on(`widget-removed.${this.hash}`, (e)=>{
this.cloneTarget.on('widget-removed', (e)=>{
if (this.cloneTarget === e.widget) {
this.cloneTarget = null
this.cleanClone()
}
})
}, {context: this})
// listen for cloneTarget's content updates
this.cloneTarget.on(`widget-created.${this.hash}`, (e)=>{
this.cloneTarget.on('widget-created', (e)=>{
var {widget} = e,
parent = widget.parent
@ -187,7 +186,7 @@ class Clone extends Container {
this.createClone()
resize.check(this.container)
})
}, {context: this})
this.cloneLock = false
@ -213,8 +212,7 @@ class Clone extends Container {
onRemove(){
if (this.cloneTarget) {
this.cloneTarget.off(`widget-removed.${this.hash}`)
this.cloneTarget.off(`widget-created.${this.hash}`)
this.cloneTarget.removeEventContext(this)
}
super.onRemove()

View file

@ -84,7 +84,7 @@ module.exports = class Formula extends Widget {
this.conditionState = true
widgetManager.on(`change.${this.hash}`,this.syncHandle.bind(this))
widgetManager.on('change', this.syncHandle.bind(this))
if (this.formulaString.length) this.updateValue({options:{}})

View file

@ -18,7 +18,7 @@ module.exports = class _switchers_base extends Widget {
}
widgetManager.on(`change.${this.hash}`,this.syncHandler.bind(this))
widgetManager.on('change', this.syncHandler.bind(this))
}