Result.js
5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var bluebird_1 = __importDefault(require("bluebird"));
var Result = /** @class */ (function () {
function Result(converter) {
this.converter = converter;
this.finalResult = [];
}
Object.defineProperty(Result.prototype, "needEmitLine", {
get: function () {
return !!this.converter.parseRuntime.subscribe && !!this.converter.parseRuntime.subscribe.onNext || this.needPushDownstream;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Result.prototype, "needPushDownstream", {
get: function () {
if (this._needPushDownstream === undefined) {
this._needPushDownstream = this.converter.listeners("data").length > 0 || this.converter.listeners("readable").length > 0;
}
return this._needPushDownstream;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Result.prototype, "needEmitAll", {
get: function () {
return !!this.converter.parseRuntime.then;
},
enumerable: true,
configurable: true
});
Result.prototype.processResult = function (resultLines) {
var _this = this;
var startPos = this.converter.parseRuntime.parsedLineNumber;
// let prom: P<any>;
return new bluebird_1.default(function (resolve, reject) {
if (_this.needEmitLine) {
processLineByLine(resultLines, _this.converter, 0, _this.needPushDownstream, function (err) {
if (err) {
reject(err);
}
else {
_this.appendFinalResult(resultLines);
resolve();
}
});
// resolve();
}
else {
_this.appendFinalResult(resultLines);
resolve();
}
});
};
Result.prototype.appendFinalResult = function (lines) {
if (this.needEmitAll) {
this.finalResult = this.finalResult.concat(lines);
}
this.converter.parseRuntime.parsedLineNumber += lines.length;
};
Result.prototype.processError = function (err) {
if (this.converter.parseRuntime.subscribe && this.converter.parseRuntime.subscribe.onError) {
this.converter.parseRuntime.subscribe.onError(err);
}
if (this.converter.parseRuntime.then && this.converter.parseRuntime.then.onrejected) {
this.converter.parseRuntime.then.onrejected(err);
}
};
Result.prototype.endProcess = function () {
if (this.needEmitAll) {
if (this.converter.parseRuntime.then && this.converter.parseRuntime.then.onfulfilled) {
this.converter.parseRuntime.then.onfulfilled(this.finalResult);
}
}
if (this.converter.parseRuntime.subscribe && this.converter.parseRuntime.subscribe.onCompleted) {
this.converter.parseRuntime.subscribe.onCompleted();
}
};
return Result;
}());
exports.Result = Result;
function processLineByLine(lines, conv, offset, needPushDownstream, cb) {
if (offset >= lines.length) {
cb();
}
else {
if (conv.parseRuntime.subscribe && conv.parseRuntime.subscribe.onNext) {
var hook_1 = conv.parseRuntime.subscribe.onNext;
var nextLine_1 = lines[offset];
var res = hook_1(nextLine_1, conv.parseRuntime.parsedLineNumber + offset);
offset++;
// if (isAsync === undefined) {
if (res && res.then) {
res.then(function () {
processRecursive(lines, hook_1, conv, offset, needPushDownstream, cb, nextLine_1);
}, cb);
}
else {
// processRecursive(lines, hook, conv, offset, needPushDownstream, cb, nextLine, false);
if (needPushDownstream) {
pushDownstream(conv, nextLine_1);
}
while (offset < lines.length) {
var line = lines[offset];
hook_1(line, conv.parseRuntime.parsedLineNumber + offset);
offset++;
if (needPushDownstream) {
pushDownstream(conv, line);
}
}
cb();
}
// } else if (isAsync === true) {
// (res as PromiseLike<void>).then(function () {
// processRecursive(lines, hook, conv, offset, needPushDownstream, cb, nextLine, true);
// }, cb);
// } else if (isAsync === false) {
// processRecursive(lines, hook, conv, offset, needPushDownstream, cb, nextLine, false);
// }
}
else {
if (needPushDownstream) {
while (offset < lines.length) {
var line = lines[offset++];
pushDownstream(conv, line);
}
}
cb();
}
}
}
function processRecursive(lines, hook, conv, offset, needPushDownstream, cb, res) {
if (needPushDownstream) {
pushDownstream(conv, res);
}
processLineByLine(lines, conv, offset, needPushDownstream, cb);
}
function pushDownstream(conv, res) {
if (typeof res === "object" && !conv.options.objectMode) {
conv.push(JSON.stringify(res) + "\n", "utf8");
}
else {
conv.push(res);
}
}
//# sourceMappingURL=Result.js.map