· 5 years ago · Oct 09, 2020, 12:02 PM
1interface IStore {
2 registerWidget: (elementId: string, element: any) => void;
3 getWidget: (elementId: string) => any;
4
5 getAuthParams: () => { [key: string]: any };
6 registerAuthParams: (params: any) => void;
7}
8
9class Store implements IStore {
10 private initialized: { [key: string]: any } = {};
11
12 private authParams: any = null;
13
14 registerWidget(elementId: string, element: any) {
15 this.initialized[elementId] = element;
16 }
17
18 getWidget(elementId: string) {
19 return this.initialized[elementId];
20 }
21
22 getAuthParams() {
23 return this.authParams;
24 }
25
26 registerAuthParams(params: any) {
27 this.authParams = params;
28 }
29}
30
31class UUPService {
32 private store: IStore;
33 private activeWidget: any = null;
34 private appKey: string = '';
35
36 constructor(s: IStore, appKey: string = 'general-uup') {
37 this.store = s;
38 this.appKey = appKey;
39 }
40
41 useWidget(elementId: string, uniq: boolean) {
42 let w = this.store.getWidget(elementId);
43 if (w && uniq) {
44 throw new Error("MUST GO TO Igor's CTO");
45 } else {
46 this.store.registerWidget(elementId, document.getElementById(elementId));
47 w = this.store.getWidget(elementId);
48
49 }
50
51 this.activeWidget = w;
52 }
53
54 open() {}
55
56 close() {}
57
58 handleRedirectCallback(): any {
59 let params = this.store.getAuthParams();
60 if (params) {
61 return params;
62 }
63
64 const qs = location.search.split('&').reduce((acc, curr) => {
65 const [key, v] = curr.split('=');
66 acc[key] = v;
67 return acc;
68 }, {} as any);
69
70 params = qs;
71
72 this.store.registerAuthParams(params);
73
74 return {
75 ...params,
76 appKey: this.appKey
77 }
78 }
79}
80
81const s = new Store();
82
83const api = window[`___game${slug}___`];
84api.register();
85
86api.setStore(s);
87
88api.render();
89
90
91const us1 = new UUPService(s);
92us1.useWidget("arkcom-popup", false);
93us1.handleRedirectCallback();
94// or
95
96this.getStore = () => {
97 return api.store || new Store();
98}
99const us2 = new UUPService(this.getStore());
100us1.useWidget("arkcom-popup", false);
101us1.handleRedirectCallback();
102