import { DataService } from './DataService'; import { DataServiceDriver } from './DataServiceDriver'; import { MemoryDataService } from './MemoryDataService'; /** * Basic key value data service. This data service can be used to communicate * between workers and the main thread. */ export class KeyValueDataService extends DataService { constructor(uid?: string, dataServiceDriver?: DataServiceDriver) { super(dataServiceDriver); if (!dataServiceDriver) { this.driver = new MemoryDataService(Object, { serialize: (d) => d, deserialize: (d) => d, }); this.driver.dataType = Object; } this.uid = uid || this.constructor.name; } /** * Get the value of a key * @param {string} key Key * @returns {Promise} Promise of the value */ getValue(key: string): Promise { return this.findByUID(key); } /** * Set a value * @param {string} key Key to use * @param {any} value Value to store * @returns {Promise} Promise of setting the value */ setValue(key: string, value: any): Promise { this.emit('set', key, value); return this.insert(key, value); } }