-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-fetch.ts
29 lines (25 loc) · 917 Bytes
/
service-fetch.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
import {BaseServiceFetchArgs, ServiceFetch} from './service-description';
import {ServiceIdentifier} from './decorators/service-identifier';
export function createServiceFetch<TClass, TServiceFetchArgs extends BaseServiceFetchArgs>({
serviceIdentifier,
serviceCreate,
serviceStore,
isSingleton,
}: {
serviceIdentifier: ServiceIdentifier<TClass>,
serviceCreate: (args: TServiceFetchArgs) => TClass,
serviceStore: Map<ServiceIdentifier<unknown>, unknown>,
isSingleton: boolean
}): ServiceFetch<TClass, TServiceFetchArgs> {
if (!isSingleton) {
return serviceCreate;
}
return (args) => {
let service = serviceStore.get(serviceIdentifier);
if (!service) {
service = serviceCreate(args);
serviceStore.set(serviceIdentifier, service);
}
return service as TClass;
};
}