Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

897 B
Raw Blame
import type { AsyncSubscription, Options, SubscribeCallback } from '@parcel/watcher';

export type NativeWatcherBackend = Pick<typeof import('@parcel/watcher'), 'subscribe'>;

export async function subscribeParcel(
  parcel: NativeWatcherBackend,
  dir: string,
  onEvents: SubscribeCallback,
  options?: Options,
  platform: NodeJS.Platform = process.platform,
): Promise<AsyncSubscription> {
  let deliver: SubscribeCallback | null = onEvents;
  const subscription = await parcel.subscribe(
    dir,
    (err, events) => deliver?.(err, events),
    options,
  );
  return {
    async unsubscribe() {
      if (deliver === null) return;
      deliver = null;
      // UPSTREAM(parcel-bundler/watcher#262): the win32 native unsubscribe frees the subscription while its read's completion is still queued
      if (platform === 'win32') return;
      await subscription.unsubscribe();
    },
  };
}