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