| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- "use strict";
- var Runner = require("./Runner");
- /**
- * This class is an array runner used to run a pipeline against a static array of data
- * @class ArrayRunner
- * @extends mungedb-aggregate.query.Runner
- * @namespace mungedb-aggregate.query
- * @module mungedb-aggregate
- * @constructor
- * @param {Array} items The array source of the data
- */
- var klass = module.exports = function ArrayRunner(array){
- base.call(this);
- if (!(array instanceof Array)) throw new Error("Array runner requires an array");
- this._array = array;
- this._position = 0;
- this._state = Runner.RunnerState.RUNNER_ADVANCED;
- }, base = Runner, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}}); //jshint ignore:line
- /**
- * Get the next result from the array.
- * @method getNext
- * @param callback {Function}
- */
- proto.getNext = function getNext(callback) {
- var obj;
- if (this._state === Runner.RunnerState.RUNNER_ADVANCED) {
- if (this._position < this._array.length){
- obj = this._array[this._position++];
- } else {
- this._state = Runner.RunnerState.RUNNER_EOF;
- }
- }
- return obj;
- };
- /**
- * Save any state required to yield.
- * @method saveState
- */
- proto.saveState = function saveState() {
- //nothing to do here
- };
- /**
- * Restore saved state, possibly after a yield. Return true if the runner is OK, false if
- * it was killed.
- * @method restoreState
- */
- proto.restoreState = function restoreState() {
- //nothing to do here
- };
- /**
- * Returns a description of the Runner
- * @method getInfo
- * @param [explain]
- * @param [planInfo]
- */
- proto.getInfo = function getInfo(explain) {
- if (explain){
- return {
- type: this.constructor.name,
- nDocs: this._array.length,
- position: this._position,
- state: this._state
- };
- }
- return undefined;
- };
|