All files / storage/src/db kv.ts

100% Statements 299/299
100% Branches 99/99
100% Functions 32/32
100% Lines 299/299

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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 4951x 1x 1x                       1x                     1x       45x         45x         45x           45x 32x 32x           45x 44x 44x           45x 61x 61x                 45x 45x 45x 45x 45x 45x 45x 45x 45x           45x 32x 32x   32x 57x   57x 26x 26x 57x 31x 31x 57x   32x 1x 1x 32x 32x   45x 45x 1x 1x 1x 1x   45x 45x           45x 32x 20x 20x 27x 20x 20x 20x 27x   20x 20x 20x   12x 32x             45x 15x 15x 15x 15x 15x             45x 94x 94x             45x 39x 39x                 45x 40x 3x 3x 3x 3x 3x   37x 37x 37x 37x   37x 37x   37x 37x 33x 33x 33x 37x 37x 4x   4x 2x 2x 2x 2x   4x 4x 4x 37x 37x 37x 37x   37x 37x 9x 2x 2x   7x 7x 7x 7x 9x 37x 40x               45x 12x 3x 3x 3x 3x 3x 3x 3x 3x 3x   9x   11x 7x 7x 7x   7x 7x   7x 7x 5x 5x 5x 7x 7x 2x 2x   2x 2x 2x 7x 7x 7x 7x   7x 7x 6x 2x 2x   4x 4x 4x 4x 6x 7x 7x   2x 12x             45x 37x 37x           45x 44x 44x           45x 44x   44x 33x 33x   44x 44x 45x   1x                                         1x 36x 36x 36x 36x 36x 36x   36x 12x 12x   24x   24x 24x 24x   24x     44x 23x     23x 23x   23x 23x   21x 21x   21x 20x     20x   20x 1x 20x 19x 1x 19x 19x     20x 28x 3x 1x 1x 1x 1x 3x 3x 20x   20x 3x   3x   3x 1x   1x 1x 1x 3x 2x 2x 3x   20x 20x   44x 13x 13x 12x 12x 13x 1x 1x 1x 13x 44x 8x 8x   21x 21x 21x 21x   21x 44x   24x 3x 3x   24x 4x 2x 2x 4x   24x 2x   2x 2x 2x   2x 2x 2x 2x 1x 1x   1x 1x   1x 1x 2x 2x 1x 1x 1x 2x 2x   24x 18x 18x 18x   24x   24x 24x     1x  
import { DB_SYNC_DELAY, IndexedStore } from './db.js';
import { anchor, captureStack, derive, microtask, type StateUnsubscribe } from '@anchorlib/core';
import {
  type DBEvent,
  type DBUnsubscribe,
  IDBStatus,
  type KVEvent,
  type KVFn,
  type KVSeed,
  type KVState,
  type KVSubscriber,
  type Operation,
  type Storable,
} from './types.js';
import { put, remove } from './helper.js';
 
/**
 * IndexedKV is an optimistic KV Storage that uses IndexedDB as its backend.
 * It provides a simple key-value store with optimistic concurrency control.
 *
 * This class extends IndexedStore and provides methods to set, get, and delete
 * key-value pairs. It also supports subscribing to changes in the store.
 *
 * @template T The type of values stored in the key-value store.
 */
export class IndexedKv<T> extends IndexedStore {
  /**
   * In-memory storage for the key-value pairs.
   */
  #storage = new Map<string, T>();
 
  /**
   * Set of awaiter functions to resolve when operations complete.
   */
  #awaiters = new Set<() => void>();
 
  /**
   * Counter for ongoing operations.
   */
  #operations = 0;
 
  /**
   * Gets a readonly object store for reading data.
   * @protected
   */
  protected get reader(): IDBObjectStore {
    return this.instance?.transaction(this.name, 'readonly').objectStore(this.name) as IDBObjectStore;
  }
 
  /**
   * Gets a readwrite object store for writing data.
   * @protected
   */
  protected get writer(): IDBObjectStore {
    return this.instance?.transaction(this.name, 'readwrite').objectStore(this.name) as IDBObjectStore;
  }
 
  /**
   * Indicates whether there are ongoing operations.
   * @returns True if there are ongoing operations, false otherwise.
   */
  public get busy(): boolean {
    return this.#operations > 0;
  }
 
  /**
   * Creates a new IndexedKv instance.
   * @param name - The name of the object store.
   * @param version - The version of the database.
   * @param dbName - The name of the database.
   * @param seeds - An optional array of seed objects to initialize the database.
   */
  constructor(
    protected name: string,
    protected version = 1,
    dbName = `${name}.kv`,
    protected seeds?: KVSeed<T>[]
  ) {
    super(dbName, version);
    this.init().open();
  }
 
  /**
   * Sets up the initial data by reading from the IndexedDB.
   * @protected
   */
  protected async setup() {
    await new Promise((resolve, reject) => {
      const request = this.reader.openCursor();
 
      request.onsuccess = (event) => {
        const cursor = (event.target as IDBRequest)?.result;
 
        if (cursor) {
          this.#storage.set(cursor.key, cursor.value);
          cursor.continue();
        } else {
          resolve(true);
        }
      };
 
      request.onerror = () => {
        reject(request.error);
      };
    });
  }
 
  protected finalize(): void {
    if (!this.#storage.size && this.seeds) {
      for (const [key, value] of this.seeds) {
        this.set(key, value);
      }
    }
 
    super.finalize();
  }
 
  /**
   * Waits for all ongoing operations to complete.
   * @returns A promise that resolves when all operations are completed.
   */
  public async completed(): Promise<true> {
    if (this.busy) {
      return await new Promise((resolve) => {
        const handler = () => {
          if (!this.busy) {
            resolve(true);
            this.#awaiters.delete(handler);
          }
        };
 
        this.#awaiters.add(handler);
      });
    }
 
    return true;
  }
 
  /**
   * Upgrades the database by creating the object store if it doesn't exist.
   * @param event - The version change event.
   * @protected
   */
  protected upgrade(event: IDBVersionChangeEvent): Promise<void> | void {
    const db = (event.target as IDBOpenDBRequest)?.result as IDBDatabase;
    if (db && !db.objectStoreNames.contains(this.name)) {
      db.createObjectStore(this.name);
    }
  }
 
  /**
   * Publishes an event to subscribers.
   * @param event - The event to publish.
   * @protected
   */
  protected publish(event: KVEvent<T>) {
    super.publish(event as DBEvent);
  }
 
  /**
   * Gets the value associated with the specified key.
   * @param name - The key to look up.
   * @returns The value associated with the key, or undefined if not found.
   */
  public get(name: string): T | undefined {
    return this.#storage.get(name);
  }
 
  /**
   * Sets the value for the specified key.
   * @param key - The key to set.
   * @param value - The value to set.
   * @param onerror - Optional error handler.
   * @returns An operation object with a promise that resolves when the operation completes.
   */
  public set(key: string, value: T, onerror?: (error: Error) => void): Operation {
    if (this.status !== IDBStatus.Open) {
      const error = new Error(`Database is in "${this.status}" state.`);
      captureStack.error.external(`Cannot perform "set" operation during "${this.status}" state.`, error, this.set);
      onerror?.(error);
      return { promise: () => Promise.resolve() };
    }
 
    const current = this.#storage.get(key);
    let resolved: Error | boolean = false;
    let resolver: () => void;
    let rejector: (error: Error) => void;
 
    this.setBusy();
    this.#storage.set(key, value);
 
    put(this.writer, key, value)
      .then(() => {
        this.publish({ type: 'set', key, value });
        resolver?.();
        resolved = true;
      })
      .catch((error) => {
        captureStack.error.external(`Unable to set value of "${key}":`, error, put, this.set);
 
        if (current) {
          this.#storage.set(key, current);
        } else {
          this.#storage.delete(key);
        }
 
        onerror?.(error);
        rejector?.(error);
        resolved = error;
      })
      .finally(() => {
        this.setFree();
      });
 
    return {
      promise: () => {
        if (resolved) {
          return resolved === true ? Promise.resolve() : Promise.reject(resolved);
        }
 
        return new Promise((resolve, reject) => {
          resolver = resolve as never;
          rejector = reject;
        });
      },
    } as Operation;
  }
 
  /**
   * Deletes the value associated with the specified key.
   * @param key - The key to delete.
   * @param onerror - Optional error handler.
   * @returns An operation object with a promise that resolves when the operation completes.
   */
  public delete(key: string, onerror?: (error: Error) => void): Operation {
    if (this.status !== IDBStatus.Open) {
      const error = new Error(`Database is in "${this.status}" state.`);
      captureStack.error.external(
        `Cannot perform "delete" operation during "${this.status}" state.`,
        error,
        this.delete
      );
      onerror?.(error);
      return { promise: () => Promise.resolve() };
    }
 
    const current = this.#storage.get(key);
 
    if (current) {
      let resolved: Error | boolean = false;
      let resolver: (value: true) => void;
      let rejector: (error: Error) => void;
 
      this.setBusy();
      this.#storage.delete(key);
 
      remove(this.writer, key)
        .then(() => {
          this.publish({ type: 'delete', key });
          resolver?.(true);
          resolved = true;
        })
        .catch((error) => {
          captureStack.error.external(`Unable to delete "${key}":`, error, remove, this.delete);
          this.#storage.set(key, current);
 
          onerror?.(error);
          rejector?.(error);
          resolved = error;
        })
        .finally(() => {
          this.setFree();
        });
 
      return {
        promise: () => {
          if (resolved) {
            return resolved === true ? Promise.resolve() : Promise.reject(resolved);
          }
 
          return new Promise((resolve, reject) => {
            resolver = resolve as never;
            rejector = reject as never;
          });
        },
      } as Operation;
    }
 
    return { promise: () => Promise.resolve() };
  }
 
  /**
   * Subscribes to changes in the store.
   * @param handler - The handler function to call when changes occur.
   * @returns A function to unsubscribe from changes.
   */
  public subscribe(handler: KVSubscriber<T>): DBUnsubscribe {
    return super.subscribe(handler);
  }
 
  /**
   * Increments the operation counter.
   * @private
   */
  private setBusy() {
    this.#operations += 1;
  }
 
  /**
   * Decrements the operation counter and notifies awaiters.
   * @private
   */
  private setFree() {
    this.#operations -= 1;
 
    if (this.#operations <= 0) {
      this.#operations = 0;
    }
 
    this.#awaiters.forEach((handler) => handler());
  }
}
 
const KV_STORES = new Map<string, KVFn>();
 
/**
 * Creates a key-value store function that provides reactive state management synchronized with IndexedDB.
 *
 * This factory function initializes an IndexedKv storage instance and returns a KVFn function
 * that can be used to create reactive state objects. Each state object automatically syncs
 * with the underlying IndexedDB storage and maintains its own initialization status.
 *
 * The returned function manages state lifecycle including:
 * - Caching states by key to prevent duplicate instances
 * - Tracking usage counts for memory management
 * - Automatically synchronizing reactive changes to IndexedDB with debouncing
 * - Handling database initialization and readiness states
 *
 * @param name - The name of the object store in IndexedDB
 * @param version - The version of the database schema (default: 1)
 * @param dbName - The name of the database (default: `${name}.kv`)
 * @param seeds - An initial set of key-value pairs to seed the database.
 * @returns A KVFn function that can create and manage reactive key-value states
 */
export function createKVStore<T extends Storable>(
  name: string,
  version = 1,
  dbName = `${name}.kv`,
  seeds?: KVSeed<T>[]
): KVFn {
  const key = `${name}@${version}`;
 
  if (KV_STORES.has(key)) {
    return KV_STORES.get(key) as KVFn;
  }
 
  const store = new IndexedKv<T>(name, version, dbName, seeds);
 
  const stateMap = new Map<string, KVState<Storable>>();
  const stateUsage = new Map<KVState<Storable>, number>();
  const stateSubscriptions = new WeakMap<KVState<Storable>, StateUnsubscribe>();
 
  function kvFn(key: string, init: T): KVState<T> {
    // Make sure access to the same key is pointing to the same state.
    // One key will have one state that in charge of synchronization.
    if (stateMap.has(key)) {
      const state = stateMap.get(key) as KVState<T>;
 
      // Track the usage count to prevent unnecessary unsubscribes.
      const usage = stateUsage.get(state) as number;
      stateUsage.set(state, usage + 1);
 
      return state;
    }
 
    const state = anchor.raw({ data: init, status: 'init' } as KVState<T>);
    const [schedule] = microtask(DB_SYNC_DELAY);
 
    const readKv = () => {
      const value = store.get(key);
 
      // Maintain optimistic behavior.
      state.status = 'ready';
 
      if (typeof value !== 'undefined') {
        state.data = value;
      } else {
        store.set(key, init, (error) => {
          anchor.assign(state, { error, status: 'error' });
        });
      }
 
      // Create synchronization if the given state data is a linkable value.
      const stateUnsubscribe = derive(state, (snapshot, event) => {
        if (event.type !== 'init' && event.keys.includes('data')) {
          schedule(() => {
            const prev = event.prev as T;
            store.set(key, snapshot.data, (error) => {
              anchor.assign(state, { data: prev, error, status: 'error' });
            });
          });
        }
      });
 
      const unsubscribe = () => {
        let usage = stateUsage.get(state) as number;
 
        usage -= 1;
 
        if (usage <= 0) {
          stateUnsubscribe();
 
          stateMap.delete(key);
          stateUsage.delete(state);
          stateSubscriptions.delete(state);
        } else {
          stateUsage.set(state, usage);
        }
      };
 
      stateSubscriptions.set(state, unsubscribe);
    };
 
    if (store.status === IDBStatus.Init) {
      const unsubscribe = store.subscribe((event) => {
        if (event.type === IDBStatus.Open) {
          readKv();
          unsubscribe();
        } else if (event.type === IDBStatus.Closed) {
          anchor.assign(state, { status: 'error', error: store.error });
          unsubscribe();
        }
      });
    } else {
      readKv();
    }
 
    if (!stateMap.has(key)) {
      stateMap.set(key, state);
      stateUsage.set(state, 1);
    }
 
    return state;
  }
 
  kvFn.store = () => {
    return store;
  };
 
  kvFn.leave = <T extends Storable>(state: KVState<T>) => {
    if (stateSubscriptions.has(state)) {
      stateSubscriptions.get(state)?.();
    }
  };
 
  kvFn.remove = (key: string) => {
    const state = stateMap.get(key);
 
    if (state) {
      state.status = 'removed';
    }
 
    store
      .delete(key)
      .promise()
      .then(() => {
        if (state) {
          const unsubscribe = stateSubscriptions.get(state);
 
          stateUsage.set(state, 1);
          unsubscribe?.();
 
          anchor.assign(state, { data: undefined });
        }
      })
      .catch((error) => {
        if (state) {
          anchor.assign(state, { error, status: 'error' });
        }
      });
  };
 
  kvFn.ready = async () => {
    await store.promise();
    return await store.completed();
  };
 
  KV_STORES.set(key, kvFn as KVFn);
 
  return kvFn as KVFn;
}
 
// Creates default KV store.
export const kv = createKVStore('anchor');