All files / json-crdt-server/src/routes/block/methods pull.ts

100% Statements 9/9
100% Branches 0/0
100% Functions 3/3
100% Lines 9/9

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 574x     4x 4x 93x   93x                                                     93x                       93x           93x 468x      
import {BlockIdRef, BlockCurRef, BlockBatchRef, BlockSnapshotRef} from '../schema';
import type {RouteDeps, Router, RouterBase} from '../../types';
 
export const pull =
  ({t, services}: RouteDeps) =>
  <R extends RouterBase>(r: Router<R>) => {
    // biome-ignore format: props
    const Request = t.Object(
      t.Key('id', BlockIdRef).options({
        title: 'Block ID',
        description: 'The ID of the block.',
      }),
      t.Key('seq', BlockCurRef).options({
        title: 'Last Known Sequence Number',
        description: 'The sequence number that the client is caught up to. If '
          + 'the client is not caught up to the latest state of the block, the '
          + 'server will return a list of batches that the client needs to apply '
          + 'to get to the latest state. If the client is too far behind, the '
          + 'server will return a snapshot of the block.'
          + '\n\n'
          + 'The initial value should be `-1`.'
          + '\n\n'
          + 'If the client sequence number is ahead of the server, it means the '
          + 'block has been deleted and re-created with the same ID. In this case, '
          + 'the server sends back the latest snapshot, extra batches, if any. '
          + 'The client should reset its state and start from the beginning.',
      }),
      t.KeyOpt('create', t.bool).options({
        title: 'Create Block',
        description: 'Whether to create a new block if it does not exist.',
      }),
    );
 
    // biome-ignore format: props
    const Response = t.Object(
      t.Key('batches', t.Array(BlockBatchRef)).options({
        title: 'Batches',
        description: 'List of batches that the client need to apply to the local state. ' +
          'Or, if `snapshot` is provided, the list of batches that the client need to apply to the snapshot to get to the latest state.',
      }),
      t.KeyOpt('snapshot', BlockSnapshotRef).options({
        title: 'Snapshot',
        description: 'The state of the block right before the first batch in the result.',
      }),
    );
 
    const Func = t.Function(Request, Response).options({
      title: 'Pull Block',
      intro: 'Catch up to the latest state of a block.',
      description: 'Returns a list of most recent change batches or a snapshot of a block.',
    });
 
    return r.add('block.pull', Func, async ({id, seq, create}) => {
      return await services.blocks.pull(id, seq, !!create);
    });
  };