UNPKG

1.41 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const _ = require("lodash");
4/**
5 * Registry of all classes in gabliam instance
6 */
7class Registry {
8 constructor() {
9 this.registry = new Map();
10 }
11 /**
12 * Add a sub registry
13 * @param {Registry} subRegistry
14 */
15 addRegistry(subRegistry) {
16 for (const [type, value] of subRegistry.registry) {
17 this.get(type).push(...value);
18 }
19 }
20 /**
21 * Get values of type in a registry
22 * @param {symbol|string} type
23 * @returns T[] return the list of velue
24 */
25 get(type) {
26 if (!this.registry.has(type)) {
27 this.registry.set(type, []);
28 }
29 return this.registry.get(type);
30 }
31 /**
32 * Add value in registry for a type
33 * @param {symbol|string} type
34 * @param {T} target
35 */
36 add(type, target) {
37 const values = this.get(type);
38 if (!_.find(values, target)) {
39 values.push(target);
40 }
41 }
42 getAllAutoBind() {
43 const autobinds = [];
44 for (const [, values] of this.registry) {
45 autobinds.push(...values.filter(v => v.autoBind));
46 }
47 return autobinds;
48 }
49 /**
50 * Remove all values for a type
51 * @param {symbol} type
52 */
53 remove(type) {
54 this.registry.delete(type);
55 }
56}
57exports.Registry = Registry;