All files / json-crdt-repo/src/remote DemoServerRemoteHistory.ts

95.45% Statements 21/22
71.42% Branches 5/7
100% Functions 9/9
100% Lines 21/21

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 12314x                                             14x   117x 117x       28x                     195x                   30x 29x                                           90x             90x             10x       1x 1x         1x               2x 2x 2x 2x           2x     117x 72x      
import {shareByKey} from '../util/rx/shareByKey';
import type {
  ServerBlock,
  ServerSnapshot,
  ServerPatch,
  ServerCursor,
  ServerHistory,
  ServerBatch,
  ServerEvent,
} from './types';
 
export interface DemoServerClient {
  call(method: string, req?: unknown): Promise<any>;
  call$(method: string, req?: unknown): any;
}
 
export type Cursor = ServerCursor;
export type DemoServerBlock = ServerBlock;
export type DemoServerSnapshot = ServerSnapshot;
export type DemoServerBatch = ServerBatch;
export type DemoServerPatch = ServerPatch;
export type DemoServerEvent = ServerEvent;
 
export class DemoServerRemoteHistory implements ServerHistory {
  constructor(
    protected readonly client: DemoServerClient,
    protected readonly clientId: number = 0,
  ) {}
 
  public async read(id: string): Promise<{block: DemoServerBlock}> {
    return await this.client.call('block.get', {id});
  }
 
  public async pull(
    id: string,
    seq: Cursor = -1,
    create = false,
  ): Promise<{
    batches: DemoServerBatch[];
    snapshot?: DemoServerSnapshot;
  }> {
    return await this.client.call('block.pull', {id, seq, create});
  }
 
  public async create(
    id: string,
    batch?: Pick<DemoServerBatch, 'patches'>,
  ): Promise<{
    snapshot: Omit<DemoServerSnapshot, 'blob'>;
    batch: Omit<DemoServerBatch, 'patches'>;
  }> {
    const res = await this.client.call('block.new', batch ? {id, batch} : {id});
    return {
      snapshot: {
        seq: res.snapshot.seq,
      },
      batch: {
        seq: res.snapshot.seq,
        ts: res.snapshot.ts,
      },
    };
  }
 
  public async update(
    id: string,
    batch: Pick<DemoServerBatch, 'patches'>,
    seq?: number,
  ): Promise<{
    batch: Omit<DemoServerBatch, 'patches'>;
    pull?: {
      batches: DemoServerBatch[];
      snapshot?: DemoServerSnapshot;
    };
  }> {
    const res = await this.client.call('block.upd', {
      create: true,
      id,
      batch,
      seq,
      clientId: this.clientId,
    });
    return {
      batch: res.batch,
      pull: res.pull,
    };
  }
 
  public async delete(id: string): Promise<void> {
    await this.client.call('block.del', {id});
  }
 
  public async scanFwd(id: string, seq: Cursor): Promise<{batches: DemoServerBatch[]}> {
    const limit = 100;
    const res = await this.client.call('block.scan', {
      id,
      seq: seq + 1,
      limit,
    });
    return res;
  }
 
  public async scanBwd(
    id: string,
    seq: Cursor,
    snapshot?: boolean,
  ): Promise<{batches: DemoServerBatch[]; snapshot?: DemoServerSnapshot}> {
    Iif (seq <= 0) throw new Error('INV_SEQ');
    const startSeq = Math.max(0, seq - 100);
    const limit = seq - startSeq;
    const res = await this.client.call('block.scan', {
      id,
      seq: startSeq,
      limit,
      snapshot: !!snapshot,
    });
    return res;
  }
 
  public readonly listen = shareByKey((id: string) =>
    this.client.call$('block.listen', {id, clientId: this.clientId}),
  ) as (id: string) => import('rxjs').Observable<{event: ServerEvent}>;
}