container.ts
106 lines
| 2.5 KiB
| video/mp2t
|
TypeScriptLexer
|
|
r1 | /* eslint max-classes-per-file: ["error", 20] */ | |
| import { describe, it } from "mocha"; | |||
|
|
r4 | import { Container } from "../Container"; | |
|
|
r8 | import { ContainerBuilder } from "../ContainerBuilder"; | |
|
|
r13 | import { ContainerServices, DepsMap, IContainerBuilder, Refs, Resolver, ServiceLocator } from "../interfaces"; | |
|
|
r1 | import { fluent } from "../traits"; | |
| class Foo { | |||
| foo = "foo"; | |||
| } | |||
| class Bar { | |||
| bar = "bar"; | |||
|
|
r4 | constructor(foo?: () => Foo) { } | |
|
|
r1 | } | |
| interface Services { | |||
| foo: Foo; | |||
| bar?: Bar; | |||
| baz: Foo; | |||
|
|
r8 | ||
|
|
r9 | box?: Foo; | |
| //container: string; | |||
|
|
r1 | } | |
| interface ServicesB { | |||
| // will give errors | |||
| // baz: Bar; | |||
| baz: Foo; | |||
| zoo?: Foo; | |||
| } | |||
|
|
r7 | declare const resolver: Resolver<Services>; | |
|
|
r9 | const foo = resolver("foo", { lazy: true, default: null }); | |
|
|
r7 | ||
|
|
r9 | const mmap = <X extends DepsMap<Services>>(m: X) => { }; | |
|
|
r8 | ||
| declare const refs: Refs<Services>; | |||
|
|
r7 | ||
|
|
r8 | if (refs && refs.name === "foo") { | |
| refs.default; | |||
| } | |||
|
|
r13 | declare const x: ServiceLocator<ContainerServices<Services>>; | |
|
|
r8 | ||
|
|
r13 | x.resolve("container").resolve("container"); | |
|
|
r8 | ||
|
|
r7 | ||
| mmap({ | |||
|
|
r9 | fooz: { name: "foo", lazy: false, default: undefined }, | |
| ooz: "bar" | |||
|
|
r7 | }); | |
|
|
r1 | interface SharedServices { | |
| foo: Foo; | |||
| bar?: Bar; | |||
| baz: Bar; | |||
| } | |||
|
|
r9 | const config = fluent<Services>() | |
|
|
r1 | .declare<ServicesB>() | |
| .register({ | |||
|
|
r9 | zoo: it => it.value(new Foo()), | |
|
|
r1 | bar: it => it | |
|
|
r4 | .lifetime("context") // тип активации, время жизни | |
| .wants({ | |||
|
|
r9 | self: "container", | |
| childContainer: "childContainer", | |||
|
|
r7 | bar: "bar", | |
|
|
r9 | foo$: { name: "foo", lazy: true } // отложенная активация, фабричный метод | |
|
|
r4 | }) | |
|
|
r9 | .override({ // переопределение сервиса | |
| box: it => it.factory(() => new Foo()) | |||
|
|
r7 | }) | |
|
|
r9 | .factory(({ foo$, bar, self, childContainer }) => // фабрика получает объект с именованными зависимостями | |
| new Bar(foo$) // создается экземпляр сервиса | |||
|
|
r4 | ), | |
| foo: it => it.factory(() => new Foo()), | |||
|
|
r9 | baz: it => it.value(new Foo()), | |
| //box: it => it.factory(() => new Foo()) | |||
|
|
r1 | }) | |
|
|
r9 | .done(); | |
| declare const container: IContainerBuilder<ContainerServices<Services>, keyof Services>; | |||
|
|
r1 | ||
|
|
r9 | const v = container.build().resolve("foo"); | |
| if (v) { | |||
| // noop | |||
| } | |||
|
|
r5 | const c2 = config.configure(container); | |
|
|
r1 | ||
|
|
r5 | c2.resolve("foo"); | |
|
|
r9 | declare const m: ContainerServices<{ foo?: Foo }>["container"]; | |
|
|
r5 | ||
|
|
r8 | m.resolve("container").resolve("container").resolve("foo"); |
