Skip to content

Commit

Permalink
feat: Add saveImmediate method to CollectionReference and DocumentRef…
Browse files Browse the repository at this point in the history
…erence

The code changes add a new method called `saveImmediate` to both the `CollectionReference` and `DocumentReference` classes. This method allows the Alwatr Store to save the collection or document immediately, bypassing any debounce delay. The purpose of this change is to provide a way to save the data without any delay when immediate saving is required.
  • Loading branch information
alimd committed Jul 3, 2024
1 parent 6a5bc90 commit aa5ab87
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 12 deletions.
33 changes: 27 additions & 6 deletions packages/reference/src/collection-reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,16 +419,27 @@ export class CollectionReference<TItem extends JsonifiableObject = JsonifiableOb
* Requests the Alwatr Store to save the collection.
* Saving may take some time in Alwatr Store due to the use of throttling.
*
* @param withDebounce Indicates whether to use the Alwatr Store's debounce delay.
* @example
* ```typescript
* collectionRef.save();
* ```
*/
save(): void {
this.logger__.logMethod?.('save');
this.updated__(null, false);
}

/**
* Requests the Alwatr Store to save the collection immediately.
*
* @example
* ```typescript
* collectionRef.save('item1');
* collectionRef.saveImmediate();
* ```
*/
save(withDebounce = false): void {
this.logger__.logMethodArgs?.('save', withDebounce);
this.updated__(null, !withDebounce);
saveImmediate(): void {
this.logger__.logMethod?.('saveImmediate');
this.updated__(null, true);
}

/**
Expand Down Expand Up @@ -500,6 +511,16 @@ export class CollectionReference<TItem extends JsonifiableObject = JsonifiableOb
}
}

/**
* Retrieves the full context of the collection.
*
* @returns The full context of the collection.
*
* @example
* ```typescript
* const context = collectionRef.getFullContext_();
* ```
*/
getFullContext_(): Readonly<CollectionContext<TItem>> {
this.logger__.logMethod?.('getFullContext_');
return this.context__;
Expand All @@ -514,7 +535,7 @@ export class CollectionReference<TItem extends JsonifiableObject = JsonifiableOb
* @param id - The ID of the item to update.
*/
private async updated__(id: string | number | null, immediate = false): Promise<void> {
this.logger__.logMethodArgs?.('updated__', {delayed: this.updateDelayed_});
this.logger__.logMethodArgs?.('updated__', {id, immediate, delayed: this.updateDelayed_});

this.hasUnprocessedChanges_ = true;
if (id !== null) this.updateMeta_(id); // meta must updated per item
Expand Down
33 changes: 27 additions & 6 deletions packages/reference/src/document-reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,18 +268,39 @@ export class DocumentReference<TDoc extends JsonifiableObject = JsonifiableObjec
* Requests the Alwatr Store to save the document.
* Saving may take some time in Alwatr Store due to the use of throttling.
*
* @param withDebounce Indicates whether to use the Alwatr Store's debounce delay.
*
* @example
* ```typescript
* documentRef.save();
* ```
*/
save(withDebounce = false): void {
this.logger__.logMethodArgs?.('save', withDebounce);
this.updated__(!withDebounce);
save(): void {
this.logger__.logMethod?.('save');
this.updated__(false);
}

/**
* Requests the Alwatr Store to save the document immediately.
*
* @example
* ```typescript
* documentRef.saveImmediate();
* ```
*/
saveImmediate(): void {
this.logger__.logMethod?.('saveImmediate');
this.updated__(true);
}

/**
* Retrieves the full context of the document.
*
* @returns The full context of the document.
*
* @example
* ```typescript
* const context = documentRef.getFullContext_();
* ```
*/
getFullContext_(): Readonly<DocumentContext<TDoc>> {
this.logger__.logMethod?.('getFullContext_');
return this.context__;
Expand All @@ -292,7 +313,7 @@ export class DocumentReference<TDoc extends JsonifiableObject = JsonifiableObjec
* This method is throttled to prevent multiple updates in a short time.
*/
private async updated__(immediate = false): Promise<void> {
this.logger__.logMethodArgs?.('updated__', {delayed: this.updateDelayed_});
this.logger__.logMethodArgs?.('updated__', {immediate, delayed: this.updateDelayed_});

this.hasUnprocessedChanges_ = true;

Expand Down

0 comments on commit aa5ab87

Please sign in to comment.