import { EventEmitter } from 'events'; import * as io from 'io-ts'; import assertType from './assertType'; import CellCollection from './CellCollection'; import IAsyncStorage from './IAsyncStorage'; export default function MemoryCellCollection( memoryParam: Record = {}, ) { const memory = memoryParam; const events = new EventEmitter() as IAsyncStorage['events']; return new CellCollection({ async read(key: string, type: io.Type): Promise { const readResult = memory[key]; if (readResult !== undefined) { assertType(readResult, type); } return readResult; }, async write( key: string, type: io.Type, value: T | undefined, ): Promise { if (value !== undefined) { assertType(value, type); } memory[key] = value; events.emit('change', [key]); }, events, }); }