1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.ClientProxy = void 0;
|
4 | const random_string_generator_util_1 = require("@nestjs/common/utils/random-string-generator.util");
|
5 | const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
|
6 | const rxjs_1 = require("rxjs");
|
7 | const operators_1 = require("rxjs/operators");
|
8 | const constants_1 = require("../constants");
|
9 | const incoming_response_deserializer_1 = require("../deserializers/incoming-response.deserializer");
|
10 | const invalid_message_exception_1 = require("../errors/invalid-message.exception");
|
11 | const identity_serializer_1 = require("../serializers/identity.serializer");
|
12 | const utils_1 = require("../utils");
|
13 | class ClientProxy {
|
14 | constructor() {
|
15 | this.routingMap = new Map();
|
16 | }
|
17 | send(pattern, data) {
|
18 | if (shared_utils_1.isNil(pattern) || shared_utils_1.isNil(data)) {
|
19 | return rxjs_1.throwError(new invalid_message_exception_1.InvalidMessageException());
|
20 | }
|
21 | return rxjs_1.defer(async () => this.connect()).pipe(operators_1.mergeMap(() => new rxjs_1.Observable((observer) => {
|
22 | const callback = this.createObserver(observer);
|
23 | return this.publish({ pattern, data }, callback);
|
24 | })));
|
25 | }
|
26 | emit(pattern, data) {
|
27 | if (shared_utils_1.isNil(pattern) || shared_utils_1.isNil(data)) {
|
28 | return rxjs_1.throwError(new invalid_message_exception_1.InvalidMessageException());
|
29 | }
|
30 | const source = rxjs_1.defer(async () => this.connect()).pipe(operators_1.mergeMap(() => this.dispatchEvent({ pattern, data })), operators_1.publish());
|
31 | source.connect();
|
32 | return source;
|
33 | }
|
34 | createObserver(observer) {
|
35 | return ({ err, response, isDisposed }) => {
|
36 | if (err) {
|
37 | return observer.error(this.serializeError(err));
|
38 | }
|
39 | else if (response !== undefined && isDisposed) {
|
40 | observer.next(this.serializeResponse(response));
|
41 | return observer.complete();
|
42 | }
|
43 | else if (isDisposed) {
|
44 | return observer.complete();
|
45 | }
|
46 | observer.next(this.serializeResponse(response));
|
47 | };
|
48 | }
|
49 | serializeError(err) {
|
50 | return err;
|
51 | }
|
52 | serializeResponse(response) {
|
53 | return response;
|
54 | }
|
55 | assignPacketId(packet) {
|
56 | const id = random_string_generator_util_1.randomStringGenerator();
|
57 | return Object.assign(packet, { id });
|
58 | }
|
59 | connect$(instance, errorEvent = constants_1.ERROR_EVENT, connectEvent = constants_1.CONNECT_EVENT) {
|
60 | const error$ = rxjs_1.fromEvent(instance, errorEvent).pipe(operators_1.map((err) => {
|
61 | throw err;
|
62 | }));
|
63 | const connect$ = rxjs_1.fromEvent(instance, connectEvent);
|
64 | return rxjs_1.merge(error$, connect$).pipe(operators_1.take(1));
|
65 | }
|
66 | getOptionsProp(obj, prop, defaultValue = undefined) {
|
67 | return (obj && obj[prop]) || defaultValue;
|
68 | }
|
69 | normalizePattern(pattern) {
|
70 | return utils_1.transformPatternToRoute(pattern);
|
71 | }
|
72 | initializeSerializer(options) {
|
73 | this.serializer =
|
74 | (options &&
|
75 | options.serializer) ||
|
76 | new identity_serializer_1.IdentitySerializer();
|
77 | }
|
78 | initializeDeserializer(options) {
|
79 | this.deserializer =
|
80 | (options &&
|
81 | options.deserializer) ||
|
82 | new incoming_response_deserializer_1.IncomingResponseDeserializer();
|
83 | }
|
84 | }
|
85 | exports.ClientProxy = ClientProxy;
|