Core
Set method

Model set method

The set method is an overloaded method which can be used to dispatch or respond to events. Likewise can be used to update a property and also destroy a controller.

Get update in progress

set(): Promise<Model.Values<this>> | undefined

If updates are pending, will return a promise which resolves when settled. Otherwise returns undefined if no update is active.

This is both to check if an update is happening, but also to flush any potential update within an async function. This ensures all side effects have already been called.

class Control extends Model {
  foo = 1;
}
 
async function example(){
  const control = new Control();
 
  control.foo = 2;
 
  // this is resolved when update, if any, is complete
  await control.set(); 
}

Call a function when event occurs

set(callback: Model.Event<this>): () => boolean

Provided function is called when any event occurs. This is synonymous in response to an assignment or dispatch. Even if the same key is edited multiple times before settling, function will receive all values.

class Control extends Model {
  foo = 1;
}
 
const control = new Control();
 
// this will call when any event occurs
control.set((key, source) => {
  console.log(key);
});
 
control.foo = 2;
control.foo = 3;
 
control.set('foo');
 
// logs "foo" three times

Dispatch an event

set(key: Model.Key<this>): void

Dispatches an event. Provided key can be either a tracked property or a custom event key. If key is a property, it will keep its original value, however listeners will refresh as if it did change.

class Control extends Model {
  foo = 1;
}
 
const control = new Control();
 
control.get(({ foo }) => {
  console.log(foo);
});
 
control.set('foo');
 
// logs "foo" twice

Update a property

set<K>(key: K, value?: Model.Value<this, K>, silent?: boolean): void

Update a property. If silent is true, will not dispatch an event.

class Control extends Model {
  foo = 1;
}
 
const control = new Control();
 
control.get(({ foo }) => {
  console.log(foo);
});
 
control.set('foo', 2);
 
// logs "foo" twice

Destroy a controller

set(status: null): void

Destroys a controller. This will dispatch the null event, remove all listeners and prevent any further updates. If a model is destroyed, it cannot be updated again.

class Control extends Model {
  foo = 1;
}
 
const control = new Control();
 
control.get(key => {
  console.log(`Event: ${foo}`);
});
 
control.set(null);
// Event: null
 
control.foo = 2;
// Error: Tried to update foo but Control-A1234 is destroyed.