Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fs:watch] memory leak when refresh page. #2432

Open
cathaysia opened this issue Feb 17, 2025 · 2 comments
Open

[fs:watch] memory leak when refresh page. #2432

cathaysia opened this issue Feb 17, 2025 · 2 comments
Labels
bug Something isn't working plugin: fs Includes former "fs-extra" and "fs-watch" plugins

Comments

@cathaysia
Copy link

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

notify-rs/notify#672

@FabianLars FabianLars added bug Something isn't working plugin: fs Includes former "fs-extra" and "fs-watch" plugins labels Feb 17, 2025
@FabianLars
Copy link
Member

tauri-apps/tauri#10266

@cathaysia
Copy link
Author

tauri-apps/tauri#12388

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working plugin: fs Includes former "fs-extra" and "fs-watch" plugins
Projects
None yet
Development

No branches or pull requests

2 participants