Repository: go-gitea/gitea
File: web_src/js/features/eventsource.sharedworker.ts (TypeScript)

Code before:
      this.notifyClients({
        type: eventType,
        data: event.data,
      });
    });
  }

  notifyClients(event: {type: string, data: any}) {
    for (const client of this.clients) {
      client.postMessage(event);
    }
  }

  status(port: MessagePort) {
    port.postMessage({
      type: 'status',
      message: `url: ${this.url} readyState: ${this.eventSource?.readyState}`,
    });
  }
}

const sourcesByUrl = new Map<string, Source | null>();
const sourcesByPort = new Map<MessagePort, Source | null>();

(self as unknown as SharedWorkerGlobalScope).addEventListener('connect', (e: MessageEvent) => {
  for (const port of e.ports || []) {
    port.addEventListener('message', (event: MessageEvent) => {
      if (!self.EventSource) {
        // some browsers (like PaleMoon, Firefox<53) don't support EventSource in SharedWorkerGlobalScope.
        // this event handler needs EventSource when doing "new Source(url)", so just post a message back to the caller,
        // in case the caller would like to use a fallback method to do its work.
        port.postMessage({type: 'no-event-source'});
        return;
      }
      if (event.data.type === 'start') {
        const url = event.data.url;
        let source = sourcesByUrl.get(url);
        if (source) {
          // we have a Source registered to this url
          source.register(port);
          sourcesByPort.set(port, source);
          return;
        }
        source = sourcesByPort.get(port);
        if (source) {
          if (source.eventSource && source.url === url) return;

          // How this has happened I don't understand...
          // deregister from that source
          const count = source.deregister(port);
          // Clean-up

Review comment (suggestion):
```suggestion
  for (const port of e.ports) {
```

Code after:
      this.notifyClients({
        type: eventType,
        data: event.data,
      });
    });
  }

  notifyClients(event: {type: string, data: any}) {
    for (const client of this.clients) {
      client.postMessage(event);
    }
  }

  status(port: MessagePort) {
    port.postMessage({
      type: 'status',
      message: `url: ${this.url} readyState: ${this.eventSource?.readyState}`,
    });
  }
}

const sourcesByUrl = new Map<string, Source | null>();
const sourcesByPort = new Map<MessagePort, Source | null>();

(self as unknown as SharedWorkerGlobalScope).addEventListener('connect', (e: MessageEvent) => {
  for (const port of e.ports) {
    port.addEventListener('message', (event: MessageEvent) => {
      if (!self.EventSource) {
        // some browsers (like PaleMoon, Firefox<53) don't support EventSource in SharedWorkerGlobalScope.
        // this event handler needs EventSource when doing "new Source(url)", so just post a message back to the caller,
        // in case the caller would like to use a fallback method to do its work.
        port.postMessage({type: 'no-event-source'});
        return;
      }
      if (event.data.type === 'start') {
        const url = event.data.url;
        let source = sourcesByUrl.get(url);
        if (source) {
          // we have a Source registered to this url
          source.register(port);
          sourcesByPort.set(port, source);
          return;
        }
        source = sourcesByPort.get(port);
        if (source) {
          if (source.eventSource && source.url === url) return;

          // How this has happened I don't understand...
          // deregister from that source
          const count = source.deregister(port);
          // Clean-up