@@ -36,6 +36,8 @@ export const unknownSubject = 'unknown-subject';
3636 */
3737// eslint-disable-next-line @typescript-eslint/no-explicit-any
3838export class Resource < C extends OptionalClass = any > {
39+ // WARNING: WHEN ADDING A PROPERTY, ALSO ADD IT TO THE CLONE METHOD
40+
3941 /** If the resource could not be fetched, we put that info here. */
4042 public error ?: Error ;
4143 /** If the commit could not be saved, we put that info here. */
@@ -57,8 +59,6 @@ export class Resource<C extends OptionalClass = any> {
5759 private subject : string ;
5860 private propvals : PropVals ;
5961
60- private queuedFetch : Promise < unknown > | undefined ;
61-
6262 public constructor ( subject : string , newResource ?: boolean ) {
6363 if ( typeof subject !== 'string' ) {
6464 throw new Error (
@@ -95,19 +95,39 @@ export class Resource<C extends OptionalClass = any> {
9595 return props ;
9696 }
9797
98- /** Checks if the content of two Resource instances is equal
99- * Warning: does not check CommitBuilder, loading state
100- */
101- public static compare ( resourceA : Resource , resourceB : Resource ) : boolean {
102- if ( resourceA . error !== resourceB . error ) {
98+ /** Checks if the content of two Resource instances is equal */
99+ public equals ( resourceB : Resource ) : boolean {
100+ if ( this . getSubject ( ) !== resourceB . getSubject ( ) ) {
103101 return false ;
104102 }
105103
106- return (
107- resourceA . getSubject ( ) === resourceB . getSubject ( ) &&
108- JSON . stringify ( Array . from ( resourceA . propvals . entries ( ) ) ) ===
109- JSON . stringify ( Array . from ( resourceB . propvals . entries ( ) ) )
110- ) ;
104+ if ( this . new !== resourceB . new ) {
105+ return false ;
106+ }
107+
108+ if ( this . error !== resourceB . error ) {
109+ return false ;
110+ }
111+
112+ if ( this . loading !== resourceB . loading ) {
113+ return false ;
114+ }
115+
116+ if (
117+ JSON . stringify ( Array . from ( this . propvals . entries ( ) ) ) ===
118+ JSON . stringify ( Array . from ( resourceB . propvals . entries ( ) ) )
119+ ) {
120+ return false ;
121+ }
122+
123+ if (
124+ JSON . stringify ( Array . from ( this . commitBuilder . set . entries ( ) ) ) ===
125+ JSON . stringify ( Array . from ( resourceB . commitBuilder . set . entries ( ) ) )
126+ ) {
127+ return false ;
128+ }
129+
130+ return true ;
111131 }
112132
113133 /** Checks if the agent has write rights by traversing the graph. Recursive function. */
@@ -163,12 +183,10 @@ export class Resource<C extends OptionalClass = any> {
163183 res . propvals = structuredClone ( this . propvals ) ;
164184 res . loading = this . loading ;
165185 res . new = this . new ;
166- // structured clone
167186 res . error = structuredClone ( this . error ) ;
168187 res . commitError = this . commitError ;
169188 res . commitBuilder = this . commitBuilder . clone ( ) ;
170189 res . appliedCommitSignatures = this . appliedCommitSignatures ;
171- res . queuedFetch = this . queuedFetch ;
172190
173191 return res as Resource < C > ;
174192 }
@@ -477,29 +495,16 @@ export class Resource<C extends OptionalClass = any> {
477495 const endpoint = new URL ( this . getSubject ( ) ) . origin + `/commit` ;
478496
479497 try {
480- // We optimistically update all viewed instances for snappy feedback
481- store . addResources ( this ) ;
482- store . notify ( this ) ;
483-
484- // If a commit is already being posted we wait for it to finish
485- // because the server can not guarantee the commits will be processed in the correct order.
486-
487- if ( this . queuedFetch ) {
488- try {
489- await this . queuedFetch ;
490- } catch ( e ) {
491- // Continue
492- }
493- }
494-
495498 this . commitError = undefined ;
496- const createdCommitPromise = store . postCommit ( commit , endpoint ) ;
497- this . queuedFetch = createdCommitPromise ;
498- store . notify ( this ) ;
499- const createdCommit = await createdCommitPromise ;
500-
499+ store . addResources ( this ) ;
500+ const createdCommit = await store . postCommit ( commit , endpoint ) ;
501+ // const res = store.getResourceLoading(this.subject);
501502 this . setUnsafe ( properties . commit . lastCommit , createdCommit . id ! ) ;
502503
504+ // Let all subscribers know that the commit has been applied
505+ // store.addResources(this);
506+ store . notifyResourceSaved ( this ) ;
507+
503508 if ( wasNew ) {
504509 // The first `SUBSCRIBE` message will not have worked, because the resource didn't exist yet.
505510 // That's why we need to repeat the process
@@ -510,9 +515,6 @@ export class Resource<C extends OptionalClass = any> {
510515 await store . saveBatchForParent ( this . getSubject ( ) ) ;
511516 }
512517
513- // Let all subscribers know that the commit has been applied
514- store . notifyResourceSaved ( this ) ;
515-
516518 return createdCommit . id as string ;
517519 } catch ( e ) {
518520 // Logic for handling error if the previousCommit is wrong.
@@ -540,7 +542,6 @@ export class Resource<C extends OptionalClass = any> {
540542 this . commitBuilder = oldCommitBuilder ;
541543 this . commitError = e ;
542544 store . addResources ( this ) ;
543- store . notify ( this . clone ( ) ) ;
544545 throw e ;
545546 }
546547 }
@@ -582,15 +583,13 @@ export class Resource<C extends OptionalClass = any> {
582583
583584 if ( value === undefined ) {
584585 this . removePropVal ( prop ) ;
585- store . notify ( this . clone ( ) ) ;
586586
587587 return ;
588588 }
589589
590590 this . propvals . set ( prop , value ) ;
591591 // Add the change to the Commit Builder, so we can commit our changes later
592592 this . commitBuilder . addSetAction ( prop , value ) ;
593- store . notify ( this . clone ( ) ) ;
594593 }
595594
596595 /**
0 commit comments