happy-react-hooks
Version:
<!-- * Author rhys.zhao * Date 2021-09-16 13:23:39 * LastEditors rhys.zhao * LastEditTime 2023-06-25 19:30:38 * Description happy-react-hooks -->
1 lines • 4.14 kB
JavaScript
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t(require("react"));else if("function"==typeof define&&define.amd)define(["react"],t);else{var r="object"==typeof exports?t(require("react")):t(e.React);for(var n in r)("object"==typeof exports?exports:e)[n]=r[n]}}(self,(e=>(()=>{"use strict";var t={787:t=>{t.exports=e}},r={};function n(e){var o=r[e];if(void 0!==o)return o.exports;var u=r[e]={exports:{}};return t[e](u,u.exports,n),u.exports}n.d=(e,t)=>{for(var r in t)n.o(t,r)&&!n.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t);var o={};return(()=>{n.d(o,{default:()=>u});var e=n(787);function t(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var n,o,u,c,i=[],a=!0,l=!1;try{if(u=(r=r.call(e)).next,0===t){if(Object(r)!==r)return;a=!1}else for(;!(a=(n=u.call(r)).done)&&(i.push(n.value),i.length!==t);a=!0);}catch(e){l=!0,o=e}finally{try{if(!a&&null!=r.return&&(c=r.return(),Object(c)!==c))return}finally{if(l)throw o}}return i}}(e,t)||function(e,t){if(e){if("string"==typeof e)return r(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?r(e,t):void 0}}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function r(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}const u={useAutoFit:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:1920,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:1080,n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"root",o=arguments.length>3&&void 0!==arguments[3]?arguments[3]:0,u=function(e,t,r){var n=window,o=n.innerWidth/e,u=n.innerHeight/t;r.style.transform="scale(".concat(o,", ").concat(u,")"),r.style.transformOrigin="left top"};(0,e.useEffect)((function(){var e="string"==typeof n?document.getElementById(n):n;e.style.width="".concat(t,"px"),e.style.height="".concat(r,"px"),u(t,r,e);var c=null,i=window.addEventListener("resize",(function(){clearTimeout(c),c=setTimeout((function(){u(t,r,e)}),o)}));return function(){clearTimeout(c),window.removeEventListener("resize",i)}}),[])},useWebSocket:function(r,n){var o=(0,e.useRef)(),u=(0,e.useRef)(),c=(0,e.useRef)(),i=t((0,e.useState)(0),2),a=i[0],l=i[1],s=t((0,e.useState)(""),2),f=s[0],d=s[1],v=function(){if(!o.current||3===o.current.readyState)try{o.current=new WebSocket(r),o.current.onopen=function(e){var t;console.log("---- websocket 连接成功 ----",e),l(null===(t=o.current)||void 0===t?void 0:t.readyState),n&&y()},o.current.onclose=function(e){var t;console.log("---- websocket 连接已关闭 ----",e),l(null===(t=o.current)||void 0===t?void 0:t.readyState)},o.current.onerror=function(e){var t;console.log("---- websocket 连接错误 ----",e),l(null===(t=o.current)||void 0===t?void 0:t.readyState)},o.current.onmessage=function(e){e.data===(null==n?void 0:n.res)&&clearTimeout(c.current),d(e.data)}}catch(e){console.log("连接过程出现错误,".concat(e))}},y=function(){var e=n.interval,t=n.send,r=n.res,o=n.timeout;if(!(e||t||r||o))throw new Error("心跳参数错误,检查interval, send, res, timeout是否存在值为空的情况");u.current=setInterval((function(){p(t),c.current=setTimeout((function(){console.log("---- 心跳超时即认为停止,即将重新连接 ----"),m(),v()}),o)}),e)},p=function(e){var t,r;1===(null===(t=o.current)||void 0===t?void 0:t.readyState)&&(null===(r=o.current)||void 0===r||r.send(e))},m=function(){var e;console.log("---- 关闭websocket ----"),clearTimeout(c.current),clearInterval(u.current),null===(e=o.current)||void 0===e||e.close(),o.current=null};return(0,e.useEffect)((function(){return o.current||v(),m}),[]),{readyState:a,message:f,sendMessage:p}}}})(),o.default})()));