linkId: add support for master/slave modes with >> and << prefixes (#251)

This commit is contained in:
jean-emmanuel 2018-05-04 17:36:07 +02:00
parent 3c770a9a3d
commit 47340cb5eb
3 changed files with 17 additions and 7 deletions

View file

@ -3,7 +3,8 @@
## next
- widgets
- `target` properties are no longer coerced to arrays and can be simple strings when there's only one value to set
- `target` and `preArgs` properties are no longer coerced to arrays and can be written as single string values
- `linkId` supports master and slave mode with `>>` and `>>` prefixes
## 0.29.2

View file

@ -16,8 +16,10 @@ All widgets share a set of generic properties described below.
### `linkId`
- type: `string|array`
- default: `empty`
- usage: widgets sharing the same `linkId` update each other's value(s) AND send their respective osc messages.
- usage:
- widgets sharing the same `linkId` update each other's value(s) AND send their respective osc messages.
- when prefixed with `>>`, the linkId will make the widget act as a master (sending but not receiving)
- when prefixed with `<<`, the linkId will make the widget act as a slave (receiving but not sending)
## Style

View file

@ -29,8 +29,14 @@ var WidgetManager = class WidgetManager extends EventEmitter {
onChange(e) {
var {id, widget, linkId, options} = e,
widgetsById = this.getWidgetById(id),
var {id, widget, linkId, options} = e
if (linkId) {
if (!Array.isArray(linkId)) linkId = [linkId]
linkId = linkId.map(x => x.replace(/^>>\s*/, '')).filter(x => x.indexOf('<<') < 0)
}
var widgetsById = this.getWidgetById(id),
widgetsByLinkId = linkId ? this.getWidgetByLinkId(linkId) : []
// Widget that share the same id will update each other
@ -89,8 +95,9 @@ var WidgetManager = class WidgetManager extends EventEmitter {
}
if (!Array.isArray(linkId)) linkId = [linkId]
linkId = linkId.map(x => x.replace(/^<<\s*/, ''))
for (var i in linkId) {
if (linkId[i]) {
if (linkId[i] && linkId[i].indexOf('>>') < 0) {
if (!this.linkIdRoute[linkId[i]]) this.linkIdRoute[linkId[i]] = []
this.linkIdRoute[linkId[i]].push(hash)
}
@ -157,7 +164,7 @@ var WidgetManager = class WidgetManager extends EventEmitter {
if (!Array.isArray(linkId)) linkId = [linkId]
for (var i in linkId) {
if (linkId[i] && this.linkIdRoute[linkId[i]].indexOf(hash) != -1) this.linkIdRoute[linkId[i]].splice(this.linkIdRoute[linkId[i]].indexOf(hash), 1)
if (linkId[i] && this.linkIdRoute[linkId[i]] && this.linkIdRoute[linkId[i]].indexOf(hash) != -1) this.linkIdRoute[linkId[i]].splice(this.linkIdRoute[linkId[i]].indexOf(hash), 1)
}
}