()
| 17 | */ |
| 18 | |
| 19 | export const socketGuard = () => { |
| 20 | return function( |
| 21 | _target: any, |
| 22 | _propertyKey: string | symbol, |
| 23 | descriptor: PropertyDescriptor, |
| 24 | ): PropertyDescriptor { |
| 25 | const originalMethod = descriptor.value; |
| 26 | let ignoreSocketError = false; |
| 27 | let timeout: NodeJS.Timeout; |
| 28 | const socketErrCodes = [50000, 50001]; |
| 29 | descriptor.value = function(this: any, ...arg: any[]): any { |
| 30 | if (!this.io.socket.connected) { |
| 31 | return (() => {})(); |
| 32 | } |
| 33 | if (socketErrCodes.includes(arg[0]?.code)) { |
| 34 | if (ignoreSocketError) { |
| 35 | ignoreSocketError = false; |
| 36 | timeout && clearTimeout(timeout); |
| 37 | return originalMethod.apply(this, [...arg, false]); |
| 38 | } |
| 39 | ignoreSocketError = true; |
| 40 | timeout = setTimeout(() => { |
| 41 | ignoreSocketError = false; |
| 42 | }, 15 * 1000); |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | return originalMethod.apply(this, arg); |
| 47 | }; |
| 48 | return descriptor; |
| 49 | }; |
| 50 | }; |
no test coverage detected