From 0e0a7106e03f033485e49356b2d2ea17702e236c Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Thu, 12 Apr 2018 14:49:01 +0200 Subject: [PATCH] props: implement @{} syntax nesting (#191) --- .../js/app/widgets/common/osc-receiver.js | 45 +++++++++++------ src/browser/js/app/widgets/common/widget.js | 49 +++++++++++-------- 2 files changed, 60 insertions(+), 34 deletions(-) diff --git a/src/browser/js/app/widgets/common/osc-receiver.js b/src/browser/js/app/widgets/common/osc-receiver.js index bf02f61d..4f31741a 100644 --- a/src/browser/js/app/widgets/common/osc-receiver.js +++ b/src/browser/js/app/widgets/common/osc-receiver.js @@ -6,23 +6,40 @@ module.exports = class OscReceiver { this.value = value this.parent = parent + this.propName = propName + this.bindedCallback = this.callback.bind(this) + this.setAddress(address) - osc.on(address + '.' + parent.hash, (args)=>{ + } - if (typeof args !== 'object') args = [args] - var preArgs = parent.getProp('preArgs') || [] - if (args.length >= preArgs.length) { - for (var i in preArgs) { - if (preArgs[i] !== args[i]) return - } - var val = args.slice(preArgs.length) - if (val.length < 2) val = val[0] - if (val !== this.value) { - this.value = val - parent.updateProps([propName], parent) - } + setAddress(address) { + + if (this.address !== address) { + + if (this.address) osc.off(this.address + '.' + parent.hash, this.bindedCallback) + + this.address = address + osc.on(this.address + '.' + parent.hash, this.bindedCallback) + + } + + } + + callback(args) { + + if (typeof args !== 'object') args = [args] + var preArgs = this.parent.getProp('preArgs') || [] + if (args.length >= preArgs.length) { + for (var i in preArgs) { + if (preArgs[i] !== args[i]) return } - }) + var val = args.slice(preArgs.length) + if (val.length < 2) val = val[0] + if (val !== this.value) { + this.value = val + this.parent.updateProps([this.propName], this.parent) + } + } } diff --git a/src/browser/js/app/widgets/common/widget.js b/src/browser/js/app/widgets/common/widget.js index 171346c2..93f05697 100644 --- a/src/browser/js/app/widgets/common/widget.js +++ b/src/browser/js/app/widgets/common/widget.js @@ -263,27 +263,12 @@ class Widget extends EventEmitter { if (typeof propValue == 'string') { - propValue = propValue.replace(/OSC\{([^\}]+)\}/g, (m)=>{ - let [address, value] = m.substr(4, m.length - 5).split(',') + propValue = propValue.replace(/@\{[^\{]*?(@\{.*?\})?[^\{]*?\}/g, (m, nested)=>{ - address = address.trim() - value = value.trim() + if (nested) { + m = m.replace(nested, this.resolveProp(propName, nested, false, this)) + } - if (!this.oscReceivers[address]) this.oscReceivers[address] = new OscReceiver(address, value, this, propName) - - var r = this.oscReceivers[address].value - r = typeof r != 'string' ? JSON.stringify(r) : r - - var varname = 'VAR_' + varnumber - varnumber++ - - variables[varname] = r - mathscope[varname] = r - - return varname - }) - - propValue = propValue.replace(/@\{([^\}]+)\}/g, (m)=>{ let id = m.substr(2, m.length - 3).split('.'), k, subk @@ -368,6 +353,30 @@ class Widget extends EventEmitter { }) + propValue = propValue.replace(/OSC\{([^\}]+)\}/g, (m)=>{ + let [address, value] = m.substr(4, m.length - 5).split(',').map(x => x.trim()), + resolvedAddress = address.replace(/VAR_[0-9]+/g, (m)=>{ + return typeof variables[m] === 'string' ? variables[m] : JSON.stringify(variables[m]) + }) + + if (!this.oscReceivers[address]) { + this.oscReceivers[address] = new OscReceiver(resolvedAddress, value, this, propName) + } else { + this.oscReceivers[address].setAddress(resolvedAddress) + } + + var r = this.oscReceivers[address].value + r = typeof r != 'string' ? JSON.stringify(r) : r + + var varname = 'VAR_' + varnumber + varnumber++ + + variables[varname] = r + mathscope[varname] = r + + return varname + }) + try { propValue = propValue.replace(/#\{(?:[^\}\\]|\\.)+\}/g, (m)=>{ @@ -395,7 +404,7 @@ class Widget extends EventEmitter { } catch (err) {} for (var k in variables) { - var v = typeof variables[k] == 'string' ? variables[k] : JSON.stringify(variables[k]) + var v = typeof variables[k] === 'string' ? variables[k] : JSON.stringify(variables[k]) propValue = propValue.replace(new RegExp(k, 'g'), v) }