Skip to content

Commit e566c4a

Browse files
adileojoepio
authored andcommitted
fix issue #309, race condition of getResourceAsync
1 parent c16b358 commit e566c4a

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This changelog covers all three packages, as they are (for now) updated as a who
1313
- Add new file preview types to the folder grid view.
1414
- Add `store.preloadClassesAndProperties` and remove `urls.properties.getAll` and `urls.classes.getAll`. This enables using `atomic-data-browser` without relying on `atomicdata.dev` being available.
1515
- Fix Dialogue form #308
16+
- Fix Race condition of `store.getResourceAsync` #309
1617

1718
## v0.35.0
1819

lib/src/store.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,33 @@ export class Store {
322322
public async getResourceAsync(subject: string): Promise<Resource> {
323323
const found = this.resources.get(subject);
324324

325-
if (found) {
325+
if (found && found.isReady()) {
326326
return found;
327327
}
328328

329+
/** Fix the case where a resource was previously requested but still not ready */
330+
if (found && !found.isReady()) {
331+
return new Promise((resolve, reject) => {
332+
const defaultTimeout = 5000;
333+
334+
const cb = res => {
335+
this.unsubscribe(subject, cb);
336+
resolve(res);
337+
};
338+
339+
this.subscribe(subject, cb);
340+
341+
setTimeout(() => {
342+
this.unsubscribe(subject, cb);
343+
reject(
344+
new Error(
345+
`Async Request for subject "${subject}" timed out after ${defaultTimeout}ms.`,
346+
),
347+
);
348+
}, defaultTimeout);
349+
});
350+
}
351+
329352
return this.fetchResourceFromServer(subject);
330353
}
331354

0 commit comments

Comments
 (0)