@configurator/ravendb
Version:
RavenDB client for Node.js
76 lines • 2.52 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChangesObservable = void 0;
class ChangesObservable {
constructor(type, connectionState, filter) {
this._subscribers = new Set();
this._errorSubscribers = new Set();
this._type = type;
this._connectionState = connectionState;
this._filter = filter;
}
on(event, handler) {
switch (event) {
case "data":
if (!this._sendHandler) {
this._sendHandler = (payload) => this.send(payload);
this._connectionState.addOnChangeNotification(this._type, this._sendHandler);
}
this._subscribers.add(handler);
this._connectionState.inc();
break;
case "error":
if (!this._errorHandler) {
this._errorHandler = (ex) => this.error(ex);
this._connectionState.addOnError(this._errorHandler);
}
this._errorSubscribers.add(handler);
break;
}
return this;
}
removeListener(event, handler) {
return this.off(event, handler);
}
off(event, handler) {
switch (event) {
case "data":
if (this._subscribers.delete(handler)) {
this._connectionState.dec();
}
if (!this._subscribers.size) {
this._connectionState.removeOnChangeNotification(this._type, this._sendHandler);
this._sendHandler = undefined;
}
break;
case "error":
this._errorSubscribers.delete(handler);
if (!this._errorSubscribers.size) {
this._connectionState.removeOnError(this._errorHandler);
this._errorHandler = undefined;
}
break;
}
return this;
}
send(msg) {
try {
if (!this._filter(msg)) {
return;
}
}
catch (e) {
this.error(e);
return;
}
this._subscribers.forEach(x => x(msg));
}
error(e) {
this._errorSubscribers.forEach(x => x(e));
}
ensureSubscribedNow() {
return this._connectionState.ensureSubscribedNow();
}
}
exports.ChangesObservable = ChangesObservable;
//# sourceMappingURL=ChangesObservable.js.map