Sitten Spynne (8) [Avatar] Offline
#1
Listing 6.27 asks the user if they'd like to overwrite unsaved changes in the file-changed event, but it doesn't actually check their response. It always overwrites. I think it's missing something like:
if (result === 1) return;
Sitten Spynne (8) [Avatar] Offline
#2
Also I ran into a lot of undesirable behavior when testing, I think some touchups got forgotten.

For example, after implementing what the current livebook shows, saving a file always asks me if I want to reload the file. What's happening is main.js's saveFileToMarkdown() calls openFile(), which sends file-opened to the renderer. file-opened checks for isDocumentEdited(), but either I missed it or nothing was ever updated to change that. So I edited the event handler for clicking the save button:
saveMarkdownButton.addEventListener('click', () => {
    updateUserInterface(false);
    mainProcess.saveMarkdown(currentWindow, filePath, markdownView.value);
});

That properly updates the window to believe the file is not edited, and causes file-opened to properly ignore the event.

It looks like this was intended, as renderFile() calls updateUserInterface, but unfortunately it won't happen until after file-open has checked if the document has been edited. This feels a bit sloppy but is the easiest fix.

Next:

Every time I close a window, I'm warned my changes will be lost if I don't save. This is a similar cause: the event doesn't check if the document has been edited. There's more than one way to skin that cat, but here's what I did:
    newWindow.on('close', (event) => {
        event.preventDefault();

        const isEdited = newWindow.isDocumentEdited();

        if (isEdited) {
            const result = dialog.showMessageBox(
                newWindow,
                {
                    "type": 'warning',
                    "title": 'Quit with unsaved changes?',
                    "message": 'Your changes will be lost if you do not save.',
                    "buttons:": [
                        'Quit Anyway',
                        'Cancel'
                    ],
                    "defaultId": 0,
                    "cancelId": 1
                }
            );

            if (result === 1) { 
                return;
            }
        }

        newWindow.destroy();
    });


Next:

I also had a problem getting the file watchers to work. It turns out nothing in the code listings calls startWatchingFile(). I'm assuming openFile() was meant to be modified to do that?

After that, things still weren't working. It turns out things are all wrong. fs.watchFile() returns a pair of some kind of file status objects so you can get detailed information about what changed. Node probably changed this, they've got a really pesky tendency to break their own fundamental APIs every third Tuesday.

It looks like fs.watch() is what we need, but its signature has changed also. I stopped here because things started getting weird and I need to think about it harder. Sometimes I get a 'rename' event instead of 'change', and the last test run I got 3 notifications for 1 external edit. And now I occasionally get crashes. Oops! I think I'm just going to leave out the "watch" behavior as I move forward.
Steve Kinney (31) [Avatar] Offline
#3
Can you shoot me an email at hello at my name dot net?