<commit_before>var _ = require('underscore');
var Promise = require("bluebird");
var events = require('events');
var EventEmitter = events.EventEmitter;
var CloudJob = require('./cloud-job.js');

module.exports = OperationPromise;

function OperationPromise(queueClient, onCompleteFn) {
    var self,
        emitter = new EventEmitter(),
        emitterMethods = ['on', 'emit'];

    function init () {
        self = new Promise(function (resolve, reject) {
            emitter.on('complete', function (result) {
                if (self.job) {
                    self.job(result).await().then(resolve);
                    delete self.job;
                } else {
                    resolve(result);
                }
            });

            emitter.on('error', function (error) {
                reject(error);
            });
        });

        _.each(emitterMethods, function(method) {
            self[method] = _.bind(emitter[method], emitter);
        });
    }

    init ();

    function awaitCloudJob (curJobInfo) {
        return new CloudJob(queueClient, curJobInfo).await();
    }

    self.setJobFn = function(job) {
        self.job = job;

        return self;
    };

    self.from = function (promise) {
        promise.then(self.resolveWhenJobCompleted, self.processErrors);
        return this;
    };

    self.resolveWhenJobCompleted = function(jobInfo) {
        self.emit('job-queue', jobInfo);

        if (jobInfo instanceof Array) {
            Promise
                .all(jobInfo.map(awaitCloudJob))
                .then(self.processComplete, self.processErrors);
        } else {
            awaitCloudJob(jobInfo)
                .then(self.processComplete, self.processErrors);
        }

        return self;
    };

    self.processErrors = function(response) {
        var details = response ? (response.data ? response.data : (response.job ? response.job : undefined)) : undefined;
        throw new Error("The request was failed " + response +
            (details ? ". Details: " + JSON.stringify(details) : ""));
    };

    self.processComplete = function (response) {
        if (onCompleteFn) {
            Promise.resolve(onCompleteFn(response)).then(function (result) {
                emitter.emit('complete', result);
            });
        } else {
            emitter.emit('complete', response);
        }
    };

    return self;
}<commit_msg>#43 Implement possibilities to create snapshot #44 Implement possibilities to delete snapshot #45 Implement possibilities to revert server to snapshot Changes according comments<commit_after>var _ = require('underscore');
var Promise = require("bluebird");
var events = require('events');
var EventEmitter = events.EventEmitter;
var CloudJob = require('./cloud-job.js');

module.exports = OperationPromise;

function OperationPromise(queueClient, onCompleteFn) {
    var self,
        emitter = new EventEmitter(),
        emitterMethods = ['on', 'emit'];

    function init () {
        self = new Promise(function (resolve, reject) {
            emitter.on('complete', function (result) {
                if (self.job) {
                    self.job(result).await().then(resolve);
                    delete self.job;
                } else {
                    resolve(result);
                }
            });

            emitter.on('error', function (error) {
                reject(error);
            });
        });

        _.each(emitterMethods, function(method) {
            self[method] = _.bind(emitter[method], emitter);
        });
    }

    init ();

    function awaitCloudJob (curJobInfo) {
        return new CloudJob(queueClient, curJobInfo).await();
    }

    self.setJobFn = function(job) {
        self.job = job;

        return self;
    };

    self.from = function (promise) {
        promise.then(self.resolveWhenJobCompleted, self.processErrors);
        return this;
    };

    self.resolveWhenJobCompleted = function(jobInfo) {
        self.emit('job-queue', jobInfo);

        if (jobInfo instanceof Array) {
            Promise
                .all(jobInfo.map(awaitCloudJob))
                .then(self.processComplete, self.processErrors);
        } else {
            awaitCloudJob(jobInfo)
                .then(self.processComplete, self.processErrors);
        }

        return self;
    };

    self.processErrors = function(response) {
        var details;
        if (response.data) {
            details = response.data;
        }
        if (response.job) {
            details = response.job;
        }

        throw new Error("The request was failed " + response +
            (details ? ". Details: " + JSON.stringify(details) : ""));
    };

    self.processComplete = function (response) {
        if (onCompleteFn) {
            Promise.resolve(onCompleteFn(response)).then(function (result) {
                emitter.emit('complete', result);
            });
        } else {
            emitter.emit('complete', response);
        }
    };

    return self;
}
{'fraction_non_alphanumeric': 0.12176681257461201, 'fraction_numerical': 0.001193792280143255, 'mean_word_length': 1.9363317757009346, 'pattern_counts': {'":': 0, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2554060', 'n_tokens_mistral': 1344, 'n_tokens_neox': 1286, 'n_words': 277}