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 */
46export 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 > > ( this . methodMap ) ;
23+ newMethodMap . set ( packageName , methods ) ;
24+ return new InProgressMethods ( newMethodMap ) ;
1425 }
1526
1627 public hasMethod ( packageName : string , method : string ) : boolean {
@@ -20,12 +31,4 @@ export class InProgressMethods {
2031 }
2132 return false ;
2233 }
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- }
3134}
0 commit comments