|
19 | 19 | /** |
20 | 20 | * This allows data loaders to be registered together into a single place, so |
21 | 21 | * they can be dispatched as one. It also allows you to retrieve data loaders by |
22 | | - * name from a central place |
| 22 | + * name from a central place. |
| 23 | + * <p> |
| 24 | + * Notes on {@link DataLoaderInstrumentation} : A {@link DataLoaderRegistry} can have an instrumentation |
| 25 | + * associated with it. As each {@link DataLoader} is added to the registry, the {@link DataLoaderInstrumentation} |
| 26 | + * of the registry is applied to that {@link DataLoader}. |
| 27 | + * <p> |
| 28 | + * The {@link DataLoader} is changed and hence the object in the registry is not the |
| 29 | + * same one as was originally registered. So you MUST get access to the {@link DataLoader} via {@link DataLoaderRegistry#getDataLoader(String)} methods |
| 30 | + * and not use the original {@link DataLoader} object. |
| 31 | + * <p> |
| 32 | + * If the {@link DataLoader} has no {@link DataLoaderInstrumentation} then the registry one is added to it. If it does have one already |
| 33 | + * then a {@link ChainedDataLoaderInstrumentation} is created with the registry {@link DataLoaderInstrumentation} in it first and then any other |
| 34 | + * {@link DataLoaderInstrumentation}s added after that. |
23 | 35 | */ |
24 | 36 | @PublicApi |
25 | 37 | public class DataLoaderRegistry { |
26 | | - protected final Map<String, DataLoader<?, ?>> dataLoaders = new ConcurrentHashMap<>(); |
| 38 | + protected final Map<String, DataLoader<?, ?>> dataLoaders; |
27 | 39 | protected final DataLoaderInstrumentation instrumentation; |
28 | 40 |
|
29 | 41 |
|
30 | 42 | public DataLoaderRegistry() { |
31 | | - instrumentation = null; |
| 43 | + this(new ConcurrentHashMap<>(),null); |
32 | 44 | } |
33 | 45 |
|
34 | 46 | private DataLoaderRegistry(Builder builder) { |
35 | | - instrument(builder.instrumentation, builder.dataLoaders); |
36 | | - this.instrumentation = builder.instrumentation; |
| 47 | + this(builder.dataLoaders,builder.instrumentation); |
37 | 48 | } |
38 | 49 |
|
39 | | - private void instrument(DataLoaderInstrumentation registryInstrumentation, Map<String, DataLoader<?, ?>> incomingDataLoaders) { |
40 | | - this.dataLoaders.putAll(incomingDataLoaders); |
| 50 | + protected DataLoaderRegistry(Map<String, DataLoader<?, ?>> dataLoaders, DataLoaderInstrumentation instrumentation ) { |
| 51 | + this.dataLoaders = instrumentDLs(dataLoaders, instrumentation); |
| 52 | + this.instrumentation = instrumentation; |
| 53 | + } |
| 54 | + |
| 55 | + private Map<String, DataLoader<?, ?>> instrumentDLs(Map<String, DataLoader<?, ?>> incomingDataLoaders, DataLoaderInstrumentation registryInstrumentation) { |
| 56 | + Map<String, DataLoader<?, ?>> dataLoaders = new ConcurrentHashMap<>(incomingDataLoaders); |
41 | 57 | if (registryInstrumentation != null) { |
42 | | - this.dataLoaders.replaceAll((k, existingDL) -> instrumentDL(registryInstrumentation, existingDL)); |
| 58 | + dataLoaders.replaceAll((k, existingDL) -> instrumentDL(registryInstrumentation, existingDL)); |
43 | 59 | } |
| 60 | + return dataLoaders; |
44 | 61 | } |
45 | 62 |
|
46 | 63 | /** |
|
0 commit comments