Skip to content

Commit

Permalink
feat(engine5): add exists and refactor stat
Browse files Browse the repository at this point in the history
  • Loading branch information
alimd committed Dec 25, 2023
1 parent 76dadba commit 9e6e4d2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
18 changes: 12 additions & 6 deletions packages/engine5/src/alwatr-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class AlwatrStore {
/**
* Keep all loaded store file context loaded in memory.
*/
protected storeFilesRecord_: Record<string, StoreFileContext> = {};
protected memoryContextRecord_: Record<string, StoreFileContext> = {};

constructor(readonly config: AlwatrStoreConfig) {
logger.logMethodArgs?.('new', config);
Expand Down Expand Up @@ -123,8 +123,14 @@ export class AlwatrStore {
this.storeFilesCollection_.update({[stat.id]: stat});
}

stat(id: string): Readonly<StoreFileStat> | null {
exists(id: string): boolean {
logger.logMethodArgs?.('exist', id);
return id in this.storeFilesCollection_.get();
}

stat(id: string): Readonly<StoreFileStat> {
logger.logMethodArgs?.('stat', id);
// TODO: error store_file_not_defined
return this.storeFilesCollection_.get()[id] ?? null;
}

Expand Down Expand Up @@ -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_<TDoc>(stat);
const context = this.memoryContextRecord_[id] = await AlwatrStore.readStoreFile_<TDoc>(stat);
return new DocumentReference(context, this.save_.bind(this));
}

Expand All @@ -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;
Expand All @@ -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));
Expand Down
7 changes: 5 additions & 2 deletions packages/engine5/src/demo-doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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<Post>(docId);

Expand Down

0 comments on commit 9e6e4d2

Please sign in to comment.