|
|
/* eslint max-classes-per-file: ["error", 20] */
|
|
|
import { describe, it } from "mocha";
|
|
|
import { Container } from "../Container";
|
|
|
import { ContainerBuilder } from "../ContainerBuilder";
|
|
|
import { ConfigurableKeys, ContainerProvided, ContainerServices, DepsMap, Refs, Resolver } from "../interfaces";
|
|
|
import { fluent } from "../traits";
|
|
|
|
|
|
class Foo {
|
|
|
foo = "foo";
|
|
|
}
|
|
|
|
|
|
class Bar {
|
|
|
bar = "bar";
|
|
|
|
|
|
constructor(foo?: () => Foo) { }
|
|
|
}
|
|
|
|
|
|
interface Services {
|
|
|
foo: Foo;
|
|
|
|
|
|
bar?: Bar;
|
|
|
|
|
|
baz: Foo;
|
|
|
|
|
|
container: string;
|
|
|
}
|
|
|
|
|
|
interface ServicesB {
|
|
|
// will give errors
|
|
|
// baz: Bar;
|
|
|
|
|
|
baz: Foo;
|
|
|
|
|
|
zoo?: Foo;
|
|
|
}
|
|
|
|
|
|
declare const resolver: Resolver<Services>;
|
|
|
|
|
|
const foo = resolver("foo", {lazy: true});
|
|
|
|
|
|
const mmap = <X extends DepsMap<Services>>(m: X) => {};
|
|
|
|
|
|
declare const refs: Refs<Services>;
|
|
|
|
|
|
if (refs && refs.name === "foo") {
|
|
|
refs.default;
|
|
|
}
|
|
|
|
|
|
declare const x: ContainerServices<Services>;
|
|
|
|
|
|
x.container.resolve("container");
|
|
|
|
|
|
|
|
|
mmap({
|
|
|
fooz: {name: "foo", lazy: false, default: undefined },
|
|
|
ooz: "bar"
|
|
|
});
|
|
|
|
|
|
interface SharedServices {
|
|
|
foo: Foo;
|
|
|
|
|
|
bar?: Bar;
|
|
|
|
|
|
baz: Bar;
|
|
|
}
|
|
|
|
|
|
const config = fluent()
|
|
|
.declare<Services>()
|
|
|
.declare<ServicesB>()
|
|
|
.register({
|
|
|
zoo: it => {},
|
|
|
bar: it => it
|
|
|
.lifetime("context") // тип активации, время жизни
|
|
|
.wants({
|
|
|
zoo: "zoo", // зависимость
|
|
|
bar: "bar",
|
|
|
|
|
|
$zoo: { name: "foo", lazy: true, } // отложенная активация,
|
|
|
//фабричный метод
|
|
|
})
|
|
|
.wants({
|
|
|
zoom: "bar"
|
|
|
})
|
|
|
.factory(({ $zoo, zoo }) => // фабрика получает объект с именованными зависимостями
|
|
|
// удобно для деструктурирования
|
|
|
new Bar($zoo) // создается экземпляр сервиса
|
|
|
),
|
|
|
foo: it => it.factory(() => new Foo()),
|
|
|
baz: it => it.value(new Foo())
|
|
|
})
|
|
|
.done({});
|
|
|
|
|
|
declare const container: ContainerBuilder<{}>;
|
|
|
const c2 = config.configure(container);
|
|
|
|
|
|
c2.resolve("foo");
|
|
|
|
|
|
declare const m :ContainerServices<{foo: Foo}>["container"];
|
|
|
|
|
|
m.resolve("container").resolve("container").resolve("foo");
|