@contember/dic 0.12.0-alpha.0 → 0.12.0-alpha.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contember/dic",
3
- "version": "0.12.0-alpha.0",
3
+ "version": "0.12.0-alpha.12",
4
4
  "license": "Apache-2.0",
5
5
  "main": "dist/src/index.js",
6
6
  "typings": "dist/src/index.d.ts",
package/src/Container.ts CHANGED
@@ -4,37 +4,43 @@ export type ServiceType = any
4
4
  export type ServiceTypeMap = { [N in ServiceName]: ServiceType | undefined }
5
5
 
6
6
  export type ServiceFactory<M extends ServiceTypeMap, T> = (accessors: Readonly<M>) => T
7
- export type ServiceSetup<M extends ServiceTypeMap, T> = (service: T, accessors: Readonly<M>) => T
7
+ export type ServiceSetup<M extends ServiceTypeMap, T> = (service: T, accessors: Readonly<M>) => void
8
8
 
9
- export type ServiceFactoryMap<M extends ServiceTypeMap> = { [N in keyof M]: ServiceFactory<M, M[N]> }
9
+ export interface ServiceDefinition<M extends ServiceTypeMap, T> {
10
+ factory: ServiceFactory<M, T>
11
+ setup: ServiceSetup<any, T>[]
12
+ }
13
+
14
+ export type ServiceDefinitionMap<M extends ServiceTypeMap> = { [N in keyof M]: ServiceDefinition<M, M[N]> }
10
15
 
11
16
  export class Builder<M extends ServiceTypeMap = {}> {
12
- constructor(private factories: ServiceFactoryMap<M>) {}
17
+ constructor(private factories: ServiceDefinitionMap<M>) {}
13
18
 
14
19
  addService<N extends ServiceName, T extends ServiceType>(
15
20
  name: N extends keyof M ? 'Service with this name already exists' : N,
16
- factory: ServiceFactory<M, T>,
21
+ factory: ServiceFactory<M, T> | ServiceDefinition<M, T>,
17
22
  ): Builder<M & { [K in N]: T }> {
18
- type TypeMapA = M
19
- type TypeMapB = { [K in N]: T }
20
- type TypeMapC = TypeMapA & TypeMapB
21
-
22
- type FactoryMapA = ServiceFactoryMap<TypeMapA>
23
- type FactoryMapB = ServiceFactoryMap<TypeMapB>
24
- type FactoryMapC = ServiceFactoryMap<TypeMapC>
25
-
26
- const factoryMapA: FactoryMapA = this.factories
27
- const factoryMapB: FactoryMapB = { [name]: factory } as FactoryMapB
28
- const factoryMapC: FactoryMapC = { ...factoryMapA, ...factoryMapB }
29
-
30
- return new Builder(factoryMapC)
23
+ return new Builder({
24
+ ...this.factories,
25
+ [name]: 'factory' in factory ? factory : { factory, setup: [] },
26
+ } as ServiceDefinitionMap<M & { [K in N]: T }>)
31
27
  }
32
28
 
33
29
  replaceService<N extends ServiceName, T extends { [P in keyof M[N]]: M[N][P] }>(
34
30
  name: N extends keyof M ? N : 'Service with this name does not exist',
35
- factory: ServiceFactory<M, T>,
31
+ factory: ServiceFactory<M, T> | ServiceDefinition<M, T>,
36
32
  ): Builder<M> {
37
- return new Builder({ ...this.factories, [name]: factory } as ServiceFactoryMap<M>)
33
+ const currentFactory = this.factories[name]
34
+ return new Builder({
35
+ ...this.factories,
36
+ [name]:
37
+ 'factory' in factory
38
+ ? factory
39
+ : {
40
+ ...currentFactory,
41
+ factory,
42
+ },
43
+ } as ServiceDefinitionMap<M>)
38
44
  }
39
45
 
40
46
  setupService<N extends ServiceName, T extends { [P in keyof M[N]]: M[N][P] }>(
@@ -44,11 +50,11 @@ export class Builder<M extends ServiceTypeMap = {}> {
44
50
  const currentFactory = this.factories[name]
45
51
  return new Builder({
46
52
  ...this.factories,
47
- [name]: accessor => {
48
- const service = currentFactory(accessor)
49
- return setup(service, accessor)
53
+ [name]: {
54
+ ...currentFactory,
55
+ setup: [...currentFactory.setup, setup],
50
56
  },
51
- } as ServiceFactoryMap<M>)
57
+ } as ServiceDefinitionMap<M>)
52
58
  }
53
59
 
54
60
  build(): Container<M> {
@@ -57,13 +63,14 @@ export class Builder<M extends ServiceTypeMap = {}> {
57
63
  }
58
64
 
59
65
  export type Container<M extends ServiceTypeMap> = ContainerImpl<M> & { [K in keyof M]: M[K] }
66
+ type ServiceRegistry<M extends ServiceTypeMap> = Map<keyof M, ServiceType>
60
67
 
61
68
  export class ContainerImpl<M extends ServiceTypeMap> {
62
- private readonly services: Partial<M> = {}
69
+ private readonly services: ServiceRegistry<M> = new Map()
63
70
  private readonly accessors: Readonly<M> = {} as any
64
71
 
65
- constructor(private factories: ServiceFactoryMap<M>) {
66
- Object.keys(this.factories).forEach(name => {
72
+ constructor(private definitions: ServiceDefinitionMap<M>) {
73
+ Object.keys(this.definitions).forEach(name => {
67
74
  Object.defineProperty(this.accessors, name, {
68
75
  get: this.get.bind(this, name),
69
76
  })
@@ -74,26 +81,30 @@ export class ContainerImpl<M extends ServiceTypeMap> {
74
81
  })
75
82
  }
76
83
 
77
- getFactories(): ServiceFactoryMap<M> {
78
- return this.factories
84
+ getDefinitions(): ServiceDefinitionMap<M> {
85
+ return this.definitions
79
86
  }
80
87
 
81
88
  get<N extends keyof M>(name: N): M[N] {
82
- const service: M[N] | undefined = this.services[name]
83
-
84
- if (service === undefined) {
85
- return (this.services[name] = this.factories[name](this.accessors))
89
+ const existingService: M[N] | undefined = this.services.get(name)
90
+ if (existingService !== undefined) {
91
+ return existingService
92
+ }
93
+ const definition = this.definitions[name]
94
+ const service = definition.factory(this.accessors)
95
+ this.services.set(name, service)
96
+ for (const setup of definition.setup) {
97
+ setup(service, this.accessors)
86
98
  }
87
-
88
99
  return service
89
100
  }
90
101
 
91
102
  pick<N extends keyof M>(...names: N[]): Container<{ [K in N]: M[K] }> {
92
- const factories: Partial<ServiceFactoryMap<{ [K in N]: M[K] }>> = {}
103
+ const factories: Partial<ServiceDefinitionMap<{ [K in N]: M[K] }>> = {}
93
104
  for (const name of names) {
94
- factories[name] = (() => this.get(name)) as any
105
+ factories[name] = { factory: () => this.get(name), setup: [] }
95
106
  }
96
- return new ContainerImpl(factories as ServiceFactoryMap<{ [K in N]: M[K] }>) as Container<{ [K in N]: M[K] }>
107
+ return new ContainerImpl(factories as ServiceDefinitionMap<{ [K in N]: M[K] }>) as Container<{ [K in N]: M[K] }>
97
108
  }
98
109
  }
99
110
 
@@ -102,7 +113,7 @@ export function mergeContainers<M1 extends ServiceTypeMap, M2 extends ServiceTyp
102
113
  containerB: Container<M2>,
103
114
  ): Container<M1 & M2> {
104
115
  return new ContainerImpl({
105
- ...containerA.getFactories(),
106
- ...containerB.getFactories(),
107
- } as ServiceFactoryMap<M1 & M2>) as Container<M1 & M2>
116
+ ...containerA.getDefinitions(),
117
+ ...containerB.getDefinitions(),
118
+ } as ServiceDefinitionMap<M1 & M2>) as Container<M1 & M2>
108
119
  }