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

Watch files for external changes #840

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions src/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,44 @@ import GLib from "gi://GLib";

export default function Document({ session, code_view, lang }) {
const { buffer } = code_view;
let handler_id = null;
let modified_changed_handler_id = null;
let changed_signal_handler_id = null;

const file = session.file.get_child(lang.default_file);

const file_monitor = file.monitor(Gio.FileMonitorFlags.NONE, null);

function watchFile() {
unwatchFile();
changed_signal_handler_id = file_monitor.connect(
"changed",
(_self, _file, _other_file, event_type) => {
const event_name = Object.entries(Gio.FileMonitorEvent).find(
([_key, value]) => value === event_type,
)?.[0];
console.log("wow", event_name);
},
);
}

function unwatchFile() {
if (changed_signal_handler_id === null) return;
file_monitor.disconnect(changed_signal_handler_id);
changed_signal_handler_id = null;
}

const source_file = new Source.File({
location: file,
});

start();

function save() {
unwatchFile();
saveSourceBuffer({ source_file, buffer })
.catch(console.error)
.finally(() => {
watchFile();
try {
session.settings.set_boolean("edited", true);
} catch (err) {
Expand All @@ -27,17 +52,19 @@ export default function Document({ session, code_view, lang }) {

function start() {
stop();
handler_id = buffer.connect("modified-changed", () => {
modified_changed_handler_id = buffer.connect("modified-changed", () => {
if (!buffer.get_modified()) return;
save();
});
watchFile();
}

function stop() {
if (handler_id !== null) {
buffer.disconnect(handler_id);
handler_id = null;
if (modified_changed_handler_id !== null) {
buffer.disconnect(modified_changed_handler_id);
modified_changed_handler_id = null;
}
unwatchFile();
}

function load() {
Expand Down