asljs-observable
v0.2.0
Published
Lightweight observable for JS. Emits events on property changes via on/off/emit. Works with objects, arrays, and primitives.
Downloads
202
Readme
observable
Part of Alexandrite Software Library – a set of high‑quality, performant JavaScript libraries for everyday use.
Lightweight observable for JS. Emits events on property changes via on/off/emit. Works with objects, arrays, and primitives.
Install
npm install asljs-observableUsage
Object example:
import { observable } from 'asljs-observable';
const obj = observable({ a: 1, b: 2 });
obj.on('set', ({ property, value, previous }) => {
console.log('set', property, previous, '→', value);
});
obj.on('set:a', ({ value }) => {
console.log('a changed to', value);
});
obj.a = 3;Array example:
const arr = observable([1, 2, 3]);
arr.on('set:1', ({ value, previous }) => {
console.log('index 1:', previous, '→', value);
});
arr[1] = 42;Primitive example:
const box = observable(10);
box.on('set', ({ value, previous }) => {
console.log('value:', previous, '→', value);
});
box.value = 11;API
observable(value, [options])
Wraps an object, array, or primitive to make it observable.
value: Target object/array/primitive to observe.options.eventful(optional): Customeventfulfactory (defaults toasljs-eventful).options.eventfulOptions(optional): Options passed to the underlyingeventfulwrapper.options.trace(optional): Trace hook(object, action, payload)invoked on'new','set','delete','define'.
Returns the original value wrapped with Eventful API and change notifications.
Events and payloads
Objects emit:
setandset:<prop>:{ property, value, previous }deleteanddelete:<prop>:{ property, previous }defineanddefine:<prop>:{ property, descriptor, previous }
Arrays emit (no define):
set/set:<index>andset:lengthdelete/delete:<index>
Primitives (boxed as { value }) emit:
setandset:value:{ property: 'value', value, previous }
