-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathservice_multi_collection.ts
57 lines (51 loc) · 1.71 KB
/
service_multi_collection.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { resolve } from "./resolution.ts";
import { ServiceIdent, ServiceStore } from "./service.ts";
import { IServiceCollection, ServiceCollection } from "./service_collection.ts";
/**
* Provides a mechanism to allow resolution of services from multiple
* collections as-if they were one.
*/
export class ServiceMultiCollection implements IServiceCollection {
private _serviceCollections: Set<ServiceCollection>;
public constructor(...serviceCollections: ServiceCollection[]) {
this._serviceCollections = new Set(serviceCollections);
}
/**
* Resolves the service uses all the service collections currently
* held.
*
* @remarks
* Resolution is done in priority order based on collection
* insertion time, which means if 2 collections have the same service,
* the collection that was added first will be the one that the service
* will be resolved from.
*/
public get<T>(ident: ServiceIdent<T>): T {
const serviceStores = Array.from(this._serviceCollections).map((sc) =>
(sc as any)._services as ServiceStore
);
return resolve(ident, serviceStores);
}
/**
* Adds the given collections to the multi-collection in order.
*/
public addCollections(...collections: ServiceCollection[]): void {
for (const collection of collections) {
this._serviceCollections.add(collection);
}
}
/**
* Removes the given collections from the multi-collection.
*/
public removeCollections(...collections: ServiceCollection[]): void {
for (const collection of collections) {
this._serviceCollections.delete(collection);
}
}
/**
* Clears all held collections.
*/
public clearCollections(): void {
this._serviceCollections.clear();
}
}