-
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 #202 from inversify/feat/add-resolve-binding-deact…
…ivation Add resolveBindingDeactivations
- Loading branch information
Showing
5 changed files
with
336 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@inversifyjs/core": minor | ||
--- | ||
|
||
Added `resolveBindingDeactivations`. |
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
247 changes: 247 additions & 0 deletions
247
packages/container/libraries/core/src/resolution/actions/resolveBindingDeactivations.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,247 @@ | ||
import { afterAll, beforeAll, describe, expect, it, jest } from '@jest/globals'; | ||
|
||
import { ServiceIdentifier } from '@inversifyjs/common'; | ||
|
||
import { BindingDeactivation } from '../../binding/models/BindingDeactivation'; | ||
import { DeactivationParams } from '../models/DeactivationParams'; | ||
import { resolveBindingDeactivations } from './resolveBindingDeactivations'; | ||
|
||
describe(resolveBindingDeactivations.name, () => { | ||
describe('having a non promise value', () => { | ||
let paramsMock: jest.Mocked<DeactivationParams>; | ||
let serviceIdentifierFixture: ServiceIdentifier; | ||
let valueFixture: unknown; | ||
|
||
beforeAll(() => { | ||
paramsMock = { | ||
getDeactivations: jest.fn(), | ||
}; | ||
serviceIdentifierFixture = 'service-id'; | ||
valueFixture = Symbol(); | ||
}); | ||
|
||
describe('when called, and params.getActivations() returns undefined', () => { | ||
let result: unknown; | ||
|
||
beforeAll(() => { | ||
result = resolveBindingDeactivations( | ||
paramsMock, | ||
serviceIdentifierFixture, | ||
valueFixture, | ||
); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should call params.getActivations', () => { | ||
expect(paramsMock.getDeactivations).toHaveBeenCalledTimes(1); | ||
expect(paramsMock.getDeactivations).toHaveBeenCalledWith( | ||
serviceIdentifierFixture, | ||
); | ||
}); | ||
|
||
it('should return undefined', () => { | ||
expect(result).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe('when called, and params.getActivations() returns sync activations', () => { | ||
let deactivationMock: jest.Mock<BindingDeactivation>; | ||
|
||
let result: unknown; | ||
|
||
beforeAll(() => { | ||
deactivationMock = jest.fn(); | ||
deactivationMock.mockReturnValueOnce(undefined); | ||
|
||
paramsMock.getDeactivations.mockReturnValueOnce([deactivationMock]); | ||
|
||
result = resolveBindingDeactivations( | ||
paramsMock, | ||
serviceIdentifierFixture, | ||
valueFixture, | ||
); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should call params.getActivations', () => { | ||
expect(paramsMock.getDeactivations).toHaveBeenCalledTimes(1); | ||
expect(paramsMock.getDeactivations).toHaveBeenCalledWith( | ||
serviceIdentifierFixture, | ||
); | ||
}); | ||
|
||
it('should call activation', () => { | ||
expect(deactivationMock).toHaveBeenCalledTimes(1); | ||
expect(deactivationMock).toHaveBeenCalledWith(valueFixture); | ||
}); | ||
|
||
it('should return undefined', () => { | ||
expect(result).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe('when called, and params.getActivations() returns async activations', () => { | ||
let deactivationMock: jest.Mock<BindingDeactivation>; | ||
|
||
let result: unknown; | ||
|
||
beforeAll(async () => { | ||
deactivationMock = jest.fn(); | ||
deactivationMock.mockReturnValueOnce(Promise.resolve(undefined)); | ||
|
||
paramsMock.getDeactivations.mockReturnValueOnce([deactivationMock]); | ||
|
||
result = await resolveBindingDeactivations( | ||
paramsMock, | ||
serviceIdentifierFixture, | ||
valueFixture, | ||
); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should call params.getActivations', () => { | ||
expect(paramsMock.getDeactivations).toHaveBeenCalledTimes(1); | ||
expect(paramsMock.getDeactivations).toHaveBeenCalledWith( | ||
serviceIdentifierFixture, | ||
); | ||
}); | ||
|
||
it('should call activation', () => { | ||
expect(deactivationMock).toHaveBeenCalledTimes(1); | ||
expect(deactivationMock).toHaveBeenCalledWith(valueFixture); | ||
}); | ||
|
||
it('should return undefined', () => { | ||
expect(result).toBeUndefined(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('having a promise value', () => { | ||
let paramsMock: jest.Mocked<DeactivationParams>; | ||
let serviceIdentifierFixture: ServiceIdentifier; | ||
let valueFixture: unknown; | ||
|
||
beforeAll(() => { | ||
paramsMock = { | ||
getDeactivations: jest.fn(), | ||
}; | ||
serviceIdentifierFixture = 'service-id'; | ||
valueFixture = Symbol(); | ||
}); | ||
|
||
describe('when called, and params.getActivations() returns undefined', () => { | ||
let result: unknown; | ||
|
||
beforeAll(async () => { | ||
result = await resolveBindingDeactivations( | ||
paramsMock, | ||
serviceIdentifierFixture, | ||
Promise.resolve(valueFixture), | ||
); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should call params.getActivations', () => { | ||
expect(paramsMock.getDeactivations).toHaveBeenCalledTimes(1); | ||
expect(paramsMock.getDeactivations).toHaveBeenCalledWith( | ||
serviceIdentifierFixture, | ||
); | ||
}); | ||
|
||
it('should return undefined', () => { | ||
expect(result).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe('when called, and params.getActivations() returns sync activations', () => { | ||
let deactivationMock: jest.Mock<BindingDeactivation>; | ||
|
||
let result: unknown; | ||
|
||
beforeAll(async () => { | ||
deactivationMock = jest.fn(); | ||
deactivationMock.mockReturnValueOnce(undefined); | ||
|
||
paramsMock.getDeactivations.mockReturnValueOnce([deactivationMock]); | ||
|
||
result = await resolveBindingDeactivations( | ||
paramsMock, | ||
serviceIdentifierFixture, | ||
Promise.resolve(valueFixture), | ||
); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should call params.getActivations', () => { | ||
expect(paramsMock.getDeactivations).toHaveBeenCalledTimes(1); | ||
expect(paramsMock.getDeactivations).toHaveBeenCalledWith( | ||
serviceIdentifierFixture, | ||
); | ||
}); | ||
|
||
it('should call activation', () => { | ||
expect(deactivationMock).toHaveBeenCalledTimes(1); | ||
expect(deactivationMock).toHaveBeenCalledWith(valueFixture); | ||
}); | ||
|
||
it('should return undefined', () => { | ||
expect(result).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe('when called, and params.getActivations() returns async activations', () => { | ||
let deactivationMock: jest.Mock<BindingDeactivation>; | ||
|
||
let result: unknown; | ||
|
||
beforeAll(async () => { | ||
deactivationMock = jest.fn(); | ||
deactivationMock.mockReturnValueOnce(Promise.resolve(undefined)); | ||
|
||
paramsMock.getDeactivations.mockReturnValueOnce([deactivationMock]); | ||
|
||
result = await resolveBindingDeactivations( | ||
paramsMock, | ||
serviceIdentifierFixture, | ||
Promise.resolve(valueFixture), | ||
); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should call params.getActivations', () => { | ||
expect(paramsMock.getDeactivations).toHaveBeenCalledTimes(1); | ||
expect(paramsMock.getDeactivations).toHaveBeenCalledWith( | ||
serviceIdentifierFixture, | ||
); | ||
}); | ||
|
||
it('should call activation', () => { | ||
expect(deactivationMock).toHaveBeenCalledTimes(1); | ||
expect(deactivationMock).toHaveBeenCalledWith(valueFixture); | ||
}); | ||
|
||
it('should return undefined', () => { | ||
expect(result).toBeUndefined(); | ||
}); | ||
}); | ||
}); | ||
}); |
71 changes: 71 additions & 0 deletions
71
packages/container/libraries/core/src/resolution/actions/resolveBindingDeactivations.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,71 @@ | ||
import { ServiceIdentifier } from '@inversifyjs/common'; | ||
|
||
import { BindingDeactivation } from '../../binding/models/BindingDeactivation'; | ||
import { isPromise } from '../../common/calculations/isPromise'; | ||
import { DeactivationParams } from '../models/DeactivationParams'; | ||
import { Resolved, SyncResolved } from '../models/Resolved'; | ||
|
||
export function resolveBindingDeactivations<TActivated>( | ||
params: DeactivationParams, | ||
serviceIdentifier: ServiceIdentifier<TActivated>, | ||
value: Resolved<TActivated>, | ||
): void | Promise<void> { | ||
const activations: Iterable<BindingDeactivation<TActivated>> | undefined = | ||
params.getDeactivations(serviceIdentifier); | ||
|
||
if (activations === undefined) { | ||
return undefined; | ||
} | ||
|
||
if (isPromise(value)) { | ||
return resolveBindingDeactivationsFromIteratorAsync( | ||
value, | ||
activations[Symbol.iterator](), | ||
); | ||
} | ||
|
||
return resolveBindingDeactivationsFromIterator( | ||
value, | ||
activations[Symbol.iterator](), | ||
); | ||
} | ||
|
||
function resolveBindingDeactivationsFromIterator<TActivated>( | ||
value: SyncResolved<TActivated>, | ||
deactivationsIterator: Iterator<BindingDeactivation<TActivated>>, | ||
): void | Promise<void> { | ||
let deactivationIteratorResult: IteratorResult< | ||
BindingDeactivation<TActivated> | ||
> = deactivationsIterator.next(); | ||
|
||
while (deactivationIteratorResult.done !== true) { | ||
const nextDeactivationValue: void | Promise<void> = | ||
deactivationIteratorResult.value(value); | ||
|
||
if (isPromise(nextDeactivationValue)) { | ||
return resolveBindingDeactivationsFromIteratorAsync( | ||
value, | ||
deactivationsIterator, | ||
); | ||
} | ||
|
||
deactivationIteratorResult = deactivationsIterator.next(); | ||
} | ||
} | ||
|
||
async function resolveBindingDeactivationsFromIteratorAsync<TActivated>( | ||
value: Resolved<TActivated>, | ||
deactivationsIterator: Iterator<BindingDeactivation<TActivated>>, | ||
): Promise<void> { | ||
const resolvedValue: SyncResolved<TActivated> = await value; | ||
|
||
let deactivationIteratorResult: IteratorResult< | ||
BindingDeactivation<TActivated> | ||
> = deactivationsIterator.next(); | ||
|
||
while (deactivationIteratorResult.done !== true) { | ||
await deactivationIteratorResult.value(resolvedValue); | ||
|
||
deactivationIteratorResult = deactivationsIterator.next(); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/container/libraries/core/src/resolution/models/DeactivationParams.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,9 @@ | ||
import { ServiceIdentifier } from '@inversifyjs/common'; | ||
|
||
import { BindingDeactivation } from '../../binding/models/BindingDeactivation'; | ||
|
||
export interface DeactivationParams { | ||
getDeactivations: <TActivated>( | ||
serviceIdentifier: ServiceIdentifier<TActivated>, | ||
) => Iterable<BindingDeactivation<TActivated>> | undefined; | ||
} |