UNPKG

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