All files / src/persistence/pouchDbStores pouchDbSupplyDistributionStores.ts

40% Statements 6/15
0% Branches 0/1
0% Functions 0/3
38.46% Lines 5/13

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  41x 41x       41x 41x         41x                                          
import { CreatePouchDbStoresDependencies } from './types';
import { EMPTY, map } from 'rxjs';
import { PouchDbDocumentStore } from './PouchDbDocumentStore';
import { StakeSummary, SupplySummary } from '@cardano-sdk/core';
import { SupplyDistributionStores } from '../types';
 
export class PouchDbStakeSummaryStore extends PouchDbDocumentStore<StakeSummary> {}
export class PouchDbSupplySummaryStore extends PouchDbDocumentStore<SupplySummary> {}
 
/**
 * @param {string} baseName used to derive underlying db names, like a network ID and/or wallet name
 */
export const createPouchDbSupplyDistributionStores = (
  baseName: string,
  { logger }: CreatePouchDbStoresDependencies
): SupplyDistributionStores => {
  const baseDbName = baseName.replace(/[^\da-z]/gi, '');
  const docsDbName = `${baseDbName}SupplyDistribution`;
  return {
    destroy() {
      Iif (!this.destroyed) {
        this.destroyed = true;
        logger.debug('Destroying PouchDb SupplyDistributionStores...');
        // since the database of document stores is shared, destroying any document store destroys all of them
        return this.lovelaceSupply.destroy().pipe(map(() => void 0));
      }
      return EMPTY;
    },
    destroyed: false,
    lovelaceSupply: new PouchDbSupplySummaryStore(docsDbName, 'lovelaceSupply', logger),
    stake: new PouchDbStakeSummaryStore(docsDbName, 'stake', logger)
  };
};