• Observers encapsulates a collection of connections to event emitters, like Evented, Mutable, Watches and the like. It therefor helps you to manage multiple event connections by grouping them - e.g. for cleaning all connections at once.

    Parameters

    • Optional options: ObserverOptions

      optional parameters to customize the creation

    Returns Observers

    Example: _track a event connection_

    import Observers from "apprt-core/Observers";
    let eventSource = new Evented();
    let observers = Observers();
    // track connection to eventSource
    observers.add(eventSource.on("event", ()=>{...}));

    // if the connection needs to be cleaned call
    observers.clean();

    Example: _group event connections_

    import Observers from "apprt-core/Observers";
    let eventSourceA = new Evented();
    let eventSourceB = new Evented();
    let observers = Observers();
    // track connection to eventSourceA, but in group "a"
    observers.group("a").add(eventSourceA.on("event", ()=>{ ... }));
    // track connection to eventSourceB, but in group "b"
    observers.group("b").add(eventSourceB.on("event", ()=>{ ... }));

    // if "a" is not longer required
    observers.group("a").clean();

    // if "b" is not longer required (shows alternative)
    observers.clean("b");

    // if none of the connections are required anymore clean all
    observers.clean();

    Example: _use the rebind functionality_

    // The bind/rebind functionality helps to encapsulate the code to connect to an observable.
    // When the observable target changes a simple call to 'bind'
    // rexecutes the connection code and clears the old connection.

    import Observers from "apprt-core/Observers";
    let Controller = class {
    constructor() {
    this.events = [];
    this._observers = Observers({
    // rebind is executed if Observers.bind is called, and used to reconnect to a new source instance
    rebind: (observers, source) => {
    observers.add(source.on("change", (evt) => {
    this.events.push(evt);
    }));
    }
    });
    }
    connect(eventSource) {
    // observe new event source
    this._observers.bind(eventSource);
    }
    disconnect() {
    // disconnect from event source
    this._observers.clean();
    }
    }

Generated using TypeDoc