Make InProgressMethods immutable so it's safer to use as react state

This commit is contained in:
Robert
2023-08-10 15:26:52 +01:00
parent a893bf7a77
commit 37c461a89b
2 changed files with 23 additions and 20 deletions

View File

@@ -1,16 +1,30 @@
/** /**
* A class that keeps track of which methods are in progress for each package. * A class that keeps track of which methods are in progress for each package.
*
* This class is immutable and therefore is safe to be used in a react useState hook.
*/ */
export class InProgressMethods { export class InProgressMethods {
// A map of in-progress method signatures for each package. // A map of in-progress method signatures for each package.
private readonly methodMap: Map<string, Set<string>>; private readonly methodMap: ReadonlyMap<string, Set<string>>;
constructor() { constructor(methodMap?: ReadonlyMap<string, Set<string>>) {
this.methodMap = new Map<string, Set<string>>(); this.methodMap = methodMap ?? new Map<string, Set<string>>();
} }
public setPackageMethods(packageName: string, methods: Set<string>): void { /**
this.methodMap.set(packageName, methods); * Sets the in-progress methods for the given package.
* Returns a new InProgressMethods instance.
*/
public setPackageMethods(
packageName: string,
methods: Set<string>,
): InProgressMethods {
const newMethodMap = new Map<string, Set<string>>();
this.methodMap.forEach((value, key) => {
newMethodMap.set(key, new Set<string>(value));
});
newMethodMap.set(packageName, methods);
return new InProgressMethods(newMethodMap);
} }
public hasMethod(packageName: string, method: string): boolean { public hasMethod(packageName: string, method: string): boolean {
@@ -20,12 +34,4 @@ export class InProgressMethods {
} }
return false; return false;
} }
public static fromExisting(methods: InProgressMethods): InProgressMethods {
const newInProgressMethods = new InProgressMethods();
methods.methodMap.forEach((value, key) => {
newInProgressMethods.methodMap.set(key, new Set<string>(value));
});
return newInProgressMethods;
}
} }

View File

@@ -141,15 +141,12 @@ export function DataExtensionsEditor({
); );
break; break;
case "setInProgressMethods": case "setInProgressMethods":
setInProgressMethods((oldInProgressMethods) => { setInProgressMethods((oldInProgressMethods) =>
const methods = oldInProgressMethods.setPackageMethods(
InProgressMethods.fromExisting(oldInProgressMethods);
methods.setPackageMethods(
msg.packageName, msg.packageName,
new Set(msg.inProgressMethods), new Set(msg.inProgressMethods),
); ),
return methods; );
});
break; break;
default: default:
assertNever(msg); assertNever(msg);