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