lume / kiwi

Fast TypeScript implementation of the Cassowary constraint solving algorithm (soon for AssemblyScript / WebAssembly).
https://codepen.io/trusktr/pen/abMLVxa
Other
149 stars 8 forks source link

[Feature Request] incremental update notification #6

Open theSherwood opened 7 months ago

theSherwood commented 7 months ago

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;
  2. 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?

trusktr commented 7 months ago

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.