/*1* cache.ts2*3* provides a simple cache for expensive operations4*5* Copyright (C) 2023 Posit Software, PBC6*/78export function cache<T>(f: () => Promise<T>): () => Promise<T> {9let value: T | undefined = undefined;10return async () => {11if (value === undefined) {12value = await f();13}14return value;15};16}1718export function cacheMap<K, V>(19f: (key: K) => Promise<V>,20): (key: K) => Promise<V> {21const map = new Map<K, V>();22return async (key: K) => {23if (!map.has(key)) {24map.set(key, await f(key));25}26return map.get(key)!;27};28}293031