The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

Sitten Spynne (8) [Avatar] Offline
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
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', () => {
    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.


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) => {

        const isEdited = newWindow.isDocumentEdited();

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

            if (result === 1) { 



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 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 (33) [Avatar] Offline
Can you shoot me an email at hello at my name dot net?