aboutsummaryrefslogtreecommitdiff
path: root/packages/integrations/vue/src/context.ts
blob: 833755044cb0aec3a4ff5680a761aa09c0d91d9e (plain) (blame)
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
import type { SSRResult } from 'astro';

const contexts = new WeakMap<SSRResult, { currentIndex: number; readonly id: string }>();

const ID_PREFIX = 's';

function getContext(rendererContextResult: SSRResult) {
	if (contexts.has(rendererContextResult)) {
		return contexts.get(rendererContextResult);
	}
	const ctx = {
		currentIndex: 0,
		get id() {
			return ID_PREFIX + this.currentIndex.toString();
		},
	};
	contexts.set(rendererContextResult, ctx);
	return ctx;
}

export function incrementId(rendererContextResult: SSRResult) {
	const ctx = getContext(rendererContextResult)!;
	const id = ctx.id;
	ctx.currentIndex++;
	return id;
}