gajus / turbowatch

Extremely fast file change detector and task orchestrator for Node.js.
Other
944 stars 23 forks source link

Provide way to access Watcher in onChange event #45

Closed Handfish closed 1 year ago

Handfish commented 1 year ago

I would like to access the Watcher object from the onChange handler in the watch configuration input. This way, a user can create public methods on their FileWatchingBackend to use in onChange.

Desired Behavior

Not sure how to reference the watcher with WatcherConstructable being a constructor. Maybe pass a reference in ChangeEvent

 onChange: async ({ spawn, files, first, abortSignal, watcher }: ChangeEvent) => {

Motivation

I would like to conditionally add and remove paths from my chokidar watch.

  public add(path: string) {
    this.chokidar.add(path);
  }

  public unwatch(path: string) {
    this.chokidar.unwatch(path);
  }
Handfish commented 1 year ago

Decided to give this a go in order to test an idea I had https://github.com/Handfish/turbowatch/commit/0255bc672fd69d677bda23a8483691200bf582a0

Presently the add and unwatch apis in Chokidar aren't working the way I'd have wished.

It was a fun modification - learned about tagged template literals and interface merging along the way. Closing as I've explored this change.