mirror of
https://github.com/zulip/zulip-desktop.git
synced 2025-10-23 03:31:56 +00:00
xo: Fix unicorn/prefer-event-target.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import {EventEmitter} from "node:events";
|
import * as z from "zod";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
type ClipboardDecrypter,
|
type ClipboardDecrypter,
|
||||||
@@ -7,11 +7,11 @@ import {
|
|||||||
import {type NotificationData, newNotification} from "./notification/index.ts";
|
import {type NotificationData, newNotification} from "./notification/index.ts";
|
||||||
import {ipcRenderer} from "./typed-ipc-renderer.ts";
|
import {ipcRenderer} from "./typed-ipc-renderer.ts";
|
||||||
|
|
||||||
type ListenerType = (...arguments_: any[]) => void;
|
type ListenerType = (...arguments_: unknown[]) => void;
|
||||||
|
|
||||||
/* eslint-disable @typescript-eslint/naming-convention */
|
/* eslint-disable @typescript-eslint/naming-convention */
|
||||||
export type ElectronBridge = {
|
export type ElectronBridge = {
|
||||||
send_event: (eventName: string | symbol, ...arguments_: unknown[]) => boolean;
|
send_event: (eventName: string, ...arguments_: unknown[]) => boolean;
|
||||||
on_event: (eventName: string, listener: ListenerType) => void;
|
on_event: (eventName: string, listener: ListenerType) => void;
|
||||||
new_notification: (
|
new_notification: (
|
||||||
title: string,
|
title: string,
|
||||||
@@ -32,15 +32,26 @@ let idle = false;
|
|||||||
// Indicates the time at which user was last active
|
// Indicates the time at which user was last active
|
||||||
let lastActive = Date.now();
|
let lastActive = Date.now();
|
||||||
|
|
||||||
export const bridgeEvents = new EventEmitter(); // eslint-disable-line unicorn/prefer-event-target
|
export const bridgeEvents = new EventTarget();
|
||||||
|
|
||||||
|
export class BridgeEvent extends Event {
|
||||||
|
constructor(
|
||||||
|
type: string,
|
||||||
|
public readonly arguments_: unknown[] = [],
|
||||||
|
) {
|
||||||
|
super(type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* eslint-disable @typescript-eslint/naming-convention */
|
/* eslint-disable @typescript-eslint/naming-convention */
|
||||||
const electron_bridge: ElectronBridge = {
|
const electron_bridge: ElectronBridge = {
|
||||||
send_event: (eventName: string | symbol, ...arguments_: unknown[]): boolean =>
|
send_event: (eventName: string, ...arguments_: unknown[]): boolean =>
|
||||||
bridgeEvents.emit(eventName, ...arguments_),
|
bridgeEvents.dispatchEvent(new BridgeEvent(eventName, arguments_)),
|
||||||
|
|
||||||
on_event(eventName: string, listener: ListenerType): void {
|
on_event(eventName: string, listener: ListenerType): void {
|
||||||
bridgeEvents.on(eventName, listener);
|
bridgeEvents.addEventListener(eventName, (event) => {
|
||||||
|
listener(...z.instanceof(BridgeEvent).parse(event).arguments_);
|
||||||
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
new_notification: (
|
new_notification: (
|
||||||
@@ -65,28 +76,25 @@ const electron_bridge: ElectronBridge = {
|
|||||||
};
|
};
|
||||||
/* eslint-enable @typescript-eslint/naming-convention */
|
/* eslint-enable @typescript-eslint/naming-convention */
|
||||||
|
|
||||||
bridgeEvents.on("total_unread_count", (unreadCount: unknown) => {
|
bridgeEvents.addEventListener("total_unread_count", (event) => {
|
||||||
if (typeof unreadCount !== "number") {
|
const [unreadCount] = z
|
||||||
throw new TypeError("Expected string for unreadCount");
|
.tuple([z.number()])
|
||||||
}
|
.parse(z.instanceof(BridgeEvent).parse(event).arguments_);
|
||||||
|
|
||||||
ipcRenderer.send("unread-count", unreadCount);
|
ipcRenderer.send("unread-count", unreadCount);
|
||||||
});
|
});
|
||||||
|
|
||||||
bridgeEvents.on("realm_name", (realmName: unknown) => {
|
bridgeEvents.addEventListener("realm_name", (event) => {
|
||||||
if (typeof realmName !== "string") {
|
const [realmName] = z
|
||||||
throw new TypeError("Expected string for realmName");
|
.tuple([z.string()])
|
||||||
}
|
.parse(z.instanceof(BridgeEvent).parse(event).arguments_);
|
||||||
|
|
||||||
const serverUrl = location.origin;
|
const serverUrl = location.origin;
|
||||||
ipcRenderer.send("realm-name-changed", serverUrl, realmName);
|
ipcRenderer.send("realm-name-changed", serverUrl, realmName);
|
||||||
});
|
});
|
||||||
|
|
||||||
bridgeEvents.on("realm_icon_url", (iconUrl: unknown) => {
|
bridgeEvents.addEventListener("realm_icon_url", (event) => {
|
||||||
if (typeof iconUrl !== "string") {
|
const [iconUrl] = z
|
||||||
throw new TypeError("Expected string for iconUrl");
|
.tuple([z.string()])
|
||||||
}
|
.parse(z.instanceof(BridgeEvent).parse(event).arguments_);
|
||||||
|
|
||||||
const serverUrl = location.origin;
|
const serverUrl = location.origin;
|
||||||
ipcRenderer.send(
|
ipcRenderer.send(
|
||||||
"realm-icon-changed",
|
"realm-icon-changed",
|
||||||
|
@@ -1,21 +1,21 @@
|
|||||||
import {contextBridge} from "electron/renderer";
|
import {contextBridge} from "electron/renderer";
|
||||||
|
|
||||||
import electron_bridge, {bridgeEvents} from "./electron-bridge.ts";
|
import electron_bridge, {BridgeEvent, bridgeEvents} from "./electron-bridge.ts";
|
||||||
import * as NetworkError from "./pages/network.ts";
|
import * as NetworkError from "./pages/network.ts";
|
||||||
import {ipcRenderer} from "./typed-ipc-renderer.ts";
|
import {ipcRenderer} from "./typed-ipc-renderer.ts";
|
||||||
|
|
||||||
contextBridge.exposeInMainWorld("electron_bridge", electron_bridge);
|
contextBridge.exposeInMainWorld("electron_bridge", electron_bridge);
|
||||||
|
|
||||||
ipcRenderer.on("logout", () => {
|
ipcRenderer.on("logout", () => {
|
||||||
bridgeEvents.emit("logout");
|
bridgeEvents.dispatchEvent(new BridgeEvent("logout"));
|
||||||
});
|
});
|
||||||
|
|
||||||
ipcRenderer.on("show-keyboard-shortcuts", () => {
|
ipcRenderer.on("show-keyboard-shortcuts", () => {
|
||||||
bridgeEvents.emit("show-keyboard-shortcuts");
|
bridgeEvents.dispatchEvent(new BridgeEvent("show-keyboard-shortcuts"));
|
||||||
});
|
});
|
||||||
|
|
||||||
ipcRenderer.on("show-notification-settings", () => {
|
ipcRenderer.on("show-notification-settings", () => {
|
||||||
bridgeEvents.emit("show-notification-settings");
|
bridgeEvents.dispatchEvent(new BridgeEvent("show-notification-settings"));
|
||||||
});
|
});
|
||||||
|
|
||||||
window.addEventListener("load", () => {
|
window.addEventListener("load", () => {
|
||||||
|
Reference in New Issue
Block a user