1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | exports.StreamableScanPromise = exports.StreamableReplicasPromise = exports.StreamableRowPromise = exports.StreamablePromise = void 0;
|
7 |
|
8 | const events_1 = __importDefault(require("events"));
|
9 |
|
10 |
|
11 |
|
12 | class StreamablePromise extends events_1.default {
|
13 | |
14 |
|
15 |
|
16 | constructor(promisefyFn) {
|
17 | super();
|
18 | this._promise = null;
|
19 | this._promiseifyFn = promisefyFn;
|
20 | }
|
21 | get promise() {
|
22 | if (!this._promise) {
|
23 | this._promise = new Promise((resolve, reject) => this._promiseifyFn(this, resolve, reject));
|
24 | }
|
25 | return this._promise;
|
26 | }
|
27 | then(onfulfilled, onrejected) {
|
28 | return this.promise.then(onfulfilled, onrejected);
|
29 | }
|
30 | catch(onrejected) {
|
31 | return this.promise.catch(onrejected);
|
32 | }
|
33 | finally(onfinally) {
|
34 | return this.promise.finally(onfinally);
|
35 | }
|
36 | |
37 |
|
38 |
|
39 | get [Symbol.toStringTag]() {
|
40 | return Promise[Symbol.toStringTag];
|
41 | }
|
42 | }
|
43 | exports.StreamablePromise = StreamablePromise;
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | class StreamableRowPromise extends StreamablePromise {
|
50 | constructor(fn) {
|
51 | super((emitter, resolve, reject) => {
|
52 | let err;
|
53 | const rows = [];
|
54 | let meta;
|
55 | emitter.on('row', (r) => rows.push(r));
|
56 | emitter.on('meta', (m) => (meta = m));
|
57 | emitter.on('error', (e) => (err = e));
|
58 | emitter.on('end', () => {
|
59 | if (err) {
|
60 | return reject(err);
|
61 | }
|
62 | resolve(fn(rows, meta));
|
63 | });
|
64 | });
|
65 | }
|
66 | }
|
67 | exports.StreamableRowPromise = StreamableRowPromise;
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | class StreamableReplicasPromise extends StreamablePromise {
|
74 | constructor(fn) {
|
75 | super((emitter, resolve, reject) => {
|
76 | let err;
|
77 | const replicas = [];
|
78 | emitter.on('replica', (r) => replicas.push(r));
|
79 | emitter.on('error', (e) => (err = e));
|
80 | emitter.on('end', () => {
|
81 | if (err) {
|
82 | return reject(err);
|
83 | }
|
84 | resolve(fn(replicas));
|
85 | });
|
86 | });
|
87 | }
|
88 | }
|
89 | exports.StreamableReplicasPromise = StreamableReplicasPromise;
|
90 | class StreamableScanPromise extends StreamablePromise {
|
91 | constructor(fn) {
|
92 | super((emitter, resolve, reject) => {
|
93 | let err;
|
94 | const results = [];
|
95 | emitter.on('result', (r) => results.push(r));
|
96 | emitter.on('error', (e) => (err = e));
|
97 | emitter.on('end', () => {
|
98 | if (err) {
|
99 | return reject(err);
|
100 | }
|
101 | resolve(fn(results));
|
102 | });
|
103 | });
|
104 | this._cancelRequested = false;
|
105 | }
|
106 | get cancelRequested() {
|
107 | return this._cancelRequested;
|
108 | }
|
109 | cancelStreaming() {
|
110 | this._cancelRequested = true;
|
111 | }
|
112 | }
|
113 | exports.StreamableScanPromise = StreamableScanPromise;
|