plugins-workspace icon indicating copy to clipboard operation
plugins-workspace copied to clipboard

[fs:watch] memory leak when refresh page.

Open cathaysia opened this issue 10 months ago • 2 comments

When I using the following hook watch a folder:

function isInterest(e: WatchEventKind) {
  // @ts-expect-error: xxx
  return e.create | e.modify | e.remove;
}

let oldChangeTime = '';
export function useFolderChanges() {
  const [path] = useAppStore(s => [s.path]);
  const [count, setCount] = useState(0);
  const [handle, setHandle] = useState<UnwatchFn | null>(null);

  useEffect(() => {
    if (!path || path.length === 0) {
      if (handle !== null) {
        handle();
        setHandle(null);
      }
      return;
    }
    if (path === oldChangeTime) {
      return;
    }
    oldChangeTime = path;
    (async () => {
      debug(`watch ${path}`);
      const h = await watch(
        path,
        e => {
          if (isInterest(e.type)) {
            setCount(count => {
              return count + 1;
            });
          }
        },
        {
          delayMs: 100,
          recursive: true,
        },
      );
      setHandle(s => {
        if (s !== null) {
          debug('cancel watch');
          s();
        }
        return h;
      });
    })();
  }, [path]);

  return count;
}

However, when I refresh webpage. Then memory will leak:

Image

Image

ref

https://github.com/notify-rs/notify/issues/672

cathaysia avatar Feb 17 '25 05:02 cathaysia

https://github.com/tauri-apps/tauri/issues/10266

FabianLars avatar Feb 17 '25 09:02 FabianLars

https://github.com/tauri-apps/tauri/issues/12388

cathaysia avatar Mar 03 '25 02:03 cathaysia