Skip to content

Commit 04cb1f7

Browse files
refactor(): refactor metadata storage collections
1 parent 6cb8391 commit 04cb1f7

11 files changed

+129
-110
lines changed
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
export class ArrayWithGlobalCacheCollection<T> {
2-
private readonly array: T[] = [];
2+
private readonly internalArray: T[] = [];
33

44
constructor(private globalArray: Array<T>) {}
55

66
getAll() {
7-
return this.array;
7+
return this.internalArray;
88
}
99

10-
push(...items): number {
10+
push(...items: T[]): number {
1111
this.globalArray.push(...items);
12-
return this.array.push(...items);
12+
return this.internalArray.push(...items);
1313
}
1414

15-
unshift(...items): number {
15+
unshift(...items: T[]): number {
1616
this.globalArray.unshift(...items);
17-
return this.array.unshift(...items);
17+
return this.internalArray.unshift(...items);
1818
}
1919

2020
reverse() {
21-
return this.array.reverse();
21+
return this.internalArray.reverse();
2222
}
2323

2424
reduce<U>(
@@ -30,6 +30,6 @@ export class ArrayWithGlobalCacheCollection<T> {
3030
) => U,
3131
initialValue: U,
3232
): U {
33-
return this.array.reduce(callbackfn, initialValue);
33+
return this.internalArray.reduce(callbackfn, initialValue);
3434
}
3535
}

packages/graphql/lib/schema-builder/collections/field-directive.collection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { MetadataListByNameCollection } from './metadata-list-by-name.collection';
21
import { PropertyDirectiveMetadata } from '../metadata';
2+
import { MetadataListByNameCollection } from './metadata-list-by-name.collection';
33

44
export class FieldDirectiveCollection extends MetadataListByNameCollection<PropertyDirectiveMetadata> {
55
sdls = new Set<string>();
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
export * from './metadata.storage.collection.list';
2-
export * from './field-directive.collection';
31
export * from './array-with-global-cache.collection';
4-
export * from './metada.collection.model.interface';
2+
export * from './field-directive.collection';
3+
export * from './metada-collection-model.interface';
54
export * from './metadata-by-name.collection';
5+
export * from './metadata-by-target.collection';
66
export * from './metadata-list-by-name.collection';
7-
export * from './metadata.storage.collection';
7+
export * from './target-metadata.collection';
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {
2+
ClassDirectiveMetadata,
3+
ClassExtensionsMetadata,
4+
ClassMetadata,
5+
PropertyDirectiveMetadata,
6+
PropertyExtensionsMetadata,
7+
ResolverClassMetadata,
8+
} from '../metadata';
9+
import { ObjectTypeMetadata } from '../metadata/object-type.metadata';
10+
11+
export interface MetadataCollectionModel {
12+
argumentType: ClassMetadata[];
13+
interface: ClassMetadata[];
14+
inputType: ClassMetadata[];
15+
objectType: ObjectTypeMetadata[];
16+
resolver: ResolverClassMetadata[];
17+
classDirectives: ClassDirectiveMetadata[];
18+
classExtensions: ClassExtensionsMetadata[];
19+
fieldDirectives: PropertyDirectiveMetadata[];
20+
fieldExtensions: PropertyExtensionsMetadata[];
21+
}

packages/graphql/lib/schema-builder/collections/metada.collection.model.interface.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
export class MetadataByNameCollection<T> {
2-
protected map = new Map<string, T>();
2+
protected internalCollection = new Map<string, T>();
33
protected all: (T extends any[] ? T[number] : T)[] = [];
44

55
getAll() {
66
return this.all;
77
}
88

99
getByName(name: string) {
10-
return this.map.get(name);
10+
return this.internalCollection.get(name);
1111
}
1212

1313
add(value: T extends any[] ? T[number] : T, name: string) {
14-
if (this.map.has(name)) return;
14+
if (this.internalCollection.has(name)) {
15+
return;
16+
}
1517

16-
this.map.set(name, value);
18+
this.internalCollection.set(name, value);
1719
this.all.push(value);
1820
}
1921

2022
unshift(value: T extends any[] ? T[number] : T, name: string) {
21-
if (this.map.has(name)) return;
22-
23-
this.map.set(name, value);
23+
if (this.internalCollection.has(name)) {
24+
return;
25+
}
26+
this.internalCollection.set(name, value);
2427
this.all.unshift(value);
2528
}
2629
}

packages/graphql/lib/schema-builder/collections/metadata.storage.collection.list.ts renamed to packages/graphql/lib/schema-builder/collections/metadata-by-target.collection.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
import { MetadataStorageCollection } from './metadata.storage.collection';
2-
import { MetadataCollectionModelInterface } from './metada.collection.model.interface';
1+
import { MetadataCollectionModel } from './metada-collection-model.interface';
2+
import { TargetMetadataCollection } from './target-metadata.collection';
33

4-
export class MetadataStorageCollectionList {
5-
private storageMap = new Map<Function, MetadataStorageCollection>();
6-
private storageList = new Array<MetadataStorageCollection>();
7-
8-
public all: MetadataCollectionModelInterface = {
4+
export class MetadataByTargetCollection {
5+
public readonly all: MetadataCollectionModel = {
96
argumentType: [],
107
interface: [],
118
inputType: [],
@@ -17,11 +14,14 @@ export class MetadataStorageCollectionList {
1714
fieldExtensions: [],
1815
};
1916

17+
private readonly storageMap = new Map<Function, TargetMetadataCollection>();
18+
private readonly storageList = new Array<TargetMetadataCollection>();
19+
2020
get(target: Function) {
2121
let metadata = this.storageMap.get(target);
2222

2323
if (!metadata) {
24-
metadata = new MetadataStorageCollection(this.all);
24+
metadata = new TargetMetadataCollection(this.all);
2525
this.storageMap.set(target, metadata);
2626
this.storageList.push(metadata);
2727
}
@@ -37,7 +37,7 @@ export class MetadataStorageCollectionList {
3737
}
3838

3939
private reversePredicate<V>(
40-
predicate: (t: MetadataStorageCollection) => Array<V>,
40+
predicate: (t: TargetMetadataCollection) => Array<V>,
4141
) {
4242
this.storageList.forEach((t) => predicate(t).reverse());
4343
}

packages/graphql/lib/schema-builder/collections/metadata-list-by-name.collection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class MetadataListByNameCollection<T> extends MetadataByNameCollection<
1515
let arrayResult = super.getByName(name);
1616
if (!arrayResult) {
1717
arrayResult = [];
18-
this.map.set(name, arrayResult);
18+
this.internalCollection.set(name, arrayResult);
1919
}
2020

2121
arrayResult.push(value);
@@ -27,7 +27,7 @@ export class MetadataListByNameCollection<T> extends MetadataByNameCollection<
2727
let arrayResult = super.getByName(name);
2828
if (!arrayResult) {
2929
arrayResult = [];
30-
this.map.set(name, arrayResult);
30+
this.internalCollection.set(name, arrayResult);
3131
}
3232

3333
arrayResult.unshift(value);

packages/graphql/lib/schema-builder/collections/metadata.storage.collection.ts renamed to packages/graphql/lib/schema-builder/collections/target-metadata.collection.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { MetadataByNameCollection } from './metadata-by-name.collection';
21
import {
32
ClassDirectiveMetadata,
43
ClassExtensionsMetadata,
@@ -8,14 +7,15 @@ import {
87
PropertyMetadata,
98
ResolverClassMetadata,
109
} from '../metadata';
11-
import { MetadataListByNameCollection } from './metadata-list-by-name.collection';
12-
import { FieldDirectiveCollection } from './field-directive.collection';
1310
import { ObjectTypeMetadata } from '../metadata/object-type.metadata';
1411
import { ArrayWithGlobalCacheCollection } from './array-with-global-cache.collection';
15-
import { MetadataCollectionModelInterface } from './metada.collection.model.interface';
12+
import { FieldDirectiveCollection } from './field-directive.collection';
13+
import { MetadataCollectionModel } from './metada-collection-model.interface';
14+
import { MetadataByNameCollection } from './metadata-by-name.collection';
15+
import { MetadataListByNameCollection } from './metadata-list-by-name.collection';
1616

17-
export class MetadataStorageCollection {
18-
constructor(private all: MetadataCollectionModelInterface) {}
17+
export class TargetMetadataCollection {
18+
constructor(private readonly all: MetadataCollectionModel) {}
1919

2020
fields = new MetadataByNameCollection<PropertyMetadata>();
2121
params = new MetadataListByNameCollection<MethodArgsMetadata>();
@@ -31,6 +31,12 @@ export class MetadataStorageCollection {
3131
this.all.classExtensions,
3232
);
3333

34+
private _argumentType: ClassMetadata;
35+
private _interface: ClassMetadata;
36+
private _inputType: ClassMetadata;
37+
private _objectType: ObjectTypeMetadata;
38+
private _resolver: ResolverClassMetadata;
39+
3440
set argumentType(val: ClassMetadata) {
3541
this._argumentType = val;
3642
this.all.argumentType.push(val);
@@ -75,10 +81,4 @@ export class MetadataStorageCollection {
7581
get resolver() {
7682
return this._resolver;
7783
}
78-
79-
_argumentType: ClassMetadata;
80-
_interface: ClassMetadata;
81-
_inputType: ClassMetadata;
82-
_objectType: ObjectTypeMetadata;
83-
_resolver: ResolverClassMetadata;
8484
}

0 commit comments

Comments
 (0)