document-drive 1.17.2 → 1.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,15 +22,15 @@ import type {
22
22
  } from "document-model/document";
23
23
  import { Unsubscribe } from "nanoevents";
24
24
  import { BaseDocumentDriveServer } from ".";
25
- import {
26
- IReceiver as IInternalListener,
27
- IInternalTransmitter,
28
- } from "./listener/transmitter/internal";
29
25
  import { IReadModeDriveServer } from "../read-mode/types";
30
26
  import { RunAsap } from "../utils";
31
27
  import { IDefaultDrivesManager } from "../utils/default-drives-manager";
32
28
  import { DriveInfo } from "../utils/graphql";
33
29
  import { OperationError, SynchronizationUnitNotFoundError } from "./error";
30
+ import {
31
+ IReceiver as IInternalListener,
32
+ IInternalTransmitter,
33
+ } from "./listener/transmitter/internal";
34
34
  import {
35
35
  ITransmitter,
36
36
  PullResponderTrigger,
@@ -105,6 +105,7 @@ export type Listener = {
105
105
  system: boolean;
106
106
  filter: ListenerFilter;
107
107
  callInfo?: ListenerCallInfo;
108
+ transmitter?: ITransmitter;
108
109
  };
109
110
 
110
111
  export type CreateListenerInput = {
@@ -317,137 +318,150 @@ export type GetStrandsOptions = {
317
318
  fromRevision?: number;
318
319
  };
319
320
 
320
- export abstract class AbstractDocumentDriveServer {
321
- /** Public methods **/
322
- abstract initialize(): Promise<Error[] | null>;
323
- abstract setDocumentModels(models: DocumentModel[]): void;
324
- abstract getDrives(): Promise<string[]>;
325
- abstract addDrive(drive: DriveInput): Promise<DocumentDriveDocument>;
326
- abstract addRemoteDrive(
321
+ export type ListenerManagerOptions = {
322
+ sequentialUpdates?: boolean;
323
+ };
324
+
325
+ export const DefaultListenerManagerOptions = {
326
+ sequentialUpdates: true,
327
+ };
328
+
329
+ type PublicKeys<T> = {
330
+ [K in keyof T]: T extends { [P in K]: T[K] } ? K : never;
331
+ }[keyof T];
332
+
333
+ type PublicPart<T> = Pick<T, PublicKeys<T>>;
334
+
335
+ export interface IBaseDocumentDriveServer {
336
+ initialize(): Promise<Error[] | null>;
337
+ setDocumentModels(models: DocumentModel[]): void;
338
+ getDrives(): Promise<string[]>;
339
+ addDrive(input: DriveInput): Promise<DocumentDriveDocument>;
340
+ addRemoteDrive(
327
341
  url: string,
328
342
  options: RemoteDriveOptions,
329
343
  ): Promise<DocumentDriveDocument>;
330
- abstract deleteDrive(id: string): Promise<void>;
331
- abstract getDrive(
332
- id: string,
344
+ deleteDrive(driveId: string): Promise<void>;
345
+ getDrive(
346
+ driveId: string,
333
347
  options?: GetDocumentOptions,
334
348
  ): Promise<DocumentDriveDocument>;
335
349
 
336
- abstract getDriveBySlug(slug: string): Promise<DocumentDriveDocument>;
350
+ getDriveBySlug(slug: string): Promise<DocumentDriveDocument>;
337
351
 
338
- abstract getDocuments(drive: string): Promise<string[]>;
339
- abstract getDocument(
340
- drive: string,
341
- id: string,
352
+ getDocuments(driveId: string): Promise<string[]>;
353
+ getDocument(
354
+ driveId: string,
355
+ documentId: string,
342
356
  options?: GetDocumentOptions,
343
357
  ): Promise<Document>;
344
358
 
345
- abstract addOperation(
346
- drive: string,
347
- id: string,
359
+ addOperation(
360
+ driveId: string,
361
+ documentId: string,
348
362
  operation: Operation,
349
363
  options?: AddOperationOptions,
350
364
  ): Promise<IOperationResult>;
351
365
 
352
- abstract addOperations(
353
- drive: string,
354
- id: string,
366
+ addOperations(
367
+ driveId: string,
368
+ documentId: string,
355
369
  operations: Operation[],
356
370
  options?: AddOperationOptions,
357
371
  ): Promise<IOperationResult>;
358
372
 
359
- abstract queueOperation(
360
- drive: string,
361
- id: string,
373
+ queueOperation(
374
+ driveId: string,
375
+ documentId: string,
362
376
  operation: Operation,
363
377
  options?: AddOperationOptions,
364
378
  ): Promise<IOperationResult>;
365
379
 
366
- abstract queueOperations(
367
- drive: string,
368
- id: string,
380
+ queueOperations(
381
+ driveId: string,
382
+ documentId: string,
369
383
  operations: Operation[],
370
384
  options?: AddOperationOptions,
371
385
  ): Promise<IOperationResult>;
372
386
 
373
- abstract queueAction(
374
- drive: string,
375
- id: string,
387
+ queueAction(
388
+ driveId: string,
389
+ documentId: string,
376
390
  action: Action,
377
391
  options?: AddOperationOptions,
378
392
  ): Promise<IOperationResult>;
379
393
 
380
- abstract queueActions(
381
- drive: string,
382
- id: string,
394
+ queueActions(
395
+ driveId: string,
396
+ documentId: string,
383
397
  actions: Action[],
384
398
  options?: AddOperationOptions,
385
399
  ): Promise<IOperationResult>;
386
400
 
387
- abstract addDriveOperation(
388
- drive: string,
401
+ addDriveOperation(
402
+ driveId: string,
389
403
  operation: Operation<DocumentDriveAction | BaseAction>,
390
404
  options?: AddOperationOptions,
391
405
  ): Promise<IOperationResult<DocumentDriveDocument>>;
392
- abstract addDriveOperations(
393
- drive: string,
406
+ addDriveOperations(
407
+ driveId: string,
394
408
  operations: Operation<DocumentDriveAction | BaseAction>[],
395
409
  options?: AddOperationOptions,
396
410
  ): Promise<IOperationResult<DocumentDriveDocument>>;
397
411
 
398
- abstract queueDriveOperation(
399
- drive: string,
412
+ queueDriveOperation(
413
+ driveId: string,
400
414
  operation: Operation<DocumentDriveAction | BaseAction>,
401
415
  options?: AddOperationOptions,
402
416
  ): Promise<IOperationResult<DocumentDriveDocument>>;
403
417
 
404
- abstract queueDriveOperations(
405
- drive: string,
418
+ queueDriveOperations(
419
+ driveId: string,
406
420
  operations: Operation<DocumentDriveAction | BaseAction>[],
407
421
  options?: AddOperationOptions,
408
422
  ): Promise<IOperationResult<DocumentDriveDocument>>;
409
423
 
410
- abstract queueDriveAction(
411
- drive: string,
424
+ queueDriveAction(
425
+ driveId: string,
412
426
  action: DocumentDriveAction | BaseAction,
413
427
  options?: AddOperationOptions,
414
428
  ): Promise<IOperationResult<DocumentDriveDocument>>;
415
429
 
416
- abstract queueDriveActions(
417
- drive: string,
430
+ queueDriveActions(
431
+ driveId: string,
418
432
  actions: Array<DocumentDriveAction | BaseAction>,
419
433
  options?: AddOperationOptions,
420
434
  ): Promise<IOperationResult<DocumentDriveDocument>>;
421
435
 
422
- abstract addAction(
423
- drive: string,
424
- id: string,
436
+ addAction(
437
+ driveId: string,
438
+ documentId: string,
425
439
  action: Action,
426
440
  options?: AddOperationOptions,
427
441
  ): Promise<IOperationResult>;
428
- abstract addActions(
429
- drive: string,
430
- id: string,
442
+ addActions(
443
+ driveId: string,
444
+ documentId: string,
431
445
  actions: Action[],
432
446
  options?: AddOperationOptions,
433
447
  ): Promise<IOperationResult>;
434
448
 
435
- abstract addDriveAction(
436
- drive: string,
449
+ addDriveAction(
450
+ driveId: string,
437
451
  action: DocumentDriveAction | BaseAction,
438
452
  options?: AddOperationOptions,
439
453
  ): Promise<IOperationResult<DocumentDriveDocument>>;
440
- abstract addDriveActions(
441
- drive: string,
454
+ addDriveActions(
455
+ driveId: string,
442
456
  actions: (DocumentDriveAction | BaseAction)[],
443
457
  options?: AddOperationOptions,
444
458
  ): Promise<IOperationResult<DocumentDriveDocument>>;
445
459
 
446
- abstract getSyncStatus(
460
+ getSyncStatus(
447
461
  syncUnitId: string,
448
462
  ): SyncStatus | SynchronizationUnitNotFoundError;
449
463
 
450
- abstract addInternalListener(
464
+ addInternalListener(
451
465
  driveId: string,
452
466
  receiver: IInternalListener,
453
467
  options: {
@@ -459,7 +473,7 @@ export abstract class AbstractDocumentDriveServer {
459
473
  ): Promise<IInternalTransmitter>;
460
474
 
461
475
  /** Synchronization methods */
462
- abstract getSynchronizationUnits(
476
+ getSynchronizationUnits(
463
477
  driveId: string,
464
478
  documentId?: string[],
465
479
  scope?: string[],
@@ -468,13 +482,13 @@ export abstract class AbstractDocumentDriveServer {
468
482
  loadedDrive?: DocumentDriveDocument,
469
483
  ): Promise<SynchronizationUnit[]>;
470
484
 
471
- abstract getSynchronizationUnit(
485
+ getSynchronizationUnit(
472
486
  driveId: string,
473
487
  syncId: string,
474
488
  loadedDrive?: DocumentDriveDocument,
475
489
  ): Promise<SynchronizationUnit | undefined>;
476
490
 
477
- abstract getSynchronizationUnitsIds(
491
+ getSynchronizationUnitsIds(
478
492
  driveId: string,
479
493
  documentId?: string[],
480
494
  scope?: string[],
@@ -482,7 +496,7 @@ export abstract class AbstractDocumentDriveServer {
482
496
  documentType?: string[],
483
497
  ): Promise<SynchronizationUnitQuery[]>;
484
498
 
485
- abstract getOperationData(
499
+ getOperationData(
486
500
  driveId: string,
487
501
  syncId: string,
488
502
  filter: GetStrandsOptions,
@@ -490,117 +504,73 @@ export abstract class AbstractDocumentDriveServer {
490
504
  ): Promise<OperationUpdate[]>;
491
505
 
492
506
  /** Internal methods **/
493
- protected abstract createDocument(
494
- drive: string,
495
- document: CreateDocumentInput,
496
- ): Promise<Document>;
497
- protected abstract deleteDocument(drive: string, id: string): Promise<void>;
498
-
499
- protected abstract getDocumentModel(documentType: string): DocumentModel;
500
- abstract getDocumentModels(): DocumentModel[];
501
-
502
- /** Event methods **/
503
- protected abstract emit<K extends keyof DriveEvents>(
504
- event: K,
505
- ...args: Parameters<DriveEvents[K]>
506
- ): void;
507
- abstract on<K extends keyof DriveEvents>(
508
- event: K,
509
- cb: DriveEvents[K],
510
- ): Unsubscribe;
511
-
512
- abstract getTransmitter(
507
+ getDocumentModels(): DocumentModel[];
508
+
509
+ getTransmitter(
513
510
  driveId: string,
514
511
  listenerId: string,
515
512
  ): Promise<ITransmitter | undefined>;
516
513
 
517
- abstract clearStorage(): Promise<void>;
514
+ clearStorage(): Promise<void>;
518
515
 
519
- abstract registerPullResponderTrigger(
516
+ registerPullResponderTrigger(
520
517
  id: string,
521
518
  url: string,
522
519
  options: Pick<RemoteDriveOptions, "pullFilter" | "pullInterval">,
523
520
  ): Promise<PullResponderTrigger>;
524
- }
525
-
526
- export type ListenerManagerOptions = {
527
- sequentialUpdates?: boolean;
528
- };
529
-
530
- export const DefaultListenerManagerOptions = {
531
- sequentialUpdates: true,
532
- };
533
-
534
- type PublicKeys<T> = {
535
- [K in keyof T]: T extends { [P in K]: T[K] } ? K : never;
536
- }[keyof T];
537
-
538
- type PublicPart<T> = Pick<T, PublicKeys<T>>;
539
521
 
540
- export type IBaseDocumentDriveServer = PublicPart<AbstractDocumentDriveServer>;
522
+ on<K extends keyof DriveEvents>(event: K, cb: DriveEvents[K]): Unsubscribe;
523
+ }
541
524
 
542
525
  export type IDocumentDriveServer = IBaseDocumentDriveServer &
543
526
  IDefaultDrivesManager &
544
527
  IReadModeDriveServer;
545
528
 
546
- export abstract class BaseListenerManager {
547
- protected drive: IBaseDocumentDriveServer;
548
- protected listenerState = new Map<string, Map<string, ListenerState>>();
549
- protected options: ListenerManagerOptions;
550
- protected transmitters: Record<
551
- DocumentDriveState["id"],
552
- Record<Listener["listenerId"], ITransmitter>
553
- > = {};
554
-
555
- constructor(
556
- drive: IBaseDocumentDriveServer,
557
- listenerState = new Map<string, Map<string, ListenerState>>(),
558
- options: ListenerManagerOptions = DefaultListenerManagerOptions,
559
- ) {
560
- this.drive = drive;
561
- this.listenerState = listenerState;
562
- this.options = { ...DefaultListenerManagerOptions, ...options };
563
- }
564
-
565
- abstract initDrive(drive: DocumentDriveDocument): Promise<void>;
566
- abstract removeDrive(driveId: DocumentDriveState["id"]): Promise<void>;
567
-
568
- abstract driveHasListeners(driveId: string): boolean;
569
- abstract addListener(listener: Listener): Promise<ITransmitter>;
570
- abstract removeListener(
571
- driveId: string,
572
- listenerId: string,
573
- ): Promise<boolean>;
574
- abstract getListener(
575
- driveId: string,
576
- listenerId: string,
577
- ): Promise<ListenerState | undefined>;
529
+ export type DriveUpdateErrorHandler = (
530
+ error: Error,
531
+ driveId: string,
532
+ listener: ListenerState,
533
+ ) => void;
578
534
 
579
- abstract getTransmitter(
580
- driveId: string,
581
- listenerId: string,
582
- ): Promise<ITransmitter | undefined>;
535
+ export interface IListenerManager {
536
+ initialize(handler: DriveUpdateErrorHandler): Promise<void>;
583
537
 
584
- abstract getStrands(
538
+ removeDrive(driveId: DocumentDriveState["id"]): Promise<void>;
539
+ driveHasListeners(driveId: string): boolean;
540
+
541
+ setListener(driveId: string, listener: Listener): Promise<void>;
542
+ removeListener(driveId: string, listenerId: string): Promise<boolean>;
543
+ getListenerState(driveId: string, listenerId: string): ListenerState;
544
+
545
+ getStrands(
585
546
  driveId: string,
586
547
  listenerId: string,
587
548
  options?: GetStrandsOptions,
588
549
  ): Promise<StrandUpdate[]>;
589
-
590
- abstract updateSynchronizationRevisions(
550
+ updateSynchronizationRevisions(
591
551
  driveId: string,
592
552
  syncUnits: SynchronizationUnit[],
593
553
  source: StrandUpdateSource,
594
554
  willUpdate?: (listeners: Listener[]) => void,
595
555
  onError?: (error: Error, driveId: string, listener: ListenerState) => void,
556
+ forceSync?: boolean,
596
557
  ): Promise<ListenerUpdate[]>;
597
-
598
- abstract updateListenerRevision(
558
+ updateListenerRevision(
599
559
  listenerId: string,
600
560
  driveId: string,
601
561
  syncId: string,
602
562
  listenerRev: number,
603
563
  ): Promise<void>;
564
+
565
+ // todo: re-evaluate
566
+ getListenerSyncUnitIds(
567
+ driveId: string,
568
+ listenerId: string,
569
+ ): Promise<SynchronizationUnitQuery[]>;
570
+ removeSyncUnits(
571
+ driveId: string,
572
+ syncUnits: Pick<SynchronizationUnit, "syncId">[],
573
+ ): Promise<void>;
604
574
  }
605
575
 
606
576
  export type ListenerStatus =