@webitel/ui-sdk 24.12.93 → 24.12.94

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webitel/ui-sdk",
3
- "version": "24.12.93",
3
+ "version": "24.12.94",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "dev": "vite",
@@ -71,9 +71,6 @@
71
71
  "./src/validators*": {
72
72
  "import": "./src/validators*"
73
73
  },
74
- "./src/websocket*": {
75
- "import": "./src/websocket*"
76
- },
77
74
  "./src/modules*": {
78
75
  "import": "./src/modules*"
79
76
  },
@@ -0,0 +1,107 @@
1
+ import { markRaw, reactive, shallowReactive } from 'vue';
2
+ import { Client } from 'webitel-sdk';
3
+
4
+ import eventBus from '../../scripts/eventBus.js';
5
+ import { endpoint, getConfig } from './config.js';
6
+ import websocketErrorEventHandler from './websocketErrorEventHandler.js';
7
+
8
+ const WebSocketClientEvent = Object.freeze({
9
+ AFTER_AUTH: 'afterAuth',
10
+ ERROR: 'error',
11
+ });
12
+
13
+ class WebSocketClientController {
14
+ cli = null;
15
+ Event = WebSocketClientEvent;
16
+
17
+ _config = getConfig();
18
+ _on = {
19
+ [WebSocketClientEvent.ERROR]: [websocketErrorEventHandler],
20
+ [WebSocketClientEvent.AFTER_AUTH]: [],
21
+ };
22
+
23
+ getCliInstance() {
24
+ if (!this.cli) this.cli = this._createCliInstance();
25
+ return this.cli;
26
+ }
27
+
28
+ async destroyCliInstance() {
29
+ if (!window.cli) return;
30
+
31
+ await window.cli.destroy();
32
+ this.cli = null;
33
+ window.cli = null;
34
+ }
35
+
36
+ addEventListener(event, callback) {
37
+ if (Array.isArray(callback)) this._on[event] = this._on[event].concat(callback);
38
+ else this._on[event].push(callback);
39
+ }
40
+
41
+ _createCliInstance = async () => {
42
+ const token = localStorage.getItem('access-token');
43
+ const configCli = getConfig();
44
+
45
+ if (typeof configCli.registerWebDevice === 'undefined') {
46
+ configCli.registerWebDevice = true;
47
+ }
48
+
49
+ const config = {
50
+ endpoint,
51
+ token,
52
+ registerWebDevice: configCli.registerWebDevice,
53
+ debug: configCli.debug,
54
+ };
55
+
56
+ // why reactive? https://github.com/vuejs/core/discussions/7811#discussioncomment-5181921
57
+ // const cli = new Client(config);
58
+ const cli = shallowReactive(new Client(config));
59
+
60
+ // why reactive? https://github.com/vuejs/core/discussions/7811#discussioncomment-5181921
61
+ cli.conversationStore = reactive(cli.conversationStore);
62
+ cli.callStore = reactive(cli.callStore);
63
+
64
+ this._on[WebSocketClientEvent.AFTER_AUTH].forEach((callback) => callback());
65
+ this._on[WebSocketClientEvent.ERROR].forEach((callback) => cli.on('error', callback));
66
+ cli.on(`show_message`, e => eventBus.$emit('notification', {
67
+ type: e.type,
68
+ text: e.message,
69
+ timeout: e.timeout,
70
+ }));
71
+
72
+ await cli.connect();
73
+
74
+ await cli.auth();
75
+
76
+ /*
77
+ cli.phone.ua contains "configuration" property, which has no setter so cannot be wrapped with reactivity.
78
+ so that, reactivity breaks
79
+ for more info, see WTEL-4236
80
+ */
81
+ await new Promise((resolve, reject) => {
82
+ const timeout = setTimeout(() => {
83
+ console.error('Phone user agent is not connected :(');
84
+ resolve();
85
+ }, 5000);
86
+
87
+ const markUa = () => cli.phone?.ua && (cli.phone.ua = markRaw(cli.phone.ua));
88
+
89
+ if (cli.phone?.ua) {
90
+ markUa();
91
+ clearTimeout(timeout);
92
+ resolve();
93
+ } else cli.on('phone_connected', () => {
94
+ markUa();
95
+ clearTimeout(timeout);
96
+ resolve();
97
+ });
98
+ });
99
+
100
+ window.cli = cli;
101
+ return cli;
102
+ };
103
+ };
104
+
105
+ const webSocketClientController = new WebSocketClientController();
106
+
107
+ export default webSocketClientController;
@@ -0,0 +1,15 @@
1
+ export const getConfig = () => {
2
+ let cliConfig = {};
3
+ try {
4
+ const CONFIG = JSON.parse(localStorage.getItem('CONFIG'));
5
+ cliConfig = CONFIG.CLI || {};
6
+ } catch {}
7
+ return cliConfig;
8
+ };
9
+
10
+ const { hostname, protocol } = window.location;
11
+ const origin = (`${protocol}//${hostname}`).replace(/^http/, 'ws');
12
+
13
+ export const endpoint = import.meta.env.MODE === 'production'
14
+ ? `${origin}/ws`
15
+ : 'wss://dev.webitel.com/ws';
@@ -0,0 +1,14 @@
1
+ import i18n from '../../locale/i18n.js';
2
+ import eventBus from '../../scripts/eventBus.js';
3
+
4
+ const websocketErrorEventHandler = (error) => {
5
+ const errLocale = `error.websocket.${error.id?.replaceAll('.', '_')}`;
6
+ const message = {
7
+ type: 'error',
8
+ text: i18n.global.te(errLocale) ? i18n.global.t(errLocale) : error,
9
+ };
10
+ eventBus.$emit('notification', message);
11
+ return error;
12
+ };
13
+
14
+ export default websocketErrorEventHandler;