Repository: toeverything/AFFiNE
File: packages/common/nbstore/src/sync/blob/index.ts (TypeScript)

Code before:
        ? {
            uploading: 0,
            downloading: 0,
            error: 0,
            overCapacity: false,
          }
        : {
            uploading: allPeers.reduce((acc, peer) => acc + peer.uploading, 0),
            downloading: allPeers.reduce(
              (acc, peer) => acc + peer.downloading,
              0
            ),
            error: allPeers.reduce((acc, peer) => acc + peer.error, 0),
            overCapacity: allPeers.some(p => p.overCapacity),
          }
    ),
    share({
      connector: () => new ReplaySubject(1),
    })
  ) as Observable<BlobSyncState>;

  blobState$(blobId: string) {
    return combineLatest(
      this.peers.map(peer => peer.blobPeerState$(blobId))
    ).pipe(
      throttleTime(1000, asyncScheduler, { trailing: true }),
      map(
        peers =>
          ({
            uploading: peers.some(p => p.uploading),
            downloading: peers.some(p => p.downloading),
            errorMessage: peers.find(p => p.errorMessage)?.errorMessage,
            overSize: peers.some(p => p.overSize),
          }) satisfies BlobSyncBlobState
      ),
      share({
        connector: () => new ReplaySubject(1),
      })
    );
  }

  constructor(
    readonly storages: PeerStorageOptions<BlobStorage>,
    readonly blobSync: BlobSyncStorage
  ) {}

  downloadBlob(blobId: string): Promise<boolean> {
    const signal = this.abortController.signal;

    return new Promise<boolean>((resolve, reject) => {
      let completed = 0;

Review comment (suggestion):
just leave scheduler undefined 

```suggestion
      throttleTime(1000, undefined, { trailing: true }),
```

Code after:
            uploading: 0,
            downloading: 0,
            error: 0,
            overCapacity: false,
          }
        : {
            uploading: allPeers.reduce((acc, peer) => acc + peer.uploading, 0),
            downloading: allPeers.reduce(
              (acc, peer) => acc + peer.downloading,
              0
            ),
            error: allPeers.reduce((acc, peer) => acc + peer.error, 0),
            overCapacity: allPeers.some(p => p.overCapacity),
          }
    ),
    share({
      connector: () => new ReplaySubject(1),
    })
  ) as Observable<BlobSyncState>;

  blobState$(blobId: string) {
    return combineLatest(
      this.peers.map(peer => peer.blobPeerState$(blobId))
    ).pipe(
      throttleTime(1000, undefined, { leading: true, trailing: true }),
      map(
        peers =>
          ({
            uploading: peers.some(p => p.uploading),
            downloading: peers.some(p => p.downloading),
            errorMessage: peers.find(p => p.errorMessage)?.errorMessage,
            overSize: peers.some(p => p.overSize),
          }) satisfies BlobSyncBlobState
      ),
      share({
        connector: () => new ReplaySubject(1),
      })
    );
  }

  constructor(
    readonly storages: PeerStorageOptions<BlobStorage>,
    readonly blobSync: BlobSyncStorage
  ) {}

  downloadBlob(blobId: string): Promise<boolean> {
    const signal = this.abortController.signal;

    return new Promise<boolean>((resolve, reject) => {
      let completed = 0;
      const totalPeers = this.peers.length;