osc-listeners: add support for relative address prefixed with the widget's address (#438)

This commit is contained in:
jean-emmanuel 2018-12-29 10:16:13 +01:00
parent a2dffa5d93
commit ff66489eae
4 changed files with 38 additions and 6 deletions

View file

@ -8,6 +8,7 @@
- regression causing osc receivers (`OSC{}`) to conflict with property listeners (`@{}`)
- widgets
- push: add `doubleTap` option
- osc listeners (`OSC{}`) now accept relative addresses (without leading '/'), automatically prefixed with the widget's address
## 0.40.3

View file

@ -56,8 +56,17 @@ OSC{/address, 1}
```
Will return `1` at first and listen for osc messages on address `/address`. Each time a value is received, the property will be updated.
If the leading slash (`/`) is omitted, the address will be prefixed with the widget's `address` property:
```
OSC{color, "auto"}
```
Will listen for osc messages on address `/widget_address/color`.
----
## Formulas: `#{}`
The following syntax allow writing mathematical formulas in widgets' properties:

View file

@ -2,7 +2,9 @@ var osc = require('../../osc')
module.exports = class OscReceiver {
constructor(address, value, parent, propName) {
constructor(options) {
var {prefix, address, value, parent, propName} = options
try {
this.value = JSON.parse(value)
@ -13,6 +15,7 @@ module.exports = class OscReceiver {
this.parent = parent
this.propNames = [propName]
this.bindedCallback = this.callback.bind(this)
this.prefix = ''
this.setAddress(address)
}
@ -21,10 +24,16 @@ module.exports = class OscReceiver {
if (this.address !== address) {
if (this.address) osc.off(this.address, this.bindedCallback, this.parent)
if (this.address) osc.off(this.prefix + this.address, this.bindedCallback, this.parent)
this.address = address
osc.on(this.address, this.bindedCallback, {context: this.parent})
if (address) this.address = address
if (this.address[0] !== '/') {
this.prefix = this.parent.getProp('address') || this.parent.resolveProp('address', undefined, false, this)
if (this.prefix[this.prefix.length - 1] !== '/') this.prefix += '/'
}
osc.on(this.prefix + this.address, this.bindedCallback, {context: this.parent})
}

View file

@ -526,7 +526,12 @@ class Widget extends EventEmitter {
})
if (!this.oscReceivers[address]) {
this.oscReceivers[address] = new OscReceiver(resolvedAddress, value, this, propName)
this.oscReceivers[address] = new OscReceiver({
address: resolvedAddress,
value: value,
parent: this,
propName: propName
})
if (oscReceiverState[address]) {
this.oscReceivers[address].value = oscReceiverState[address]
delete oscReceiverState[address]
@ -711,7 +716,15 @@ class Widget extends EventEmitter {
case 'preArgs':
case 'target':
case 'noSync':
if (propName == 'precision') this.precision = Math.min(20,Math.max(this.getProp('precision', undefined, false),0))
if (propName === 'address') {
for (var k in this.oscReceivers) {
var receiver = this.oscReceivers[k]
if (receiver.prefix !== '') {
receiver.setAddress()
}
}
}
if (propName === 'precision') this.precision = Math.min(20,Math.max(this.getProp('precision', undefined, false),0))
var data = {},
oldData = {
preArgs: propName == 'preArgs' ? oldPropValue : this.getProp('preArgs'),