cloudrx
Version:
TypeScript library for streaming cloud provider events using RxJS with automatic persistence and replay capabilities
128 lines • 5.14 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Memory = void 0;
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
const base_1 = require("../base");
class Memory extends base_1.CloudProvider {
constructor(id, options) {
super(id, options);
this.options = options;
this._all = new rxjs_1.ReplaySubject();
this._latest = new rxjs_1.ReplaySubject(1);
this._initialized = false;
this.delays = {
init: 2000,
emission: 1000,
storage: 25,
};
this.delays = {
init: this.options?.delays?.init ?? this.delays.init,
emission: this.options?.delays?.emission ?? this.delays.emission,
storage: this.options?.delays?.storage ?? this.delays.storage,
};
}
_init() {
return new rxjs_1.Observable((subscriber) => {
if (this.signal.aborted) {
this.logger.debug?.(`[${this.id}] Init aborted`);
subscriber.error(this.options?.signal?.reason);
return;
}
if (this._initialized) {
this.logger.debug?.(`[${this.id}] Already initialized`);
subscriber.next(this);
subscriber.complete();
return;
}
this.logger.debug?.(`[${this.id}] Starting initialization with ${this.delays.init}ms delay`);
const initialization = (0, rxjs_1.timer)(this.delays.init)
.pipe((0, operators_1.takeUntil)((0, rxjs_1.fromEvent)(this.signal, 'abort')), (0, rxjs_1.map)(() => {
this.logger.debug?.(`[${this.id}] Initialization complete`);
this._initialized = true;
subscriber.next(this);
subscriber.complete();
}))
.subscribe();
this.logger.debug?.(`[${this.id}] Starting emission interval every ${this.delays.emission}ms`);
const emission = (0, rxjs_1.interval)(this.delays.emission)
.pipe((0, operators_1.takeUntil)((0, rxjs_1.fromEvent)(this.signal, 'abort')), (0, rxjs_1.map)(() => {
this._all.next([]);
this._latest.next([]);
}))
.subscribe();
return () => {
this.logger.debug?.(`[${this.id}] Init cleanup`);
initialization.unsubscribe();
emission.unsubscribe();
};
});
}
_stream(all) {
return new rxjs_1.Observable((subscriber) => {
if (!this._initialized) {
this.logger.debug?.(`[${this.id}] Stream requested but not initialized`);
subscriber.error(new Error('Provider not initialized - call init() first'));
return;
}
const streamType = all ? 'all' : 'latest';
const stream = all ? this._all : this._latest;
const subscription = stream.subscribe({
next: (records) => {
if (records.length > 0) {
this.logger.debug?.(`[${this.id}] ${streamType} stream emitted ${records.length} records`);
}
subscriber.next(records);
},
error: (error) => {
this.logger.debug?.(`[${this.id}] ${streamType} stream error:`, error);
subscriber.error(error);
},
complete: () => subscriber.complete(),
});
return () => {
subscription.unsubscribe();
};
});
}
_store(item) {
return new rxjs_1.Observable((subscriber) => {
if (!this._initialized) {
this.logger.debug?.(`[${this.id}] Store requested but not initialized`);
subscriber.error(new Error('Provider not initialized - call init() first'));
return;
}
const id = crypto.randomUUID();
this.logger.debug?.(`[${this.id}] Storing item with id ${id}:`, item);
const data = {
payload: JSON.stringify(item),
};
const record = {
id,
data,
};
const emission = (0, rxjs_1.timer)(this.delays.storage)
.pipe((0, operators_1.takeUntil)((0, rxjs_1.fromEvent)(this.signal, 'abort')), (0, rxjs_1.map)(() => {
this._all.next([record]);
this._latest.next([record]);
const matcher = (event) => event.id === id;
subscriber.next(matcher);
subscriber.complete();
}))
.subscribe();
return () => {
emission.unsubscribe();
};
});
}
_unmarshall(event) {
const marker = event.id;
const item = JSON.parse(event.data.payload);
return {
...item,
__marker__: marker,
};
}
}
exports.Memory = Memory;
//# sourceMappingURL=provider.js.map