rox-ssr-multi-entrypoints
Version:
Rollout.io SDK with support for multiple entrypoints
80 lines • 3.94 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.Rox = void 0;
const rox_ssr_1 = require("rox-ssr");
class RoxClientMultiEntrypoints extends rox_ssr_1._RoxClient {
constructor() {
super(...arguments);
this._confFetchedHandlers = [];
}
register(namespace, roxContainer) {
try {
super.register(namespace, roxContainer);
}
catch (e) {
if (!e.message || !e.message.includes('A namespace must be unique')) {
// register failed for a reason other than namespace already existing
throw e;
}
}
// calling register multiple times is legitimate, but underlying SDK does not support it
// Each webpack entrypoint actually gets its own instance
// of Flags. However, the Rox singleton remains unique.
// In this case, we rewrite the Flags with the values stored within Rox.
if (this.flags && this.flags.length > 0) {
this.flags.forEach((f) => {
if (f === null || f === void 0 ? void 0 : f.name) {
roxContainer[f.name.replace(`${namespace}.`, '')] = f; // eslint-disable-line no-param-reassign
}
});
}
}
setup(apiKey, options) {
const _super = Object.create(null, {
setup: { get: () => super.setup }
});
return __awaiter(this, void 0, void 0, function* () {
if (this._initialSetup) {
// this is a legitimate call only if parameters remain the same
if (this._initialSetup.apiKey !== apiKey
|| this._initialSetup.devModeSecret !== (options === null || options === void 0 ? void 0 : options.devModeSecret)) {
throw new Error('Rox cannot be initialized more than once');
}
// Add the configurationFetchedHandler to the list of handlers
// _configurationFetchedHandlers can only grow but this is not a memory leak; there can
// only be a finite number of entry points
if (options === null || options === void 0 ? void 0 : options.configurationFetchedHandler) {
this._confFetchedHandlers.push(options.configurationFetchedHandler);
}
return;
}
const _options = Object.assign({}, options);
if (options === null || options === void 0 ? void 0 : options.configurationFetchedHandler) {
this._confFetchedHandlers.push(options.configurationFetchedHandler);
}
_options.configurationFetchedHandler = this._confFetchedHandler;
yield _super.setup.call(this, apiKey, options);
this._initialSetup = {
apiKey,
devModeSecret: options === null || options === void 0 ? void 0 : options.devModeSecret,
};
});
}
_confFetchedHandler() {
const roxClient = this;
return (fetcherResults) => {
roxClient._confFetchedHandlers.forEach((cfh) => cfh(fetcherResults));
};
}
}
exports.Rox = new RoxClientMultiEntrypoints();
//# sourceMappingURL=rox.js.map