@ -25,6 +25,9 @@ export class ResourceChangeset<T extends HalResource|{ [key:string]:unknown; } =
/** Reference and load promise for the current form */
protected form $ = input < FormResource > ( ) ;
/** Request cache for objects within the changeset for the current form */
protected cache : { [ key :string ] : Promise < unknown > } = { } ;
/** Flag whether this is currently being saved */
public inFlight = false ;
@ -94,6 +97,10 @@ export class ResourceChangeset<T extends HalResource|{ [key:string]:unknown; } =
. toPromise ( ) ;
}
/ * *
* Cache some promised value in the course of this changeset .
* Will get cleared automatically by the changeset on destroy / submission
* /
/ * *
* Posts to the form with the current changes
@ -106,6 +113,7 @@ export class ResourceChangeset<T extends HalResource|{ [key:string]:unknown; } =
. $links
. update ( payload )
. then ( ( form :FormResource ) = > {
this . cache = { } ;
this . form $ . putValue ( form ) ;
this . setNewDefaults ( form ) ;
this . push ( ) ;
@ -223,6 +231,7 @@ export class ResourceChangeset<T extends HalResource|{ [key:string]:unknown; } =
public clear() {
this . state && this . state . clear ( ) ;
this . changeset . clear ( ) ;
this . cache = { } ;
this . form $ . clear ( ) ;
}
@ -252,6 +261,18 @@ export class ResourceChangeset<T extends HalResource|{ [key:string]:unknown; } =
return this . form $ . getValueOr ( this . pristineResource ) . schema ;
}
/ * *
* Access some promised value
* that should be cached for the lifetime duration of the form .
* /
public cacheValue < T > ( key :string , request : ( ) = > Promise < T > ) : Promise < T > {
if ( this . cache [ key ] ) {
return this . cache [ key ] as Promise < T > ;
}
return this . cache [ key ] = request ( ) ;
}
protected get minimalPayload() {
return { lockVersion : this.pristineResource.lockVersion , _links : { } } ;
}