<commit_before>﻿﻿﻿module.exports = {
    downloadOperationPromise: null, // TODO concurrent operations support
    startAsync: function (success, fail, args) {
        try {
            var uri = new Windows.Foundation.Uri(args[0]),
                resultFilePath = args[1];

            var completeHandler = function() {
                success();
            };
            var errorHandler = function(err) {
                fail(err);
            };
            var progressHandler = function (operation) {

                var progress = 100 * operation.progress.bytesReceived / operation.progress.totalBytesToReceive;

                success({
                    progress: progress,
                    keepCallback: true
                });
            };

            
            // TODO
            // After app termination, an app should enumerate all existing DownloadOperation instances at next start-up using
            // GetCurrentDownloadsAsync. When a Windows Store app using Background Transfer is terminated, incomplete downloads 
            // will persist in the background. If the app is restarted after termination and operations from the previous
            // session are not enumerated and re-attached to the current session, they will remain incomplete and continue to occupy resources
            // http://msdn.microsoft.com/library/windows/apps/br207126
            // TODO - resume incomplete download instead of triggering a new one
            // Windows.Networking.BackgroundTransfer.BackgroundDownloader.getCurrentDownloadsAsync().done(function (downloads) {
            //    for (var i = 0; i < downloads.size; i++) {
            //        console.log(downloads[i].toString());

            //         // to find existing download
            //        downloads[i].requestedUri.absoluteUri == uri.absoluteUri
                    
            //    }
            //}, function (error) {
            //    console.log(error.message);
            //});

            Windows.Storage.StorageFile.getFileFromPathAsync(resultFilePath).done(
                function (file) {
                    var downloadOperation = Windows.Networking.BackgroundTransfer.BackgroundDownloader().createDownload(uri, file);
                    downloadOperationPromise = downloadOperation.startAsync().then(completeHandler, errorHandler, progressHandler);
                }, errorHandler);
        } catch(ex) {
            fail(ex);
        }

    },
    stop: function (success, fail, args) {
        try {
            downloadOperationPromise && downloadOperationPromise.cancel();
            success();
        } catch(ex) {
            fail(ex);
        }
    }
};<commit_msg>windows8. now resumes incomplete download operation<commit_after>??module.exports = {
    downloadOperationPromise: null, // TODO concurrent operations support
    startAsync: function (success, fail, args) {
        try {
            var uri = new Windows.Foundation.Uri(args[0]),
                resultFilePath = args[1];

            var completeHandler = function() {
                success();
            };
            var errorHandler = function(err) {
                fail(err);
            };
            var progressHandler = function (operation) {

                var progress = 100 * operation.progress.bytesReceived / operation.progress.totalBytesToReceive;

                success({
                    progress: progress,
                    keepCallback: true
                });
            };

            var downloadLocation = null;
            Windows.Storage.StorageFile.getFileFromPathAsync(resultFilePath).then(
                function (file) {
                    downloadLocation = file;
                }, errorHandler).then(function () {
                    return Windows.Networking.BackgroundTransfer.BackgroundDownloader.getCurrentDownloadsAsync();
                }, errorHandler).then(function (downloads) {
                    var downloadOperation = null;

                    // After app termination, an app should enumerate all existing DownloadOperation instances at next start-up using
                    // GetCurrentDownloadsAsync. When a Windows Store app using Background Transfer is terminated, incomplete downloads 
                    // will persist in the background. If the app is restarted after termination and operations from the previous
                    // session are not enumerated and re-attached to the current session, they will remain incomplete and continue to occupy resources
                    // http://msdn.microsoft.com/library/windows/apps/br207126
                    for (var i = 0; i < downloads.size; i++) {
                        if (downloads[i].requestedUri.absoluteUri == uri.absoluteUri) {
                            downloadOperationPromise = downloads[i].attachAsync();
                            return downloadOperationPromise;
                        }

                    }

                    // new download
                    downloadOperation = Windows.Networking.BackgroundTransfer.BackgroundDownloader().createDownload(uri, downloadLocation);
                    downloadOperationPromise = downloadOperation.startAsync();

                    return downloadOperationPromise;

                }, errorHandler).then(completeHandler, errorHandler, progressHandler);
        } catch(ex) {
            fail(ex);
        }

    },
    stop: function (success, fail, args) {
        try {
            downloadOperationPromise && downloadOperationPromise.cancel();
            success();
        } catch(ex) {
            fail(ex);
        }
    }
};
{'fraction_non_alphanumeric': 0.0846167463267835, 'fraction_numerical': 0.0044255620463798905, 'mean_word_length': 1.7072352659319598, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 45, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '918389', 'n_tokens_mistral': 1273, 'n_tokens_neox': 1166, 'n_words': 327}