Skip to content

Commit

Permalink
fix: The caching mechanism for searching an entity response array by …
Browse files Browse the repository at this point in the history
…ID is storing the wrong values.
  • Loading branch information
adam-coster committed Aug 26, 2021
1 parent e854e97 commit 223ad66
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/lib/clientLib/BravoResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class BravoResponseEntities<
* but with multiple caches (one for each identifier field).
*/
private _entitiesCachedById: {
[identifierField: string]: { [id: string]: Entity };
[identifierName: string]: { [identifierValue: string]: Entity };
} = {};

constructor(
Expand Down Expand Up @@ -121,15 +121,16 @@ export class BravoResponseEntities<
): Promise<Entity | undefined> {
// Ensure we have the per-identifier name cache
this._entitiesCachedById[identifierName] ||= {};
const cache = this._entitiesCachedById[identifierName];
const entity = cache[identifierValue];
if (entity) {
return entity;
const cache = this._entitiesCachedById[identifierName]!;
if (cache[identifierValue]) {
return cache[identifierValue];
}

// Need to iterate over the list! But we can cache as we go.
for await (const entity of this) {
cache[identifierValue] ||= entity;
// @ts-expect-error
const entityIdValue = entity[identifierName];
cache[entityIdValue] ||= entity;
// @ts-expect-error
if (entity[identifierName] === identifierValue) {
return entity;
Expand Down

0 comments on commit 223ad66

Please sign in to comment.