Fix memory leak with abort controller
This commit is contained in:
		
							parent
							
								
									19878f6e28
								
							
						
					
					
						commit
						698021baf1
					
				| 
						 | 
					@ -47,7 +47,6 @@ export class ChatRequest {
 | 
				
			||||||
        count = count || 1
 | 
					        count = count || 1
 | 
				
			||||||
        _this.updating = true
 | 
					        _this.updating = true
 | 
				
			||||||
        _this.updatingMessage = 'Generating Image...'
 | 
					        _this.updatingMessage = 'Generating Image...'
 | 
				
			||||||
        const signal = _this.controller.signal
 | 
					 | 
				
			||||||
        const size = this.chat.settings.imageGenerationSize
 | 
					        const size = this.chat.settings.imageGenerationSize
 | 
				
			||||||
        const request: RequestImageGeneration = {
 | 
					        const request: RequestImageGeneration = {
 | 
				
			||||||
          prompt,
 | 
					          prompt,
 | 
				
			||||||
| 
						 | 
					@ -55,6 +54,16 @@ export class ChatRequest {
 | 
				
			||||||
          size,
 | 
					          size,
 | 
				
			||||||
          n: count
 | 
					          n: count
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        // fetchEventSource doesn't seem to throw on abort,
 | 
				
			||||||
 | 
					        // so we deal with it ourselves
 | 
				
			||||||
 | 
					        _this.controller = new AbortController()
 | 
				
			||||||
 | 
					        const signal = _this.controller.signal
 | 
				
			||||||
 | 
					        const abortListener = (e:Event) => {
 | 
				
			||||||
 | 
					          chatResponse.updateFromError('User aborted request.')
 | 
				
			||||||
 | 
					          signal.removeEventListener('abort', abortListener)
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        signal.addEventListener('abort', abortListener)
 | 
				
			||||||
 | 
					        // Create request
 | 
				
			||||||
        const fetchOptions = {
 | 
					        const fetchOptions = {
 | 
				
			||||||
          method: 'POST',
 | 
					          method: 'POST',
 | 
				
			||||||
          headers: {
 | 
					          headers: {
 | 
				
			||||||
| 
						 | 
					@ -180,7 +189,15 @@ export class ChatRequest {
 | 
				
			||||||
          // (streaming doesn't return counts, so we need to do it client side)
 | 
					          // (streaming doesn't return counts, so we need to do it client side)
 | 
				
			||||||
          chatResponse.setPromptTokenCount(promptTokenCount)
 | 
					          chatResponse.setPromptTokenCount(promptTokenCount)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					          // fetchEventSource doesn't seem to throw on abort,
 | 
				
			||||||
 | 
					          // so we deal with it ourselves
 | 
				
			||||||
 | 
					          _this.controller = new AbortController()
 | 
				
			||||||
          const signal = _this.controller.signal
 | 
					          const signal = _this.controller.signal
 | 
				
			||||||
 | 
					          const abortListener = (e:Event) => {
 | 
				
			||||||
 | 
					            chatResponse.updateFromError('User aborted request.')
 | 
				
			||||||
 | 
					            signal.removeEventListener('abort', abortListener)
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
 | 
					          signal.addEventListener('abort', abortListener)
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
          const fetchOptions = {
 | 
					          const fetchOptions = {
 | 
				
			||||||
            method: 'POST',
 | 
					            method: 'POST',
 | 
				
			||||||
| 
						 | 
					@ -192,15 +209,6 @@ export class ChatRequest {
 | 
				
			||||||
            signal
 | 
					            signal
 | 
				
			||||||
          }
 | 
					          }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
          // fetchEventSource doesn't seem to throw on abort,
 | 
					 | 
				
			||||||
          // so we deal with it ourselves
 | 
					 | 
				
			||||||
          const abortListener = (e:Event) => {
 | 
					 | 
				
			||||||
            _this.controller = new AbortController()
 | 
					 | 
				
			||||||
            chatResponse.updateFromError('User aborted request.')
 | 
					 | 
				
			||||||
            signal.removeEventListener('abort', abortListener)
 | 
					 | 
				
			||||||
          }
 | 
					 | 
				
			||||||
          signal.addEventListener('abort', abortListener)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
          if (opts.streaming) {
 | 
					          if (opts.streaming) {
 | 
				
			||||||
            /**
 | 
					            /**
 | 
				
			||||||
             * Streaming request/response
 | 
					             * Streaming request/response
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue