Skip to content

Commit

Permalink
address feddbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
inromualdo committed Jan 8, 2025
1 parent 1cf9167 commit 64c95e8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/lib/remote-place-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ export default class RemotePlaceCache {
return this.cache;
}

private static getCacheKey(domain: string, placeType?: string): string {
return placeType ? `${domain}:${placeType}` : domain;
private static getCacheKey(domain: string, placeType: string): string {
return `${domain}:${placeType}`;
}

public static add(place: Place, chtApi: ChtApi): void {
Expand All @@ -64,6 +64,18 @@ export default class RemotePlaceCache {
const domain = chtApi?.chtSession?.authInfo?.domain;
if (!domain) {
this.getCache().flushAll();
return;
}

if (!contactTypeName) {
// Clear all keys matching domain prefix
const keys = this.getCache().keys();
const domainPrefix = `${domain}:`;
keys.forEach(key => {
if (key.startsWith(domainPrefix)) {
this.getCache().del(key);
}
});
} else {
const cacheKey = this.getCacheKey(domain, contactTypeName);
this.getCache().del(cacheKey);
Expand All @@ -87,4 +99,11 @@ export default class RemotePlaceCache {
type: 'remote',
}));
}

// For testing purposes
public static hasData(domain: string, placeType: string): boolean {
const cacheKey = this.getCacheKey(domain, placeType);

return !!this.getCache().get(cacheKey);
}
}
20 changes: 20 additions & 0 deletions test/lib/remote-place-cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,25 @@ describe('lib/remote-place-cache.ts', () => {
chtApi.chtSession.authInfo.domain = 'http://other';
RemotePlaceCache.clear(chtApi, 'other');
});

it('clears all place types when clearing domain', async () => {
const chtApi = mockChtApi([doc]);
const domain = chtApi.chtSession.authInfo.domain;

const contactTypeAsHierarchyLevel: HierarchyConstraint = {
contact_type: contactType.name,
property_name: 'level',
friendly_name: 'pretend another ContactType needs this',
type: 'name',
required: true,
level: 0,
};
await RemotePlaceCache.getPlacesWithType(chtApi, contactType, contactTypeAsHierarchyLevel);
expect(RemotePlaceCache.hasData(domain, contactType.name)).to.be.true;

// Clear domain
RemotePlaceCache.clear(chtApi);
expect(RemotePlaceCache.hasData(domain, contactType.name)).to.be.false;
});
});

0 comments on commit 64c95e8

Please sign in to comment.