-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore-key.ts
More file actions
70 lines (62 loc) · 2.08 KB
/
store-key.ts
File metadata and controls
70 lines (62 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import type { ScreenContext } from '../../context/types.js'
import type { OutputStore } from './types.js'
// ---------------------------------------------------------------------------
// Private
// ---------------------------------------------------------------------------
/**
* Symbol key used to attach the {@link OutputStore} to the screen context.
*
* @private
*/
const OUTPUT_STORE_KEY: unique symbol = Symbol('kidd.outputStore')
// ---------------------------------------------------------------------------
// Exports
// ---------------------------------------------------------------------------
/**
* Options for {@link injectOutputStore}.
*/
interface InjectOutputStoreOptions {
/**
* The screen context record to extend.
*/
readonly ctx: Record<string | symbol, unknown>
/**
* The output store to attach.
*/
readonly store: OutputStore
}
/**
* Return a new context with the {@link OutputStore} attached via a hidden
* Symbol key. Does not mutate the original context.
*
* @param options - The injection options.
* @returns A new record containing all original entries plus the store.
*/
export function injectOutputStore({
ctx,
store,
}: InjectOutputStoreOptions): Record<string | symbol, unknown> {
return { ...ctx, [OUTPUT_STORE_KEY]: store }
}
/**
* Retrieve the {@link OutputStore} from a screen context.
*
* Asserts that the store exists — this is a framework invariant
* guaranteed by `screen()` calling {@link injectOutputStore}.
*
* @param ctx - The screen context to read from.
* @returns The output store attached by {@link injectOutputStore}.
*/
export function getOutputStore(ctx: ScreenContext): OutputStore {
const store = (ctx as unknown as Record<symbol, unknown>)[OUTPUT_STORE_KEY] as
| OutputStore
| undefined
if (store === undefined) {
// Framework invariant — always a programmer error, not a recoverable failure.
// eslint-disable-next-line no-throw-literal
throw new Error(
'OutputStore not found on ScreenContext. Ensure useOutputStore is called within a screen() context.'
)
}
return store
}