1 |
|
2 | (function() {
|
3 | var AbstractError, AbstractIterator, AlreadyEndError, Errors, InvalidArgumentError, NotImplementedError, inherits, util;
|
4 |
|
5 | util = require("abstract-object/lib/util");
|
6 |
|
7 | inherits = util.inherits;
|
8 |
|
9 | Errors = require("./abstract-error");
|
10 |
|
11 | AbstractError = Errors.AbstractError;
|
12 |
|
13 | NotImplementedError = Errors.NotImplementedError;
|
14 |
|
15 | InvalidArgumentError = Errors.InvalidArgumentError;
|
16 |
|
17 | AlreadyEndError = Errors.AlreadyEndError;
|
18 |
|
19 | module.exports = AbstractIterator = (function() {
|
20 | AbstractIterator.AlreadyEndError = AlreadyEndError;
|
21 |
|
22 | function AbstractIterator(db) {
|
23 | this.db = db;
|
24 | this._ended = false;
|
25 | this._nexting = false;
|
26 | }
|
27 |
|
28 | AbstractIterator.prototype._next = function(callback) {
|
29 | var self;
|
30 | self = this;
|
31 | if (this._nextSync) {
|
32 | return setImmediate(function() {
|
33 | var e, result;
|
34 | try {
|
35 | result = self._nextSync();
|
36 | self._nexting = false;
|
37 | if (result) {
|
38 | return callback(null, result[0], result[1]);
|
39 | } else {
|
40 | return callback();
|
41 | }
|
42 | } catch (_error) {
|
43 | e = _error;
|
44 | self._nexting = false;
|
45 | return callback(e);
|
46 | }
|
47 | });
|
48 | } else {
|
49 | return setImmediate(function() {
|
50 | self._nexting = false;
|
51 | return callback();
|
52 | });
|
53 | }
|
54 | };
|
55 |
|
56 | AbstractIterator.prototype._end = function(callback) {
|
57 | var self;
|
58 | self = this;
|
59 | if (this._endSync) {
|
60 | return setImmediate(function() {
|
61 | var e, result;
|
62 | try {
|
63 | result = self._endSync();
|
64 | return callback(null, result);
|
65 | } catch (_error) {
|
66 | e = _error;
|
67 | return callback(e);
|
68 | }
|
69 | });
|
70 | } else {
|
71 | return setImmediate(function() {
|
72 | return callback();
|
73 | });
|
74 | }
|
75 | };
|
76 |
|
77 | AbstractIterator.prototype.nextSync = function() {
|
78 | var result;
|
79 | if (this._nextSync) {
|
80 | this._nexting = true;
|
81 | result = this._nextSync();
|
82 | this._nexting = false;
|
83 | return result;
|
84 | }
|
85 | throw new NotImplementedError();
|
86 | };
|
87 |
|
88 | AbstractIterator.prototype.endSync = function() {
|
89 | if (this._endSync) {
|
90 | return this._endSync();
|
91 | }
|
92 | throw new NotImplementedError();
|
93 | };
|
94 |
|
95 | AbstractIterator.prototype.next = function(callback) {
|
96 | var self;
|
97 | if (typeof callback !== "function") {
|
98 | throw new InvalidArgumentError("next() requires a callback argument");
|
99 | }
|
100 | if (this._ended) {
|
101 | return callback(new AlreadyEndError("cannot call next() after end()"));
|
102 | }
|
103 | if (this._nexting) {
|
104 | return callback(new AlreadyEndError("cannot call next() before previous next() has completed"));
|
105 | }
|
106 | this._nexting = true;
|
107 | self = this;
|
108 | return this._next(function() {
|
109 | self._nexting = false;
|
110 | return callback.apply(null, arguments);
|
111 | });
|
112 | };
|
113 |
|
114 | AbstractIterator.prototype.end = function(callback) {
|
115 | if (typeof callback !== "function") {
|
116 | throw new InvalidArgumentError("end() requires a callback argument");
|
117 | }
|
118 | if (this._ended) {
|
119 | return callback(new AlreadyEndError("end() already called on iterator"));
|
120 | }
|
121 | this._ended = true;
|
122 | return this._end(callback);
|
123 | };
|
124 |
|
125 | return AbstractIterator;
|
126 |
|
127 | })();
|
128 |
|
129 | }).call(this);
|