kiwi icon indicating copy to clipboard operation
kiwi copied to clipboard

[Feature Request] incremental update notification

Open theSherwood opened this issue 2 years ago • 1 comments

Is there any easy way to see which variables have changed during a call to solver.updateVariables? The api doesn't seem to give me a convenient way to incrementally update anything depending on the kiwi solver's state (such as UI), without checking each variable individually.

2 possible approaches for the API that spring to mind are:

  1. Having the variable constructor take a callback
type VariableConstructor = (name?: string, callback: (variable: Kiwi.Variable, new_value: any) => void) => Kiwi.Variable;
  1. Returning a list of tuples of dirty variables and their new values from solver.updateVariables
type VariableValue = any;
type SolverUpdateVariables = (this: Kiwi.Solver) => [Kiwi.Variable, VariableValue][];

Thoughts?

theSherwood avatar Jan 19 '24 14:01 theSherwood

Thanks for the ideation! Sounds like a good idea in general to be able to react to changes of (derive from) certain variables.

Thoughts?

I think the callback approach is good because it will cost initial memory for the callbacks, and after that will only be calling the callbacks upon updates if the callbacks exist, whereas if the updateVariables method returns a new array of modified variables on each call, that could add higher runtime cost for every calculation in creating the new arrays (imagining it running during animation). I think that the subscription is also more common of a pattern people will be familiar with.

Also added a comment in the PR.

trusktr avatar Jan 25 '24 03:01 trusktr