addons/shared/lazyInit.tsblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318// See https://advancedweb.hu/the-async-lazy-initializer-pattern-in-javascript/
b69ab319
b69ab3110/**
b69ab3111 * Because Promises are eager in JavaScript, we need to introduce an extra layer
b69ab3112 * to lazily invoke an async operation. lazyInit() takes a function that
b69ab3113 * represents the async operation, but does not call it until the function
b69ab3114 * returned by lazyInit() itself is called. Note that lazyInit() is idempotent:
b69ab3115 * once it is called, it will always return the original Promise created by
b69ab3116 * calling the async operation.
b69ab3117 *
b69ab3118 * ```
b69ab3119 * // Note getObj is a *function*, not a *Promise*.
b69ab3120 * const getObj = lazyInit(async () => {
b69ab3121 * const value = await expensiveOperation();
b69ab3122 * return value + 1;
b69ab3123 * });
b69ab3124 *
b69ab3125 * ...
b69ab3126 *
b69ab3127 * // expensiveObjCreation() will not be called until getObj() is called, and if
b69ab3128 * // it is called, it will only be called once.
b69ab3129 * const objRef1 = await getObj();
b69ab3130 * const objRef2 = await getObj();
b69ab3131 * ```
b69ab3132 */
b69ab3133export default function lazyInit<T>(init: () => Promise<T>): () => Promise<T> {
b69ab3134 let promise: Promise<T> | null = null;
b69ab3135 return () => (promise = promise ?? init());
b69ab3136}