1 |
|
2 |
|
3 | import { ConsoleLogger as LoggerClass } from './Logger';
|
4 |
|
5 | const logger = new LoggerClass('Amplify');
|
6 |
|
7 | export class AmplifyClass {
|
8 |
|
9 | private _components = [];
|
10 | private _config = {};
|
11 |
|
12 |
|
13 | private _modules = {};
|
14 |
|
15 |
|
16 |
|
17 | Auth = null;
|
18 | Analytics = null;
|
19 | API = null;
|
20 | Credentials = null;
|
21 | Storage = null;
|
22 | I18n = null;
|
23 | Cache = null;
|
24 | PubSub = null;
|
25 | Interactions = null;
|
26 | Pushnotification = null;
|
27 | UI = null;
|
28 | XR = null;
|
29 | Predictions = null;
|
30 | DataStore = null;
|
31 | Geo = null;
|
32 | Notifications = null;
|
33 |
|
34 | Logger = LoggerClass;
|
35 | ServiceWorker = null;
|
36 |
|
37 | register(comp) {
|
38 | logger.debug('component registered in amplify', comp);
|
39 | this._components.push(comp);
|
40 | if (typeof comp.getModuleName === 'function') {
|
41 | this._modules[comp.getModuleName()] = comp;
|
42 | this[comp.getModuleName()] = comp;
|
43 | } else {
|
44 | logger.debug('no getModuleName method for component', comp);
|
45 | }
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | comp.configure(this._config);
|
54 | }
|
55 |
|
56 | configure(config?) {
|
57 | if (!config) return this._config;
|
58 |
|
59 | this._config = Object.assign(this._config, config);
|
60 | logger.debug('amplify config', this._config);
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | Object.entries(this._modules).forEach(([Name, comp]) => {
|
66 |
|
67 | Object.keys(comp).forEach(property => {
|
68 |
|
69 | if (this._modules[property]) {
|
70 | comp[property] = this._modules[property];
|
71 | }
|
72 | });
|
73 | });
|
74 |
|
75 | this._components.map(comp => {
|
76 | comp.configure(this._config);
|
77 | });
|
78 |
|
79 | return this._config;
|
80 | }
|
81 |
|
82 | addPluggable(pluggable) {
|
83 | if (
|
84 | pluggable &&
|
85 | pluggable['getCategory'] &&
|
86 | typeof pluggable['getCategory'] === 'function'
|
87 | ) {
|
88 | this._components.map(comp => {
|
89 | if (
|
90 | comp['addPluggable'] &&
|
91 | typeof comp['addPluggable'] === 'function'
|
92 | ) {
|
93 | comp.addPluggable(pluggable);
|
94 | }
|
95 | });
|
96 | }
|
97 | }
|
98 | }
|
99 |
|
100 | export const Amplify = new AmplifyClass();
|