Skip to content

Commit 24a1628

Browse files
committed
Expose GetSlots and CreateSlots instead of the slots itself
to avoid dual package hazards
1 parent 6e3f4d6 commit 24a1628

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

lib/slots.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,19 +150,30 @@ interface SlotsToTypes {
150150

151151
type SlotKey = keyof SlotsToTypes;
152152

153-
const globalTemporalSlotMapKey = Symbol.for('@@Temporal__slots__private_do_not_use');
153+
const globalSlots = new WeakMap();
154154

155-
(globalThis as any)[globalTemporalSlotMapKey] ||= new WeakMap();
155+
function _GetSlots(container: Slots[SlotKey]['usedBy']) {
156+
return globalSlots.get(container);
157+
}
156158

157-
const slots = (globalThis as any)[globalTemporalSlotMapKey];
159+
const GetSlotsSymbol = Symbol.for('@@Temporal__GetSlots');
158160

159-
export function CreateSlots(container: AnyTemporalType): void {
160-
slots.set(container, Object.create(null));
161-
}
161+
// expose GetSlots to avoid dual package hazards
162+
(globalThis as any)[GetSlotsSymbol] ||= _GetSlots;
163+
164+
const GetSlots = (globalThis as any)[GetSlotsSymbol];
162165

163-
function GetSlots<T extends AnyTemporalType>(container: T) {
164-
return slots.get(container);
166+
function _CreateSlots(container: Slots[SlotKey]['usedBy']): void {
167+
globalSlots.set(container, Object.create(null));
165168
}
169+
170+
const CreateSlotsSymbol = Symbol.for('@@Temporal__CreateSlots');
171+
172+
// expose CreateSlots to avoid dual package hazards
173+
(globalThis as any)[CreateSlotsSymbol] ||= _CreateSlots;
174+
175+
export const CreateSlots = (globalThis as any)[CreateSlotsSymbol];
176+
166177
// TODO: is there a better way than 9 overloads to make HasSlot into a type
167178
// guard that takes a variable number of parameters?
168179
export function HasSlot<ID1 extends SlotKey>(container: unknown, id1: ID1): container is Slots[ID1]['usedBy'];
@@ -292,5 +303,11 @@ export function SetSlot<KeyT extends SlotKey>(
292303
id: KeyT,
293304
value: Slots[KeyT]['value']
294305
): void {
295-
GetSlots(container)[id] = value;
306+
const slot = GetSlots(container);
307+
308+
if (id in slot) {
309+
throw new TypeError(`${id} already has set`);
310+
}
311+
312+
slot[id] = value;
296313
}

0 commit comments

Comments
 (0)