(params)
| 611 | return this.#singletons |
| 612 | } |
| 613 | register(params) { |
| 614 | if (ServiceContainer.isConstructor(params)) { |
| 615 | if (typeof params.name !== "string") { |
| 616 | throw new Error("params.name is not a string.") |
| 617 | } |
| 618 | params = { name: params.name, definition: params } |
| 619 | } |
| 620 | if (typeof params !== "object") { |
| 621 | throw new Error("params is not an object.") |
| 622 | } |
| 623 | ;["name", "definition"].forEach((key) => { |
| 624 | if (!(key in params)) { |
| 625 | console.error("Invalid service %o registration.", params) |
| 626 | throw new Error(`params.${key} is not defined.`) |
| 627 | } |
| 628 | }) |
| 629 | const opts = { definition: params.definition } |
| 630 | if ("dependencies" in params) { |
| 631 | if (Array.isArray(params.dependencies)) { |
| 632 | params.dependencies.forEach((dep) => { |
| 633 | if (typeof dep !== "string") { |
| 634 | throw new Error("dependency name is not a string.") |
| 635 | } |
| 636 | }) |
| 637 | opts.dependencies = params.dependencies |
| 638 | } else { |
| 639 | throw new Error("params.dependencies is not an array.") |
| 640 | } |
| 641 | } |
| 642 | if (params.singleton) { |
| 643 | opts.singleton = true |
| 644 | } |
| 645 | this.#services.set(params.name, opts) |
| 646 | return Object.assign({ name: params.name }, opts) |
| 647 | } |
| 648 | get(name) { |
| 649 | const ctorInfos = this.#services.get(name) |
| 650 | if (!ctorInfos) { |
no test coverage detected