Yaffle/EventSource

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

Open

#120 opened on Oct 23, 2018

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

Description

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
    })

Contributor guide

.close() does not abort current event source fetch connections · Yaffle/EventSource#120 | Good First Issue