UNPKG

3.66 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 });
6exports.StreamableScanPromise = exports.StreamableReplicasPromise = exports.StreamableRowPromise = exports.StreamablePromise = void 0;
7/* eslint jsdoc/require-jsdoc: off */
8const events_1 = __importDefault(require("events"));
9/**
10 * @internal
11 */
12class StreamablePromise extends events_1.default {
13 /**
14 * @internal
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 * @internal
38 */
39 get [Symbol.toStringTag]() {
40 return Promise[Symbol.toStringTag];
41 }
42}
43exports.StreamablePromise = StreamablePromise;
44/**
45 * Provides the ability to be used as both a promise, or an event emitter. Enabling
46 * an application to easily retrieve all results using async/await, while also enabling
47 * streaming of results by listening for the row and meta events.
48 */
49class 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}
67exports.StreamableRowPromise = StreamableRowPromise;
68/**
69 * Provides the ability to be used as both a promise, or an event emitter. Enabling
70 * an application to easily retrieve all results using async/await, while also enabling
71 * streaming of results by listening for the replica event.
72 */
73class 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}
89exports.StreamableReplicasPromise = StreamableReplicasPromise;
90class 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}
113exports.StreamableScanPromise = StreamableScanPromise;