Skip to content

Commit

Permalink
wip: fix local variable value propogation
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Feb 24, 2025
1 parent 3f2da62 commit 2c1dc67
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 23 deletions.
8 changes: 7 additions & 1 deletion companion/lib/Controls/ControlTypes/Button/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export abstract class ButtonControlBase<TJson, TOptions extends Record<string, a
controlId,
commitChange: this.commitChange.bind(this),
triggerRedraw: this.triggerRedraw.bind(this),
localVariablesChanged: this.onVariablesChanged.bind(this),
localVariablesChanged: this.onLocalVariablesChanged.bind(this),
instanceDefinitions: deps.instance.definitions,
internalModule: deps.internalModule,
moduleHost: deps.instance.moduleHost,
Expand Down Expand Up @@ -208,6 +208,12 @@ export abstract class ButtonControlBase<TJson, TOptions extends Record<string, a
return result
}

onLocalVariablesChanged(allChangedVariables: Set<string>): void {
this.onVariablesChanged(allChangedVariables)

this.deps.internalModule.onVariablesChanged(allChangedVariables, this.controlId)
}

abstract onVariablesChanged(allChangedVariables: Set<string>): void

/**
Expand Down
11 changes: 10 additions & 1 deletion companion/lib/Controls/ControlTypes/Triggers/Events/Variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ export class TriggersEventVariables {
*/
readonly #eventBus: TriggerEvents

/**
* The control id of the parent trigger
*/
readonly #controlId: string

/**
* Execute the actions of the parent trigger
*/
Expand All @@ -62,6 +67,7 @@ export class TriggersEventVariables {
this.#logger = LogController.createLogger(`Controls/Triggers/Events/Timer/${controlId}`)

this.#eventBus = eventBus
this.#controlId = controlId
this.#executeActions = executeActions

this.#eventBus.on('variables_changed', this.#onVariablesChanged)
Expand All @@ -85,7 +91,10 @@ export class TriggersEventVariables {
* Handler for the variable_changed event
* @param allChangedVariables Set of all the variables that have changed
*/
#onVariablesChanged = (allChangedVariables: Set<string>): void => {
#onVariablesChanged = (allChangedVariables: Set<string>, fromControlId: string | null): void => {
// If the event is from a control, but not the same control, ignore it
if (fromControlId && fromControlId !== this.#controlId) return

if (this.#enabled) {
let execute = false

Expand Down
7 changes: 5 additions & 2 deletions companion/lib/Controls/Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -957,12 +957,15 @@ export class ControlsController extends CoreBase {
/**
* Propagate variable changes to the controls
*/
onVariablesChanged(allChangedVariablesSet: Set<string>): void {
onVariablesChanged(allChangedVariablesSet: Set<string>, fromControlId: string | null): void {
// Inform triggers of the change
this.triggers.emit('variables_changed', allChangedVariablesSet)
this.triggers.emit('variables_changed', allChangedVariablesSet, fromControlId)

if (allChangedVariablesSet.size > 0) {
for (const control of this.#controls.values()) {
// If the changes are local variables and from another control, ignore them
if (fromControlId && fromControlId !== control.controlId) continue

if (control.supportsStyle) {
control.onVariablesChanged(allChangedVariablesSet)
}
Expand Down
2 changes: 1 addition & 1 deletion companion/lib/Controls/TriggerEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface TriggerEventsEvents {

trigger_enabled: [controlId: string, enabled: boolean]
control_press: [controlId: string, pressed: boolean, surfaceId: string | undefined]
variables_changed: [changed: Set<string>]
variables_changed: [changed: Set<string>, fromControlId: string | null]
}

/**
Expand Down
8 changes: 4 additions & 4 deletions companion/lib/Internal/Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,13 +525,13 @@ export class InternalController {
this.#variablesController.definitions.setVariableDefinitions('internal', variables)
}

variablesChanged(all_changed_variables_set: Set<string>): void {
onVariablesChanged(changedVariablesSet: Set<string>, fromControlId: string | null): void {
if (!this.#initialized) throw new Error(`InternalController is not initialized`)

// Inform all fragments
for (const fragment of this.#fragments) {
if ('variablesChanged' in fragment && typeof fragment.variablesChanged === 'function') {
fragment.variablesChanged(all_changed_variables_set)
if (typeof fragment.onVariablesChanged === 'function') {
fragment.onVariablesChanged(changedVariablesSet, fromControlId)
}
}

Expand All @@ -542,7 +542,7 @@ export class InternalController {
if (!feedback.referencedVariables || !feedback.referencedVariables.length) continue

// Check a referenced variable was changed
if (!feedback.referencedVariables.some((variable) => all_changed_variables_set.has(variable))) continue
if (!feedback.referencedVariables.some((variable) => changedVariablesSet.has(variable))) continue

newValues.push({
id: id,
Expand Down
2 changes: 2 additions & 0 deletions companion/lib/Internal/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export interface InternalModuleFragment {

getVariableDefinitions?: () => VariableDefinitionTmp[]
updateVariables?: () => void

onVariablesChanged?: (changedVariablesSet: Set<string>, fromControlId: string | null) => void
}

export interface ExecuteFeedbackResultWithReferences {
Expand Down
30 changes: 18 additions & 12 deletions companion/lib/Internal/Variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class InternalVariables implements InternalModuleFragment {
/**
* The dependencies of variables that should retrigger each feedback
*/
#variableSubscriptions = new Map<string, Set<string>>()
#variableSubscriptions = new Map<string, { controlId: string; variables: Set<string> }>()

constructor(internalModule: InternalController, controlsController: ControlsController) {
this.#internalModule = internalModule
Expand Down Expand Up @@ -170,7 +170,7 @@ export class InternalVariables implements InternalModuleFragment {
feedback
)

this.#variableSubscriptions.set(feedback.id, result.variableIds)
this.#variableSubscriptions.set(feedback.id, { controlId: feedback.controlId, variables: result.variableIds })

return compareValues(feedback.options.op, result.text, feedback.options.value)
} else if (feedback.definitionId == 'variable_variable') {
Expand All @@ -183,14 +183,17 @@ export class InternalVariables implements InternalModuleFragment {
feedback
)

this.#variableSubscriptions.set(feedback.id, new Set([...result1.variableIds, ...result2.variableIds]))
this.#variableSubscriptions.set(feedback.id, {
controlId: feedback.controlId,
variables: new Set([...result1.variableIds, ...result2.variableIds]),
})

return compareValues(feedback.options.op, result1.text, result2.text)
} else if (feedback.definitionId == 'check_expression') {
const parser = this.#controlsController.createVariablesAndExpressionParser(feedback.location, null)
const res = parser.executeExpression(feedback.options.expression, 'boolean')

this.#variableSubscriptions.set(feedback.id, res.variableIds)
this.#variableSubscriptions.set(feedback.id, { controlId: feedback.controlId, variables: res.variableIds })

if (res.ok) {
return !!res.value
Expand All @@ -210,23 +213,26 @@ export class InternalVariables implements InternalModuleFragment {
/**
* Some variables have been changed
*/
variablesChanged(all_changed_variables_set: Set<string>): void {
onVariablesChanged(changedVariablesSet: Set<string>, fromControlId: string | null): void {
/**
* Danger: It is important to not do any debounces here.
* Doing so will cause triggers which are 'on variable change' with a condition to check the variable value to break
*/

const affected_ids: string[] = []
for (const [id, names] of this.#variableSubscriptions.entries()) {
for (const name of names) {
if (all_changed_variables_set.has(name)) {
affected_ids.push(id)
const affectedFeedbackIds: string[] = []
for (const [id, { controlId, variables }] of this.#variableSubscriptions.entries()) {
// Skip if the changes are local variables from a different control
if (fromControlId && controlId !== fromControlId) continue

for (const name of variables) {
if (changedVariablesSet.has(name)) {
affectedFeedbackIds.push(id)
break
}
}
}
if (affected_ids.length > 0) {
this.#internalModule.checkFeedbacksById(...affected_ids)
if (affectedFeedbackIds.length > 0) {
this.#internalModule.checkFeedbacksById(...affectedFeedbackIds)
}
}

Expand Down
4 changes: 2 additions & 2 deletions companion/lib/Registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,8 @@ export class Registry {
})

this.variables.values.on('variables_changed', (all_changed_variables_set) => {
this.internalModule.variablesChanged(all_changed_variables_set)
this.controls.onVariablesChanged(all_changed_variables_set)
this.internalModule.onVariablesChanged(all_changed_variables_set, null)
this.controls.onVariablesChanged(all_changed_variables_set, null)
this.instance.moduleHost.onVariablesChanged(all_changed_variables_set)
this.#preview.onVariablesChanged(all_changed_variables_set)
this.surfaces.onVariablesChanged(all_changed_variables_set)
Expand Down

0 comments on commit 2c1dc67

Please sign in to comment.