Options
All
  • Public
  • Public/Protected
  • All
Menu

External module "observer/index"

Module with functions that emulate vue's reactivity mechanism.

Index

Variables

Let currentEvaluatingObservable

currentEvaluatingObservable: IObservable<any> | undefined

The IObservable currently being created and evaluated.

This property is globally unique because only one IObservable can be evaluated at a time.

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

    Parameters

    • data: object

      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>

defineReactiveProperty

  • defineReactiveProperty<T>(obj: object, key: string | number, observable: IObservable<T>): void
  • Creates a reactive property on a specified object.

    For a property to be considered reactive it needs to be proxied with a getter/setter and also have an associated Observable instance.

    Reactive properties

    const obj = {};
    defineReactiveProperty(obj, 'number', new Observable(99));
    
    // Note that even though the value is proxied you can still access it as you normally access properties.
    console.log(obj.number) // output: 99
    obj.number = 105;
    console.log(obj.number) // output: 105

    Type parameters

    • T

      Any valid javascript value.

    Parameters

    • obj: object

      Object on which to create the reactive property.

    • key: string | number

      Key for the new property.

    • observable: IObservable<T>

      Observable instance that stores the value of the reactive property.

    Returns void

extractObservableFromProperty

  • extractObservableFromProperty(object: object, key: string | number): IObservable<any> | undefined
  • Extracts the Observable instance from a property on an object.

    This function will only work if the defineReactiveProperty method was used to define that property.

    Parameters

    • object: object

      Object where you have a reactive property.

    • key: string | number

      Key of the property that has an observable instance.

    Returns IObservable<any> | undefined

modifyPropertyWatcherList

  • modifyPropertyWatcherList<T>(observedData: T, path: string, watcher: WatcherFunction<any>, 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: object

    Parameters

    Returns void

navigateToPropertyPath

  • navigateToPropertyPath<T>(obj: T, path: string, callback: function): void
  • Traverses a string path for an object to find the property it is pointing to.

    Once the property is found a callback function is called passing in the property's parent object and the property name.

    const data = {
      nested: {
       anotherNested: {
         property: 'value',
         number: 66
       }
      }
    };
    
    navigateToPropertyPath(data, 'nested.anotherNested.property', (obj, property) => {
     console.log(obj, property);
    });
    
    // output: {property: 'value', number: 66} property

    Type parameters

    • T: object

    Parameters

    • obj: T

      Object to traverse.

    • path: string

      Path to a property on the obj.

    • callback: function

      Callback to be called when the property is found.

        • (obj: object, key: string): void
        • Parameters

          • obj: object
          • key: string

          Returns void

    Returns void

observe

  • Takes a data object and recursively makes all its properties reactive.

    Computed Properties

    Function definitions within the data object are treated as computed property definitions.

    const observed = observe({
     price: 55,
     quantity: 10,
     total() {
       return this.price * this.quantity;
     }
    });
    
    console.log(observed); // output: { price: 55, quantity: 10, total: 550 }

    Type parameters

    • T: object

      Plain javascript object.

    Parameters

    • data: T

      Object to process.

    Returns ObservedData<T>

observeObject

  • observeObject<T>(data: T, observable?: IObservable<T>): void
  • Iterate over a data object and make all its properties reactive.

    Type parameters

    • T: object

      Any object type: array, object, class etc.

    Parameters

    • data: T

      Data object.

    • Optional observable: IObservable<T>

      Observable for the data object

    Returns void

removePropertyWatcher

  • removePropertyWatcher<T>(data: object, path: string, watcher: WatcherFunction<T>): void
  • Removes a watcher function from a property.

    const observed = observe({
     price: 43,
     qty: 10,
     total() {
       return this.qty * this.price;
     }
    });
    
    const watcher = (value, oldValue) => {
     console.log(value, oldValue);
    }
    
    addPropertyWatcher(observed, 'price', watcher);
    
    // 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

    Parameters

    • data: object

      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