widgets: solve multi-value-dependency pattern resolution using a single queue instead of timeouts

This commit is contained in:
jean-emmanuel 2024-02-13 17:38:45 +01:00
parent e2215b865a
commit 8b92400c1e
5 changed files with 41 additions and 26 deletions

View file

@ -3,7 +3,9 @@
## 1.25.8
- bug fixes
- scripting: `getProp()` now correctly returns a copy of the property if it is an object
- scripting: `getProp()` now correctly returns a copy of the property if it is an object
- prevent infinite loop when loading widgets with complex inter-dependencies
## 1.25.7

View file

@ -43,6 +43,8 @@ function updateWidget(widget, options={}) {
return
}
widgetManager.valueChangeLock()
var reuseChildren = options.reuseChildren !== false && widget instanceof Panel
&& !(widget instanceof Matrix)
@ -146,6 +148,8 @@ function updateWidget(widget, options={}) {
}
}
widgetManager.valueChangeUnlock()
editor.widgetTree.updateTree(editor.selectedWidgets)
if (editor.selectedWidgets.includes(widget) && !options.preventSelect) {

View file

@ -19,6 +19,9 @@ class WidgetManager extends EventEmitter {
this.preArgsSeparator = '||||'
this.valueChangeLockDepth = 0
this.valueChangeQueue = {}
this.on('value-changed', this.onChange.bind(this))
ipc.on('connect', ()=>{
@ -29,8 +32,29 @@ class WidgetManager extends EventEmitter {
}
valueChangeLock() {
this.valueChangeLockDepth++
}
valueChangeUnlock() {
this.valueChangeLockDepth--
if (this.valueChangeLockDepth == 0) {
for (var h in this.valueChangeQueue) {
if (this.valueChangeQueue[h]) this.applyValueChange(this.valueChangeQueue[h])
}
this.valueChangeQueue = {}
}
}
onChange(e) {
if (this.valueChangeLockDepth === 0) this.applyValueChange(e)
else this.valueChangeQueue[e.widget.hash] = e
}
applyValueChange(e) {
var {id, widget, linkId, options} = e
if (linkId) {
@ -78,6 +102,7 @@ class WidgetManager extends EventEmitter {
}
}
this.trigger('value-changed-lazy', e)
}
createAddressRef(widget, preArgs, address) {

View file

@ -43,6 +43,8 @@ var Parser = class Parser {
var props = data
widgetManager.valueChangeLock()
// Set default widget type
props.type = tab ? 'tab' : props.type || 'fader'
@ -107,6 +109,8 @@ var Parser = class Parser {
parentNode.appendChild(widget.container)
widget.mounted = true
widgetManager.valueChangeUnlock()
// Editor needs to get the container object
return widget

View file

@ -198,9 +198,6 @@ class Widget extends EventEmitter {
this.mounted = false
this.visible = true
this.isValueChanging = false
this.valueChangingTimeout = null
this.valueChangingQueue = {}
if (options.value !== undefined) this.value = options.value
this.parsers = {}
@ -348,18 +345,14 @@ class Widget extends EventEmitter {
changed(options) {
// emitting value-changed might trigger feedback from multiple widgets
// in which case we'll want to hold on until we're done syncing
// and dedupe feedback actions caused by advanced syntaxes
// see this.linkedValueChangedCallback in createLinkedPropsBindings()
this.isValueChanging = true
widgetManager.valueChangeLock()
this.trigger('value-changed', {
widget: this,
options: options,
id: this.getProp('id'),
linkId: this.getProp('linkId')
})
this.isValueChanging = false
widgetManager.valueChangeUnlock()
}
@ -491,27 +484,14 @@ class Widget extends EventEmitter {
if (!this.linkedValueChangedCallback && Object.keys(this.linkedPropsValue).length) {
this.linkedValueChangedCallback = (e)=>{
if (!this.parent.mounted || this.isValueChanging && e.widget !== this) {
clearTimeout(this.valueChangingTimeout)
this.valueChangingQueue[e.widget.hash] = e
this.valueChangingTimeout = setTimeout(()=>{
if (!this.removed) {
for (var h in this.valueChangingQueue) {
this.onLinkedPropsChanged(this.valueChangingQueue[h], 'value-changed')
}
}
this.valueChangingQueue = {}
})
} else {
this.onLinkedPropsChanged(e, 'value-changed')
}
this.onLinkedPropsChanged(e, 'value-changed')
}
widgetManager.on('value-changed', this.linkedValueChangedCallback, {context: this})
widgetManager.on('value-changed-lazy', this.linkedValueChangedCallback, {context: this})
} else if (this.linkedPropsValueCallback && !Object.keys(this.linkedPropsValue).length) {
widgetManager.off('value-changed', this.linkedValueChangedCallback)
widgetManager.off('value-changed-lazy', this.linkedValueChangedCallback)
delete this.linkedValueChangedCallback
}