Skip to content

Commit 3a2f4d8

Browse files
Merge branch 'roypeled-type-metadata-storage_with_maps'
2 parents d9339b0 + 04cb1f7 commit 3a2f4d8

13 files changed

+545
-147
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
export class ArrayWithGlobalCacheCollection<T> {
2+
private readonly internalArray: T[] = [];
3+
4+
constructor(private globalArray: Array<T>) {}
5+
6+
getAll() {
7+
return this.internalArray;
8+
}
9+
10+
push(...items: T[]): number {
11+
this.globalArray.push(...items);
12+
return this.internalArray.push(...items);
13+
}
14+
15+
unshift(...items: T[]): number {
16+
this.globalArray.unshift(...items);
17+
return this.internalArray.unshift(...items);
18+
}
19+
20+
reverse() {
21+
return this.internalArray.reverse();
22+
}
23+
24+
reduce<U>(
25+
callbackfn: (
26+
previousValue: U,
27+
currentValue: T,
28+
currentIndex: number,
29+
array: T[],
30+
) => U,
31+
initialValue: U,
32+
): U {
33+
return this.internalArray.reduce(callbackfn, initialValue);
34+
}
35+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { PropertyDirectiveMetadata } from '../metadata';
2+
import { MetadataListByNameCollection } from './metadata-list-by-name.collection';
3+
4+
export class FieldDirectiveCollection extends MetadataListByNameCollection<PropertyDirectiveMetadata> {
5+
sdls = new Set<string>();
6+
fieldNames = new Set<string>();
7+
8+
add(value: PropertyDirectiveMetadata) {
9+
if (this.sdls.has(value.sdl) && this.fieldNames.has(value.fieldName)) {
10+
return;
11+
}
12+
13+
super.add(value, value.fieldName);
14+
15+
this.sdls.add(value.sdl);
16+
this.fieldNames.add(value.fieldName);
17+
this.globalArray?.push(value);
18+
}
19+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export * from './array-with-global-cache.collection';
2+
export * from './field-directive.collection';
3+
export * from './metada-collection-model.interface';
4+
export * from './metadata-by-name.collection';
5+
export * from './metadata-by-target.collection';
6+
export * from './metadata-list-by-name.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+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export class MetadataByNameCollection<T> {
2+
protected internalCollection = new Map<string, T>();
3+
protected all: (T extends any[] ? T[number] : T)[] = [];
4+
5+
getAll() {
6+
return this.all;
7+
}
8+
9+
getByName(name: string) {
10+
return this.internalCollection.get(name);
11+
}
12+
13+
add(value: T extends any[] ? T[number] : T, name: string) {
14+
if (this.internalCollection.has(name)) {
15+
return;
16+
}
17+
18+
this.internalCollection.set(name, value);
19+
this.all.push(value);
20+
}
21+
22+
unshift(value: T extends any[] ? T[number] : T, name: string) {
23+
if (this.internalCollection.has(name)) {
24+
return;
25+
}
26+
this.internalCollection.set(name, value);
27+
this.all.unshift(value);
28+
}
29+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { MetadataCollectionModel } from './metada-collection-model.interface';
2+
import { TargetMetadataCollection } from './target-metadata.collection';
3+
4+
export class MetadataByTargetCollection {
5+
public readonly all: MetadataCollectionModel = {
6+
argumentType: [],
7+
interface: [],
8+
inputType: [],
9+
objectType: [],
10+
resolver: [],
11+
classDirectives: [],
12+
classExtensions: [],
13+
fieldDirectives: [],
14+
fieldExtensions: [],
15+
};
16+
17+
private readonly storageMap = new Map<Function, TargetMetadataCollection>();
18+
private readonly storageList = new Array<TargetMetadataCollection>();
19+
20+
get(target: Function) {
21+
let metadata = this.storageMap.get(target);
22+
23+
if (!metadata) {
24+
metadata = new TargetMetadataCollection(this.all);
25+
this.storageMap.set(target, metadata);
26+
this.storageList.push(metadata);
27+
}
28+
29+
return metadata;
30+
}
31+
32+
compile() {
33+
this.reversePredicate((t) => t.classDirectives.getAll());
34+
this.reversePredicate((t) => t.classExtensions.getAll());
35+
this.reversePredicate((t) => t.fieldDirectives.getAll());
36+
this.reversePredicate((t) => t.fieldExtensions.getAll());
37+
}
38+
39+
private reversePredicate<V>(
40+
predicate: (t: TargetMetadataCollection) => Array<V>,
41+
) {
42+
this.storageList.forEach((t) => predicate(t).reverse());
43+
}
44+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { MetadataByNameCollection } from './metadata-by-name.collection';
2+
3+
export class MetadataListByNameCollection<T> extends MetadataByNameCollection<
4+
T[]
5+
> {
6+
constructor(protected globalArray: Array<T> = null) {
7+
super();
8+
}
9+
10+
getByName(name: string): T[] {
11+
return super.getByName(name) || [];
12+
}
13+
14+
add(value: T, name: string) {
15+
let arrayResult = super.getByName(name);
16+
if (!arrayResult) {
17+
arrayResult = [];
18+
this.internalCollection.set(name, arrayResult);
19+
}
20+
21+
arrayResult.push(value);
22+
this.all.push(value);
23+
this.globalArray && this.globalArray.push(value);
24+
}
25+
26+
unshift(value: T, name: string) {
27+
let arrayResult = super.getByName(name);
28+
if (!arrayResult) {
29+
arrayResult = [];
30+
this.internalCollection.set(name, arrayResult);
31+
}
32+
33+
arrayResult.unshift(value);
34+
this.all.push(value);
35+
this.globalArray && this.globalArray.unshift(value);
36+
}
37+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import {
2+
ClassDirectiveMetadata,
3+
ClassExtensionsMetadata,
4+
ClassMetadata,
5+
MethodArgsMetadata,
6+
PropertyExtensionsMetadata,
7+
PropertyMetadata,
8+
ResolverClassMetadata,
9+
} from '../metadata';
10+
import { ObjectTypeMetadata } from '../metadata/object-type.metadata';
11+
import { ArrayWithGlobalCacheCollection } from './array-with-global-cache.collection';
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';
16+
17+
export class TargetMetadataCollection {
18+
constructor(private readonly all: MetadataCollectionModel) {}
19+
20+
fields = new MetadataByNameCollection<PropertyMetadata>();
21+
params = new MetadataListByNameCollection<MethodArgsMetadata>();
22+
fieldDirectives = new FieldDirectiveCollection(this.all.fieldDirectives);
23+
fieldExtensions =
24+
new MetadataListByNameCollection<PropertyExtensionsMetadata>(
25+
this.all.fieldExtensions,
26+
);
27+
classDirectives = new ArrayWithGlobalCacheCollection<ClassDirectiveMetadata>(
28+
this.all.classDirectives,
29+
);
30+
classExtensions = new ArrayWithGlobalCacheCollection<ClassExtensionsMetadata>(
31+
this.all.classExtensions,
32+
);
33+
34+
private _argumentType: ClassMetadata;
35+
private _interface: ClassMetadata;
36+
private _inputType: ClassMetadata;
37+
private _objectType: ObjectTypeMetadata;
38+
private _resolver: ResolverClassMetadata;
39+
40+
set argumentType(val: ClassMetadata) {
41+
this._argumentType = val;
42+
this.all.argumentType.push(val);
43+
}
44+
45+
get argumentType() {
46+
return this._argumentType;
47+
}
48+
49+
set interface(val: ClassMetadata) {
50+
this._interface = val;
51+
this.all.interface.push(val);
52+
}
53+
54+
get interface() {
55+
return this._interface;
56+
}
57+
58+
set inputType(val: ClassMetadata) {
59+
this._inputType = val;
60+
this.all.inputType.push(val);
61+
}
62+
63+
get inputType() {
64+
return this._inputType;
65+
}
66+
67+
set objectType(val: ObjectTypeMetadata) {
68+
this._objectType = val;
69+
this.all.objectType.push(val);
70+
}
71+
72+
get objectType() {
73+
return this._objectType;
74+
}
75+
76+
set resolver(val: ResolverClassMetadata) {
77+
this._resolver = val;
78+
this.all.resolver.push(val);
79+
}
80+
81+
get resolver() {
82+
return this._resolver;
83+
}
84+
}

0 commit comments

Comments
 (0)