-
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 #184 from inversify/refactor/add-binding-fluent-sy…
…ntax Add binding fluent syntax
- Loading branch information
Showing
9 changed files
with
1,424 additions
and
0 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
88 changes: 88 additions & 0 deletions
88
packages/container/libraries/container/src/binding/actions/getBindingId.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, | ||
updateReflectMetadata, | ||
} from '@inversifyjs/reflect-metadata-utils'; | ||
|
||
import { getBindingId } from './getBindingId'; | ||
|
||
describe(getBindingId.name, () => { | ||
describe('when called, and getReflectMetadata() returns undefined', () => { | ||
let result: unknown; | ||
|
||
beforeAll(() => { | ||
( | ||
getReflectMetadata as jest.Mock<typeof getReflectMetadata> | ||
).mockReturnValueOnce(0); | ||
|
||
result = getBindingId(); | ||
}); | ||
|
||
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', | ||
0, | ||
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 = getBindingId(); | ||
}); | ||
|
||
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', | ||
Number.MAX_SAFE_INTEGER, | ||
expect.any(Function), | ||
); | ||
}); | ||
|
||
it('should return default id', () => { | ||
expect(result).toBe(Number.MAX_SAFE_INTEGER); | ||
}); | ||
}); | ||
}); |
29 changes: 29 additions & 0 deletions
29
packages/container/libraries/container/src/binding/actions/getBindingId.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,29 @@ | ||
import { | ||
getReflectMetadata, | ||
updateReflectMetadata, | ||
} from '@inversifyjs/reflect-metadata-utils'; | ||
|
||
const ID_METADATA: string = '@inversifyjs/container/bindingId'; | ||
|
||
export function getBindingId(): number { | ||
const bindingId: number = | ||
getReflectMetadata<number>(Object, ID_METADATA) ?? 0; | ||
|
||
if (bindingId === Number.MAX_SAFE_INTEGER) { | ||
updateReflectMetadata( | ||
Object, | ||
ID_METADATA, | ||
bindingId, | ||
() => Number.MIN_SAFE_INTEGER, | ||
); | ||
} else { | ||
updateReflectMetadata( | ||
Object, | ||
ID_METADATA, | ||
bindingId, | ||
(id: number) => id + 1, | ||
); | ||
} | ||
|
||
return bindingId; | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/container/libraries/container/src/common/models/Writable.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,3 @@ | ||
export type Writable<T> = { | ||
-readonly [TKey in keyof T]: T[TKey]; | ||
}; |
53 changes: 53 additions & 0 deletions
53
packages/container/libraries/container/src/container/binding/models/BindingFluentSyntax.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,53 @@ | ||
import { Newable, ServiceIdentifier } from '@inversifyjs/common'; | ||
import { | ||
BindingActivation, | ||
BindingDeactivation, | ||
BindingMetadata, | ||
DynamicValueBuilder, | ||
Factory, | ||
Provider, | ||
ResolutionContext, | ||
} from '@inversifyjs/core'; | ||
|
||
export interface BindToFluentSyntax<T> { | ||
to(type: Newable<T>): BindInWhenOnFluentSyntax<T>; | ||
toSelf(): BindInWhenOnFluentSyntax<T>; | ||
toConstantValue(value: T): BindWhenOnFluentSyntax<T>; | ||
toDynamicValue(builder: DynamicValueBuilder<T>): BindInWhenOnFluentSyntax<T>; | ||
toFactory( | ||
factory: T extends Factory<unknown> | ||
? (context: ResolutionContext) => T | ||
: never, | ||
): BindWhenOnFluentSyntax<T>; | ||
toProvider( | ||
provider: T extends Provider<unknown> | ||
? (context: ResolutionContext) => T | ||
: never, | ||
): BindWhenOnFluentSyntax<T>; | ||
toService(service: ServiceIdentifier<T>): void; | ||
} | ||
|
||
export interface BindInFluentSyntax<T> { | ||
inSingletonScope(): BindWhenOnFluentSyntax<T>; | ||
inTransientScope(): BindWhenOnFluentSyntax<T>; | ||
inRequestScope(): BindWhenOnFluentSyntax<T>; | ||
} | ||
|
||
export interface BindInWhenOnFluentSyntax<T> | ||
extends BindInFluentSyntax<T>, | ||
BindWhenOnFluentSyntax<T> {} | ||
|
||
export interface BindOnFluentSyntax<T> { | ||
onActivation(activation: BindingActivation<T>): BindWhenFluentSyntax<T>; | ||
onDeactivation(deactivation: BindingDeactivation<T>): BindWhenFluentSyntax<T>; | ||
} | ||
|
||
export interface BindWhenOnFluentSyntax<T> | ||
extends BindWhenFluentSyntax<T>, | ||
BindOnFluentSyntax<T> {} | ||
|
||
export interface BindWhenFluentSyntax<T> { | ||
when( | ||
constraint: (metadata: BindingMetadata) => boolean, | ||
): BindOnFluentSyntax<T>; | ||
} |
Oops, something went wrong.