@@ -150,14 +150,30 @@ interface SlotsToTypes {
150
150
151
151
type SlotKey = keyof SlotsToTypes ;
152
152
153
- const slots = new WeakMap ( ) ;
154
- export function CreateSlots ( container : AnyTemporalType ) : void {
155
- slots . set ( container , Object . create ( null ) ) ;
153
+ const globalSlots = new WeakMap < Slots [ keyof Slots ] [ 'usedBy' ] , Record < keyof Slots , Slots [ keyof Slots ] [ 'value' ] > > ( ) ;
154
+
155
+ function _GetSlots ( container : Slots [ keyof Slots ] [ 'usedBy' ] ) {
156
+ return globalSlots . get ( container ) ;
156
157
}
157
158
158
- function GetSlots < T extends AnyTemporalType > ( container : T ) {
159
- return slots . get ( container ) ;
159
+ const GetSlotsSymbol = Symbol . for ( '@@Temporal__GetSlots' ) ;
160
+
161
+ // expose GetSlots to avoid dual package hazards
162
+ ( globalThis as any ) [ GetSlotsSymbol ] ||= _GetSlots ;
163
+
164
+ const GetSlots = ( globalThis as any ) [ GetSlotsSymbol ] as typeof _GetSlots ;
165
+
166
+ function _CreateSlots ( container : Slots [ keyof Slots ] [ 'usedBy' ] ) : void {
167
+ globalSlots . set ( container , Object . create ( null ) ) ;
160
168
}
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 ] as typeof _CreateSlots ;
176
+
161
177
// TODO: is there a better way than 9 overloads to make HasSlot into a type
162
178
// guard that takes a variable number of parameters?
163
179
export function HasSlot < ID1 extends SlotKey > ( container : unknown , id1 : ID1 ) : container is Slots [ ID1 ] [ 'usedBy' ] ;
@@ -278,7 +294,7 @@ export function GetSlot<KeyT extends keyof Slots>(
278
294
container : Slots [ typeof id ] [ 'usedBy' ] ,
279
295
id : KeyT
280
296
) : Slots [ KeyT ] [ 'value' ] {
281
- const value = GetSlots ( container ) [ id ] ;
297
+ const value = GetSlots ( container ) ?. [ id ] ;
282
298
if ( value === undefined ) throw new TypeError ( `Missing internal slot ${ id } ` ) ;
283
299
return value ;
284
300
}
@@ -287,5 +303,13 @@ export function SetSlot<KeyT extends SlotKey>(
287
303
id : KeyT ,
288
304
value : Slots [ KeyT ] [ 'value' ]
289
305
) : void {
290
- GetSlots ( container ) [ id ] = value ;
306
+ const slots = GetSlots ( container ) ;
307
+
308
+ if ( slots === undefined ) throw new TypeError ( 'Missing slots for the given container' ) ;
309
+
310
+ const existingSlot = slots [ id ] ;
311
+
312
+ if ( existingSlot ) throw new TypeError ( `${ id } already has set` ) ;
313
+
314
+ slots [ id ] = value ;
291
315
}
0 commit comments