Skip to content

Commit 16cc350

Browse files
refactor: Extract helper methods for index management
- Add ensureIndexSet() helper to reduce duplication in updateIndices() - Add removeFromIndexSet() helper to reduce duplication in removeFromIndices() - Improve code maintainability and readability - No functional changes, all tests still passing Co-authored-by: xuyushun441-sys <255036401+xuyushun441-sys@users.noreply.github.com>
1 parent 400b624 commit 16cc350

1 file changed

Lines changed: 41 additions & 32 deletions

File tree

packages/core/src/api-registry.ts

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -649,25 +649,16 @@ export class ApiRegistry {
649649
*/
650650
private updateIndices(api: ApiRegistryEntry): void {
651651
// Index by type
652-
if (!this.apisByType.has(api.type)) {
653-
this.apisByType.set(api.type, new Set());
654-
}
655-
this.apisByType.get(api.type)!.add(api.id);
652+
this.ensureIndexSet(this.apisByType, api.type).add(api.id);
656653

657654
// Index by status
658655
const status = api.metadata?.status || 'active';
659-
if (!this.apisByStatus.has(status)) {
660-
this.apisByStatus.set(status, new Set());
661-
}
662-
this.apisByStatus.get(status)!.add(api.id);
656+
this.ensureIndexSet(this.apisByStatus, status).add(api.id);
663657

664658
// Index by tags
665659
const tags = api.metadata?.tags || [];
666660
for (const tag of tags) {
667-
if (!this.apisByTag.has(tag)) {
668-
this.apisByTag.set(tag, new Set());
669-
}
670-
this.apisByTag.get(tag)!.add(api.id);
661+
this.ensureIndexSet(this.apisByTag, tag).add(api.id);
671662
}
672663
}
673664

@@ -680,33 +671,51 @@ export class ApiRegistry {
680671
*/
681672
private removeFromIndices(api: ApiRegistryEntry): void {
682673
// Remove from type index
683-
const typeSet = this.apisByType.get(api.type);
684-
if (typeSet) {
685-
typeSet.delete(api.id);
686-
if (typeSet.size === 0) {
687-
this.apisByType.delete(api.type);
688-
}
689-
}
674+
this.removeFromIndexSet(this.apisByType, api.type, api.id);
690675

691676
// Remove from status index
692677
const status = api.metadata?.status || 'active';
693-
const statusSet = this.apisByStatus.get(status);
694-
if (statusSet) {
695-
statusSet.delete(api.id);
696-
if (statusSet.size === 0) {
697-
this.apisByStatus.delete(status);
698-
}
699-
}
678+
this.removeFromIndexSet(this.apisByStatus, status, api.id);
700679

701680
// Remove from tag indices
702681
const tags = api.metadata?.tags || [];
703682
for (const tag of tags) {
704-
const tagSet = this.apisByTag.get(tag);
705-
if (tagSet) {
706-
tagSet.delete(api.id);
707-
if (tagSet.size === 0) {
708-
this.apisByTag.delete(tag);
709-
}
683+
this.removeFromIndexSet(this.apisByTag, tag, api.id);
684+
}
685+
}
686+
687+
/**
688+
* Helper to ensure an index set exists and return it
689+
*
690+
* @param map - Index map
691+
* @param key - Index key
692+
* @returns The Set for this key (created if needed)
693+
* @private
694+
* @internal
695+
*/
696+
private ensureIndexSet(map: Map<string, Set<string>>, key: string): Set<string> {
697+
if (!map.has(key)) {
698+
map.set(key, new Set());
699+
}
700+
return map.get(key)!;
701+
}
702+
703+
/**
704+
* Helper to remove an ID from an index set and clean up empty sets
705+
*
706+
* @param map - Index map
707+
* @param key - Index key
708+
* @param id - API ID to remove
709+
* @private
710+
* @internal
711+
*/
712+
private removeFromIndexSet(map: Map<string, Set<string>>, key: string, id: string): void {
713+
const set = map.get(key);
714+
if (set) {
715+
set.delete(id);
716+
// Clean up empty sets to avoid memory leaks
717+
if (set.size === 0) {
718+
map.delete(key);
710719
}
711720
}
712721
}

0 commit comments

Comments
 (0)