5e46be5468
* Support signals in Preact islands * Add a changeset * Only add signals if we need them * Refactor signal logic into its own module * Keep track of the signals used
32 lines
706 B
TypeScript
32 lines
706 B
TypeScript
import type { RendererContext, SignalLike, PropNameToSignalMap } 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;
|
|
}
|