mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-07-24 01:18:10 -07:00
next: ai part 3
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
const PREFIX = /** @type {const} */ ({
|
||||
category: "c",
|
||||
fact: "f",
|
||||
guide: "g",
|
||||
metric: "m",
|
||||
source: "s",
|
||||
});
|
||||
|
||||
export class AskRefs {
|
||||
/** @type {Map<string, number>} */
|
||||
#counts = new Map();
|
||||
|
||||
/** @type {Map<string, { kind: keyof typeof PREFIX, value: any }>} */
|
||||
#items = new Map();
|
||||
|
||||
/** @type {Map<string, string>} */
|
||||
#keys = new Map();
|
||||
|
||||
/**
|
||||
* @param {keyof typeof PREFIX} kind
|
||||
* @param {any} value
|
||||
* @param {string} stableKey
|
||||
*/
|
||||
issue(kind, value, stableKey) {
|
||||
const key = `${kind}:${stableKey}`;
|
||||
const existing = this.#keys.get(key);
|
||||
if (existing) return existing;
|
||||
|
||||
const count = (this.#counts.get(kind) ?? 0) + 1;
|
||||
const ref = `${PREFIX[kind]}${count}`;
|
||||
this.#counts.set(kind, count);
|
||||
this.#items.set(ref, { kind, value });
|
||||
this.#keys.set(key, ref);
|
||||
return ref;
|
||||
}
|
||||
|
||||
/** @param {string} ref @param {keyof typeof PREFIX} [expectedKind] */
|
||||
get(ref, expectedKind) {
|
||||
const item = this.#items.get(ref);
|
||||
if (!item) throw new Error(`Unknown reference ${ref}`);
|
||||
if (expectedKind && item.kind !== expectedKind) {
|
||||
throw new Error(`${ref} is not a ${expectedKind} reference`);
|
||||
}
|
||||
return item.value;
|
||||
}
|
||||
|
||||
/** @param {string} ref */
|
||||
kind(ref) {
|
||||
const item = this.#items.get(ref);
|
||||
if (!item) throw new Error(`Unknown reference ${ref}`);
|
||||
return item.kind;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user