@botonic/plugin-contentful
Version:
Botonic Plugin Contentful is one of the **[available](https://github.com/hubtype/botonic/tree/master/packages)** plugins for Botonic. **[Contentful](http://www.contentful.com)** is a CMS (Content Management System) which manages contents of a great variet
56 lines • 1.5 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DynamicSingletonMap = exports.SingletonMap = exports.Singleton = void 0;
class Singleton {
constructor(factory) {
this.factory = factory;
this.isValueSet = false;
}
get value() {
if (!this.isValueSet) {
this.factoryOutput = this.factory();
this.isValueSet = true;
}
return this.factoryOutput;
}
}
exports.Singleton = Singleton;
/**
* Singletons dictionary accessible through a string
*/
class SingletonMap {
constructor(map) {
this.map = {};
for (const [k, v] of Object.entries(map)) {
this.map[k] = new Singleton(v);
}
}
value(key) {
const lazy = this.map[key];
if (!lazy) {
throw new Error(`No factory for key ${key}`);
}
return lazy.value;
}
}
exports.SingletonMap = SingletonMap;
/**
* Given a factory function (string) => T,
* it ensures that we don't invoke it more than once per each input value
*/
class DynamicSingletonMap {
constructor(factories) {
this.factories = factories;
this.values = {};
}
value(key) {
if (!(key in this.values)) {
const val = this.factories(key);
this.values[key] = val;
return val;
}
return this.values[key];
}
}
exports.DynamicSingletonMap = DynamicSingletonMap;
//# sourceMappingURL=singletons.js.map