MCPcopy Create free account
hub / github.com/anomalyco/opencode / memo

Function memo

packages/console/core/src/util/memo.ts:1–18  ·  view source on GitHub ↗
(fn: () => T, cleanup?: (input: T) => Promise<void>)

Source from the content-addressed store, hash-verified

1export function memo<T>(fn: () => T, cleanup?: (input: T) => Promise<void>) {
2 let value: T | undefined
3 let loaded = false
4
5 const result = (): T => {
6 if (loaded) return value as T
7 loaded = true
8 value = fn()
9 return value as T
10 }
11 result.reset = async () => {
12 if (cleanup && value) await cleanup(value)
13 loaded = false
14 value = undefined
15 }
16
17 return result
18}

Callers 2

index.tsFile · 0.90
createTimelineProjectionFunction · 0.85

Calls 1

cleanupFunction · 0.50

Tested by

no test coverage detected