-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(inversify-code-examples): add bindingWhenSyntaxApiWhen example
- Loading branch information
1 parent
105fdd1
commit 2476337
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
...s/docs/tools/inversify-code-examples/src/examples/v7/bindingWhenSyntaxApiWhen.int.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,9 @@ | ||
import { describe, expect, it } from '@jest/globals'; | ||
|
||
import { ninjaDamage } from './bindingWhenSyntaxApiWhen'; | ||
|
||
describe('BindingWhenSyntax API (when)', () => { | ||
it('should bind right ninja weapon', () => { | ||
expect(ninjaDamage).toBe(5); | ||
}); | ||
}); |
61 changes: 61 additions & 0 deletions
61
packages/docs/tools/inversify-code-examples/src/examples/v7/bindingWhenSyntaxApiWhen.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,61 @@ | ||
import { | ||
BindingMetadata, | ||
Container, | ||
inject, | ||
injectable, | ||
named, | ||
} from 'inversify7'; | ||
|
||
interface Weapon { | ||
damage: number; | ||
} | ||
|
||
export class Katana implements Weapon { | ||
public readonly damage: number = 10; | ||
} | ||
|
||
export class Shuriken implements Weapon { | ||
public readonly damage: number = 5; | ||
} | ||
|
||
const container: Container = new Container(); | ||
|
||
// Begin-example | ||
const ninjaId: symbol = Symbol.for('Ninja'); | ||
const weaponId: symbol = Symbol.for('Weapon'); | ||
|
||
@injectable() | ||
class Ninja { | ||
constructor( | ||
@inject(weaponId) | ||
@named('shuriken') | ||
public readonly weapon: Weapon, | ||
) {} | ||
} | ||
|
||
container.bind<Ninja>(ninjaId).to(Ninja); | ||
|
||
const whenTargetNamedConstraint: ( | ||
name: string, | ||
) => (bindingMetadata: BindingMetadata) => boolean = | ||
(name: string) => | ||
(bindingMetadata: BindingMetadata): boolean => | ||
bindingMetadata.name === name; | ||
|
||
container | ||
.bind<Weapon>(weaponId) | ||
.to(Katana) | ||
.when(whenTargetNamedConstraint('katana')); | ||
|
||
container | ||
.bind<Weapon>(weaponId) | ||
.to(Shuriken) | ||
.when(whenTargetNamedConstraint('shuriken')); | ||
|
||
const ninja: Ninja = container.get(ninjaId); | ||
|
||
// Returns 5 | ||
const ninjaDamage: number = ninja.weapon.damage; | ||
// End-example | ||
|
||
export { ninjaDamage }; |