utils-uniapp 1.2.8 → 1.2.10
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.
|
@@ -1,4 +1,28 @@
|
|
|
1
1
|
// WebSocketClient.d.ts
|
|
2
|
+
export type WebSocketFailInfo =
|
|
3
|
+
| {
|
|
4
|
+
type: 'connectError';
|
|
5
|
+
url: string;
|
|
6
|
+
willRetry: boolean;
|
|
7
|
+
event: UniApp.GeneralCallbackResult;
|
|
8
|
+
}
|
|
9
|
+
| {
|
|
10
|
+
type: 'maxReconnect';
|
|
11
|
+
url: string;
|
|
12
|
+
willRetry: false;
|
|
13
|
+
maxReconnect: number;
|
|
14
|
+
};
|
|
15
|
+
export declare interface WebSocketClientEventMap {
|
|
16
|
+
open: (res: UniApp.OnSocketOpenCallbackResult) => void;
|
|
17
|
+
reconnect: (res: UniApp.OnSocketOpenCallbackResult) => void;
|
|
18
|
+
message: (res: UniApp.OnSocketMessageCallbackResult) => void;
|
|
19
|
+
close: (res: UniApp.OnSocketCloseCallbackResult) => void;
|
|
20
|
+
error: (err: UniApp.GeneralCallbackResult) => void;
|
|
21
|
+
fail: (info: WebSocketFailInfo) => void;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export declare type WebSocketEventName = keyof WebSocketClientEventMap;
|
|
25
|
+
export declare type WebSocketEventHandler<K extends WebSocketEventName> = WebSocketClientEventMap[K];
|
|
2
26
|
export declare class WebSocketClient {
|
|
3
27
|
/**
|
|
4
28
|
* @param url websocket地址
|
|
@@ -24,22 +48,18 @@ export declare class WebSocketClient {
|
|
|
24
48
|
* @param event 事件名 open | message | close | error
|
|
25
49
|
* @param fn 回调函数
|
|
26
50
|
*/
|
|
27
|
-
on(event:
|
|
28
|
-
on(event: 'message', fn: (res: UniApp.OnSocketMessageCallbackResult) => void): this;
|
|
29
|
-
on(event: 'close', fn: (res: UniApp.OnSocketCloseCallbackResult) => void): this;
|
|
30
|
-
on(event: 'error', fn: (err: UniApp.GeneralCallbackResult) => void): this;
|
|
31
|
-
|
|
51
|
+
on<K extends WebSocketEventName>(event: K, fn: WebSocketEventHandler<K>): this;
|
|
32
52
|
/**
|
|
33
53
|
* 移除事件监听
|
|
34
54
|
* @param event 事件名
|
|
35
55
|
* @param fn 回调函数
|
|
36
56
|
*/
|
|
37
|
-
off(event:
|
|
57
|
+
off<K extends WebSocketEventName>(event: K, fn: WebSocketEventHandler<K>): this;
|
|
38
58
|
|
|
39
59
|
/**
|
|
40
60
|
* 内部触发事件(外部一般不用)
|
|
41
61
|
*/
|
|
42
|
-
emit(event:
|
|
62
|
+
emit(event: WebSocketEventName, ...args: any[]): void;
|
|
43
63
|
|
|
44
64
|
/**
|
|
45
65
|
* 建立连接
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const
|
|
1
|
+
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const c=require("../_rollupPluginBabelHelpers-63255c86.js");var u=function(){function i(o){var e,n=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{};c._classCallCheck(this,i),this.url=o,this.reconnectDelay=n.reconnectDelay||1e3,this.heartbeatInterval=n.heartbeatInterval||3e4,this.heartbeatTimeout=n.heartbeatTimeout||1e4,this.heartbeatMsg=n.heartbeatMsg||"ping",this.maxReconnect=(e=n.maxReconnect)!==null&&e!==void 0?e:-1,this.socketTask=null,this.isConnected=!1,this.heartbeatTimer=null,this.heartbeatTimeoutTimer=null,this.reconnectTimer=null,this.reconnectCount=0,this.manualClose=!1,this.events={open:[],message:[],close:[],error:[],reconnect:[],fail:[]}}return c._createClass(i,[{key:"on",value:function(e,n){return this.events[e]&&this.events[e].push(n),this}},{key:"off",value:function(e,n){if(!this.events[e])return this;var s=this.events[e].indexOf(n);return s>-1&&this.events[e].splice(s,1),this}},{key:"emit",value:function(e){for(var n=arguments.length,s=new Array(n>1?n-1:0),t=1;t<n;t++)s[t-1]=arguments[t];this.events[e]&&this.events[e].forEach(function(a){try{a.apply(void 0,s)}catch(r){console.error("[".concat(e,"] callback error:"),r)}})}},{key:"connect",value:function(){var e=this;if(this.socketTask){var n=this.socketTask;this.socketTask=null,n.close({code:1e3,reason:"reconnect"})}this.manualClose=!1,this.socketTask=uni.connectSocket({url:this.url,success:function(){},complete:function(){}});var s=this.socketTask;return s.onOpen(function(t){var a=e.reconnectCount>0;e.isConnected=!0,e.reconnectCount=0,console.log("Connection established"),e.emit("open",t),a&&(console.log("Reconnected successfully"),e.emit("reconnect",t)),e.startHeartbeat()}),s.onMessage(function(t){e.resetHeartbeatTimeout(),e.emit("message",t)}),s.onClose(function(t){e.isConnected=!1,e.stopHeartbeat(),s===e.socketTask&&(console.log("Connection closed",t),e.emit("close",t),!e.manualClose&&e.reconnect())}),s.onError(function(t){if(console.error("WebSocket error: ",t),e.emit("error",t),s===e.socketTask){if(e.isConnected=!1,e.stopHeartbeat(),!e.manualClose){var a=e.maxReconnect===-1||e.reconnectCount<e.maxReconnect;e.emit("fail",{type:"connectError",url:e.url,willRetry:a,event:t})}try{s.close({code:1e3,reason:"error"})}catch(r){}e.manualClose||e.reconnect()}}),this}},{key:"send",value:function(e){return this.isConnected&&this.socketTask?this.socketTask.send({data:e}):console.error("WebSocket is not connected"),this}},{key:"startHeartbeat",value:function(){var e=this;this.stopHeartbeat(),this.heartbeatTimer=setInterval(function(){e.isConnected&&(e.send(e.heartbeatMsg),e.startHeartbeatTimeout())},this.heartbeatInterval)}},{key:"stopHeartbeat",value:function(){this.heartbeatTimer&&(clearInterval(this.heartbeatTimer),this.heartbeatTimer=null),this.clearHeartbeatTimeout()}},{key:"startHeartbeatTimeout",value:function(){var e=this;this.clearHeartbeatTimeout(),this.heartbeatTimeoutTimer=setTimeout(function(){console.warn("Heartbeat timeout, close socket"),e.socketTask&&e.socketTask.close()},this.heartbeatTimeout)}},{key:"resetHeartbeatTimeout",value:function(){this.clearHeartbeatTimeout()}},{key:"clearHeartbeatTimeout",value:function(){this.heartbeatTimeoutTimer&&(clearTimeout(this.heartbeatTimeoutTimer),this.heartbeatTimeoutTimer=null)}},{key:"reconnect",value:function(){var e=this;if(!(this.isConnected||this.reconnectTimer)){if(this.maxReconnect!==-1&&this.reconnectCount>=this.maxReconnect){console.log("Reach max reconnect count, stop reconnect"),this.emit("fail",{type:"maxReconnect",maxReconnect:this.maxReconnect,url:this.url});return}this.reconnectCount+=1,console.log("Reconnecting in ".concat(this.reconnectDelay/1e3," seconds... count:").concat(this.reconnectCount)),this.reconnectTimer=setTimeout(function(){e.reconnectTimer=null,e.connect()},this.reconnectDelay)}}},{key:"close",value:function(){var e=arguments.length>0&&arguments[0]!==void 0?arguments[0]:1e3,n=arguments.length>1&&arguments[1]!==void 0?arguments[1]:"";this.manualClose=!0,this.stopHeartbeat(),this.reconnectTimer&&(clearTimeout(this.reconnectTimer),this.reconnectTimer=null),this.reconnectCount=0,this.socketTask&&this.socketTask.close({code:e,reason:n})}}])}();exports.WebSocketClient=u;exports.default=u;
|
package/lib/magic/index.d.ts
CHANGED
|
@@ -50,7 +50,7 @@ export function pipeAsync<T extends [...NextFn[], FirstFn]>(...fns: T): T extend
|
|
|
50
50
|
export function debounce<A extends any[], R>(
|
|
51
51
|
fn: (...argvs: A) => R,
|
|
52
52
|
delay?: number
|
|
53
|
-
): ((...argvs: A) =>
|
|
53
|
+
): ((...argvs: A) => number) & {
|
|
54
54
|
stop(): void;
|
|
55
55
|
};
|
|
56
56
|
|
|
@@ -72,7 +72,13 @@ export function debouncePromise<A extends any[], R>(
|
|
|
72
72
|
* @param { number } wait 节流时间,默认500
|
|
73
73
|
* @param { boolean } immediate 是否立即执行,默认true
|
|
74
74
|
*/
|
|
75
|
-
export function throttle<
|
|
75
|
+
export function throttle<A extends any[], R>(
|
|
76
|
+
func: (...argvs: A) => R,
|
|
77
|
+
wait?: number,
|
|
78
|
+
immediate?: boolean
|
|
79
|
+
): ((...argvs: A) => number) & {
|
|
80
|
+
stop(): void;
|
|
81
|
+
};
|
|
76
82
|
|
|
77
83
|
/**
|
|
78
84
|
* 路径参数编码(JSON序列化后encodeURIComponent)
|
package/lib/magic/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("../_rollupPluginBabelHelpers-63255c86.js");function M(){for(var e=arguments.length,
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("../_rollupPluginBabelHelpers-63255c86.js");function M(){for(var e=arguments.length,t=new Array(e),o=0;o<e;o++)t[o]=arguments[o];if(t.some(function(r){return typeof r!="function"}))throw new TypeError("compose 参数必须是函数");return t.length===0?function(){for(var r=arguments.length,n=new Array(r),a=0;a<r;a++)n[a]=arguments[a];return n.length===1?n[0]:n}:t.reduce(function(r,n){return function(){for(var a=this,i=arguments.length,c=new Array(i),u=0;u<i;u++)c[u]=arguments[u];return n.call.apply(n,[this,function(){for(var s=arguments.length,m=new Array(s),h=0;h<s;h++)m[h]=arguments[h];return r.apply(a,m.concat(c))}].concat(c))}},function(r){return r})}function Q(){for(var e=arguments.length,t=new Array(e),o=0;o<e;o++)t[o]=arguments[o];if(t.some(function(r){return typeof r!="function"}))throw new TypeError("composeAsync 参数必须是函数");return t.length===0?l._asyncToGenerator(l._regenerator().m(function r(){var n,a,i,c=arguments;return l._regenerator().w(function(u){for(;;)switch(u.n){case 0:for(n=c.length,a=new Array(n),i=0;i<n;i++)a[i]=c[i];return u.a(2,a.length===1?a[0]:a)}},r)})):t.reduce(function(r,n){return l._asyncToGenerator(l._regenerator().m(function a(){var i=this,c,u,s,m=arguments;return l._regenerator().w(function(h){for(;;)switch(h.n){case 0:for(c=m.length,u=new Array(c),s=0;s<c;s++)u[s]=m[s];return h.a(2,n.call.apply(n,[this,l._asyncToGenerator(l._regenerator().m(function w(){var v,g,f,p=arguments;return l._regenerator().w(function(d){for(;;)switch(d.n){case 0:for(v=p.length,g=new Array(v),f=0;f<v;f++)g[f]=p[f];return d.a(2,r.apply(i,g.concat(u)))}},w)}))].concat(u)))}},a,this)}))},function(){var r=l._asyncToGenerator(l._regenerator().m(function n(a){return l._regenerator().w(function(i){for(;;)switch(i.n){case 0:return i.a(2,a)}},n)}));return function(n){return r.apply(this,arguments)}}())}function j(){for(var e=arguments.length,t=new Array(e),o=0;o<e;o++)t[o]=arguments[o];if(t.some(function(r){return typeof r!="function"}))throw new TypeError("pipe 参数必须是函数");return t.length===0?function(){for(var r=arguments.length,n=new Array(r),a=0;a<r;a++)n[a]=arguments[a];return n.length===1?n[0]:n}:t.reverse().reduce(function(r,n){return function(){for(var a=this,i=arguments.length,c=new Array(i),u=0;u<i;u++)c[u]=arguments[u];return n.call.apply(n,[this,function(){for(var s=arguments.length,m=new Array(s),h=0;h<s;h++)m[h]=arguments[h];return r.apply(a,m.concat(c))}].concat(c))}},function(r){return r})}function O(){for(var e=arguments.length,t=new Array(e),o=0;o<e;o++)t[o]=arguments[o];if(t.some(function(r){return typeof r!="function"}))throw new TypeError("pipeAsync 参数必须是函数");return t.length===0?l._asyncToGenerator(l._regenerator().m(function r(){var n,a,i,c=arguments;return l._regenerator().w(function(u){for(;;)switch(u.n){case 0:for(n=c.length,a=new Array(n),i=0;i<n;i++)a[i]=c[i];return u.a(2,a.length===1?a[0]:a)}},r)})):t.reverse().reduce(function(r,n){return l._asyncToGenerator(l._regenerator().m(function a(){var i=this,c,u,s,m=arguments;return l._regenerator().w(function(h){for(;;)switch(h.n){case 0:for(c=m.length,u=new Array(c),s=0;s<c;s++)u[s]=m[s];return h.a(2,n.call.apply(n,[this,l._asyncToGenerator(l._regenerator().m(function w(){var v,g,f,p=arguments;return l._regenerator().w(function(d){for(;;)switch(d.n){case 0:for(v=p.length,g=new Array(v),f=0;f<v;f++)g[f]=p[f];return d.a(2,r.apply(i,g.concat(u)))}},w)}))].concat(u)))}},a,this)}))},function(){var r=l._asyncToGenerator(l._regenerator().m(function n(a){return l._regenerator().w(function(i){for(;;)switch(i.n){case 0:return i.a(2,a)}},n)}));return function(n){return r.apply(this,arguments)}}())}function F(e,t){var o=null;r.stop=function(){return o&&clearTimeout(o)};function r(){for(var n=this,a=arguments.length,i=new Array(a),c=0;c<a;c++)i[c]=arguments[c];return r.stop(),o=setTimeout(function(){e.apply(n,i)},t),o}return r}function G(e,t){var o=null,r=[];n.stop=function(){if(o){var a;(a=r.shift())===null||a===void 0||a("cancel"),clearTimeout(o)}};function n(){for(var a=this,i=arguments.length,c=new Array(i),u=0;u<i;u++)c[u]=arguments[u];return new Promise(function(s,m){n.stop(),r.push(m),o=setTimeout(function(){s(e.apply(a,c))},t)})}return n}function _(e){var t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:500,o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:!0,r,n,a=o?function(){if(!n){n=!0;for(var i=arguments.length,c=new Array(i),u=0;u<i;u++)c[u]=arguments[u];return typeof e=="function"&&e.apply(this,c),r=setTimeout(function(){n=!1},t),r}}:function(){for(var i=this,c=arguments.length,u=new Array(c),s=0;s<c;s++)u[s]=arguments[s];if(!n)return n=!0,r=setTimeout(function(){n=!1,typeof e=="function"&&e.apply(i,u)},t),r};return a.stop=function(){return r&&clearTimeout(r)},a}function $(e){return encodeURIComponent(JSON.stringify(e||{}))}function H(e){return JSON.parse(decodeURIComponent(e||"{}"))}function R(){var e=arguments.length>0&&arguments[0]!==void 0?arguments[0]:"",t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{},o=t.silent,r=o===void 0?!1:o,n=t.decodePlus,a=n===void 0?!0:n,i=t.strict,c=i===void 0?!1:i,u={};if(typeof e!="string")return u;function s(U,I){var A=U;a&&(A=A.replace(/\+/g," "));try{return decodeURIComponent(A)}catch(E){return r||console.warn("Failed to decode ".concat(I?"key":"value",": ").concat(A),E),c?null:(I&&(A=A.replace(/%[0-9a-f]{2}/gi,"")),A)}}var m=e.split("#")[0],h=m.indexOf("?"),w=h>=0?m.slice(h+1):m;if(!w)return u;for(var v=w.split("&"),g=0;g<v.length;g++){var f=v[g];if(f){var p=f.indexOf("="),d=void 0,P=void 0;p===-1?(d=f,P=""):(d=f.slice(0,p),P=f.slice(p+1));var y=s(d,!0);if(!(y===null||!y)){var S=s(P,!1);if(S!==null){var T=y,b=!1;/\[\]$/.test(y)?(T=y.slice(0,-2),b=!0):/^(.+)\[\d+\]$/.test(y)&&(T=y.replace(/\[\d+\]$/,""),b=!0);var C=u[T];C===void 0?u[T]=b?[S]:S:Array.isArray(C)?C.push(S):u[T]=[C,S]}}}}return u}function L(e){return typeof window=="undefined"?{}:R(window.location.search,e)}function J(e){if(typeof window=="undefined")return{};var t=window.location.hash,o=t.indexOf("?");if(o===-1)return{};var r=t.substring(o);return R(r,e)}function q(){var e=arguments.length>0&&arguments[0]!==void 0?arguments[0]:0,t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:"-";return[Date.now().toString(),Math.random().toString(16).slice(2,10),Math.random().toString(16).slice(7),e.toString()].join(t)}function B(e){return e.replace(/([^-])([^-]*)/g,function(t,o,r){return o.toLocaleUpperCase()+r.toLocaleLowerCase()}).replace(/-/g,"")}function D(e,t){e=e.split("."),t=t.split(".");for(var o=Math.max(e.length,t.length);e.length<o;)e.push("0");for(;t.length<o;)t.push("0");for(var r=0;r<o;r++){var n=parseInt(e[r]),a=parseInt(t[r]);if(n>a)return 1;if(n<a)return-1}return 0}function N(){return"#"+Math.floor(Math.random()*16777215).toString(16).padEnd(6,"0")}function z(){var e=arguments.length>0&&arguments[0]!==void 0?arguments[0]:8,t=arguments.length>1?arguments[1]:void 0;e=e||32,t||(t="ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678");for(var o=t.length,r="",n=0;n<e;n++)r+=t.charAt(Math.floor(Math.random()*o));return r}function V(e){var t,o=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{};if(!e||l._typeof(e)!=="object")return"";var r=o.addQuestionMark,n=r===void 0?!1:r,a=o.arrayFormat,i=a===void 0?"comma":a,c=o.keepEmptyArray,u=c===void 0?!1:c,s=[],m={indices:function(g,f){f.forEach(function(p,d){s.push("".concat(g,"[").concat(d,"]=").concat(encodeURIComponent(p)))})},brackets:function(g,f){f.forEach(function(p){s.push("".concat(g,"[]=").concat(encodeURIComponent(p)))})},repeat:function(g,f){f.forEach(function(p){s.push("".concat(g,"=").concat(encodeURIComponent(p)))})},comma:function(g,f){var p=f.map(function(d){return encodeURIComponent(d)}).join(",");s.push("".concat(g,"=").concat(p))}},h=(t=m[i])!==null&&t!==void 0?t:m.comma;Object.keys(e).forEach(function(v){var g=e[v];if(g!=null){var f=encodeURIComponent(v);if(Array.isArray(g)){var p=g.filter(function(P){return P!=null});if(p.length===0){u&&s.push("".concat(f,"="));return}h(f,p);return}var d=g;l._typeof(d)==="object"&&d!==null&&(d=JSON.stringify(d)),s.push("".concat(f,"=").concat(encodeURIComponent(d)))}});var w=s.join("&");return n&&w?"?".concat(w):w}function W(e){var t=e.scene;return t&&(e=l._objectSpread2(l._objectSpread2({fromCode:!0},e),R(t))),e}exports.compareVersion=D;exports.compose=M;exports.composeAsync=Q;exports.debounce=F;exports.debouncePromise=G;exports.decodeURIParams=H;exports.encodeURIParams=$;exports.getOnLoadParams=W;exports.getPascalCase=B;exports.getQueryParams=R;exports.getQueryParamsFromHash=J;exports.getQueryParamsFromSearch=L;exports.getRandom=q;exports.getRandomStr=z;exports.pipe=j;exports.pipeAsync=O;exports.randomColor=N;exports.throttle=_;exports.toQueryString=V;
|