Skip to content

Commit

Permalink
feat(core): add and expose resolveServiceDeactivations instead of res…
Browse files Browse the repository at this point in the history
…olveBindingDeactivations
  • Loading branch information
notaphplover committed Dec 22, 2024
1 parent cbd2b27 commit 8dc32c4
Show file tree
Hide file tree
Showing 11 changed files with 545 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .changeset/modern-grapes-fly.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"@inversifyjs/core": minor
---

Added `resolveBindingDeactivations`.
Added `resolveServiceDeactivations`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { beforeAll, describe, expect, it } from '@jest/globals';

import { bindingScopeValues } from '../models/BindingScope';
import { bindingTypeValues } from '../models/BindingType';
import { ConstantValueBinding } from '../models/ConstantValueBinding';
import { ServiceRedirectionBinding } from '../models/ServiceRedirectionBinding';
import { isScopedBinding } from './isScopedBinding';

describe(isScopedBinding.name, () => {
describe('having a ServiceRedirectionBinding', () => {
let serviceRedirectionBindingFixture: ServiceRedirectionBinding<unknown>;

beforeAll(() => {
serviceRedirectionBindingFixture = {
id: 1,
isSatisfiedBy: () => true,
moduleId: undefined,
serviceIdentifier: 'service-id',
targetServiceIdentifier: 'target-service-id',
type: bindingTypeValues.ServiceRedirection,
};
});

describe('when called', () => {
let result: unknown;

beforeAll(() => {
result = isScopedBinding(serviceRedirectionBindingFixture);
});

it('should return false', () => {
expect(result).toBe(false);
});
});
});

describe('having a ConstantValueBinding', () => {
let constantValueBindingFixture: ConstantValueBinding<unknown>;

beforeAll(() => {
constantValueBindingFixture = {
cache: {
isRight: false,
value: undefined,
},
id: 1,
isSatisfiedBy: () => true,
moduleId: undefined,
onActivation: undefined,
onDeactivation: undefined,
scope: bindingScopeValues.Singleton,
serviceIdentifier: 'service-id',
type: bindingTypeValues.ConstantValue,
value: 'foo',
};
});

describe('when called', () => {
let result: unknown;

beforeAll(() => {
result = isScopedBinding(constantValueBindingFixture);
});

it('should return true', () => {
expect(result).toBe(true);
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {
Binding,
BindingScope,
BindingType,
ScopedBinding,
} from '@inversifyjs/core';

export function isScopedBinding<T>(
binding: Binding<T>,
): binding is Binding<T> & ScopedBinding<BindingType, BindingScope, T> {
return (
(binding as Partial<ScopedBinding<BindingType, BindingScope, T>>).scope !==
undefined
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { bindingScopeValues } from '../models/BindingScope';
import { bindingTypeValues } from '../models/BindingType';
import { ConstantValueBinding } from '../models/ConstantValueBinding';

export class ConstantValueBindingFixtures {
public static get any(): ConstantValueBinding<unknown> {
return {
cache: {
isRight: false,
value: undefined,
},
id: 1,
isSatisfiedBy: () => true,
moduleId: undefined,
onActivation: undefined,
onDeactivation: undefined,
scope: bindingScopeValues.Singleton,
serviceIdentifier: 'service-id',
type: bindingTypeValues.ConstantValue,
value: Symbol.for('constant-value-binding-fixture-value'),
};
}

public static get withCacheWithIsRightFalse(): ConstantValueBinding<unknown> {
return {
...ConstantValueBindingFixtures.any,
cache: {
isRight: false,
value: undefined,
},
};
}

public static get withCacheWithIsRightTrue(): ConstantValueBinding<unknown> {
return {
...ConstantValueBindingFixtures.any,
cache: {
isRight: true,
value: Symbol.for('constant-value-binding-fixture-value'),
},
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { bindingScopeValues } from '../models/BindingScope';
import { bindingTypeValues } from '../models/BindingType';
import { InstanceBinding } from '../models/InstanceBinding';

export class InstanceBindingFixtures {
public static get any(): InstanceBinding<unknown> {
return {
cache: {
isRight: false,
value: undefined,
},
id: 1,
implementationType: class Foo {},
isSatisfiedBy: () => true,
moduleId: undefined,
onActivation: undefined,
onDeactivation: undefined,
scope: bindingScopeValues.Singleton,
serviceIdentifier: 'service-id',
type: bindingTypeValues.Instance,
};
}

public static get withCacheWithScopeSingleton(): InstanceBinding<unknown> {
return {
...InstanceBindingFixtures.any,
scope: bindingScopeValues.Singleton,
};
}
}
4 changes: 2 additions & 2 deletions packages/container/libraries/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import { PlanServiceNodeParent } from './planning/models/PlanServiceNodeParent';
import { PlanServiceRedirectionBindingNode } from './planning/models/PlanServiceRedirectionBindingNode';
import { PlanTree } from './planning/models/PlanTree';
import { resolve } from './resolution/actions/resolve';
import { resolveBindingDeactivations } from './resolution/actions/resolveBindingDeactivations';
import { resolveServiceDeactivations } from './resolution/actions/resolveServiceDeactivations';
import { DeactivationParams } from './resolution/models/DeactivationParams';
import { GetOptions } from './resolution/models/GetOptions';
import { GetOptionsTagConstraint } from './resolution/models/GetOptionsTagConstraint';
Expand Down Expand Up @@ -135,7 +135,7 @@ export {
plan,
preDestroy,
resolve,
resolveBindingDeactivations,
resolveServiceDeactivations,
tagged,
unmanaged,
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,18 @@ export class ClassMetadataFixtures {

return fixture;
}

public static get withPreDestroyMethodName(): ClassMetadata {
const fixture: ClassMetadata = {
constructorArguments: [],
lifecycle: {
postConstructMethodName: undefined,
preDestroyMethodName: 'preDestroy',
},
properties: new Map(),
scope: undefined,
};

return fixture;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ describe(resolveBindingDeactivations.name, () => {

beforeAll(() => {
paramsMock = {
getBindings: jest.fn(),
getDeactivations: jest.fn(),
};
} as Partial<
jest.Mocked<DeactivationParams>
> as jest.Mocked<DeactivationParams>;
serviceIdentifierFixture = 'service-id';
valueFixture = Symbol();
});
Expand Down Expand Up @@ -133,8 +136,11 @@ describe(resolveBindingDeactivations.name, () => {

beforeAll(() => {
paramsMock = {
getBindings: jest.fn(),
getDeactivations: jest.fn(),
};
} as Partial<
jest.Mocked<DeactivationParams>
> as jest.Mocked<DeactivationParams>;
serviceIdentifierFixture = 'service-id';
valueFixture = Symbol();
});
Expand Down
Loading

0 comments on commit 8dc32c4

Please sign in to comment.