* Accept a function that we will execute later and return a deferred placeholder. * * @template TOriginal * @template TResult * @param {(this: Config, config: Config, original: TOriginal) => TResult | Promise } func * @returns {DeferredConfig}
(func)
| 34 | * @returns {DeferredConfig} |
| 35 | */ |
| 36 | function deferConfig(func) { |
| 37 | /** @type {DeferredConfig} */ |
| 38 | const obj = Object.create(DeferredConfig.prototype); |
| 39 | obj.prepare = function(config, prop, property) { |
| 40 | const original = prop[property]._original; |
| 41 | |
| 42 | if (isAsyncFunction(func)) { |
| 43 | obj.resolve = async function () { |
| 44 | const promise = func.call(config, config, original); |
| 45 | |
| 46 | Object.defineProperty(prop, property, { value: promise }); |
| 47 | Object.defineProperty(prop, property, { value: await promise }); |
| 48 | |
| 49 | return promise; |
| 50 | } |
| 51 | } else { |
| 52 | obj.resolve = function() { |
| 53 | const value = func.call(config, config, original); |
| 54 | |
| 55 | Object.defineProperty(prop, property, {value: value}); |
| 56 | |
| 57 | return value; |
| 58 | }; |
| 59 | } |
| 60 | |
| 61 | Object.defineProperty(prop, property, { get: function() { return obj.resolve(); } }); |
| 62 | |
| 63 | return obj; |
| 64 | }; |
| 65 | |
| 66 | return obj; |
| 67 | } |
| 68 | |
| 69 | export { deferConfig, DeferredConfig }; |