import { Store } from './types' export default class StoreSubscriber implements Store { constructor(private store: Store) {} get() { return this.store.get() } update(newState: Partial) { this.store.update(newState) this.subscriptions.forEach(sb => sb()) } private subscriptions: Function[] = [] subscribe(selector: (g: G) => T, cb: (val: T) => void) { let prevVal = selector(this.get()) const checkSubscription = () => { const newVal = selector(this.get()) if (newVal !== prevVal) { prevVal = newVal cb(newVal) } } this.subscriptions.push(checkSubscription) } }