jinaga
Version:
Data management for web and mobile applications.
104 lines • 4.54 kB
JavaScript
;
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.WsGraphNetwork = void 0;
const ws_graph_client_1 = require("./ws-graph-client");
class WsGraphNetwork {
constructor(httpNetwork, store, wsEndpoint, getUserIdentity, getAuthorizationHeader) {
this.httpNetwork = httpNetwork;
// Internal per-feed event maps
this.onResponseHandlers = new Map();
this.onErrorHandlers = new Map();
const getWsUrl = () => __awaiter(this, void 0, void 0, function* () {
try {
const url = new URL(wsEndpoint);
// Append Authorization token if provided (browsers cannot set custom WS headers)
if (getAuthorizationHeader) {
try {
const auth = yield getAuthorizationHeader();
if (auth) {
url.searchParams.set("authorization", auth);
}
}
catch (_a) {
// ignore auth retrieval failures
}
}
// Optionally append user identity
if (getUserIdentity) {
try {
const id = yield getUserIdentity();
if (id) {
url.searchParams.set("uid", `${encodeURIComponent(id.provider)}:${encodeURIComponent(id.id)}`);
}
}
catch (_b) {
// ignore identity retrieval failures
}
}
return url.toString();
}
catch (_c) {
return wsEndpoint;
}
});
this.wsClient = new ws_graph_client_1.WsGraphClient(getWsUrl, store, (feed, bookmark) => this.onBookmarkAdvance(feed, bookmark), (err) => this.onGlobalError(err), getUserIdentity, (envelopes) => this.onFactsAdded(envelopes));
}
feeds(start, specification) {
return this.httpNetwork.feeds(start, specification);
}
fetchFeed(feed, bookmark) {
return this.httpNetwork.fetchFeed(feed, bookmark);
}
streamFeed(feed, bookmark, onResponse, onError, feedRefreshIntervalSeconds) {
// Register a temporary handler for BOOK events for this feed
this.onResponseHandlers.set(feed, onResponse);
this.onErrorHandlers.set(feed, onError);
const unsubscribe = this.wsClient.subscribe(feed, bookmark, feedRefreshIntervalSeconds);
return () => {
this.onResponseHandlers.delete(feed);
this.onErrorHandlers.delete(feed);
unsubscribe();
};
}
load(factReferences) {
return this.httpNetwork.load(factReferences);
}
onBookmarkAdvance(feed, bookmark) {
return __awaiter(this, void 0, void 0, function* () {
const handler = this.onResponseHandlers.get(feed);
if (handler) {
// Facts already persisted via graph stream, notify empty refs with updated bookmark
yield handler([], bookmark);
}
});
}
onGlobalError(err) {
// Broadcast error to all active feeds
for (const h of this.onErrorHandlers.values()) {
h(err);
}
}
// Phase 3.4: Observer-notification bridge
setFactsAddedListener(listener) {
this.factsAddedListener = listener;
}
// Called by WsGraphClient when facts are added via WS
onFactsAdded(envelopes) {
return __awaiter(this, void 0, void 0, function* () {
if (this.factsAddedListener) {
yield this.factsAddedListener(envelopes);
}
});
}
}
exports.WsGraphNetwork = WsGraphNetwork;
//# sourceMappingURL=wsGraphNetwork.js.map