dsheiko / nw-autoupdater

⛔️ [DEPRECATED] Library provides low-level API to control NW.js app auto-updates
MIT License
75 stars 36 forks source link

download event doesn't work properly #9

Closed Arti3DPlayer closed 7 years ago

Arti3DPlayer commented 7 years ago

Hello i have the following code:

var AutoUpdater = require("nw-autoupdater");
        var updater = new AutoUpdater(nw.App.manifest, {
            strategy: "ScriptSwap"
        });

        updater.on("download", function(downloadSize, totalSize ) {
            console.log('Downloading...');
            console.log("download progress", Math.floor(downloadSize/totalSize * 100), "%" );
        });

        updater.on("install", function(installFiles, totalFiles ) {
            console.log("install progress", Math.floor(installFiles/totalFiles * 100 ), "%" );
        });

        // Download/unpack update if any available
        updater.readRemoteManifest().then(function(rManifest) {
            updater.checkNewVersion(rManifest).then(function(needsUpdate ) {
                if (!needsUpdate) {
                    console.log('App is up to date..');
                    return;
                }

                if (!confirm("New release is available. Do you want to upgrade?")) {
                    return;
                }

                updater.download(rManifest).then(function (updateFile) {
                    console.log(updateFile)
                    // updater.unpack(updateFile).then(function() {
                    //     console.log('The application will automatically restart to finish installing the update');
                    //     updater.restartToSwap();
                    // });
                }, function (error) {
                    console.log(error)
                });
            }, function (error) {
                console.log(error)
            });
        }, function (error) {
            console.log(error)
        });

But download event only fired only once when zip file already finished.

Version: "nw-autoupdater": "^1.1.0",

dsheiko commented 7 years ago

Well, that is because of debounce:

const onProgress = ( length ) => {
      this.emit( "download", length, release.size );
 };
download( release.url, os.tmpdir(), debounce( onProgress, debounceTime ))

I've made it waiting for too long DEBOUNCE_TIME = 500 before rechecking the state and firing the event. I'ww think of reducing the default value. But you change with the options of download/unpack methods:

updater.download(rManifest, { debounceTime:  50 })