Enterprise-CMCS/macpro-mako

View on GitHub
react-app/src/utils/basic-observable.ts

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
export class Observer<T> {
  private subscribers: Array<(observed: T) => void>;
  observed: T | null;

  constructor() {
    this.subscribers = [];
    this.observed = null;
  }

  subscribe = (subscriber: (newObserved: T | null) => void) => {
    this.subscribers.push(subscriber);

    // unsubscribe function
    return () => {
      const index = this.subscribers.indexOf(subscriber);
      this.subscribers.splice(index, 1);
    };
  };

  publish = (data: T | null) => {
    this.subscribers.forEach((subscriber) => subscriber(data));
  };
}