Function: observe

index.observe

observe<T>(target, hooks): () => void

Attaches scoped reactivity lifecycle hooks to a specific reactive target object, domain model, or store.

Type parameters

Name Type Description
T extends object The type of target object or store.

Parameters

Name Type Description
target T The reactive object or store to observe.
hooks ReactivityHooks Reactivity lifecycle hooks to attach.

Returns

fn

An unsubscribe function to detach the hooks from the target.

(): void

Returns

void

Remarks

Unlike global hooks, scoped hooks only receive notifications when the specified target object or its nested reactive properties are tracked or mutated.

Example

import { observe, makeReactive } from '@banksia/signals';

const user = makeReactive({ name: 'Alice' });
const unsubscribe = observe(user, {
  onNotify(source, change) {
    console.log('User mutated:', source.meta.property, change.newValue);
  },
});