smartdata/ts/smartdata.classes.watcher.ts

27 lines
736 B
TypeScript
Raw Normal View History

2022-05-16 22:33:44 +00:00
import * as plugins from './smartdata.plugins.js';
/**
* a wrapper for the native mongodb cursor. Exposes better
*/
export class SmartdataDbWatcher<T = any> {
// STATIC
2022-05-17 19:26:17 +00:00
public readyDeferred = plugins.smartpromise.defer();
2022-05-16 22:33:44 +00:00
// INSTANCE
public changeStream: plugins.mongodb.ChangeStream<T>;
public changeSubject = new plugins.smartrx.rxjs.Subject<T>();
constructor(changeStreamArg: plugins.mongodb.ChangeStream<T>) {
this.changeStream = changeStreamArg;
2022-05-17 19:26:17 +00:00
this.changeStream.on('change', async (item: T) => {
2022-05-16 22:33:44 +00:00
this.changeSubject.next(item);
})
2022-05-17 19:26:17 +00:00
plugins.smartdelay.delayFor(0).then(() => {
this.readyDeferred.resolve();
});
2022-05-16 22:33:44 +00:00
}
public async close() {
await this.changeStream.close();
}
}