UNPKG

@kurrent/kurrentdb-client

Version:
71 lines 2.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Subscription = void 0; const stream_1 = require("stream"); const utils_1 = require("../../utils"); class Subscription extends stream_1.Transform { convertGrpcEvent; #grpcStream; #checkpointReached; id; constructor(createGRPCStream, convertGrpcEvent, options, checkpointReached) { super({ ...options, objectMode: true }); this.convertGrpcEvent = convertGrpcEvent; this.#grpcStream = createGRPCStream(); this.#checkpointReached = checkpointReached; this.initialize(); } initialize = async () => { try { (await this.#grpcStream) .on("error", (err) => { if ((0, utils_1.isClientCancellationError)(err)) return; const error = (0, utils_1.convertToCommandError)(err); this.emit("error", error); }) .pipe(this); } catch (error) { this.emit("error", error); } }; async _transform(resp, _encoding, next) { if (resp.hasConfirmation?.()) { this.id = resp.getConfirmation()?.getSubscriptionId(); this.emit("confirmation"); } if (resp.hasCaughtUp?.()) { this.emit("caughtUp"); } if (resp.hasFellBehind?.()) { this.emit("fellBehind"); } if (resp.hasCheckpoint?.() && this.#checkpointReached) { const checkpoint = resp.getCheckpoint(); const position = { commit: BigInt(checkpoint.getCommitPosition()), prepare: BigInt(checkpoint.getPreparePosition()), }; await this.#checkpointReached(this, position); } if (resp.hasEvent?.()) { const resolved = this.convertGrpcEvent(resp.getEvent()); return next(null, resolved); } next(); } async unsubscribe() { const stream = await this.#grpcStream; return new Promise((resolve) => { // https://github.com/grpc/grpc-node/issues/1464 // https://github.com/grpc/grpc-node/issues/1652 setImmediate(() => { stream.cancel(); resolve(); }); }); } } exports.Subscription = Subscription; //# sourceMappingURL=Subscription.js.map