Function observable

  • Creates a RxJs style observable

    const o = observable(stream => {
    // Code to run for initialisation when we go from idle to at least one subscriber
    // Won't run again for additional subscribers, but WILL run again if we lose
    // all subscribers and then get one

    // To send a value:
    stream.set(someValue);

    // Optional: return function to call when all subscribers are removed
    return () => {
    // Code to run when all subscribers are removed
    }
    });

    For example:

    const xy = observable<(stream => {
    // Send x,y coords from PointerEvent
    const send = (event) => {
    stream.set({ x: event.x, y: event.y });
    }
    window.addEventListener(`pointermove`, send);
    return () => {
    // Unsubscribe
    window.removeEventListener(`pointermove`, send);
    }
    });

    xy.onValue(value => {
    console.log(value);
    });

    Type Parameters

    • V

    Parameters

    • init: ((stream: Reactive<V> & {
          set(value: V): void;
      }) => undefined | (() => void))
        • (stream): undefined | (() => void)
        • Parameters

          • stream: Reactive<V> & {
                set(value: V): void;
            }

          Returns undefined | (() => void)

    Returns Reactive<V>