UNPKG

jinaga

Version:

Data management for web and mobile applications.

136 lines 6.08 kB
"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.WebClient = exports.SyncStatusNotifier = void 0; const serialize_1 = require("../fork/serialize"); const trace_1 = require("../util/trace"); const ContentType_1 = require("./ContentType"); const serializer_1 = require("./serializer"); class SyncStatusNotifier { constructor() { this.syncStatusHandlers = []; } onSyncStatus(handler) { this.syncStatusHandlers.push(handler); } notify(status) { this.syncStatusHandlers.forEach(handler => { handler(status); }); } } exports.SyncStatusNotifier = SyncStatusNotifier; function delay(timeSeconds) { return new Promise((resolve, reject) => { setTimeout(resolve, timeSeconds * 1000); }); } class WebClient { constructor(httpConnection, syncStatusNotifier, config) { this.httpConnection = httpConnection; this.syncStatusNotifier = syncStatusNotifier; this.config = config; this.saveContentTypes = null; } login() { return __awaiter(this, void 0, void 0, function* () { return yield this.httpConnection.get('/login'); }); } save(envelopes) { return __awaiter(this, void 0, void 0, function* () { if (this.saveContentTypes === null) { this.saveContentTypes = yield this.httpConnection.getAcceptedContentTypes('/save'); } if (this.saveContentTypes.includes(ContentType_1.ContentTypeGraph)) { yield this.post('/save', ContentType_1.ContentTypeGraph, undefined, (0, serializer_1.serializeGraph)(envelopes)); } else { yield this.post('/save', ContentType_1.ContentTypeJson, ContentType_1.ContentTypeJson, JSON.stringify((0, serialize_1.serializeSave)(envelopes))); } }); } saveWithRetry(envelopes) { return __awaiter(this, void 0, void 0, function* () { if (this.saveContentTypes === null) { this.saveContentTypes = yield this.httpConnection.getAcceptedContentTypes('/save'); } if (this.saveContentTypes.includes(ContentType_1.ContentTypeGraph)) { yield this.postWithLimitedRetry('/save', ContentType_1.ContentTypeGraph, undefined, (0, serializer_1.serializeGraph)(envelopes)); } else { yield this.postWithLimitedRetry('/save', ContentType_1.ContentTypeJson, ContentType_1.ContentTypeJson, JSON.stringify((0, serialize_1.serializeSave)(envelopes))); } }); } load(load) { return __awaiter(this, void 0, void 0, function* () { return yield this.post('/load', ContentType_1.ContentTypeJson, ContentType_1.ContentTypeJson, JSON.stringify(load)); }); } loadWithRetry(load) { return __awaiter(this, void 0, void 0, function* () { return yield this.postWithLimitedRetry('/load', ContentType_1.ContentTypeJson, ContentType_1.ContentTypeJson, JSON.stringify(load)); }); } feeds(request) { return __awaiter(this, void 0, void 0, function* () { return yield this.post('/feeds', ContentType_1.ContentTypeText, ContentType_1.ContentTypeJson, request); }); } feed(feed, bookmark) { return __awaiter(this, void 0, void 0, function* () { return yield this.httpConnection.get(`/feeds/${feed}?b=${bookmark}`); }); } streamFeed(feed, bookmark, onResponse, onError, feedRefreshIntervalSeconds) { return this.httpConnection.getStream(`/feeds/${feed}?b=${bookmark}`, r => onResponse(r), onError, feedRefreshIntervalSeconds); } post(path, contentType, accept, body) { return __awaiter(this, void 0, void 0, function* () { const response = yield this.httpConnection.post(path, contentType, accept, body, this.config.timeoutSeconds); if (response.result === 'success') { return response.response; } else { throw new Error(response.error); } }); } postWithLimitedRetry(path, contentType, accept, body) { return __awaiter(this, void 0, void 0, function* () { let timeoutSeconds = this.config.timeoutSeconds; let retrySeconds = 1; while (true) { const response = yield this.httpConnection.post(path, contentType, accept, body, this.config.timeoutSeconds); if (response.result === 'success') { return response.response; } else if (response.result === 'failure') { throw new Error(response.error); } else { if (retrySeconds <= 4) { trace_1.Trace.warn(`Retrying in ${retrySeconds} seconds: ${response.error}`); yield delay(retrySeconds + Math.random()); timeoutSeconds = Math.min(timeoutSeconds * 2, 60); retrySeconds = retrySeconds * 2; } else { throw new Error(response.error); } } } }); } } exports.WebClient = WebClient; //# sourceMappingURL=web-client.js.map