Skip to content

Commit

Permalink
feat(memory): use WeakMap instead normal Map to allow GC
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalLytek committed Dec 22, 2019
1 parent 9568cfd commit a195e8a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/metadata/builder/MetadataBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import MissingClassMetadataError from "@src/errors/MissingClassMetadataError";
import MissingFieldsError from "@src/errors/MissingFieldsError";

export default class MetadataBuilder {
private readonly typeMetadataByClassMap = new Map<
private readonly typeMetadataByClassMap = new WeakMap<
ClassType,
BuiltObjectTypeMetadata
>();
Expand All @@ -30,7 +30,7 @@ export default class MetadataBuilder {
const objectTypeFieldsMetadata = MetadataStorage.get().findFieldMetadata(
typeClass,
);
if (objectTypeFieldsMetadata.length === 0) {
if (!objectTypeFieldsMetadata || objectTypeFieldsMetadata.length === 0) {
throw new MissingFieldsError(typeClass);
}

Expand Down
20 changes: 13 additions & 7 deletions packages/core/src/metadata/storage/MetadataStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import FieldMetadata from "@src/metadata/storage/definitions/FieldMetadata";
import ClassType from "@src/interfaces/ClassType";

export default class MetadataStorage {
protected objectTypesMetadata: ObjectTypeMetadata[] = [];
protected fieldsMetadata: FieldMetadata[] = [];
protected objectTypesMetadataMap = new WeakMap<
ClassType,
ObjectTypeMetadata
>();
protected fieldsMetadataMap = new WeakMap<ClassType, FieldMetadata[]>();

protected constructor() {}

Expand All @@ -16,16 +19,19 @@ export default class MetadataStorage {
}

collectObjectTypeMetadata(metadata: ObjectTypeMetadata): void {
this.objectTypesMetadata.push(metadata);
this.objectTypesMetadataMap.set(metadata.target, metadata);
}
findObjectTypeMetadata(typeClass: ClassType): ObjectTypeMetadata | undefined {
return this.objectTypesMetadata.find(it => it.target === typeClass);
return this.objectTypesMetadataMap.get(typeClass);
}

collectFieldMetadata(metadata: FieldMetadata): void {
this.fieldsMetadata.push(metadata);
this.fieldsMetadataMap.set(metadata.target, [
...(this.fieldsMetadataMap.get(metadata.target) ?? []),
metadata,
]);
}
findFieldMetadata(typeClass: ClassType): FieldMetadata[] {
return this.fieldsMetadata.filter(it => it.target === typeClass);
findFieldMetadata(typeClass: ClassType): FieldMetadata[] | undefined {
return this.fieldsMetadataMap.get(typeClass);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/schema/SchemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
} from "@src/metadata/storage/definitions/common";

export default class SchemaGenerator {
private readonly typeByClassMap = new Map<ClassType, GraphQLObjectType>();
private readonly typeByClassMap = new WeakMap<ClassType, GraphQLObjectType>();
private readonly metadataBuilder: MetadataBuilder;

constructor(private readonly buildSchemaOptions: BuildSchemaOptions) {
Expand Down

0 comments on commit a195e8a

Please sign in to comment.