-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #208 from inversify/feat/add-container
Add container
- Loading branch information
Showing
19 changed files
with
1,693 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@inversifyjs/container": minor | ||
--- | ||
|
||
Added `Container`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@inversifyjs/container": minor | ||
--- | ||
|
||
Added `InversifyContainerError`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@inversifyjs/container": minor | ||
--- | ||
|
||
Added `ContainerModule`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@inversifyjs/container": minor | ||
--- | ||
|
||
Added `BindToFluentSyntax`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
packages/container/libraries/container/src/container/actions/getContainerModuleId.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { afterAll, beforeAll, describe, expect, it, jest } from '@jest/globals'; | ||
|
||
jest.mock('@inversifyjs/reflect-metadata-utils'); | ||
|
||
import { | ||
getReflectMetadata, | ||
setReflectMetadata, | ||
updateReflectMetadata, | ||
} from '@inversifyjs/reflect-metadata-utils'; | ||
|
||
import { getContainerModuleId } from './getContainerModuleId'; | ||
|
||
describe(getContainerModuleId.name, () => { | ||
describe('when called, and getReflectMetadata() returns undefined', () => { | ||
let result: unknown; | ||
|
||
beforeAll(() => { | ||
( | ||
getReflectMetadata as jest.Mock<typeof getReflectMetadata> | ||
).mockReturnValueOnce(0); | ||
|
||
result = getContainerModuleId(); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should call getReflectMetadata()', () => { | ||
expect(getReflectMetadata).toHaveBeenCalledTimes(1); | ||
expect(getReflectMetadata).toHaveBeenCalledWith( | ||
Object, | ||
'@inversifyjs/container/bindingId', | ||
); | ||
}); | ||
|
||
it('should call updateReflectMetadata()', () => { | ||
expect(updateReflectMetadata).toHaveBeenCalledTimes(1); | ||
expect(updateReflectMetadata).toHaveBeenCalledWith( | ||
Object, | ||
'@inversifyjs/container/bindingId', | ||
expect.any(Function), | ||
expect.any(Function), | ||
); | ||
}); | ||
|
||
it('should return default id', () => { | ||
expect(result).toBe(0); | ||
}); | ||
}); | ||
|
||
describe('when called, and getReflectMetadata() returns Number.MAX_SAFE_INTEGER', () => { | ||
let result: unknown; | ||
|
||
beforeAll(() => { | ||
( | ||
getReflectMetadata as jest.Mock<typeof getReflectMetadata> | ||
).mockReturnValueOnce(Number.MAX_SAFE_INTEGER); | ||
|
||
result = getContainerModuleId(); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should call getReflectMetadata()', () => { | ||
expect(getReflectMetadata).toHaveBeenCalledTimes(1); | ||
expect(getReflectMetadata).toHaveBeenCalledWith( | ||
Object, | ||
'@inversifyjs/container/bindingId', | ||
); | ||
}); | ||
|
||
it('should call setReflectMetadata()', () => { | ||
expect(setReflectMetadata).toHaveBeenCalledTimes(1); | ||
expect(setReflectMetadata).toHaveBeenCalledWith( | ||
Object, | ||
'@inversifyjs/container/bindingId', | ||
Number.MIN_SAFE_INTEGER, | ||
); | ||
}); | ||
|
||
it('should return expected result', () => { | ||
expect(result).toBe(Number.MAX_SAFE_INTEGER); | ||
}); | ||
}); | ||
}); |
25 changes: 25 additions & 0 deletions
25
packages/container/libraries/container/src/container/actions/getContainerModuleId.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { | ||
getReflectMetadata, | ||
setReflectMetadata, | ||
updateReflectMetadata, | ||
} from '@inversifyjs/reflect-metadata-utils'; | ||
|
||
const ID_METADATA: string = '@inversifyjs/container/bindingId'; | ||
|
||
export function getContainerModuleId(): number { | ||
const bindingId: number = | ||
getReflectMetadata<number>(Object, ID_METADATA) ?? 0; | ||
|
||
if (bindingId === Number.MAX_SAFE_INTEGER) { | ||
setReflectMetadata(Object, ID_METADATA, Number.MIN_SAFE_INTEGER); | ||
} else { | ||
updateReflectMetadata( | ||
Object, | ||
ID_METADATA, | ||
() => bindingId, | ||
(id: number) => id + 1, | ||
); | ||
} | ||
|
||
return bindingId; | ||
} |
50 changes: 50 additions & 0 deletions
50
packages/container/libraries/container/src/container/models/ContainerModule.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { beforeAll, describe, expect, it, jest } from '@jest/globals'; | ||
|
||
jest.mock('../actions/getContainerModuleId'); | ||
|
||
import { getContainerModuleId } from '../actions/getContainerModuleId'; | ||
import { ContainerModule, ContainerModuleLoadOptions } from './ContainerModule'; | ||
|
||
describe(ContainerModule.name, () => { | ||
let containerModuleIdfixture: number; | ||
let loadMock: jest.Mock< | ||
(options: ContainerModuleLoadOptions) => Promise<void> | ||
>; | ||
|
||
beforeAll(() => { | ||
containerModuleIdfixture = 1; | ||
loadMock = jest.fn(); | ||
|
||
( | ||
getContainerModuleId as jest.Mock<typeof getContainerModuleId> | ||
).mockReturnValue(containerModuleIdfixture); | ||
}); | ||
|
||
describe('.id', () => { | ||
describe('when called', () => { | ||
let result: unknown; | ||
|
||
beforeAll(() => { | ||
result = new ContainerModule(loadMock).id; | ||
}); | ||
|
||
it('should return expected value', () => { | ||
expect(result).toBe(containerModuleIdfixture); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('.load', () => { | ||
describe('when called', () => { | ||
let result: unknown; | ||
|
||
beforeAll(() => { | ||
result = new ContainerModule(loadMock).load; | ||
}); | ||
|
||
it('should return expected value', () => { | ||
expect(result).toBe(loadMock); | ||
}); | ||
}); | ||
}); | ||
}); |
41 changes: 41 additions & 0 deletions
41
packages/container/libraries/container/src/container/models/ContainerModule.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { ServiceIdentifier } from '@inversifyjs/common'; | ||
import { BindingActivation, BindingDeactivation } from '@inversifyjs/core'; | ||
|
||
import { BindToFluentSyntax } from '../../binding/models/BindingFluentSyntax'; | ||
import { getContainerModuleId } from '../actions/getContainerModuleId'; | ||
import { IsBoundOptions } from './isBoundOptions'; | ||
|
||
export interface ContainerModuleLoadOptions { | ||
bind<T>(serviceIdentifier: ServiceIdentifier<T>): BindToFluentSyntax<T>; | ||
isBound( | ||
serviceIdentifier: ServiceIdentifier, | ||
options?: IsBoundOptions, | ||
): boolean; | ||
onActivation<T>( | ||
serviceIdentifier: ServiceIdentifier<T>, | ||
activation: BindingActivation<T>, | ||
): void; | ||
onDeactivation<T>( | ||
serviceIdentifier: ServiceIdentifier<T>, | ||
deactivation: BindingDeactivation<T>, | ||
): void; | ||
unbind(serviceIdentifier: ServiceIdentifier): Promise<void>; | ||
} | ||
|
||
export class ContainerModule { | ||
readonly #id: number; | ||
readonly #load: (options: ContainerModuleLoadOptions) => Promise<void>; | ||
|
||
constructor(load: (options: ContainerModuleLoadOptions) => Promise<void>) { | ||
this.#id = getContainerModuleId(); | ||
this.#load = load; | ||
} | ||
|
||
public get id(): number { | ||
return this.#id; | ||
} | ||
|
||
public get load(): (options: ContainerModuleLoadOptions) => Promise<void> { | ||
return this.#load; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
packages/container/libraries/container/src/container/models/isBoundOptions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { GetOptionsTagConstraint, MetadataName } from '@inversifyjs/core'; | ||
|
||
export interface IsBoundOptions { | ||
name?: MetadataName; | ||
tag?: GetOptionsTagConstraint; | ||
} |
Oops, something went wrong.