Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API for updating state #24

Open
mathisonian opened this issue Aug 10, 2018 · 2 comments
Open

API for updating state #24

mathisonian opened this issue Aug 10, 2018 · 2 comments

Comments

@mathisonian
Copy link

Hey @freeman-lab,

I'm playing around with some ideas to improve the authoring process for Idyll, and one of them is to automatically give the user a control panel that hooks into the reactive state (see idyll-lang/idyll#320).

This is working great, but it only updates in one direction. Is there a way to programmatically update the control panel's state?

@fnky
Copy link

fnky commented Oct 20, 2018

Was looking for updating state too. I was working on ways to update the state, input value and the content of the "value" element, using Object.defineProperty to define a dynamic getter/setter.

This would allow a consistent API such as:

const panel = control([
  { type: 'range', label: 'alpha', min: 0, max: 255, initial: 255 },
  { type: 'checkbox', label: 'visible', initial: false },
  {
    type: 'button',
    label: 'toggle visibility',
    action: () => {
      panel.state['checkbox'] = !panel.state['checkbox'];
    }
  }
]);

// getter
panel.state['range']; // 255

// setter
panel.state['alpha'] = 64;
panel.state['alpha']; // 64

It's worth noting that the state is updated as a side-effect of initializing components. This would break the above usage for setting panel.state['alpha'].

This is due to the components being emitting their initialization asynchronously (within setTimeout). This could cause confusion when using the setter above.

To avoid invalidated state, we could introduce state scheduling:

Change state management to be asynchronous. Updating the state should schedule state changes—for example with an updateState/setState method.

If you then need the current state before scheduling the next, you can provide a function to the update method, which provides the guaranteed current state from arguments.

Similar to how state management works in React.

Would like to know if there's a simpler way of doing it and ensure that state is up-to-date without the side-effects.

@tracycollins
Copy link

Any news on this? Without this proposed API, is there another way to do this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants