@94ai/softphone 5.0.11 → 5.0.13

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.
@@ -0,0 +1,123 @@
1
+ function generateUUID() { // Public Domain/MIT
2
+ let d = Date.now();
3
+ if (typeof performance !== 'undefined' && typeof performance.now === 'function'){
4
+ d += performance.now(); //use high-precision timer if available
5
+ }
6
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
7
+ const r = (d + Math.random() * 16) % 16 | 0;
8
+ d = Math.floor(d / 16);
9
+ return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
10
+ });
11
+ }
12
+
13
+ function debounce(func, wait, immediate) {
14
+ let timeout, args, context, timestamp, result
15
+
16
+ const later = function() {
17
+ // 据上一次触发时间间隔
18
+ const last = +new Date() - timestamp
19
+
20
+ // 上次被包装函数被调用时间间隔last小于设定时间间隔wait
21
+ if (last < wait && last > 0) {
22
+ timeout = setTimeout(later, wait - last)
23
+ } else {
24
+ timeout = null
25
+ // 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用
26
+ if (!immediate) {
27
+ result = func.apply(context, args)
28
+ if (!timeout) context = args = null
29
+ }
30
+ }
31
+ }
32
+
33
+ return function(...args) {
34
+ // @ts-ignore
35
+ context = this
36
+ timestamp = +new Date()
37
+ const callNow = immediate && !timeout
38
+ // 如果延时不存在,重新设定延时
39
+ if (!timeout) timeout = setTimeout(later, wait)
40
+ if (callNow) {
41
+ result = func.apply(context, args)
42
+ // @ts-ignore
43
+ context = args = null
44
+ }
45
+
46
+ return result
47
+ }
48
+ }
49
+
50
+ // 用于存储传入 nextTick 的回调函数
51
+ const callbacks = [];
52
+ // 标记是否已经安排了回调函数的执行
53
+ let pending = false;
54
+
55
+ // 执行队列中的所有回调函数
56
+ function flushCallbacks() {
57
+ pending = false;
58
+ // 复制一份当前的回调队列,避免在执行过程中添加新的回调影响循环
59
+ const copies = callbacks.slice(0);
60
+ callbacks.length = 0;
61
+ // 依次执行每个回调函数,并处理可能出现的异常
62
+ for (let i = 0; i < copies.length; i++) {
63
+ try {
64
+ copies[i]();
65
+ } catch (e) {
66
+ console.error('Error in nextTick callback:', e);
67
+ }
68
+ }
69
+ }
70
+
71
+ // 根据浏览器支持情况选择合适的异步执行方式
72
+ let timerFunc;
73
+ if (typeof Promise !== 'undefined') {
74
+ // 如果支持 Promise,使用 Promise 的 then 方法创建微任务
75
+ const p = Promise.resolve();
76
+ timerFunc = () => {
77
+ p.then(flushCallbacks);
78
+ };
79
+ } else if (typeof MutationObserver !== 'undefined') {
80
+ // 如果支持 MutationObserver,使用它创建微任务
81
+ let counter = 1;
82
+ const observer = new MutationObserver(flushCallbacks);
83
+ const textNode = document.createTextNode(String(counter));
84
+ observer.observe(textNode, {
85
+ characterData: true
86
+ });
87
+ timerFunc = () => {
88
+ counter = (counter + 1) % 2;
89
+ textNode.data = String(counter);
90
+ };
91
+ } else if (typeof setImmediate !== 'undefined') {
92
+ // 如果支持 setImmediate,使用它创建宏任务
93
+ timerFunc = () => {
94
+ setImmediate(flushCallbacks);
95
+ };
96
+ } else {
97
+ // 最后使用 setTimeout 作为兜底方案创建宏任务
98
+ timerFunc = () => {
99
+ setTimeout(flushCallbacks, 0);
100
+ };
101
+ }
102
+ // 实现 nextTick 函数,支持传入回调函数或返回 Promise
103
+ async function nextTick(cb) {
104
+ return new Promise((resolve) => {
105
+ // 将回调函数和 resolve 函数封装成一个新的回调
106
+ callbacks.push(() => {
107
+ if (cb) {
108
+ try {
109
+ cb();
110
+ } catch (e) {
111
+ console.error('Error in nextTick callback:', e);
112
+ }
113
+ }
114
+ // 当回调执行完毕后,resolve Promise
115
+ resolve(true);
116
+ });
117
+ // 如果当前没有正在执行的回调安排,安排一次新的执行
118
+ if (!pending) {
119
+ pending = true;
120
+ timerFunc();
121
+ }
122
+ });
123
+ }