Skip to content

Commit

Permalink
refactor: Update type imports in alwatr-store.ts
Browse files Browse the repository at this point in the history
Refactor the type imports in alwatr-store.ts to use the `JsonObject` type instead of `JsonifiableObject`. This aligns with the recent refactoring of the type definitions in the repository.
  • Loading branch information
alimd committed Sep 2, 2024
1 parent 68064c4 commit b6a4aba
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
24 changes: 12 additions & 12 deletions packages/reference/src/collection-reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {waitForImmediate, waitForTimeout} from '@alwatr/wait';
import {logger} from './logger.js';
import {getStoreId, getStorePath} from './util.js';

import type {Dictionary, JsonifiableObject} from '@alwatr/type-helper';
import type {Dictionary, JsonObject} from '@alwatr/type-helper';

logger.logModule?.('collection-reference');

Expand All @@ -23,7 +23,7 @@ logger.logModule?.('collection-reference');
*
* @template TItem - The data type of the collection items.
*/
export class CollectionReference<TItem extends JsonifiableObject = JsonifiableObject> {
export class CollectionReference<TItem extends JsonObject = JsonObject> {
/**
* Alwatr store engine version string.
*/
Expand All @@ -43,7 +43,7 @@ export class CollectionReference<TItem extends JsonifiableObject = JsonifiableOb
* @template TItem The collection item data type.
* @returns A new collection reference class.
*/
static newRefFromData<TItem extends JsonifiableObject>(
static newRefFromData<TItem extends JsonObject>(
stat: StoreFileId,
initialData: CollectionContext<TItem>['data'] | null,
updatedCallback: (from: CollectionReference<TItem>) => void,
Expand Down Expand Up @@ -79,7 +79,7 @@ export class CollectionReference<TItem extends JsonifiableObject = JsonifiableOb
* @template TItem The collection item data type.
* @returns A new collection reference class.
*/
static newRefFromContext<TItem extends JsonifiableObject>(
static newRefFromContext<TItem extends JsonObject>(
context: CollectionContext<TItem>,
updatedCallback: (from: CollectionReference<TItem>) => void,
debugDomain?: string,
Expand Down Expand Up @@ -147,7 +147,7 @@ export class CollectionReference<TItem extends JsonifiableObject = JsonifiableOb
this.context__.meta.fv = 3;
}

this.save();
this.updated__();
}

/**
Expand Down Expand Up @@ -214,7 +214,7 @@ export class CollectionReference<TItem extends JsonifiableObject = JsonifiableOb
set schemaVer(ver: number) {
this.logger__.logMethodArgs?.('set schemaVer', {old: this.context__.meta.schemaVer, new: ver});
this.context__.meta.schemaVer = ver;
this.save();
this.updated__();
}

/**
Expand Down Expand Up @@ -420,7 +420,7 @@ export class CollectionReference<TItem extends JsonifiableObject = JsonifiableOb
removeItem(itemId: string | number): void {
this.logger__.logMethodArgs?.('removeItem', itemId);
delete this.context__.data[itemId];
this.updated__(null);
this.updated__();
}

/**
Expand Down Expand Up @@ -574,13 +574,13 @@ export class CollectionReference<TItem extends JsonifiableObject = JsonifiableOb
* Update the document metadata and invoke the updated callback.
* This method is throttled to prevent multiple updates in a short time.
*
* @param id - The ID of the item to update.
* @param itemId - The ID of the item to update.
*/
private async updated__(id: string | number | null, immediate = false): Promise<void> {
this.logger__.logMethodArgs?.('updated__', {id, immediate, delayed: this.updateDelayed_});
private async updated__(itemId: string | number | null = null, immediate = false): Promise<void> {
this.logger__.logMethodArgs?.('updated__', {id: itemId, immediate, delayed: this.updateDelayed_});

this.hasUnprocessedChanges_ = true;
if (id !== null) this.refreshMeta_(id); // meta must updated per item
if (itemId !== null) this.refreshMeta_(itemId); // meta must updated per item

if (immediate === false && this.updateDelayed_ === true) return;
// else
Expand All @@ -597,7 +597,7 @@ export class CollectionReference<TItem extends JsonifiableObject = JsonifiableOb
if (this.updateDelayed_ !== true) return; // another parallel update finished!
this.updateDelayed_ = false;

if (id === null) this.refreshMeta_(id); // root meta not updated for null
if (itemId === null) this.refreshMeta_(itemId); // root meta not updated for null

if (this._freeze === true) return; // prevent save if frozen
this.updatedCallback__.call(null, this);
Expand Down
36 changes: 18 additions & 18 deletions packages/reference/src/document-reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import {waitForImmediate, waitForTimeout} from '@alwatr/wait';
import {logger} from './logger.js';
import {getStoreId, getStorePath} from './util.js';

import type {Dictionary, JsonifiableObject} from '@alwatr/type-helper';
import type {Dictionary, JsonObject} from '@alwatr/type-helper';

logger.logModule?.('document-reference');

/**
* Represents a reference to a document of the AlwatrStore.
* Provides methods to interact with the document, such as get, set, update and save.
*/
export class DocumentReference<TDoc extends JsonifiableObject = JsonifiableObject> {
export class DocumentReference<TDoc extends JsonObject = JsonObject> {
/**
* Alwatr store engine version string.
*/
Expand All @@ -33,7 +33,7 @@ export class DocumentReference<TDoc extends JsonifiableObject = JsonifiableObjec
* @template TDoc The document data type.
* @returns A new document reference class.
*/
static newRefFromData<TDoc extends JsonifiableObject>(
static newRefFromData<TDoc extends JsonObject>(
statId: StoreFileId,
initialData: TDoc,
updatedCallback: (from: DocumentReference<TDoc>) => unknown,
Expand Down Expand Up @@ -68,7 +68,7 @@ export class DocumentReference<TDoc extends JsonifiableObject = JsonifiableObjec
* @template TDoc The document data type.
* @returns A new document reference class.
*/
static newRefFromContext<TDoc extends JsonifiableObject>(
static newRefFromContext<TDoc extends JsonObject>(
context: DocumentContext<TDoc>,
updatedCallback: (from: DocumentReference<TDoc>) => unknown,
debugDomain?: string,
Expand Down Expand Up @@ -136,7 +136,7 @@ export class DocumentReference<TDoc extends JsonifiableObject = JsonifiableObjec
this.context__.meta.fv = 3;
}

this.save();
this.updated__();
}

/**
Expand All @@ -150,7 +150,7 @@ export class DocumentReference<TDoc extends JsonifiableObject = JsonifiableObjec
readonly path: string;

/**
* Indicates whether the collection has unsaved changes.
* Indicates whether the document has unsaved changes.
*/
hasUnprocessedChanges_ = false;

Expand Down Expand Up @@ -198,22 +198,22 @@ export class DocumentReference<TDoc extends JsonifiableObject = JsonifiableObjec
set schemaVer(ver: number) {
this.logger__.logMethodArgs?.('set schemaVer', {old: this.context__.meta.schemaVer, new: ver});
this.context__.meta.schemaVer = ver;
this.save();
this.updated__();
}

/**
* Indicates whether the collection data is frozen and cannot be saved.
* Indicates whether the document data is frozen and cannot be saved.
*/
private _freeze = false;

/**
* Gets the freeze status of the collection data.
* Gets the freeze status of the document data.
*
* @returns `true` if the collection data is frozen, `false` otherwise.
* @returns `true` if the document data is frozen, `false` otherwise.
*
* @example
* ```typescript
* const isFrozen = collectionRef.freeze;
* const isFrozen = documentRef.freeze;
* console.log(isFrozen); // Output: false
* ```
*/
Expand All @@ -222,14 +222,14 @@ export class DocumentReference<TDoc extends JsonifiableObject = JsonifiableObjec
}

/**
* Sets the freeze status of the collection data.
* Sets the freeze status of the document data.
*
* @param value - The freeze status to set.
*
* @example
* ```typescript
* collectionRef.freeze = true;
* console.log(collectionRef.freeze); // Output: true
* documentRef.freeze = true;
* console.log(documentRef.freeze); // Output: true
* ```
*/
set freeze(value: boolean) {
Expand Down Expand Up @@ -274,11 +274,11 @@ export class DocumentReference<TDoc extends JsonifiableObject = JsonifiableObjec
*
* @example
* ```typescript
* documentRef.overwriteData({ a: 1, b: 2, c: 3 });
* documentRef.replaceData({ a: 1, b: 2, c: 3 });
* ```
*/
replaceData(data: TDoc): void {
this.logger__.logMethodArgs?.('overwriteData', data);
this.logger__.logMethodArgs?.('replaceData', data);
(this.context__.data as unknown) = data;
this.updated__();
}
Expand Down Expand Up @@ -310,7 +310,7 @@ export class DocumentReference<TDoc extends JsonifiableObject = JsonifiableObjec
*/
save(): void {
this.logger__.logMethod?.('save');
this.updated__(false);
this.updated__();
}

/**
Expand All @@ -323,7 +323,7 @@ export class DocumentReference<TDoc extends JsonifiableObject = JsonifiableObjec
*/
saveImmediate(): void {
this.logger__.logMethod?.('saveImmediate');
this.updated__(true);
this.updated__(/* immediate: */ true);
}

/**
Expand Down

0 comments on commit b6a4aba

Please sign in to comment.