Skip to content

Commit

Permalink
Revise the codes
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerwins committed May 20, 2024
1 parent ff84a7b commit 63fbc54
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
28 changes: 12 additions & 16 deletions src/document/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,11 @@ export interface PresenceChangedEvent<P extends Indexable>
}

type DocumentEventCallbackMap<P extends Indexable> = {
default: NextFn<
| SnapshotEvent
| LocalChangeEvent<OperationInfo, P>
| RemoteChangeEvent<OperationInfo, P>
>;
presence: NextFn<
| InitializedEvent<P>
| WatchedEvent<P>
Expand Down Expand Up @@ -727,11 +732,7 @@ export class Document<T, P extends Indexable = Indexable> {
* The callback will be called when the document is changed.
*/
public subscribe(
next: NextFn<
| SnapshotEvent
| LocalChangeEvent<OperationInfo, P>
| RemoteChangeEvent<OperationInfo, P>
>,
next: DocumentEventCallbackMap<P>['default'],
error?: ErrorFn,
complete?: CompleteFn,
): Unsubscribe;
Expand All @@ -742,12 +743,7 @@ export class Document<T, P extends Indexable = Indexable> {
*/
public subscribe(
type: 'presence',
next: NextFn<
| InitializedEvent<P>
| WatchedEvent<P>
| UnwatchedEvent<P>
| PresenceChangedEvent<P>
>,
next: DocumentEventCallbackMap<P>['presence'],
error?: ErrorFn,
complete?: CompleteFn,
): Unsubscribe;
Expand All @@ -757,7 +753,7 @@ export class Document<T, P extends Indexable = Indexable> {
*/
public subscribe(
type: 'my-presence',
next: NextFn<InitializedEvent<P> | PresenceChangedEvent<P>>,
next: DocumentEventCallbackMap<P>['my-presence'],
error?: ErrorFn,
complete?: CompleteFn,
): Unsubscribe;
Expand All @@ -768,7 +764,7 @@ export class Document<T, P extends Indexable = Indexable> {
*/
public subscribe(
type: 'others',
next: NextFn<WatchedEvent<P> | UnwatchedEvent<P> | PresenceChangedEvent<P>>,
next: DocumentEventCallbackMap<P>['others'],
error?: ErrorFn,
complete?: CompleteFn,
): Unsubscribe;
Expand All @@ -778,7 +774,7 @@ export class Document<T, P extends Indexable = Indexable> {
*/
public subscribe(
type: 'connection',
next: NextFn<ConnectionChangedEvent>,
next: DocumentEventCallbackMap<P>['connection'],
error?: ErrorFn,
complete?: CompleteFn,
): Unsubscribe;
Expand All @@ -788,7 +784,7 @@ export class Document<T, P extends Indexable = Indexable> {
*/
public subscribe(
type: 'sync',
next: NextFn<SyncStatusChangedEvent>,
next: DocumentEventCallbackMap<P>['sync'],
error?: ErrorFn,
complete?: CompleteFn,
): Unsubscribe;
Expand All @@ -812,7 +808,7 @@ export class Document<T, P extends Indexable = Indexable> {
*/
public subscribe(
type: 'all',
next: NextFn<TransactionEvent<P>>,
next: DocumentEventCallbackMap<P>['all'],
error?: ErrorFn,
complete?: CompleteFn,
): Unsubscribe;
Expand Down
10 changes: 5 additions & 5 deletions test/integration/client_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,15 @@ describe.sequential('Client', function () {

const unsub1 = {
syncEvent: d1.subscribe('sync', (event) => {
eventCollectorSync1.add(event.value as DocumentSyncStatus);
eventCollectorSync1.add(event.value);
}),
doc: d1.subscribe((event) => {
eventCollectorD1.add(event.type);
}),
};
const unsub2 = {
syncEvent: d2.subscribe('sync', (event) => {
eventCollectorSync2.add(event.value as DocumentSyncStatus);
eventCollectorSync2.add(event.value);
}),
doc: d2.subscribe((event) => {
eventCollectorD2.add(event.type);
Expand Down Expand Up @@ -236,7 +236,7 @@ describe.sequential('Client', function () {
// 02. c2 changes the sync mode to realtime sync mode.
const eventCollector = new EventCollector();
const unsub1 = d2.subscribe('sync', (event) => {
eventCollector.add(event.value as DocumentSyncStatus);
eventCollector.add(event.value);
});
await c2.changeSyncMode(d2, SyncMode.Realtime);
await eventCollector.waitFor(DocumentSyncStatus.Synced); // sync occurs when resuming
Expand Down Expand Up @@ -414,7 +414,7 @@ describe.sequential('Client', function () {

const eventCollector = new EventCollector();
const unsub1 = d2.subscribe('sync', (event) => {
eventCollector.add(event.value as DocumentSyncStatus);
eventCollector.add(event.value);
});

// 01. c2 attach the doc with realtime sync mode at first.
Expand Down Expand Up @@ -491,7 +491,7 @@ describe.sequential('Client', function () {
// and sync with push-only mode: CP(2, 2) -> CP(3, 2)
const eventCollector = new EventCollector();
const unsub = d1.subscribe('sync', (event) => {
eventCollector.add(event.value as DocumentSyncStatus);
eventCollector.add(event.value);
});
d1.update((root) => {
root.counter.increase(1);
Expand Down

0 comments on commit 63fbc54

Please sign in to comment.