• For the given object, if a watch is added to the given path the callback will be invoked. When all the watches are removed, the handle returned by the callback will be invoked.

    A common use case for this is to notify changes to a property only if someone is watching it, this avoids unnecessary work from the watch if no one is watching the property.

    class MyClass extends observable() {
    _foo = "foo";
    _bar = "bar";

    get foo() {
    return `${this._foo} ${this._bar}`;
    }

    get bar() {
    return this._bar;
    }
    set bar(value: string) {
    this._bar = value;
    }
    }

    const myClass = new MyClass();

    onWatch(myClass, "foo", () =>
    watch(myClass, "bar", () => notifyChange(myClass, "foo"), {
    sync: true,
    })
    );

    Type Parameters

    Parameters

    • obj: TObsv

      The object to watch.

    • path: keyof TObsv

      The property to watch.

    • callback: (() => IHandle)

      The callback to invoke when the property is watched.

    Returns IHandle

    Error if the object is not observable.