lib/fsevents-handler.js
function setFSEventsListener(path, realPath, listener, rawEmitter) {
  let watchPath = sysPath.extname(path) ? sysPath.dirname(path) : path;
  const parentPath = sysPath.dirname(watchPath);
  let cont = FSEventsWatchers.get(watchPath);

  // If we've accumulated a substantial number of paths that
  // could have been consolidated by watching one directory
  // above the current one, create a watcher on the parent
  // path instead, so that we do consolidate going forward.
  if (couldConsolidate(parentPath)) {
    watchPath = parentPath;
  }

  const resolvedPath = sysPath.resolve(path);
  const hasSymlink = resolvedPath !== realPath;

  const filteredListener = (fullPath, flags, info) => {
    if (hasSymlink) fullPath = fullPath.replace(realPath, resolvedPath);
    if (
      fullPath === resolvedPath ||
      !fullPath.indexOf(resolvedPath + sysPath.sep)
    ) listener(fullPath, flags, info);
  };

  // check if there is already a watcher on a parent path
  // modifies `watchPath` to the parent path when it finds a match
  const watchedParent = () => {
    for (const watchedPath of FSEventsWatchers.keys()) {
      if (realPath.indexOf(sysPath.resolve(watchedPath) + sysPath.sep) === 0) {
        watchPath = watchedPath;
        cont = FSEventsWatchers.get(watchPath);
        return true;
      }
    }
  };

  if (cont || watchedParent()) {
    cont.listeners.add(filteredListener);
  } else {
    cont = {
      listeners: new Set([filteredListener]),
      rawEmitter: rawEmitter,
      watcher: createFSEventsInstance(watchPath, (fullPath, flags) => {
        const info = fsevents.getInfo(fullPath, flags);
        cont.listeners.forEach(list => {
          list(fullPath, flags, info);
        });

        cont.rawEmitter(info.event, fullPath, info);
      })
    };
    FSEventsWatchers.set(watchPath, cont);
  }

  // removes this instance's listeners and closes the underlying fsevents
  // instance if there are no more listeners left
  return () => {
    const wl = cont.listeners;

    wl.delete(filteredListener);
    if (!wl.size) {
      FSEventsWatchers.delete(watchPath);
      cont.watcher.stop();
      cont.rawEmitter = cont.watcher = null;
      Object.freeze(cont);
      Object.freeze(cont.listeners);
    }
  };
}
function setFSEventsListener(path, realPath, listener, rawEmitter) {
  let watchPath = sysPath.extname(path) ? sysPath.dirname(path) : path;
  const parentPath = sysPath.dirname(watchPath);
  let cont = FSEventsWatchers.get(watchPath);

  // If we've accumulated a substantial number of paths that
  // could have been consolidated by watching one directory
  // above the current one, create a watcher on the parent
  // path instead, so that we do consolidate going forward.
  if (couldConsolidate(parentPath)) {
    watchPath = parentPath;
  }

  const resolvedPath = sysPath.resolve(path);
  const hasSymlink = resolvedPath !== realPath;

  const filteredListener = (fullPath, flags, info) => {
    if (hasSymlink) fullPath = fullPath.replace(realPath, resolvedPath);
    if (
      fullPath === resolvedPath ||
      !fullPath.indexOf(resolvedPath + sysPath.sep)
    ) listener(fullPath, flags, info);
  };

  // check if there is already a watcher on a parent path
  // modifies `watchPath` to the parent path when it finds a match
  const watchedParent = () => {
    for (const watchedPath of FSEventsWatchers.keys()) {
      if (realPath.indexOf(sysPath.resolve(watchedPath) + sysPath.sep) === 0) {
        watchPath = watchedPath;
        cont = FSEventsWatchers.get(watchPath);
        return true;
      }
    }
  };

  if (cont || watchedParent()) {
    cont.listeners.add(filteredListener);
  } else {
    cont = {
      listeners: new Set([filteredListener]),
      rawEmitter: rawEmitter,
      watcher: createFSEventsInstance(watchPath, (fullPath, flags) => {
        const info = fsevents.getInfo(fullPath, flags);
        cont.listeners.forEach(list => {
          list(fullPath, flags, info);
        });

        cont.rawEmitter(info.event, fullPath, info);
      })
    };
    FSEventsWatchers.set(watchPath, cont);
  }

  // removes this instance's listeners and closes the underlying fsevents
  // instance if there are no more listeners left
  return () => {
    const wl = cont.listeners;

    wl.delete(filteredListener);
    if (!wl.size) {
      FSEventsWatchers.delete(watchPath);
      cont.watcher.stop();
      cont.rawEmitter = cont.watcher = null;
      Object.freeze(cont);
      Object.freeze(cont.listeners);
    }
  };
}
function setFSEventsListener ( path , realPath , listener , rawEmitter ) { let watchPath = sysPath . extname ( path ) ? sysPath . dirname ( path ) : path ; const parentPath = sysPath . dirname ( watchPath ) ; let cont = FSEventsWatchers . get ( watchPath ) ; // If we've accumulated a substantial number of paths that // could have been consolidated by watching one directory // above the current one, create a watcher on the parent // path instead, so that we do consolidate going forward. if ( couldConsolidate ( parentPath ) ) { watchPath = parentPath ; } const resolvedPath = sysPath . resolve ( path ) ; const hasSymlink = resolvedPath !== realPath ; const filteredListener = ( fullPath , flags , info ) => { if ( hasSymlink ) fullPath = fullPath . replace ( realPath , resolvedPath ) ; if ( fullPath === resolvedPath || ! fullPath . indexOf ( resolvedPath + sysPath . sep ) ) listener ( fullPath , flags , info ) ; } ; // check if there is already a watcher on a parent path // modifies `watchPath` to the parent path when it finds a match const watchedParent = ( ) => { for ( const watchedPath of FSEventsWatchers . keys ( ) ) { if ( realPath . indexOf ( sysPath . resolve ( watchedPath ) + sysPath . sep ) === 0 ) { watchPath = watchedPath ; cont = FSEventsWatchers . get ( watchPath ) ; return true ; } } } ; if ( cont || watchedParent ( ) ) { cont . listeners . add ( filteredListener ) ; } else { cont = { listeners : new Set ( [ filteredListener ] ) , rawEmitter : rawEmitter , watcher : createFSEventsInstance ( watchPath , ( fullPath , flags ) => { const info = fsevents . getInfo ( fullPath , flags ) ; cont . listeners . forEach ( list => { list ( fullPath , flags , info ) ; } ) ; cont . rawEmitter ( info . event , fullPath , info ) ; } ) } ; FSEventsWatchers . set ( watchPath , cont ) ; } // removes this instance's listeners and closes the underlying fsevents // instance if there are no more listeners left return ( ) => { const wl = cont . listeners ; wl . delete ( filteredListener ) ; if ( ! wl . size ) { FSEventsWatchers . delete ( watchPath ) ; cont . watcher . stop ( ) ; cont . rawEmitter = cont . watcher = null ; Object . freeze ( cont ) ; Object . freeze ( cont . listeners ) ; } } ; }
Instantiates the fsevents interface or binds listeners to an existing one covering
the same file tree.
@param {Path} path           - to be watched
@param {Path} realPath       - real path for symlinks
@param {Function} listener   - called when fsevents emits events
@param {Function} rawEmitter - passes data to listeners of the 'raw' event
@returns {Function} closer
Instantiates the fsevents interface or binds listeners to an existing one covering the same file tree .