Esempio n. 1
0
        _iterator: function _iterator(err, res) {

            if (this._interrupt) {
                return this;
            }

            if (!this._alive) {
                throw new Error('this bucks object already destroyed.');
            }

            //taskから渡ってきたresを配列に保持して最後に返す
            res ? this._results.push(res) : this._results.push(null);

            var task  = this._tasks.shift();

            if (!task) { // end of task
                return this.destroy(err);
            }

            try {
                var self = this;
                // handle task
                task(err, res, function (err, res) {
                    self._iterator(err, res);
                    return this;
                }, this._taskcount++);

            } catch (exception) {

                if (!this._results) {
                    // this instance already destroyed.
                    // this exception is uncaught exception.
                    if (Bucks.DEBUG) {
                        // if DEBUG, logging
                        logError(exception);
                    }
                    if (Bucks._isOnError) {
                        Bucks._onError(exception, this);
                    } else {
                        throw exception;
                    }
                }

                // handle error
                this._iterator(exception, null);
            }
            return this;
        },
Esempio n. 2
0
        destroy: function destroy(err) {
            if (!this._alive) {
                return this;
            }
            var ress = this._results;
            var callback = this.callback;
            var failure  = this.failure;
            var dispose = this.dispose;

            this._tasks = null;
            this._taskcount = 0;
            this._results = null;
            this.callback = none;
            this.failure = none;
            this.dispose = none;

            // @TODO: Change where to run to finally.
            //dispose.call(this); // run the "dispose()"

            ress.shift(); // remove null-result created on first iterate

            try {
                if (callback) {
                    callback(err, ress);
                } else if (err) {
                    // if err and no callback,
                    // throw to exec failure callback
                    throw err;
                }
            } catch (ex) {
                if (failure) {
                    try {
                        failure(ex);
                    } catch (ex1) {
                        if (Bucks._isOnError) {
                            Bucks._onError(ex1, this);
                        } else {
                            throw ex1;
                        }
                    }
                } else {
                    // if err and no failure callback
                    // throw it
                    if (Bucks.DEBUG) {
                        // if DEBUG, logging
                        logError(ex);
                    }
                    if (Bucks._isOnError) {
                        Bucks._onError(ex, this);
                    } else {
                        throw ex;
                    }

                }
            } finally {

                try {
                    dispose.call(this); // run the "dispose()"
                } catch (ex1) {
                    if (Bucks._isOnError) {
                        Bucks._onError(ex1, this);
                    } else {
                        throw ex1;
                    }
                }

                // @TODO: excluded from the deleted
                // delete this._interrupt;

                delete this._alive;
                delete Bucks.running[this.__id];
                delete Bucks.living[this.__id];
                delete this.__id;
            }
            return this;
        },