Options
All
  • Public
  • Public/Protected
  • All
Menu

Module "observer/watcher"

Contains functionality to add watchers to observed data.

Index

Functions

addPropertyWatcher

  • Adds a watcher function to a property that gets called when the property changes.

    const observed = observe({
     price: 43,
     qty: 10,
     total() {
       return this.qty * this.price;
     }
    });
    
    addPropertyWatcher(observed, 'price', (value, oldValue) => {
     console.log(value, oldValue);
    });
    
    // watcher is called on data change
    observed.price = 50; // output: 50 43

    Type parameters

    • T

      Return type of the watcher function.

    Parameters

    • data: Obj

      Object observed with observe.

    • path: string

      Path to the property on the data object.

    • watcher: WatcherFunction<T>

      Function to add to the properties' watchers.

    Returns WatcherFunction<T>

modifyPropertyWatcherList

  • modifyPropertyWatcherList<T, U>(observedData: T, path: string, watcher: WatcherFunction<U>, operation: "add" | "remove"): void
  • Finds the observable attached to a property within observed data and adds or removes a watcher from its watcher list.

    Type parameters

    • T: Obj

      Object on which to register a property watcher.

    • U

      Return type of the property watcher.

    Parameters

    Returns void

removePropertyWatcher

  • Removes a watcher function from a property.

    const observed = observe({
     price: 43,
     qty: 10,
     total() {
       return this.qty * this.price;
     }
    });
    
    const watcher = addPropertyWatcher(observed, 'price', (value, oldValue) => {
     console.log(value, oldValue);
    });
    
    // watcher is called on data change
    observed.price = 50; // output: 50 43
    
    removePropertyWatcher(observed, 'price', watcher);
    
    // no output since watcher was removed
    observed.price = 90;

    Type parameters

    • T

      Return type of the watcher function.

    Parameters

    • data: Obj

      Object observed with observe.

    • path: string

      Path to the property on the data object.

    • watcher: WatcherFunction<T>

      Function to remove from the properties' watchers.

    Returns void

Generated using TypeDoc