import {EventBus} from "../event"; import {Factory} from "./types"; export class Make { public static classOf(service: any): Factory { return this.class(() => new service()); } public static class(factory: Factory): Factory { return container => container.autowire(factory(container)); } /** * creates a factory for singleton service */ public static singleton(factory: Factory): Factory { /** * cannot be converted to array function because the function holds some "state" */ return function (container) { return this.instance || (container.autowire(this.instance = factory(container))); }; } /** * creates a listener singleton factory */ public static listener(factory: Factory): Factory { return this.singleton(container => container.create(EventBus).subscribe(factory(container))); } /** * shorthand for simple singleton service */ public static service(service: any): Factory { return this.singleton(() => new service()); } /** * creates singleton factory, subscribed to event bus */ public static subscriber(service: any): Factory { return this.listener(() => new service()); } /** * return an instance */ public static instance(instance: T): Factory { return function () { return instance; }; } }