UNPKG

@botonic/plugin-contentful

Version:

## What Does This Plugin Do?

56 lines 1.5 kB
"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