All files / src/persistence/inMemoryStores InMemoryKeyValueStore.ts

100% Statements 21/21
100% Branches 6/6
100% Functions 4/4
100% Lines 16/16

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 2941x 41x     41x         360x 358x 358x 359x 359x 17x   16x     257x 256x 256x 40x   256x 256x      
import { EMPTY, Observable, of } from 'rxjs';
import { InMemoryCollectionStore } from './InMemoryCollectionStore';
import { KeyValueCollection, KeyValueStore } from '../types';
 
export class InMemoryKeyValueStore<K, V>
  extends InMemoryCollectionStore<KeyValueCollection<K, V>>
  implements KeyValueStore<K, V>
{
  getValues(keys: K[]): Observable<V[]> {
    if (this.destroyed || keys.length === 0) return EMPTY;
    const result: V[] = [];
    for (const key of keys) {
      const value = this.docs.find((doc) => doc.key === key)?.value;
      if (!value) return EMPTY;
      result.push(value);
    }
    return of(result);
  }
  setValue(key: K, value: V): Observable<void> {
    if (this.destroyed) return EMPTY;
    const storedDocIndex = this.docs.findIndex((doc) => doc.key === key);
    if (storedDocIndex >= 0) {
      this.docs.splice(storedDocIndex, 1);
    }
    this.docs.push({ key, value });
    return of(void 0);
  }
}