OLD | NEW |
1 part of angular.core; | 1 library angular.core.registry; |
2 | 2 |
| 3 import 'package:di/di.dart' show Injector; |
| 4 |
3 abstract class AnnotationMap<K> { | 5 abstract class AnnotationMap<K> { |
4 final Map<K, Type> _map = {}; | 6 final Map<K, Type> _map = {}; |
5 | 7 |
6 AnnotationMap(Injector injector, MetadataExtractor extractMetadata) { | 8 AnnotationMap(Injector injector, MetadataExtractor extractMetadata) { |
7 injector.types.forEach((type) { | 9 injector.types.forEach((type) { |
8 var meta = extractMetadata(type) | 10 extractMetadata(type) |
9 .where((annotation) => annotation is K) | 11 .where((annotation) => annotation is K) |
10 .forEach((annotation) { | 12 .forEach((annotation) { |
11 _map[annotation] = type; | 13 _map[annotation] = type; |
12 }); | 14 }); |
13 }); | 15 }); |
14 } | 16 } |
15 | 17 |
16 Type operator[](K annotation) { | 18 Type operator[](K annotation) { |
17 var value = _map[annotation]; | 19 var value = _map[annotation]; |
18 if (value == null) throw 'No $annotation found!'; | 20 if (value == null) throw 'No $annotation found!'; |
19 return value; | 21 return value; |
20 } | 22 } |
21 | 23 |
22 forEach(fn(K, Type)) => _map.forEach(fn); | 24 void forEach(fn(K, Type)) { |
| 25 _map.forEach(fn); |
| 26 } |
23 | 27 |
24 List<K> annotationsFor(Type type) { | 28 List<K> annotationsFor(Type type) { |
25 var res = <K>[]; | 29 final res = <K>[]; |
26 forEach((ann, annType) { | 30 forEach((ann, annType) { |
27 if (annType == type) res.add(ann); | 31 if (annType == type) res.add(ann); |
28 }); | 32 }); |
29 return res; | 33 return res; |
30 } | 34 } |
31 } | 35 } |
32 | 36 |
33 abstract class AnnotationsMap<K> { | 37 abstract class AnnotationsMap<K> { |
34 final Map<K, List<Type>> map = {}; | 38 final Map<K, List<Type>> map = {}; |
35 | 39 |
36 AnnotationsMap(Injector injector, MetadataExtractor extractMetadata) { | 40 AnnotationsMap(Injector injector, MetadataExtractor extractMetadata) { |
37 injector.types.forEach((type) { | 41 injector.types.forEach((type) { |
38 var meta = extractMetadata(type) | 42 extractMetadata(type) |
39 .where((annotation) => annotation is K) | 43 .where((annotation) => annotation is K) |
40 .forEach((annotation) { | 44 .forEach((annotation) { |
41 map.putIfAbsent(annotation, () => []).add(type); | 45 map.putIfAbsent(annotation, () => []).add(type); |
42 }); | 46 }); |
43 }); | 47 }); |
44 } | 48 } |
45 | 49 |
46 List operator[](K annotation) { | 50 List operator[](K annotation) { |
47 var value = map[annotation]; | 51 var value = map[annotation]; |
48 if (value == null) throw 'No $annotation found!'; | 52 if (value == null) throw 'No $annotation found!'; |
49 return value; | 53 return value; |
50 } | 54 } |
51 | 55 |
52 forEach(fn(K, Type)) { | 56 void forEach(fn(K, Type)) { |
53 map.forEach((annotation, types) { | 57 map.forEach((annotation, types) { |
54 types.forEach((type) { | 58 types.forEach((type) { |
55 fn(annotation, type); | 59 fn(annotation, type); |
56 }); | 60 }); |
57 }); | 61 }); |
58 } | 62 } |
59 | 63 |
60 List<K> annotationsFor(Type type) { | 64 List<K> annotationsFor(Type type) { |
61 var res = <K>[]; | 65 var res = <K>[]; |
62 forEach((ann, annType) { | 66 forEach((ann, annType) { |
63 if (annType == type) res.add(ann); | 67 if (annType == type) res.add(ann); |
64 }); | 68 }); |
65 return res; | 69 return res; |
66 } | 70 } |
67 } | 71 } |
68 | 72 |
69 | 73 abstract class MetadataExtractor { |
70 @NgInjectableService() | 74 Iterable call(Type type); |
71 class MetadataExtractor { | |
72 Iterable call(Type type) { | |
73 if (reflectType(type) is TypedefMirror) return []; | |
74 var metadata = reflectClass(type).metadata; | |
75 if (metadata == null) return []; | |
76 return metadata.map((InstanceMirror im) => im.reflectee); | |
77 } | |
78 } | 75 } |
OLD | NEW |