UNPKG

featurehub-javascript-client-sdk

Version:
107 lines 4.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FeatureHubEventSourceClient = void 0; const feature_hub_config_1 = require("./feature_hub_config"); const models_1 = require("./models"); const featurehub_repository_1 = require("./featurehub_repository"); class FeatureHubEventSourceClient { constructor(config, repository) { this._stopped = false; this._config = config; this._repository = repository; } get stopped() { return this._stopped; } init() { if (this._staleEnvironmentTimeoutId) { return; } const options = {}; if (this._header) { options.headers = { 'x-featurehub': this._header }; } feature_hub_config_1.fhLog.trace('listening at ', this._config.url()); this.eventSource = FeatureHubEventSourceClient.eventSourceProvider(this._config.url(), options); for (const name of [models_1.SSEResultState.Features, models_1.SSEResultState.Feature, models_1.SSEResultState.DeleteFeature, models_1.SSEResultState.Bye, models_1.SSEResultState.Failure, models_1.SSEResultState.Ack, models_1.SSEResultState.Config]) { const fName = name.toString(); this.eventSource.addEventListener(fName, e => { try { const data = JSON.parse(e.data); feature_hub_config_1.fhLog.trace(`received ${fName}`, data); if (fName === models_1.SSEResultState.Config) { this.processConfig(data); } else { this._repository.notify(name, data); } } catch (e) { feature_hub_config_1.fhLog.error('SSE: Failed to understand result', e); } }); } this.eventSource.onerror = (e) => { if (!this._stopped) { if (this._repository.readyness !== featurehub_repository_1.Readyness.Ready || (e.status && (e.status > 504 || (e.status >= 400 && e.status < 500)))) { feature_hub_config_1.fhLog.error('Connection failed and repository not in ready state indicating persistent failure', e); this._repository.notify(models_1.SSEResultState.Failure, null); this.close(); } else { feature_hub_config_1.fhLog.trace('refreshing connection in case of staleness', e); } } }; } close() { if (this.eventSource != null) { const es = this.eventSource; this.eventSource = undefined; es.close(); } } clientEvaluated() { return this._config.clientEvaluated(); } contextChange(header) { this._header = header; if (this.eventSource !== undefined) { this.close(); } this.init(); return Promise.resolve(undefined); } poll() { if (this.eventSource === undefined) { this.init(); } return new Promise((resolve) => resolve()); } requiresReplacementOnHeaderChange() { return true; } processConfig(data) { if (data['edge.stale']) { this._stopped = true; this.close(); this._staleEnvironmentTimeoutId = setTimeout(() => { clearTimeout(this._staleEnvironmentTimeoutId); this._staleEnvironmentTimeoutId = undefined; this._stopped = false; this.init(); }, data['edge.stale'] * 1000); } } } exports.FeatureHubEventSourceClient = FeatureHubEventSourceClient; FeatureHubEventSourceClient.eventSourceProvider = (url, dict) => { const realUrl = (dict === null || dict === void 0 ? void 0 : dict.headers) && dict.headers['x-featurehub'] ? url + '?xfeaturehub=' + encodeURI(dict.headers['x-featurehub']) : url; return new EventSource(realUrl, dict); }; //# sourceMappingURL=featurehub_eventsource.js.map