astro/packages/integrations/solid/src/context.ts

29 lines
517 B
TypeScript
Raw Normal View History

import type { RendererContext } from './types';
type Context = {
id: string;
c: number;
2022-08-10 19:13:28 +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)) {
return contexts.get(result)!;
}
let ctx = {
c: 0,
get id() {
return 's' + this.c.toString();
2022-08-10 19:13:28 +00:00
},
};
contexts.set(result, ctx);
return ctx;
}
export function incrementId(ctx: Context): string {
let id = ctx.id;
ctx.c++;
return id;
}