Yaffle/EventSource

.close() does not abort current event source fetch connections

Open

#120 opened on 2018年10月23日

GitHub で見る
 (20 comments) (2 reactions) (0 assignees)JavaScript (1,873 stars) (320 forks)batch import
help wanted

説明

Fetch supports abort, if it is not provided or called it leaves long leaving TCP connections open. Abort package : https://www.npmjs.com/package/abortcontroller-polyfill

Example code/changes that could make this work:

  1. Create a controller as described in https://www.npmjs.com/package/abortcontroller-polyfill

  2. Add the abort signal to the call of transport

  transport.open(xhr, onStart, onProgress, onFinish, requestURL, withCredentials, requestHeaders, controller.signal);
  1. Store the controller in order to be able to access it in close
    es.controller = controller; 
  1. Update close
  EventSourcePolyfill.prototype.close = function () {
    this.controller.abort();
    this._close();
  };
  1. Update the definition of transport when the browser supports fetch
  FetchTransport.prototype.open = function (xhr, onStartCallback, onProgressCallback, onFinishCallback, url, withCredentials, headers, signal) {
    // cache: "no-store"
    // https://bugs.chromium.org/p/chromium/issues/detail?id=453190
    var textDecoder = new TextDecoder();
    fetch(url, {
      headers: headers,
      credentials: withCredentials ? "include" : "same-origin",
      signal: signal
    })

コントリビューターガイド