Skip to content

Commit 37c461a

Browse files
Make InProgressMethods immutable so it's safer to use as react state
1 parent a893bf7 commit 37c461a

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed
Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
11
/**
22
* A class that keeps track of which methods are in progress for each package.
3+
*
4+
* This class is immutable and therefore is safe to be used in a react useState hook.
35
*/
46
export class InProgressMethods {
57
// A map of in-progress method signatures for each package.
6-
private readonly methodMap: Map<string, Set<string>>;
8+
private readonly methodMap: ReadonlyMap<string, Set<string>>;
79

8-
constructor() {
9-
this.methodMap = new Map<string, Set<string>>();
10+
constructor(methodMap?: ReadonlyMap<string, Set<string>>) {
11+
this.methodMap = methodMap ?? new Map<string, Set<string>>();
1012
}
1113

12-
public setPackageMethods(packageName: string, methods: Set<string>): void {
13-
this.methodMap.set(packageName, methods);
14+
/**
15+
* Sets the in-progress methods for the given package.
16+
* Returns a new InProgressMethods instance.
17+
*/
18+
public setPackageMethods(
19+
packageName: string,
20+
methods: Set<string>,
21+
): InProgressMethods {
22+
const newMethodMap = new Map<string, Set<string>>();
23+
this.methodMap.forEach((value, key) => {
24+
newMethodMap.set(key, new Set<string>(value));
25+
});
26+
newMethodMap.set(packageName, methods);
27+
return new InProgressMethods(newMethodMap);
1428
}
1529

1630
public hasMethod(packageName: string, method: string): boolean {
@@ -20,12 +34,4 @@ export class InProgressMethods {
2034
}
2135
return false;
2236
}
23-
24-
public static fromExisting(methods: InProgressMethods): InProgressMethods {
25-
const newInProgressMethods = new InProgressMethods();
26-
methods.methodMap.forEach((value, key) => {
27-
newInProgressMethods.methodMap.set(key, new Set<string>(value));
28-
});
29-
return newInProgressMethods;
30-
}
3137
}

extensions/ql-vscode/src/view/data-extensions-editor/DataExtensionsEditor.tsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,12 @@ export function DataExtensionsEditor({
141141
);
142142
break;
143143
case "setInProgressMethods":
144-
setInProgressMethods((oldInProgressMethods) => {
145-
const methods =
146-
InProgressMethods.fromExisting(oldInProgressMethods);
147-
methods.setPackageMethods(
144+
setInProgressMethods((oldInProgressMethods) =>
145+
oldInProgressMethods.setPackageMethods(
148146
msg.packageName,
149147
new Set(msg.inProgressMethods),
150-
);
151-
return methods;
152-
});
148+
),
149+
);
153150
break;
154151
default:
155152
assertNever(msg);

0 commit comments

Comments
 (0)