All files / json-crdt/extensions Extension.ts

100% Statements 8/8
100% Branches 0/0
100% Functions 2/2
100% Lines 8/8

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 3048x                 48x                 176x 176x 176x 176x 176x       1846x      
import {type NodeBuilder, s, type nodes} from '../../json-crdt-patch';
import type {ExtNode} from './ExtNode';
import type {ModelApi} from '../model';
import type {JsonNode} from '../nodes';
import type {JsonNodeToSchema} from '../schema/types';
import type {ExtApi} from './types';
 
export type AnyExtension = Extension<any, any, any, any, any, any>;
 
export class Extension<
  Id extends number,
  DataNode extends JsonNode,
  ENode extends ExtNode<DataNode, any>,
  EApi extends ExtApi<ENode>,
  DataArgs extends any[] = any[],
  DataSchema extends NodeBuilder = JsonNodeToSchema<DataNode>,
> {
  constructor(
    public readonly id: Id,
    public readonly name: string,
    public readonly Node: new (data: DataNode) => ENode,
    public readonly Api: new (node: ENode, api: ModelApi<any>) => EApi,
    public readonly schema: (...args: DataArgs) => DataSchema,
  ) {}
 
  public new(...args: DataArgs): nodes.ext<Id, DataSchema> {
    return s.ext<Id, DataSchema>(this.id, this.schema(...args));
  }
}