sciter-js-sdk
sciter-js-sdk copied to clipboard
sys.fs.watch — callback is never called
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);
//}
}
}
I can confirm. It is also not working on my end.
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.