spacehub

surshar / claude-code

Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.

Built by Anthropic · github.com/anthropics

claude-code / mods / sec-default / hooks / policy / create-policy-memo / create-policy-memo.ts
28 lines · 781 BRaw
1import type { Settings } from 'claude-code'
2
3/**
4 * A reader of managed policy that serves one read to every caller inside a
5 * window after it, rejections included (a failed read still fails closed).
6 *
7 * @param ttlMs how long a read is served after it starts
8 * @param now the clock, for a test
9 * @returns `(read) => policy`: `read` runs when nothing fresh is held
10 */
11export function createPolicyMemo(
12 ttlMs: number,
13 now: () => number = Date.now,
14): (read: () => Promise<Settings>) => Promise<Settings> {
15 let heldAt = Number.NEGATIVE_INFINITY
16 let held: Promise<Settings> | undefined
17
18 return read => {
19 const isStale = held === undefined || now() - heldAt > ttlMs
20
21 if (isStale) {
22 heldAt = now()
23 held = read()
24 }
25
26 return held ?? read()
27 }
28}