How to use clearTimeout method of events Package

Best K6 code snippet using events.clearTimeout

jslib.go

Source:jslib.go Github

copy

Full Screen

1package gotalk2const JSLibSHA1Base64 = "YLDgrGNN3M7gn9qZDeBmFusYWIQ="3const JSLibString = "var gotalk=(function(){var I=Object.defineProperty,me=Object.prototype.hasOwnProperty,B=(e,t)=>()=>(t||(t={exports:{}},e(t.exports,t)),t.exports),Z=e=>I(e,\"__esModule\",{value:!0}),$=(e,t)=>{Z(e);for(var r in t)I(e,r,{get:t[r],enumerable:!0})},ve=(e,t)=>{if(Z(e),typeof t==\"object\"||typeof t==\"function\")for(var r in t)!me.call(e,r)&&r!==\"default\"&&(n=>{I(e,n,{get:()=>t[n],enumerable:!0})})(r);return e},w=e=>e&&e.__esModule?e:ve(I({},\"default\",{value:e,enumerable:!0}),e);var P=B(C=>{$(C,{sizeOf:()=>ee});function ee(e){for(var t=0,r=0,n;n=e.charCodeAt(r++);t+=n>>11?3:n>>7?2:1);return t}function z(e){return 255&e}if(typeof TextDecoder!=\"undefined\"){var ye=new TextDecoder(\"utf8\"),be=new TextEncoder(\"utf8\");C.decode=function(t){return ye.decode(t)},C.encode=function(t){return be.encode(t)}}else C.decode=function(t){var r=0,n=t.length-r,o,s,i=\"\";for(r=0;r<n;)o=t[r++],s=z(o),s<128||(s>>5==6?o=(o<<6&2047)+(t[r++]&63):s>>4==14?(o=(o<<12&65535)+(z(t[r++])<<6&4095),o+=t[r++]&63):s>>3==30&&(o=(o<<18&2097151)+(z(t[r++])<<12&262143),o+=z(t[r++])<<6&4095,o+=t[r++]&63)),i+=String.fromCharCode(o);return i},C.encode=function(t){for(var r=0,n=t.length,o,s=0,i=new Uint8Array(ee(t));r!==n;)o=t.charCodeAt(r++),o<128?i[s++]=o:o<2048?(i[s++]=o>>6|192,i[s++]=o&63|128):o<65536?(i[s++]=o>>12|224,i[s++]=o>>6&63|128,i[s++]=o&63|128):(i[s++]=o>>18|240,i[s++]=o>>12&63|128,i[s++]=o>>6&63|128,i[s++]=o&63|128);return i}});var j=B(A=>{function V(){}A.global=typeof global!=\"undefined\"?global:typeof self!=\"undefined\"?self:typeof window!=\"undefined\"?window:A;A.console=typeof console!=\"undefined\"?console:{log:V,warn:V,error:V};A.document=typeof document!=\"undefined\"?document:null});var J=B(d=>{const T=w(P());d.Version=1;var te=d.MsgTypeSingleReq=114,re=d.MsgTypeStreamReq=115,Oe=d.MsgTypeStreamReqPart=112,Ie=d.MsgTypeSingleRes=82,Be=d.MsgTypeStreamRes=83,ze=d.MsgTypeErrorRes=69,N=d.MsgTypeRetryRes=101,L=d.MsgTypeNotification=110,U=d.MsgTypeHeartbeat=104,ne=d.MsgTypeProtocolError=102;d.ErrorAbnormal=0;d.ErrorUnsupported=1;d.ErrorInvalidMsg=2;d.ErrorTimeout=3;d.HeartbeatMsgMaxLoad=65535;function R(e,t,r,n){for(var o=t||0,s=0,i,a=r.toString(16),g=n-a.length;g--;)e[o++]=48;for(;!isNaN(i=a.charCodeAt(s++));)e[o++]=i}function oe(e,t){var r=h(t);return R(r,0,e,t),r}function H(e){for(var t=0,r=0,n=0,o=!1;r<e.length;r++)n=e[r],n<64?(n<48&&(o=!0),n-=48):n<71?(n<65&&(o=!0),n-=55):n<103&&96<n?n-=87:o=!0,t=t<<4|n&15;if(o)throw new Error(\"invalid hexint \"+String.fromCharCode.apply(null,e));return t}d.binary={makeFixnum:oe,versionBuf:oe(d.Version,2),parseVersion:H,parseMsg:function(e){var t,r,n,o,s=0,i=0,a;return t=e[0],a=1,t===U?(s=H(e.subarray(a,a+4)),a+=4):t!==L&&t!==ne&&(r=e.subarray(a,a+4),a+=4),t==te||t==re||t==L?(o=H(e.subarray(a,a+3)),a+=3,n=T.decode(e.subarray(a,a+o)),a+=o):t===N&&(s=H(e.subarray(a,a+8)),a+=8),i=H(e.subarray(a,a+8)),{t:t,id:r,name:n,wait:s,size:i}},makeMsg:function(e,t,r,n,o){var s,i,a=t?13:9;return r&&r.length!==0&&(i=T.encode(r),a+=3+i.length),s=h(a),s[0]=e,a=1,t&&t.length===4&&(typeof t==\"string\"?(s[1]=t.charCodeAt(0),s[2]=t.charCodeAt(1),s[3]=t.charCodeAt(2),s[4]=t.charCodeAt(3)):(s[1]=t[0],s[2]=t[1],s[3]=t[2],s[4]=t[3]),a+=4),i&&(R(s,a,i.length,3),a+=3,s.set(i,a),a+=i.length),e===N&&(R(s,a,n,8),a+=8),R(s,a,o,8),s},makeHeartbeatMsg:function(e){var t=h(13),r=1;return t[0]=U,R(t,r,e,4),r+=4,R(t,r,Math.round(new Date().getTime()/1e3),8),r+=8,t}};var we=\"00000000\";function x(e,t){var r=e.toString(16);return we.substr(0,t-r.length)+r}d.text={makeFixnum:x,versionBuf:x(d.Version,2),parseVersion:function(e){return parseInt(e.substr(0,2),16)},parseMsg:function(e){var t,r,n,o=0,s=0,i;return t=e.charCodeAt(0),i=1,t===U?(o=parseInt(e.substr(i,4),16),i+=4):t!==L&&t!==ne&&(r=e.substr(i,4),i+=4),t==te||t==re||t==L?n=e.substring(i+3,e.length-8):t==N&&(o=parseInt(e.substr(i,8),16),i+=8),s=parseInt(e.substr(e.length-8),16),{t:t,id:r,name:n,wait:o,size:s}},makeMsg:function(e,t,r,n,o){var s=String.fromCharCode(e);return t&&t.length===4&&(s+=t),r&&r.length!==0&&(s+=x(T.sizeOf(r),3),s+=r),e===N&&(s+=x(n,8)),s+=x(o,8),s},makeHeartbeatMsg:function(e){var t=String.fromCharCode(U);return t+=x(e,4),t+=x(Math.round(new Date().getTime()/1e3),8),t}}});var ge=B(y=>{$(y,{default:()=>ie});const M=w(j()),f=w(J()),q=w(P());var u=y,ie=y,ae=f.text,fe=f.binary;u.version=\"1.2.1\";u.protocol=f;u.Buf=h;u.developmentMode=!1;u.defaultResponderAddress=\"\";var le=\"\",p={wsproto:\"\",proto:\"\",host:\"\",path:\"\"};function Ee(){M.document&&xe(),u.developmentMode=Me(p.host)}function xe(){var e=M.document.currentScript.src;if(!e)return;var t=e.indexOf(\"://\")+3;if(t==2)return;p.proto=e.substr(0,t-2);var r=e.indexOf(\"/\",t);if(r==-1)return;p.wsproto=p.proto==\"https:\"?\"wss://\":\"ws://\",p.host=e.substring(t,r),e=e.substr(r),t=e.lastIndexOf(\"?\"),t!=-1&&(e=e.substr(0,t)),p.path=e.substring(e.indexOf(\"/\"),e.lastIndexOf(\"/\")+1),u.defaultResponderAddress=p.wsproto+p.host+p.path,le=u.defaultResponderAddress}function Me(e){var t=e,r=t.lastIndexOf(\":\");return r!=-1&&(t=t.substr(0,r)),r=t.lastIndexOf(\".\"),r==-1?t==\"localhost\":t==\"127.0.0.1\"||t.substr(r)==\".local\"}function F(){u.developmentMode&&M.console.warn.apply(M.console,Array.prototype.slice.call(arguments))}function G(e){if(!e||e.length==0)return null;typeof e!=\"string\"&&(e=q.decode(e));try{return JSON.parse(e)}catch(t){F(\"[gotalk] ignoring invalid json\",e)}}function l(e,t){return Object.create(l.prototype,{handlers:{value:e,enumerable:!0},protocol:{value:t||(h?f.binary:f.text),enumerable:!0,writable:!0},heartbeatInterval:{value:20*1e3,enumerable:!0,writable:!0},ws:{value:null,writable:!0,enumerable:!0},keepalive:{value:null,writable:!0,enumerable:!0},_isOpen:{value:!1,writable:!0},_sendq:{value:[],writable:!0},sendBufferLimit:{value:100,writable:!0,enumerable:!0},nextOpID:{value:0,writable:!0},nextStreamID:{value:0,writable:!0},pendingRes:{value:{},writable:!0},hasPendingRes:{get:function(){for(var r in this.pendingRes)return!0}},pendingClose:{value:!1,writable:!0}})}l.prototype=c.mixin(l.prototype);y.Sock=l;function Se(e,t){if(e.pendingClose=!1,e.stopSendingHeartbeats(),e.ws&&(e.ws.onmessage=null,e.ws.onerror=null,e.ws.onclose=null,e.ws=null),e.nextOpID=0,e.hasPendingRes){var r=t||new Error(\"connection closed\");for(var n in e.pendingRes)e.pendingRes[n](r);e.pendingRes={}}}var ke={1e3:\"normal\",1001:\"going away\",1002:\"protocol error\",1003:\"unsupported\",1005:\"no status\",1006:\"abnormal\",1007:\"inconsistent\",1008:\"invalid message\",1009:\"too large\"},E=Symbol(\"CLOSE_ERROR\");function K(e){var t=ke[e];return\"#\"+e+(t?\" (\"+t+\")\":\"\")}l.prototype.adoptWebSocket=function(e){var t=this;if(e.readyState!==WebSocket.OPEN)throw new Error(\"web socket readyState != OPEN\");e.binaryType=\"arraybuffer\",t.ws=e,e.onclose=function(r){var n=e[E]||null;!n&&r.code!==1e3&&(n=new Error(\"websocket closed: \"+K(r.code))),Se(t,n),t._connectionStatusChange(!1),t.emit(\"close\",n)},e.onmessage=function(r){e._bufferedMessages||(e._bufferedMessages=[]),e._bufferedMessages.push(r.data)}};l.prototype.adopt=function(e){if(adopt instanceof WebSocket)return this.adoptWebSocket(e);throw new Error(\"unsupported transport\")};l.prototype.handshake=function(){this.ws.send(this.protocol.versionBuf)};l.prototype.end=function(){var e=this;e.keepalive&&(e.keepalive.disable(),e.keepalive=null),!e.pendingClose&&e.hasPendingRes?e.pendingClose=!0:e.ws&&e.ws.close(1e3)};l.prototype.address=function(){var e=this;return e.ws?e.ws.url:null};var X=y.ErrAbnormal=Error(\"unsupported protocol\");X.isGotalkProtocolError=!0;X.code=f.ErrorAbnormal;var W=y.ErrUnsupported=Error(\"unsupported protocol\");W.isGotalkProtocolError=!0;W.code=f.ErrorUnsupported;var D=y.ErrInvalidMsg=Error(\"invalid protocol message\");D.isGotalkProtocolError=!0;D.code=f.ErrorInvalidMsg;var Q=y.ErrTimeout=Error(\"timeout\");Q.isGotalkProtocolError=!0;Q.code=f.ErrorTimeout;l.prototype.sendHeartbeat=function(e){var t=this,r=t.protocol.makeHeartbeatMsg(Math.round(e*f.HeartbeatMsgMaxLoad));try{t.ws.send(r)}catch(n){throw(!this.ws||this.ws.readyState>WebSocket.OPEN)&&(n=new Error(\"socket is closed\")),n}};l.prototype.startSendingHeartbeats=function(){var e=this;if(e.heartbeatInterval<10)throw new Error(\"Sock.heartbeatInterval is too low\");clearTimeout(e._sendHeartbeatsTimer);var t=function(){clearTimeout(e._sendHeartbeatsTimer),e.sendHeartbeat(0),e._sendHeartbeatsTimer=setTimeout(t,e.heartbeatInterval)};e._sendHeartbeatsTimer=setTimeout(t,1)};l.prototype.stopSendingHeartbeats=function(){var e=this;clearTimeout(e._sendHeartbeatsTimer)};l.prototype.startReading=function(){var e=this,t=e.ws,r;function n(i){if(r=typeof i.data==\"string\"?ae.parseMsg(i.data):fe.parseMsg(h(i.data)),r.t===f.MsgTypeProtocolError){var a=r.size;a===f.ErrorAbnormal?t[E]=X:a===f.ErrorUnsupported?t[E]=W:a===f.ErrorTimeout?t[E]=Q:t[E]=D,t.close(4e3+a)}else r.size!==0&&r.t!==f.MsgTypeHeartbeat?t.onmessage=o:(e.handleMsg(r),r=null)}function o(i){var a=i.data;t.onmessage=n,e.handleMsg(r,typeof a==\"string\"?a:h(a)),r=null}function s(i){var a=typeof i.data==\"string\"?ae.parseVersion(i.data):fe.parseVersion(h(i.data));a!==f.Version?(t[E]=W,e.closeError(f.ErrorUnsupported)):(t.onmessage=n,e.heartbeatInterval>0&&e.startSendingHeartbeats())}t.onmessage=s,t._bufferedMessages&&(t._bufferedMessages.forEach(function(i){t.onmessage({data:i})}),t._bufferedMessages=null)};var S={};l.prototype.handleMsg=function(e,t){var r=this,n=S[e.t];n?n.call(r,e,t):(r.ws&&(r.ws[E]=D),r.closeError(f.ErrorInvalidMsg))};S[f.MsgTypeSingleReq]=function(e,t){var r=this,n,o;if(n=r.handlers.findRequestHandler(e.name),o=function(s){r.sendMsg(f.MsgTypeSingleRes,e.id,null,0,s)},o.error=function(s){var i=s.message||String(s);r.sendMsg(f.MsgTypeErrorRes,e.id,null,0,i)},typeof n!=\"function\")o.error('no such operation \"'+e.name+'\"');else try{n(t,o,e.name)}catch(s){F(\"[gotalk] handler error:\",s.stack||\"\"+s),o.error(\"internal error\")}};function Y(e,t){var r=e.id;typeof r!=\"string\"&&(r=String.fromCharCode.apply(null,r));var n=this,o=n.pendingRes[r];if((e.t!==f.MsgTypeStreamRes||!t||(t.length||t.size)===0)&&(delete n.pendingRes[r],n.pendingClose&&!n.hasPendingRes&&n.end()),typeof o!=\"function\")return;e.t===f.MsgTypeErrorRes?(typeof t!=\"string\"&&(t=q.decode(t)),o(new Error(t),null)):o(null,t)}S[f.MsgTypeSingleRes]=Y;S[f.MsgTypeStreamRes]=Y;S[f.MsgTypeErrorRes]=Y;S[f.MsgTypeNotification]=function(e,t){var r=this.handlers.findNotificationHandler(e.name);r&&r(t,e.name)};S[f.MsgTypeHeartbeat]=function(e){this.emit(\"heartbeat\",{time:new Date(e.size*1e3),load:e.wait})};l.prototype._connectionStatusChange=function(e){if(this._isOpen==e)return;this._isOpen=e,e&&Te(this)};function de(e,t,r){try{e.ws.send(t),r&&e.ws.send(r)}catch(n){if((!e.ws||e.ws.readyState>WebSocket.OPEN)&&(n=new Error(\"socket is closed\"),ue(e,t,r))){M.console.warn(\"gotalk send error: \"+n+\" (retrying)\");return}throw n}}function Te(e){if(e._sendq.length==0)return;var t=e._sendq;e._sendq=[];for(var r,n,o=0;o<t.length;o++)n=t[o],de(e,n[0],n[1])}function ue(e,t,r){return e._sendq.length>=e.sendBufferLimit?!1:(e._sendq.push([t,r]),!0)}l.prototype.sendMsg=function(e,t,r,n,o){var s=0;o&&(typeof o==\"string\"&&this.protocol===f.binary?s=q.sizeOf(o):s=o.length||o.size||0,s==0&&(o=null));var i=this,a=i.protocol.makeMsg(e,t,r,n,s);if(i._isOpen)de(i,a,o);else if(!ue(i,a,o))throw new Error(\"socket is closed\")};l.prototype.closeError=function(e){var t=this,r;if(t.ws){try{t.ws.send(t.protocol.makeMsg(f.MsgTypeProtocolError,null,null,0,e))}catch(n){}t.ws.close(4e3+e)}};l.prototype.notify=function(e,t){var r=JSON.stringify(t);return this.bufferNotify(e,r)};l.prototype.bufferNotify=function(e,t){this.sendMsg(f.MsgTypeNotification,null,e,0,t)};var ce=\"0000\";l.prototype.bufferRequest=function(e,t,r){var n=this;return new Promise(function(o,s){var i=n.nextOpID++;n.nextOpID===1679616&&(n.nextOpID=0),i=i.toString(36),i=ce.substr(0,4-i.length)+i;var a=function(g,_){g?s(g):o(_),r&&r(g,_)};n.pendingRes[i]=a;try{n.sendMsg(f.MsgTypeSingleReq,i,e,0,t)}catch(g){delete n.pendingRes[i],a(g)}})};l.prototype.request=function(e,t,r){var n;t!==void 0&&(r===void 0&&typeof t==\"function\"?r=t:n=JSON.stringify(t));var o=this.bufferRequest(e,n).then(function(s){var i=G(s);return r&&r(null,i),i});return r&&(o=o.catch(function(s){r(s)})),o};l.prototype.requestp=l.prototype.request;l.prototype.bufferRequestp=l.prototype.bufferRequest;var O=function(e,t,r){return Object.create(O.prototype,{s:{value:e},op:{value:t,enumerable:!0},id:{value:r,enumerable:!0}})};c.mixin(O.prototype);O.prototype.write=function(e){this.ended||(this.started?this.s.sendMsg(f.MsgTypeStreamReqPart,this.id,null,0,e):(this.started=!0,this.s.sendMsg(f.MsgTypeStreamReq,this.id,this.op,0,e)),(!e||e.length===0||e.size===0)&&(this.ended=!0))};O.prototype.end=function(){this.write(null)};l.prototype.streamRequest=function(e){var t=this,r=t.nextStreamID++;t.nextStreamID===46656&&(t.nextStreamID=0),r=r.toString(36),r=\"!\"+ce.substr(0,3-r.length)+r;var n=O(t,e,r);return t.pendingRes[r]=function(o,s){o?n.emit(\"end\",o):!s||s.length===0?n.emit(\"end\",null):n.emit(\"data\",s)},n};function b(){return Object.create(b.prototype,{reqHandlers:{value:{}},reqFallbackHandler:{value:null,writable:!0},noteHandlers:{value:{}},noteFallbackHandler:{value:null,writable:!0}})}y.Handlers=b;b.prototype.handleBufferRequest=function(e,t){e?this.reqHandlers[e]=t:this.reqFallbackHandler=t};b.prototype.handleRequest=function(e,t){return this.handleBufferRequest(e,function(r,n,o){var s=function(a){return n(JSON.stringify(a))};s.error=n.error;var i=G(r);t(i,s,o)})};b.prototype.handleBufferNotification=function(e,t){e?this.noteHandlers[e]=t:this.noteFallbackHandler=t};b.prototype.handleNotification=function(e,t){this.handleBufferNotification(e,function(r,n){t(G(r),n)})};b.prototype.findRequestHandler=function(e){var t=this.reqHandlers[e];return t||this.reqFallbackHandler};b.prototype.findNotificationHandler=function(e){var t=this.noteHandlers[e];return t||this.noteFallbackHandler};var pe=!1;function Re(e,t,r){var n;try{n=new WebSocket(t),n.binaryType=\"arraybuffer\",n.onclose=function(o){u.developmentMode&&!pe&&le==u.defaultResponderAddress&&(pe=!0,F(\"gotalk connection failed with code \"+K(o.code)+\". If you are serving gotalk.js yourself, remember to set gotalk.defaultResponderAddress to the gotalk websocket endpoint.\"));var s=new Error(\"connection failed: \"+K(o.code));r&&r(s)},n.onopen=function(o){n.onerror=void 0,e.adoptWebSocket(n),e.handshake(),e._connectionStatusChange(!0),r&&r(null,e),e.emit(\"open\",e),e.startReading()},n.onmessage=function(o){n._bufferedMessages||(n._bufferedMessages=[]),n._bufferedMessages.push(o.data)}}catch(o){F(\"[gotalk] WebSocket init error:\",o.stack||\"\"+o),e._connectionStatusChange(!1),r&&r(o),e.emit(\"close\",o)}}function _e(e){e||(e=u.defaultResponderAddress);var t=e.substr(0,4);if(t!=\"ws:/\"&&t!=\"wss:\"&&(p.proto&&(e[0]==\"/\"?e[1]==\"/\"?e=p.wsproto+e:e=p.wsproto+p.host+e:e=p.wsproto+p.host+\"/\"+e)),!e)throw new Error(\"address not specified\");return e}l.prototype.open=function(e,t){var r=this;return!t&&typeof e==\"function\"&&(t=e,e=null),Re(r,_e(e),t),r};u.open=function(e,t,r,n){return l(r||u.defaultHandlers,n).open(e,t)};l.prototype.openKeepAlive=function(e){var t=this;return t.keepalive&&t.keepalive.disable(),t.keepalive=se(t,e),t.keepalive.enable(),t};u.connection=function(e,t,r){return l(t||u.defaultHandlers,r).openKeepAlive(e)};u.defaultHandlers=b();u.handleBufferRequest=function(e,t){return u.defaultHandlers.handleBufferRequest(e,t)};u.handle=function(e,t){return u.defaultHandlers.handleRequest(e,t)};u.handleBufferNotification=function(e,t){return u.defaultHandlers.handleBufferNotification(e,t)};u.handleNotification=function(e,t){return u.defaultHandlers.handleNotification(e,t)};Ee()});const De=w(P());var h=function(){return typeof Uint8Array==\"undefined\"?null:function(t){return t instanceof Uint8Array?t:new Uint8Array(t instanceof ArrayBuffer?t:new ArrayBuffer(t))}}();function c(){}c.prototype.addListener=function(e,t){if(typeof t!=\"function\")throw TypeError(\"listener must be a function\");if(!this.__events)return Object.defineProperty(this,\"__events\",{value:{},enumerable:!1,writable:!0}),this.__events[e]=[t],this;var r=this.__events[e];return r===void 0?(this.__events[e]=[t],this):(r.push(t),this)};c.prototype.on=c.prototype.addListener;c.prototype.once=function(e,t){var r=!1,n=function(){this.removeListener(e,n),r||(r=!0,t.apply(this,arguments))};return this.on(e,n)};c.prototype.removeListener=function(e,t){var r,n=this.__events?this.__events[e]:void 0;if(n!==void 0){for(;(r=n.indexOf(t))!==-1;)n.splice(r,1);return n.length===0&&delete this.__events[e],n.length}return this};c.prototype.removeAllListeners=function(e){this.__events&&(e?delete this.__events[e]:delete this.__events)};c.prototype.listeners=function(e){return e?this.__events?this.__events[e]:void 0:this.__events};c.prototype.emit=function(e){var t=this.__events?this.__events[e]:void 0;if(t===void 0)return!1;for(var r=0,n=t.length,o=Array.prototype.slice.call(arguments,1);r!==n;++r)t[r].apply(this,o);return!0};c.mixin=function(t){for(var r=t;r;){if(r.__proto__===Object.prototype)return r.__proto__=c.prototype,t;r=r.__proto__}return t};const m=w(j()),he=w(J());var v=new c;v.available=!1;v.onLine=!0;m.global.addEventListener&&(v.available=!0,v.onLine=typeof navigator!=\"undefined\"?navigator.onLine:!0,m.global.addEventListener(\"offline\",function(e){v.onLine=!1}),m.global.addEventListener(\"online\",function(e){v.onLine=!0,v.emit(\"online\")}));function se(e,t,r,n){r?r<100&&(r=100):r=500,(!n||n<r)&&(n=5e3);var o,s,i,a=0,g,_;return o={isEnabled:!1,isConnected:!1,enable:function(){o.enabled||(o.enabled=!0,a=0,o.isConnected||s())},disable:function(){o.enabled&&(clearTimeout(g),o.enabled=!1,a=0)}},s=function(){clearTimeout(g),e.open(t,function(k){_=new Date,k?i(k):(a=0,o.isConnected=!0,e.once(\"close\",i))})},i=function(k){if(clearTimeout(g),o.isConnected=!1,!o.enabled)return;if(v.available&&!v.onLine&&!(m.document&&m.document.location&&m.document.location.hostname!==\"localhost\"&&m.document.location.hostname!==\"127.0.0.1\"&&m.document.location.hostname!==\"[::1]\")){v.once(\"online\",i),a=0;return}k?k.isGotalkProtocolError?k.code===he.ErrorTimeout?a=0:a=n:a=a?Math.min(n,a*2):r:a=Math.max(100,r-(new Date-_)),g=setTimeout(s,a)},o}return ge();})();\n"...

Full Screen

Full Screen

events.go

Source:events.go Github

copy

Full Screen

...37func (e *Events) Exports() modules.Exports {38 return modules.Exports{39 Named: map[string]interface{}{40 "setTimeout": e.setTimeout,41 "clearTimeout": e.clearTimeout,42 "setInterval": e.setInterval,43 "clearInterval": e.clearInterval,44 },45 }46}47func noop() error { return nil }48func (e *Events) getTimerStopCh() (uint32, chan struct{}) {49 id := atomic.AddUint32(&e.timerStopCounter, 1)50 ch := make(chan struct{})51 e.timerStopsLock.Lock()52 e.timerStops[id] = ch53 e.timerStopsLock.Unlock()54 return id, ch55}56func (e *Events) stopTimerCh(id uint32) bool { //nolint:unparam57 e.timerStopsLock.Lock()58 defer e.timerStopsLock.Unlock()59 ch, ok := e.timerStops[id]60 if !ok {61 return false62 }63 delete(e.timerStops, id)64 close(ch)65 return true66}67func (e *Events) call(callback goja.Callable, args []goja.Value) error {68 // TODO: investigate, not sure GlobalObject() is always the correct value for `this`?69 _, err := callback(e.vu.Runtime().GlobalObject(), args...)70 return err71}72func (e *Events) setTimeout(callback goja.Callable, delay float64, args ...goja.Value) uint32 {73 runOnLoop := e.vu.RegisterCallback()74 id, stopCh := e.getTimerStopCh()75 if delay < 0 {76 delay = 077 }78 go func() {79 timer := time.NewTimer(time.Duration(delay * float64(time.Millisecond)))80 defer func() {81 e.stopTimerCh(id)82 if !timer.Stop() {83 <-timer.C84 }85 }()86 select {87 case <-timer.C:88 runOnLoop(func() error {89 return e.call(callback, args)90 })91 case <-stopCh:92 runOnLoop(noop)93 case <-e.vu.Context().Done():94 e.vu.State().Logger.Warnf("setTimeout %d was stopped because the VU iteration was interrupted", id)95 runOnLoop(noop)96 }97 }()98 return id99}100func (e *Events) clearTimeout(id uint32) {101 e.stopTimerCh(id)102}103func (e *Events) setInterval(callback goja.Callable, delay float64, args ...goja.Value) uint32 {104 runOnLoop := e.vu.RegisterCallback()105 id, stopCh := e.getTimerStopCh()106 go func() {107 ticker := time.NewTicker(time.Duration(delay * float64(time.Millisecond)))108 defer func() {109 e.stopTimerCh(id)110 ticker.Stop()111 }()112 for {113 select {114 case <-ticker.C:...

Full Screen

Full Screen

clearTimeout

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 c := cron.New()4 c.AddFunc("* * * * * ?", func() { fmt.Println("Every minute on the half hour") })5 c.Start()6 time.Sleep(100 * time.Second)7}

Full Screen

Full Screen

clearTimeout

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 c := make(chan int)4 go func() {5 time.Sleep(2 * time.Second)6 }()7 select {8 fmt.Println("received")9 case <-time.After(1 * time.Second):10 fmt.Println("timeout")11 }12}13import (14func main() {15 c := make(chan int)16 go func() {17 time.Sleep(1 * time.Second)18 }()19 select {20 fmt.Println("received")21 case <-time.After(2 * time.Second):22 fmt.Println("timeout")23 }24}25Related Posts: Node.js setTimeout() and clearTimeout() Methods26Node.js setInterval() and clearInterval() Methods27Node.js setImmediate() and clearImmediate() Methods28Node.js process.nextTick() Method29Node.js process.exit() Method30Node.js process.cwd() Method31Node.js process.chdir() Method32Node.js process.kill() Method33Node.js process.memoryUsage() Method34Node.js process.uptime() Method35Node.js process.disconnect() Method36Node.js process.send() Method37Node.js process.hrtime() Method38Node.js process.cpuUsage() Method39Node.js process.abort() Method40Node.js process.umask() Method41Node.js process.getuid() Method42Node.js process.geteuid() Method43Node.js process.getgid() Method44Node.js process.getegid() Method45Node.js process.setuid() Method46Node.js process.seteuid() Method47Node.js process.setgid() Method48Node.js process.setegid() Method49Node.js process.getgroups() Method50Node.js process.setgroups() Method51Node.js process.initgroups() Method

Full Screen

Full Screen

clearTimeout

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 c := make(chan int, 1)4 go func() {5 time.Sleep(5 * time.Second)6 }()7 select {8 fmt.Println("Received")9 case <-time.After(2 * time.Second):10 fmt.Println("Timeout")11 }12}13import (14func main() {15 c := make(chan int, 1)16 go func() {17 time.Sleep(2 * time.Second)18 }()19 select {20 fmt.Println("Received")21 case <-time.After(5 * time.Second):22 fmt.Println("Timeout")23 }24}

Full Screen

Full Screen

clearTimeout

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 c := make(chan bool)4 go func() {5 fmt.Println("Hello")6 time.Sleep(10 * time.Second)7 fmt.Println("World")8 }()9}10clearTimeout(timer)11import (12func main() {13 c := make(chan bool)14 go func() {15 fmt.Println("Hello")16 timer := time.NewTimer(10 * time.Second)17 fmt.Println("World")18 }()19}20clearTimeout(timer)21import (22func main()

Full Screen

Full Screen

clearTimeout

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 c := make(chan string)4 go func() {5 time.Sleep(4 * time.Second)6 }()7 go func() {8 time.Sleep(4 * time.Second)9 }()10 for i := 0; i < 2; i++ {11 select {12 fmt.Println(res)13 case <-time.After(1 * time.Second):14 fmt.Println("timeout")15 }16 }17}18import (19func main() {20 c := make(chan string, 1)21 go func() {22 time.Sleep(2 * time.Second)23 }()24 go func() {25 time.Sleep(4 * time.Second)26 }()27 select {28 fmt.Println(res)29 case <-time.After(3 * time.Second):30 fmt.Println("timeout")31 }32}33import (34func main() {35 c := make(chan string, 1)36 go func() {37 time.Sleep(2 * time.Second)38 }()39 go func() {40 time.Sleep(4 * time.Second)41 }()42 select {43 fmt.Println(res)44 case <-time.After(1 * time.Second):

Full Screen

Full Screen

clearTimeout

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 runtime.GOMAXPROCS(2)4 timer1 := time.NewTimer(time.Second * 2)5 fmt.Println("Timer 1 expired")6 timer2 := time.NewTimer(time.Second)7 go func() {8 fmt.Println("Timer 2 expired")9 }()10 stop2 := timer2.Stop()11 if stop2 {12 fmt.Println("Timer 2 stopped")13 }14}

Full Screen

Full Screen

clearTimeout

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("main method started")4 ch := make(chan bool)5 go func() {6 time.Sleep(2 * time.Second)7 fmt.Println("goroutine executed")8 }()9 time.Sleep(1 * time.Second)10 fmt.Println("main method stopped")11}12import (13func main() {14 fmt.Println("main method started")15 ch := make(chan bool)16 go func() {17 time.Sleep(2 * time.Second)18 fmt.Println("goroutine executed")19 }()20 time.Sleep(1 * time.Second)21 close(ch)22 fmt.Println("main method stopped")23}

Full Screen

Full Screen

clearTimeout

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 c := make(chan string)4 go func() {5 time.Sleep(2 * time.Second)6 }()7 t := time.NewTimer(1 * time.Second)8 select {9 fmt.Println(res)10 fmt.Println("timeout 1")11 }12 t2 := time.NewTimer(3 * time.Second)13 go func() {14 time.Sleep(2 * time.Second)15 }()16 select {17 fmt.Println(res)18 fmt.Println("timeout 2")19 }20}21import (22func main() {23 c := make(chan string)24 go func() {25 time.Sleep(2 * time.Second)26 }()27 t := time.NewTimer(1 * time.Second)28 select {29 fmt.Println(res)30 fmt.Println("timeout 1")31 }32 t.Stop()33 t2 := time.NewTimer(3 * time.Second)34 go func() {35 time.Sleep(2 * time.Second)36 }()37 select {38 fmt.Println(res)39 fmt.Println("timeout 2")40 }41}

Full Screen

Full Screen

clearTimeout

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("start")4 c := make(chan string)5 go func() {6 time.Sleep(2 * time.Second)7 }()8 go func() {9 time.Sleep(2 * time.Second)10 }()11 t := time.NewTimer(1 * time.Second)12 for i := 0; i < 2; i++ {13 select {14 fmt.Println(res)15 fmt.Println("timeout")16 }17 }18}19import (20func main() {21 fmt.Println("start")22 c := make(chan string)23 go func() {24 time.Sleep(2 * time.Second)25 }()26 go func() {27 time.Sleep(2 * time.Second)28 }()

Full Screen

Full Screen

clearTimeout

Using AI Code Generation

copy

Full Screen

1import "fmt"2import "time"3import "events"4func main() {5 fmt.Println("Clear Timeout Example")6 var timeout = events.setTimeout(func() {7 fmt.Println("Hello World")8 }, 1000)9 fmt.Println("Hello World")10 events.clearTimeout(timeout)11}12import "fmt"13import "time"14import "events"15func main() {16 fmt.Println("Clear Immediate Example")17 var immediate = events.setImmediate(func() {18 fmt.Println("Hello World")19 })20 fmt.Println("Hello World")21 events.clearImmediate(immediate)22}23import "fmt"24import "time"25import "events"26func main() {27 fmt.Println("Clear Immediate Example")28 var immediate = events.setImmediate(func() {29 fmt.Println("Hello World")30 })31 fmt.Println("Hello World")32 events.clearImmediate(immediate)33}34import "fmt"35import "time"36import "events"37func main() {38 fmt.Println("Once Example")39 var eventEmitter = events.newEventEmitter()40 eventEmitter.once("event", func() {41 fmt.Println("Hello World")42 })43 eventEmitter.emit("event")44 eventEmitter.emit("event")45}46import "fmt"47import "time"48import "events"49func main() {50 fmt.Println("Prepend Listener Example")51 var eventEmitter = events.newEventEmitter()52 eventEmitter.prependListener("event", func() {53 fmt.Println("Hello World")54 })55 eventEmitter.emit("event")56}57import "fmt"58import "time"59import "events"60func main() {61 fmt.Println("Prepend Once Listener Example")62 var eventEmitter = events.newEventEmitter()63 eventEmitter.prependOnceListener("event", func() {64 fmt.Println("Hello World")65 })66 eventEmitter.emit("

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run K6 automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful