Skip to content
This repository was archived by the owner on Aug 29, 2021. It is now read-only.

Commit 84ca1fb

Browse files
committed
fix: stop exposing the deps
1 parent 22553ed commit 84ca1fb

File tree

1 file changed

+40
-21
lines changed

1 file changed

+40
-21
lines changed

id/src/resolved-map.ts

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,32 @@ function isResolved(value: any): value is IsResolved<any> {
2121
return value.hasOwnProperty("resolved");
2222
}
2323

24-
export type Resolving<T> = HasDeps & HasValue<T>;
25-
export type Resolved<T, R> = IsResolved<R> & HasValue<T> & HasDeps;
26-
export type MaybeResolving<T, R> = Resolving<T> | Resolved<T, R>;
27-
type ResolvingOrDeps<T, R> = MaybeResolving<T, R> | HasDeps;
24+
type InternalResolving<T> = HasDeps & HasValue<T>;
25+
type InternalResolved<T, R> = IsResolved<R> & HasValue<T> & HasDeps;
26+
type InternalMaybeResolving<T, R> =
27+
| InternalResolving<T>
28+
| InternalResolved<T, R>;
29+
30+
export type Resolved<T, R> = IsResolved<R> & HasValue<T>;
31+
export type MaybeResolving<T, R> = HasValue<T> | Resolved<T, R>;
32+
type ResolvingOrDeps<T, R> = InternalMaybeResolving<T, R> | HasDeps;
2833

2934
type Unresolved<T> = HasValue<T>;
3035
type Requested = HasDeps;
31-
type Stored<T, R> = Resolving<T> | Resolved<T, R> | Unresolved<T> | Requested;
36+
type Stored<T, R> =
37+
| InternalResolving<T>
38+
| InternalResolved<T, R>
39+
| Unresolved<T>
40+
| Requested;
3241

33-
type Resolver<T, R> = (map: ResolvedIDMap<T, R>, id: ID, value: T) => R;
42+
type Resolver<T, R> = (value: T, id: ID, map: ResolvedIDMap<T, R>) => R;
3443

3544
export class ResolvedIDMap<T, R> {
45+
private static removeDeps(value: any): any {
46+
const copy = { ...value };
47+
delete copy.deps;
48+
return copy;
49+
}
3650
private inner: IDMap<Stored<T, R>> = new IDMap();
3751
private resolver: Resolver<T, R>;
3852
private resolving?: ID;
@@ -42,6 +56,7 @@ export class ResolvedIDMap<T, R> {
4256
}
4357

4458
public set(id: ID, raw: T) {
59+
this.reset(id);
4560
this.inner.set(id, { raw });
4661
}
4762

@@ -58,7 +73,7 @@ export class ResolvedIDMap<T, R> {
5873
public getCycle(id: ID): MaybeResolving<T, R> | undefined {
5974
const value = this.resolve(id);
6075
if (value && hasValue(value)) {
61-
return value;
76+
return ResolvedIDMap.removeDeps(value);
6277
}
6378
return undefined;
6479
}
@@ -75,7 +90,7 @@ export class ResolvedIDMap<T, R> {
7590
const result = this.getCycle(id);
7691
if (result) {
7792
if (isResolved(result)) {
78-
return result;
93+
return ResolvedIDMap.removeDeps(result);
7994
}
8095
throw new Error(
8196
`Value for ${id} requested during its own resolution. To avoid this error being thrown, use \`getCycle\` instead`
@@ -92,13 +107,14 @@ export class ResolvedIDMap<T, R> {
92107
private reset(id: ID): void {
93108
const value = this.inner.get(id);
94109
if (value && hasDeps(value)) {
95-
for (const [dep] of value.deps) {
96-
this.reset(dep);
97-
}
110+
const { deps } = value;
98111
delete value.deps;
99112
if (isResolved(value)) {
100113
delete value.resolved;
101114
}
115+
for (const [dep] of deps) {
116+
this.reset(dep);
117+
}
102118
}
103119
}
104120

@@ -115,24 +131,27 @@ export class ResolvedIDMap<T, R> {
115131
}
116132
return undefined;
117133
}
118-
if (hasDeps(value)) {
119-
if (previousResolving) {
120-
value.deps.add(previousResolving);
121-
}
122-
}
123-
if (!hasValue(value)) {
124-
return value;
134+
if (hasDeps(value) && previousResolving) {
135+
value.deps.add(previousResolving);
125136
}
126-
if (isResolved(value)) {
137+
if (
138+
// No value, only requested dependencies
139+
!hasValue(value) ||
140+
// Resolved
141+
isResolved(value) ||
142+
// Resolving
143+
hasDeps(value)
144+
) {
127145
return value;
128146
}
129-
const asserted = value as Resolved<T, R>;
147+
// Unresolved and no deps.
148+
const asserted = value as InternalResolved<T, R>;
130149
this.resolving = id;
131150
asserted.deps = new IDSet();
132151
if (previousResolving) {
133152
asserted.deps.add(previousResolving);
134153
}
135-
asserted.resolved = this.resolver(this, id, value.raw);
154+
asserted.resolved = this.resolver(value.raw, id, this);
136155
this.resolving = previousResolving;
137156
return asserted;
138157
}

0 commit comments

Comments
 (0)