1+ /**
2+ * Internal node mostly for {@link WeakIdentityCache}, although has nothing to do with {@link WeakRef}.
3+ */
14class TupleNode {
25 private hasValue = false ;
36 private value ?: any | undefined ;
@@ -115,6 +118,29 @@ export class WeakIdentityCache<K extends [...any], V extends WeakKey> {
115118 } ) ;
116119 }
117120
121+ /**
122+ * Provides a {@link V} for the given {@link K}.
123+ * This allows you to skip the builder, however, this {@link K} will still be passed to the cleanup function when it is GC'ed.
124+ *
125+ * If this {@link K} already has a value, rejects the update and returns `false`.
126+ * Otherwise, returns `true`.
127+ */
128+ provide ( v : V , ...k : K ) : boolean {
129+ const wr = this . #root. get ( k ) ;
130+ let curr = wr ?. deref ( ) ;
131+ if ( curr !== undefined ) {
132+ return false ;
133+ }
134+
135+ curr = v ;
136+ const update = new WeakRef ( curr ) ;
137+ this . #root. set ( k , update ) ;
138+ this . #reg. register ( curr , k , curr ) ;
139+ this . #count++ ;
140+
141+ return true ;
142+ }
143+
118144 /**
119145 * Gets this {@link K} from the cache.
120146 *
@@ -123,15 +149,16 @@ export class WeakIdentityCache<K extends [...any], V extends WeakKey> {
123149 get ( ...k : K ) : V {
124150 const wr = this . #root. get ( k ) ;
125151 let curr = wr ?. deref ( ) ;
126-
127- if ( curr === undefined ) {
128- curr = this . #build( ...k ) ;
129- const update = new WeakRef ( curr ) ;
130- this . #root. set ( k , update ) ;
131- this . #reg. register ( curr , k , curr ) ;
132- this . #count++ ;
152+ if ( curr !== undefined ) {
153+ return curr ;
133154 }
134155
156+ curr = this . #build( ...k ) ;
157+ const update = new WeakRef ( curr ) ;
158+ this . #root. set ( k , update ) ;
159+ this . #reg. register ( curr , k , curr ) ;
160+ this . #count++ ;
161+
135162 return curr ;
136163 }
137164
@@ -156,8 +183,9 @@ export class WeakIdentityCache<K extends [...any], V extends WeakKey> {
156183
157184 /**
158185 * Deletes this {@link K} from the cache.
186+ * Returns `true` if it existed and was deleted.
159187 *
160- * This will prevent it from being passed to the cleanup function.
188+ * This will prevent the given { @link K} from being passed to the cleanup function.
161189 */
162190 delete ( ...k : K ) : boolean {
163191 const wr = this . #root. get ( k ) ;
@@ -176,7 +204,20 @@ export class WeakIdentityCache<K extends [...any], V extends WeakKey> {
176204 return false ;
177205 }
178206
207+ /**
208+ * Gets the current size of actual values.
209+ *
210+ * This is useful for testing but might change as things are GC'ed.
211+ */
179212 get size ( ) {
180213 return this . #count;
181214 }
182215}
216+
217+ /**
218+ * Builds a helper which provides shared named symbols.
219+ */
220+ export function buildScopedSymbolCache ( id : string ) : ( s : string ) => Symbol {
221+ const cache = new WeakIdentityCache < [ string ] , Symbol > ( ( s ) => Symbol ( `${ id } :${ s } ` ) ) ;
222+ return cache . get . bind ( cache ) ;
223+ }
0 commit comments