All files Registry.ts

76.32% Statements 29/38
71.43% Branches 10/14
87.5% Functions 7/8
76.32% Lines 29/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83                                    10x                       13x 13x 12x 12x 12x 12x 12x   13x       9x 9x 9x 9x   9x       20x 20x 20x 20x     20x     20x 23x   20x       20x 17x   20x         10x 10x 10x 6x        
import * as ReactDom from 'react-dom';
import { Subject, Observable } from 'rxjs';
import { Store } from './Store';
import { getDescription } from './utils';
import { Action, ActionLike } from './types';
import { Notify } from './Notify';
import { createOutputStream } from './createOutputStream';
import { StateLogger } from './StateLogger';
 
export class Registry {
  private nameCount: Map<string, number> = new Map();
  private displayNames: Map<symbol, string> = new Map();
  private storesMap: Map<symbol, Store> = new Map();
  private stores: Store[] = [];
  private input$!: Subject<Action>;
  private output$!: Observable<Action>;
 
  constructor() {
    this.initStreams();
  }
 
  reset() {
    this.nameCount.clear();
    this.displayNames.clear();
    this.storesMap.clear();
    this.stores = [];
    this.initStreams();
  }
 
  getDisplayName(name: symbol) {
    const description = getDescription(name);
    if (!this.displayNames.has(name)) {
      let count = this.nameCount.get(description) || 0;
      count++;
      this.nameCount.set(description, count);
      const displayName = count > 1 ? `${description}#${count}` : description;
      this.displayNames.set(name, displayName);
    }
    return this.displayNames.get(name)!;
  }
 
  getStore(name: symbol) {
    Eif (!this.storesMap.has(name)) {
      const store = new Store(name, this.getDisplayName(name));
      this.storesMap.set(name, store);
      this.stores.push(store);
    }
    return this.storesMap.get(name)!;
  }
 
  dispatch(action: ActionLike) {
    ReactDom.unstable_batchedUpdates(() => {
      const notify = new Notify();
      let stateLogger: StateLogger | null = null;
      Iif (process.env.NODE_ENV === 'development') {
        stateLogger = new StateLogger(this.stores);
      }
      Iif (stateLogger) {
        stateLogger.calcState('prev');
      }
      for (const store of this.stores) {
        store.dispatch(action, notify);
      }
      Iif (stateLogger) {
        stateLogger.calcState('next');
        stateLogger.log(action);
      }
      for (const fn of notify.handlers) {
        fn();
      }
      this.input$.next(action as Action);
    });
  }
 
  private initStreams() {
    this.input$ = new Subject();
    this.output$ = createOutputStream(this.input$, this.stores);
    this.output$.subscribe(action => {
      this.dispatch(action);
    });
  }
}