Skip to content

Commit

Permalink
Merge pull request #359 from AliMD/feat/_last
Browse files Browse the repository at this point in the history
fix(storage): `_last` id issue, update cache keys issue
  • Loading branch information
alimd authored Nov 2, 2022
2 parents 0c17280 + 62a5459 commit 2fe8c12
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
16 changes: 9 additions & 7 deletions packages/core/storage/src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ export class AlwatrStorage<DocumentType extends DocumentObject> {

/**
* All document ids in array.
*
* Contain `_last`!
*/
get keys(): Array<string> {
if (this._keys === null) {
Expand All @@ -72,7 +70,7 @@ export class AlwatrStorage<DocumentType extends DocumentObject> {
}

/**
* Size of the storage (count `_last`).
* Size of the storage.
*/
get length(): number {
return this.keys.length;
Expand Down Expand Up @@ -132,7 +130,6 @@ export class AlwatrStorage<DocumentType extends DocumentObject> {

const documentObject = this._storage[documentId];
if (typeof documentObject === 'string') {
// for example _last
return this.get(documentObject);
}
else if (documentObject == null) {
Expand Down Expand Up @@ -167,7 +164,7 @@ export class AlwatrStorage<DocumentType extends DocumentObject> {
this._logger.logMethodArgs('set', documentObject._id);

const oldData = this._storage[documentObject._id];
if (oldData == null) this._keys = null; // Clear cached keys on new docId
if (oldData == null) this._keys = null; // Clear cached keys

if (fastInstance !== true) {
documentObject = JSON.parse(JSON.stringify(documentObject));
Expand All @@ -179,7 +176,6 @@ export class AlwatrStorage<DocumentType extends DocumentObject> {
documentObject._createdBy = oldData?._createdBy ?? documentObject._updatedBy;
documentObject._rev = (oldData?._rev ?? 0) + 1;

this._storage._last = documentObject._id;
this._storage[documentObject._id] = documentObject;

this.save();
Expand All @@ -203,6 +199,10 @@ export class AlwatrStorage<DocumentType extends DocumentObject> {
}
// else
delete this._storage[documentId];

// Clear cached keys
this._keys = null;

this.save();
return true;
}
Expand All @@ -224,7 +224,6 @@ export class AlwatrStorage<DocumentType extends DocumentObject> {
async forAll(callbackfn: (documentObject: DocumentType) => void | false | Promise<void | false>): Promise<void> {
const keys = this.keys;
for (const documentId of keys) {
if (documentId === '_last') continue; // prevent to duplicate latest key.
const documentObject = this.get(documentId);
if (documentObject != null) {
const retVal = await callbackfn(documentObject);
Expand Down Expand Up @@ -272,5 +271,8 @@ export class AlwatrStorage<DocumentType extends DocumentObject> {
this.forceSave();
}
this._storage = {};

// Clear cached keys
this._keys = null;
}
}
4 changes: 1 addition & 3 deletions packages/core/storage/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ export interface DocumentObject {
_updatedBy: string;
}

export type DocumentListStorage<DocType extends DocumentObject> = Record<string, DocType | undefined> & {
_last?: string;
};
export type DocumentListStorage<DocType extends DocumentObject> = Record<string, DocType | undefined>

export type AlwatrStorageConfig = {
/**
Expand Down

0 comments on commit 2fe8c12

Please sign in to comment.