Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

2.8 KB
Raw Blame
import { fireAndForget } from '@core/util/fireAndForget';
import { DEFAULT_ITEM_TYPE } from '@service-storage/client';
import { AsyncBatcher } from '@tanstack/pacer';
import { fetchRestPreviewBatch } from './fetchers';
import { previewKeys } from './keys';
import type { ItemEntity, PreviewItem } from './types';

type PendingRequest = {
  item: ItemEntity;
  resolve: (preview: PreviewItem) => void;
  reject: (error: Error) => void;
};

class PreviewDataLoader {
  // map item id to array of pending requests
  private pendingRequests = new Map<string, PendingRequest[]>();

  private batcher = new AsyncBatcher<ItemEntity>(
    async (items) => {
      return await fetchRestPreviewBatch(items);
    },
    {
      wait: 30,
      maxSize: 50,
      asyncRetryerOptions: {
        maxAttempts: 3,
        backoff: 'exponential',
        baseWait: 200,
        jitter: 0.1,
        throwOnError: 'last',
      },
      onSuccess: (results: Map<string, PreviewItem>, batch) => {
        for (const item of batch) {
          const key = this.getCacheKey(item);
          const requests = this.pendingRequests.get(key);

          if (requests) {
            // A missing batch entry means no fetcher handled this item, not
            // that it was deleted — only fetchers may report 'does_not_exist'.
            const preview =
              results.get(item.id) ??
              ({
                id: item.id,
                type: item.type ?? DEFAULT_ITEM_TYPE,
                loading: false,
                access: 'no_access',
              } as PreviewItem);

            for (const request of requests) {
              request.resolve(preview);
            }

            this.pendingRequests.delete(key);
          }
        }
      },
      onError: (error, batch) => {
        for (const item of batch) {
          const key = this.getCacheKey(item);
          const requests = this.pendingRequests.get(key);

          if (requests) {
            const err =
              error instanceof Error
                ? error
                : new Error('Failed to fetch preview');
            for (const request of requests) {
              request.reject(err);
            }
            this.pendingRequests.delete(key);
          }
        }
      },
      throwOnError: false,
    }
  );

  load(item: ItemEntity): Promise<PreviewItem> {
    const key = this.getCacheKey(item);

    return new Promise((resolve, reject) => {
      const existing = this.pendingRequests.get(key);
      if (existing) {
        existing.push({ item, resolve, reject });
      } else {
        this.pendingRequests.set(key, [{ item, resolve, reject }]);
        fireAndForget(this.batcher.addItem(item), 'preview-loader');
      }
    });
  }

  private getCacheKey(item: ItemEntity): string {
    return previewKeys.item(item.id).queryKey.join(':');
  }
}

export const previewDataLoader = new PreviewDataLoader();