Enable Typescript strictFunctionTypes (#32911)

1. Enable
[strictFunctionTypes](https://www.typescriptlang.org/tsconfig/#strictFunctionTypes)
2. Introduce `DOMEvent` helper type which sets `e.target`. Surely not
totally correct with that `Partial` but seems to work.
3. Various type-related refactors, change objects in
`eventsource.sharedworker.ts` to `Map`.
This commit is contained in:
silverwind 2024-12-21 19:59:25 +01:00 committed by GitHub
parent 09a0041965
commit c0e80dbe26
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 94 additions and 84 deletions

View file

@ -1,13 +1,10 @@
const sourcesByUrl = {};
const sourcesByPort = {};
class Source {
url: string;
eventSource: EventSource;
listening: Record<string, any>;
clients: Array<any>;
listening: Record<string, boolean>;
clients: Array<MessagePort>;
constructor(url) {
constructor(url: string) {
this.url = url;
this.eventSource = new EventSource(url);
this.listening = {};
@ -20,7 +17,7 @@ class Source {
this.listen('error');
}
register(port) {
register(port: MessagePort) {
if (this.clients.includes(port)) return;
this.clients.push(port);
@ -31,7 +28,7 @@ class Source {
});
}
deregister(port) {
deregister(port: MessagePort) {
const portIdx = this.clients.indexOf(port);
if (portIdx < 0) {
return this.clients.length;
@ -47,7 +44,7 @@ class Source {
this.eventSource = null;
}
listen(eventType) {
listen(eventType: string) {
if (this.listening[eventType]) return;
this.listening[eventType] = true;
this.eventSource.addEventListener(eventType, (event) => {
@ -58,13 +55,13 @@ class Source {
});
}
notifyClients(event) {
notifyClients(event: {type: string, data: any}) {
for (const client of this.clients) {
client.postMessage(event);
}
}
status(port) {
status(port: MessagePort) {
port.postMessage({
type: 'status',
message: `url: ${this.url} readyState: ${this.eventSource.readyState}`,
@ -72,7 +69,11 @@ class Source {
}
}
self.addEventListener('connect', (e: Event & {ports: Array<any>}) => {
const sourcesByUrl: Map<string, Source | null> = new Map();
const sourcesByPort: Map<MessagePort, Source | null> = new Map();
// @ts-expect-error: typescript bug?
self.addEventListener('connect', (e: MessageEvent) => {
for (const port of e.ports) {
port.addEventListener('message', (event) => {
if (!self.EventSource) {
@ -84,14 +85,14 @@ self.addEventListener('connect', (e: Event & {ports: Array<any>}) => {
}
if (event.data.type === 'start') {
const url = event.data.url;
if (sourcesByUrl[url]) {
if (sourcesByUrl.get(url)) {
// we have a Source registered to this url
const source = sourcesByUrl[url];
const source = sourcesByUrl.get(url);
source.register(port);
sourcesByPort[port] = source;
sourcesByPort.set(port, source);
return;
}
let source = sourcesByPort[port];
let source = sourcesByPort.get(port);
if (source) {
if (source.eventSource && source.url === url) return;
@ -101,30 +102,30 @@ self.addEventListener('connect', (e: Event & {ports: Array<any>}) => {
// Clean-up
if (count === 0) {
source.close();
sourcesByUrl[source.url] = null;
sourcesByUrl.set(source.url, null);
}
}
// Create a new Source
source = new Source(url);
source.register(port);
sourcesByUrl[url] = source;
sourcesByPort[port] = source;
sourcesByUrl.set(url, source);
sourcesByPort.set(port, source);
} else if (event.data.type === 'listen') {
const source = sourcesByPort[port];
const source = sourcesByPort.get(port);
source.listen(event.data.eventType);
} else if (event.data.type === 'close') {
const source = sourcesByPort[port];
const source = sourcesByPort.get(port);
if (!source) return;
const count = source.deregister(port);
if (count === 0) {
source.close();
sourcesByUrl[source.url] = null;
sourcesByPort[port] = null;
sourcesByUrl.set(source.url, null);
sourcesByPort.set(port, null);
}
} else if (event.data.type === 'status') {
const source = sourcesByPort[port];
const source = sourcesByPort.get(port);
if (!source) {
port.postMessage({
type: 'status',