NodeFileSystemObserver.d.ts 2.4 KB

12345678910111213141516171819202122232425262728293031
  1. import type { IFileSystemChangeRecord, IFileSystemDirectoryHandle, IFileSystemFileHandle, IFileSystemHandle, IFileSystemObserver, IFileSystemObserverObserveOptions, IFileSystemSyncAccessHandle } from '@jsonjoy.com/fs-fsa';
  2. import type { FsCallbackApi } from '@jsonjoy.com/fs-node-utils';
  3. import type * as misc from '@jsonjoy.com/fs-node-utils/lib/types/misc';
  4. import type { NodeFsaContext, NodeFsaFs } from './types';
  5. export type NodeFsaWatchFs = NodeFsaFs & Pick<FsCallbackApi, 'watch'>;
  6. /**
  7. * A `FileSystemObserver` implementation backed by the underlying Node.js-like
  8. * `fs.watch`. This is a best-effort profile, per the File System Observer
  9. * proposal's allowance for local file systems: `rename` events are classified
  10. * into `"appeared"`/`"disappeared"` records by stat-ing the path, no
  11. * `"moved"` records are ever produced (pairing renames is unreliable — same
  12. * as Chrome on Windows), and a backend watcher error surfaces as a terminal
  13. * `"errored"` record for that observation.
  14. *
  15. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemObserver
  16. */
  17. export declare class NodeFileSystemObserver implements IFileSystemObserver {
  18. protected readonly fs: NodeFsaWatchFs;
  19. protected readonly callback: (records: IFileSystemChangeRecord[], observer: IFileSystemObserver) => void;
  20. protected readonly _observations: Map<IFileSystemSyncAccessHandle | IFileSystemFileHandle | IFileSystemDirectoryHandle, misc.IFSWatcher>;
  21. protected _records: IFileSystemChangeRecord[];
  22. protected _flushScheduled: boolean;
  23. constructor(fs: NodeFsaWatchFs, callback: (records: IFileSystemChangeRecord[], observer: IFileSystemObserver) => void);
  24. observe(handle: IFileSystemFileHandle | IFileSystemDirectoryHandle | IFileSystemSyncAccessHandle, options?: IFileSystemObserverObserveOptions): Promise<void>;
  25. unobserve(handle: IFileSystemFileHandle | IFileSystemDirectoryHandle | IFileSystemSyncAccessHandle): void;
  26. /** Disconnect and stop all observations. */
  27. disconnect(): void;
  28. protected onEvent(root: IFileSystemFileHandle | IFileSystemDirectoryHandle | IFileSystemSyncAccessHandle, watcher: misc.IFSWatcher, rootPath: string, isDirectory: boolean, ctx: NodeFsaContext, eventType: string, filename: string): Promise<void>;
  29. protected _handle(absolute: string, stats: misc.IStats, ctx: NodeFsaContext): IFileSystemHandle;
  30. protected _enqueue(record: IFileSystemChangeRecord): void;
  31. }