-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
context.ts
32 lines (28 loc) · 707 Bytes
/
context.ts
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
import type { PropNameToSignalMap, RendererContext, SignalLike } from './types';
export type Context = {
id: string;
c: number;
signals: Map<SignalLike, string>;
propsToSignals: Map<Record<string, any>, PropNameToSignalMap>;
};
const contexts = new WeakMap<RendererContext['result'], Context>();
export function getContext(result: RendererContext['result']): Context {
if (contexts.has(result)) {
return contexts.get(result)!;
}
let ctx = {
c: 0,
get id() {
return 'p' + this.c.toString();
},
signals: new Map(),
propsToSignals: new Map(),
};
contexts.set(result, ctx);
return ctx;
}
export function incrementId(ctx: Context): string {
let id = ctx.id;
ctx.c++;
return id;
}