2022-08-10 19:10:31 +00:00
|
|
|
import type { RendererContext } from './types';
|
|
|
|
|
|
|
|
type Context = {
|
|
|
|
id: string;
|
|
|
|
c: number;
|
2022-08-10 19:13:28 +00:00
|
|
|
};
|
2022-08-10 19:10:31 +00:00
|
|
|
|
|
|
|
const contexts = new WeakMap<RendererContext['result'], Context>();
|
|
|
|
|
|
|
|
export function getContext(result: RendererContext['result']): Context {
|
2022-08-10 19:13:28 +00:00
|
|
|
if (contexts.has(result)) {
|
2022-08-10 19:10:31 +00:00
|
|
|
return contexts.get(result)!;
|
|
|
|
}
|
|
|
|
let ctx = {
|
|
|
|
c: 0,
|
|
|
|
get id() {
|
|
|
|
return 's' + this.c.toString();
|
2022-08-10 19:13:28 +00:00
|
|
|
},
|
2022-08-10 19:10:31 +00:00
|
|
|
};
|
|
|
|
contexts.set(result, ctx);
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function incrementId(ctx: Context): string {
|
|
|
|
let id = ctx.id;
|
|
|
|
ctx.c++;
|
|
|
|
return id;
|
|
|
|
}
|