From 9e6e4d21d702272f83bfcf8b9b587299a250c754 Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Mon, 11 Dec 2023 13:12:00 +0330 Subject: [PATCH] feat(engine5): add exists and refactor stat --- packages/engine5/src/alwatr-store.ts | 18 ++++++++++++------ packages/engine5/src/demo-doc.ts | 7 +++++-- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/packages/engine5/src/alwatr-store.ts b/packages/engine5/src/alwatr-store.ts index ac313fbf..a3b0865d 100644 --- a/packages/engine5/src/alwatr-store.ts +++ b/packages/engine5/src/alwatr-store.ts @@ -91,7 +91,7 @@ export class AlwatrStore { /** * Keep all loaded store file context loaded in memory. */ - protected storeFilesRecord_: Record = {}; + protected memoryContextRecord_: Record = {}; constructor(readonly config: AlwatrStoreConfig) { logger.logMethodArgs?.('new', config); @@ -123,8 +123,14 @@ export class AlwatrStore { this.storeFilesCollection_.update({[stat.id]: stat}); } - stat(id: string): Readonly | null { + exists(id: string): boolean { + logger.logMethodArgs?.('exist', id); + return id in this.storeFilesCollection_.get(); + } + + stat(id: string): Readonly { logger.logMethodArgs?.('stat', id); + // TODO: error store_file_not_defined return this.storeFilesCollection_.get()[id] ?? null; } @@ -153,7 +159,7 @@ export class AlwatrStore { logger.error?.('getDocument', 'document_wrong_type', stat); throw new Error('document_not_found', {cause: stat}); } - const context = this.storeFilesRecord_[id] = await AlwatrStore.readStoreFile_(stat); + const context = this.memoryContextRecord_[id] = await AlwatrStore.readStoreFile_(stat); return new DocumentReference(context, this.save_.bind(this)); } @@ -164,7 +170,7 @@ export class AlwatrStore { logger.error?.('updated_', 'store_file_not_defined', {id}); return; } - const context = this.storeFilesRecord_[id]; + const context = this.memoryContextRecord_[id]; if (context === undefined) { logger.error?.('updated_', 'store_file_unloaded', {id}); return; @@ -175,12 +181,12 @@ export class AlwatrStore { unload(id: string): void { logger.logMethodArgs?.('unload', id); // TODO: this.save_(id); - delete this.storeFilesRecord_[id]; + delete this.memoryContextRecord_[id]; } deleteFile(id: string): void { logger.logMethodArgs?.('deleteFile', id); - if (id in this.storeFilesRecord_) { + if (id in this.memoryContextRecord_) { this.unload(id); } // TODO: AlwatrStore.deleteStoreFile_(this.stat(id)); diff --git a/packages/engine5/src/demo-doc.ts b/packages/engine5/src/demo-doc.ts index e02614f6..6980c17a 100644 --- a/packages/engine5/src/demo-doc.ts +++ b/packages/engine5/src/demo-doc.ts @@ -25,8 +25,8 @@ async function quickstart() { logger.logProperty?.('docId', docId); - // Check document stat. return null if not exist. - logger.logProperty?.('stat', alwatrStore.stat(docId)); + // Check the document exist? + logger.logProperty?.('exists', alwatrStore.exists(docId)); // Create a new document. alwatrStore.defineDoc( @@ -41,6 +41,9 @@ async function quickstart() { }, ); + // Check the document stat. + logger.logProperty?.('stat', alwatrStore.stat(docId)); + // Create new document reference of specific id. const myPostDoc = await alwatrStore.doc(docId);