sciter-js-sdk icon indicating copy to clipboard operation
sciter-js-sdk copied to clipboard

sys.fs.watch — callback is never called

Open GirkovArpa opened this issue 3 years ago • 2 comments

The first call of console.log prints this:

onFileChange('foo.txt', (text) => console.log(text))

But when the file is edited and saved, the second call to console.log is never reached.

import * as sys from '@sys';

onFileChange('foo.txt', (text) => console.log(text));

function onFileChange(filename, callback) {
  console.log(`onFileChange("${filename}", ${callback})`);

  sys.fs.watch(filename, _callback);
  
  function _callback(path, event) {
    console.log(`_callback('${path}', ${event})`);
    //if (event === 0x02) {
      const file = fs.$readfile(path);
      const text = decode(file, 'utf-8');
      callback(text);
    //}
  }
}

GirkovArpa avatar Nov 22 '21 05:11 GirkovArpa

I can confirm. It is also not working on my end.

thecodrr avatar Feb 16 '22 02:02 thecodrr

You MUST store the monitor instance somewhere and you SHOULD close it at some point:

Try this:

<html>
    <head>
        <title>Test</title>
        <style></style>
        <script|module>

import * as sys from "@sys";

var monitor = sys.fs.watch( URL.toPath(__DIR__), function(path,event,status) {
  document.body.append(<text>Change in {path} event {event} status {status}</text>);
});

document.on("beforeunload", function () {
  monitor.close();
})

        </script>
    </head>
    <body>
    </body>
</html>

If you just do this sys.fs.watch(filename, _callback); then monitor will be created and immediately destroyed - nothing will hold it from destruction / GC.

Such auto destruction is made intentionally. Usually you will assign monitor as a member variable of some DOM element:

document.monitor = sys.fs.watch( ... )

In this case you don't need to close it - it will be destroyed with the document.

To be short: each FS monitor shall have an owner, you cannot create monitor in the air as it must be destroyed.

c-smile avatar Feb 16 '22 03:02 c-smile