jinaga
Version:
Data management for web and mobile applications.
86 lines • 3.53 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Subscriber = void 0;
const trace_1 = require("../util/trace");
class Subscriber {
constructor(feed, network, store, notifyFactsAdded, refreshIntervalSeconds) {
this.feed = feed;
this.network = network;
this.store = store;
this.notifyFactsAdded = notifyFactsAdded;
this.refreshIntervalSeconds = refreshIntervalSeconds;
this.refCount = 0;
this.bookmark = "";
this.resolved = false;
}
addRef() {
this.refCount++;
return this.refCount === 1;
}
release() {
this.refCount--;
return this.refCount === 0;
}
start() {
return __awaiter(this, void 0, void 0, function* () {
this.bookmark = yield this.store.loadBookmark(this.feed);
yield new Promise((resolve, reject) => {
this.resolved = false;
// Refresh the connection at the configured interval.
this.disconnect = this.connectToFeed(resolve, reject);
this.timer = setInterval(() => {
if (this.disconnect) {
this.disconnect();
}
this.disconnect = this.connectToFeed(resolve, reject);
}, this.refreshIntervalSeconds * 1000);
});
});
}
stop() {
if (this.timer) {
clearInterval(this.timer);
this.timer = undefined;
}
if (this.disconnect) {
this.disconnect();
this.disconnect = undefined;
}
}
connectToFeed(resolve, reject) {
return this.network.streamFeed(this.feed, this.bookmark, (factReferences, nextBookmark) => __awaiter(this, void 0, void 0, function* () {
const knownFactReferences = yield this.store.whichExist(factReferences);
const unknownFactReferences = factReferences.filter(fr => !knownFactReferences.includes(fr));
if (unknownFactReferences.length > 0) {
const graph = yield this.network.load(unknownFactReferences);
yield this.store.save(graph);
if (graph.length > 0) {
trace_1.Trace.counter("facts_saved", graph.length);
}
yield this.store.saveBookmark(this.feed, nextBookmark);
this.bookmark = nextBookmark;
yield this.notifyFactsAdded(graph);
}
if (!this.resolved) {
this.resolved = true;
resolve();
}
}), err => {
if (!this.resolved) {
this.resolved = true;
reject(err);
}
});
}
}
exports.Subscriber = Subscriber;
//# sourceMappingURL=subscriber.js.map