All files / util/events sync-store.ts

46.66% Statements 7/15
100% Branches 0/0
16.66% Functions 1/6
58.33% Lines 7/12

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 3157x                   57x             57x 3283x 3283x   3283x   3283x              
import {FanOut} from 'thingies/lib/fanout';
 
export interface SyncStore<T> {
  subscribe: SyncStoreSubscribe;
  getSnapshot: () => T;
}
 
export type SyncStoreSubscribe = (callback: () => void) => SyncStoreUnsubscribe;
export type SyncStoreUnsubscribe = () => void;
 
export class FanoutSyncStore<T> implements SyncStore<T> {
  private readonly fanout: FanOut<void> = new FanOut<void>();
  public readonly subscribe: SyncStoreSubscribe = (cb) => this.fanout.listen(cb);
 
  constructor(public readonly getSnapshot: () => T) {}
}
 
export class ValueSyncStore<V> implements SyncStore<V> {
  private readonly fanout: FanOut<void> = new FanOut<void>();
  public readonly subscribe: SyncStoreSubscribe = (cb) => this.fanout.listen(cb);
 
  constructor(public value: V) {}
 
  public readonly getSnapshot: () => V = () => this.value;
 
  public next(value: V): void {
    this.value = value;
    this.fanout.emit();
  }
}