@@ -369,6 +369,28 @@ export interface PresenceChangedEvent<P extends Indexable>
369
369
value : { clientID : ActorID ; presence : P } ;
370
370
}
371
371
372
+ type DocEventCallbackMap < P extends Indexable > = {
373
+ default : NextFn <
374
+ | SnapshotEvent
375
+ | LocalChangeEvent < OperationInfo , P >
376
+ | RemoteChangeEvent < OperationInfo , P >
377
+ > ;
378
+ presence : NextFn <
379
+ | InitializedEvent < P >
380
+ | WatchedEvent < P >
381
+ | UnwatchedEvent < P >
382
+ | PresenceChangedEvent < P >
383
+ > ;
384
+ 'my-presence' : NextFn < InitializedEvent < P > | PresenceChangedEvent < P > > ;
385
+ others : NextFn < WatchedEvent < P > | UnwatchedEvent < P > | PresenceChangedEvent < P > > ;
386
+ connection : NextFn < ConnectionChangedEvent > ;
387
+ sync : NextFn < SyncStatusChangedEvent > ;
388
+ all : NextFn < TransactionEvent < P > > ;
389
+ } ;
390
+ export type DocEventTopic = keyof DocEventCallbackMap < never > ;
391
+ export type DocEventCallback < P extends Indexable > =
392
+ DocEventCallbackMap < P > [ DocEventTopic ] ;
393
+
372
394
/**
373
395
* Indexable key, value
374
396
* @public
@@ -710,7 +732,7 @@ export class Document<T, P extends Indexable = Indexable> {
710
732
* The callback will be called when the document is changed.
711
733
*/
712
734
public subscribe (
713
- nextOrObserver : Observer < DocEvent > | NextFn < DocEvent > ,
735
+ next : DocEventCallbackMap < P > [ 'default' ] ,
714
736
error ?: ErrorFn ,
715
737
complete ?: CompleteFn ,
716
738
) : Unsubscribe ;
@@ -721,7 +743,7 @@ export class Document<T, P extends Indexable = Indexable> {
721
743
*/
722
744
public subscribe (
723
745
type : 'presence' ,
724
- next : NextFn < DocEvent < P > > ,
746
+ next : DocEventCallbackMap < P > [ 'presence' ] ,
725
747
error ?: ErrorFn ,
726
748
complete ?: CompleteFn ,
727
749
) : Unsubscribe ;
@@ -731,7 +753,7 @@ export class Document<T, P extends Indexable = Indexable> {
731
753
*/
732
754
public subscribe (
733
755
type : 'my-presence' ,
734
- next : NextFn < DocEvent < P > > ,
756
+ next : DocEventCallbackMap < P > [ 'my-presence' ] ,
735
757
error ?: ErrorFn ,
736
758
complete ?: CompleteFn ,
737
759
) : Unsubscribe ;
@@ -742,7 +764,7 @@ export class Document<T, P extends Indexable = Indexable> {
742
764
*/
743
765
public subscribe (
744
766
type : 'others' ,
745
- next : NextFn < DocEvent < P > > ,
767
+ next : DocEventCallbackMap < P > [ 'others' ] ,
746
768
error ?: ErrorFn ,
747
769
complete ?: CompleteFn ,
748
770
) : Unsubscribe ;
@@ -752,7 +774,7 @@ export class Document<T, P extends Indexable = Indexable> {
752
774
*/
753
775
public subscribe (
754
776
type : 'connection' ,
755
- next : NextFn < DocEvent < P > > ,
777
+ next : DocEventCallbackMap < P > [ 'connection' ] ,
756
778
error ?: ErrorFn ,
757
779
complete ?: CompleteFn ,
758
780
) : Unsubscribe ;
@@ -762,7 +784,7 @@ export class Document<T, P extends Indexable = Indexable> {
762
784
*/
763
785
public subscribe (
764
786
type : 'sync' ,
765
- next : NextFn < DocEvent < P > > ,
787
+ next : DocEventCallbackMap < P > [ 'sync' ] ,
766
788
error ?: ErrorFn ,
767
789
complete ?: CompleteFn ,
768
790
) : Unsubscribe ;
@@ -775,7 +797,9 @@ export class Document<T, P extends Indexable = Indexable> {
775
797
TOperationInfo extends OperationInfoOf < T , TPath > ,
776
798
> (
777
799
targetPath : TPath ,
778
- next : NextFn < DocEvent < P , TOperationInfo > > ,
800
+ next : NextFn <
801
+ LocalChangeEvent < TOperationInfo , P > | RemoteChangeEvent < TOperationInfo , P >
802
+ > ,
779
803
error ?: ErrorFn ,
780
804
complete ?: CompleteFn ,
781
805
) : Unsubscribe ;
@@ -784,7 +808,7 @@ export class Document<T, P extends Indexable = Indexable> {
784
808
*/
785
809
public subscribe (
786
810
type : 'all' ,
787
- next : NextFn < TransactionEvent < P > > ,
811
+ next : DocEventCallbackMap < P > [ 'all' ] ,
788
812
error ?: ErrorFn ,
789
813
complete ?: CompleteFn ,
790
814
) : Unsubscribe ;
@@ -795,11 +819,13 @@ export class Document<T, P extends Indexable = Indexable> {
795
819
TPath extends PathOf < T > ,
796
820
TOperationInfo extends OperationInfoOf < T , TPath > ,
797
821
> (
798
- arg1 : TPath | string | Observer < DocEvent < P > > | NextFn < DocEvent < P > > ,
822
+ arg1 : TPath | DocEventTopic | DocEventCallbackMap < P > [ 'default' ] ,
799
823
arg2 ?:
800
- | NextFn < DocEvent < P , TOperationInfo > >
801
- | NextFn < DocEvent < P > >
802
- | NextFn < TransactionEvent < P > >
824
+ | NextFn <
825
+ | LocalChangeEvent < TOperationInfo , P >
826
+ | RemoteChangeEvent < TOperationInfo , P >
827
+ >
828
+ | DocEventCallback < P >
803
829
| ErrorFn ,
804
830
arg3 ?: ErrorFn | CompleteFn ,
805
831
arg4 ?: CompleteFn ,
@@ -809,7 +835,7 @@ export class Document<T, P extends Indexable = Indexable> {
809
835
throw new Error ( 'Second argument must be a callback function' ) ;
810
836
}
811
837
if ( arg1 === 'presence' ) {
812
- const callback = arg2 as NextFn < DocEvent < P > > ;
838
+ const callback = arg2 as DocEventCallbackMap < P > [ 'presence' ] ;
813
839
return this . eventStream . subscribe (
814
840
( event ) => {
815
841
for ( const docEvent of event ) {
@@ -830,21 +856,19 @@ export class Document<T, P extends Indexable = Indexable> {
830
856
) ;
831
857
}
832
858
if ( arg1 === 'my-presence' ) {
833
- const callback = arg2 as NextFn < DocEvent < P > > ;
859
+ const callback = arg2 as DocEventCallbackMap < P > [ 'my-presence' ] ;
834
860
return this . eventStream . subscribe (
835
861
( event ) => {
836
862
for ( const docEvent of event ) {
837
863
if (
838
864
docEvent . type !== DocEventType . Initialized &&
839
- docEvent . type !== DocEventType . Watched &&
840
- docEvent . type !== DocEventType . Unwatched &&
841
865
docEvent . type !== DocEventType . PresenceChanged
842
866
) {
843
867
continue ;
844
868
}
845
869
846
870
if (
847
- docEvent . type !== DocEventType . Initialized &&
871
+ docEvent . type === DocEventType . PresenceChanged &&
848
872
docEvent . value . clientID !== this . changeID . getActorID ( )
849
873
) {
850
874
continue ;
@@ -858,7 +882,7 @@ export class Document<T, P extends Indexable = Indexable> {
858
882
) ;
859
883
}
860
884
if ( arg1 === 'others' ) {
861
- const callback = arg2 as NextFn < DocEvent < P > > ;
885
+ const callback = arg2 as DocEventCallbackMap < P > [ 'others' ] ;
862
886
return this . eventStream . subscribe (
863
887
( event ) => {
864
888
for ( const docEvent of event ) {
@@ -880,7 +904,7 @@ export class Document<T, P extends Indexable = Indexable> {
880
904
) ;
881
905
}
882
906
if ( arg1 === 'connection' ) {
883
- const callback = arg2 as NextFn < DocEvent < P > > ;
907
+ const callback = arg2 as DocEventCallbackMap < P > [ 'connection' ] ;
884
908
return this . eventStream . subscribe (
885
909
( event ) => {
886
910
for ( const docEvent of event ) {
@@ -895,7 +919,7 @@ export class Document<T, P extends Indexable = Indexable> {
895
919
) ;
896
920
}
897
921
if ( arg1 === 'sync' ) {
898
- const callback = arg2 as NextFn < DocEvent < P > > ;
922
+ const callback = arg2 as DocEventCallbackMap < P > [ 'sync' ] ;
899
923
return this . eventStream . subscribe (
900
924
( event ) => {
901
925
for ( const docEvent of event ) {
@@ -910,31 +934,28 @@ export class Document<T, P extends Indexable = Indexable> {
910
934
) ;
911
935
}
912
936
if ( arg1 === 'all' ) {
913
- const callback = arg2 as NextFn < TransactionEvent < P > > ;
937
+ const callback = arg2 as DocEventCallbackMap < P > [ 'all' ] ;
914
938
return this . eventStream . subscribe ( callback , arg3 , arg4 ) ;
915
939
}
916
940
const target = arg1 ;
917
- const callback = arg2 as NextFn < DocEvent < P > > ;
941
+ const callback = arg2 as NextFn <
942
+ | LocalChangeEvent < TOperationInfo , P >
943
+ | RemoteChangeEvent < TOperationInfo , P >
944
+ > ;
918
945
return this . eventStream . subscribe (
919
946
( event ) => {
920
947
for ( const docEvent of event ) {
921
948
if (
922
- docEvent . type !== DocEventType . Snapshot &&
923
949
docEvent . type !== DocEventType . LocalChange &&
924
950
docEvent . type !== DocEventType . RemoteChange
925
951
) {
926
952
continue ;
927
953
}
928
954
929
- if ( docEvent . type === DocEventType . Snapshot ) {
930
- target === '$' && callback ( docEvent ) ;
931
- continue ;
932
- }
933
-
934
- const targetOps : Array < OperationInfo > = [ ] ;
955
+ const targetOps : Array < TOperationInfo > = [ ] ;
935
956
for ( const op of docEvent . value . operations ) {
936
957
if ( this . isSameElementOrChildOf ( op . path , target ) ) {
937
- targetOps . push ( op ) ;
958
+ targetOps . push ( op as TOperationInfo ) ;
938
959
}
939
960
}
940
961
targetOps . length &&
@@ -949,7 +970,7 @@ export class Document<T, P extends Indexable = Indexable> {
949
970
) ;
950
971
}
951
972
if ( typeof arg1 === 'function' ) {
952
- const callback = arg1 as NextFn < DocEvent < P > > ;
973
+ const callback = arg1 as DocEventCallbackMap < P > [ 'default' ] ;
953
974
const error = arg2 as ErrorFn ;
954
975
const complete = arg3 as CompleteFn ;
955
976
return this . eventStream . subscribe (
0 commit comments