Skip to content

Commit de7ac12

Browse files
committed
DependenciesMap iterator
1 parent b6ecc26 commit de7ac12

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

packages/stack-shared/src/utils/maps.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ type DependenciesMapInner<V> = (
144144
)
145145
);
146146

147+
/**
148+
* A map that stores values indexed by an array of keys. If the keys are objects and the environment supports WeakRefs,
149+
* they are stored in a WeakMap.
150+
*/
147151
export class DependenciesMap<K extends any[], V> {
148152
private _inner: DependenciesMapInner<V> = { map: new MaybeWeakMap(), hasValue: false, value: undefined };
149153

@@ -190,6 +194,15 @@ export class DependenciesMap<K extends any[], V> {
190194
}
191195
}
192196

197+
private *_iterateInner(dependencies: any[], inner: DependenciesMapInner<V>): IterableIterator<[K, V]> {
198+
if (inner.hasValue) {
199+
yield [dependencies as K, inner.value];
200+
}
201+
for (const [key, value] of inner.map) {
202+
yield* this._iterateInner([...dependencies, key], value);
203+
}
204+
}
205+
193206
get(dependencies: K): V | undefined {
194207
return Result.or(this._unwrapFromInner(dependencies, this._inner), undefined);
195208
}
@@ -211,5 +224,9 @@ export class DependenciesMap<K extends any[], V> {
211224
this._inner = { map: new MaybeWeakMap(), hasValue: false, value: undefined };
212225
}
213226

227+
*[Symbol.iterator](): IterableIterator<[K, V]> {
228+
yield* this._iterateInner([], this._inner);
229+
}
230+
214231
[Symbol.toStringTag] = "DependenciesMap";
215232
}

0 commit comments

Comments
 (0)