@todesktop/client-util
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -19,3 +19,3 @@ /** | ||
[key: string]: string; | ||
}>(eventMappings: E) => (event: "*" | keyof E, callback: (...any: any[]) => void) => void; | ||
}>(eventMappings: E) => (event: "*" | keyof E, callback: (...any: any[]) => void, eventId?: string) => void; | ||
export declare type MethodFunc = (...args: any[]) => unknown; | ||
@@ -22,0 +22,0 @@ export declare type ConstructorFunc = new (...args: unknown[]) => unknown; |
10
index.js
/** | ||
* Barebones EventEmitter implementation for API namespaces. | ||
*/ | ||
import { nanoid } from "nanoid"; | ||
import { checkIfCompatibleWithBackend } from "./checkIfCompatibleWithBackend.js"; | ||
@@ -12,9 +13,10 @@ const safeWindowToDesktop = typeof window !== "undefined" && window["todesktop"]; | ||
export const setupAddEventListener = (eventMappings) => async (event, callback) => { | ||
const eventId = nanoid(); | ||
const eventsToListenFor = event === "*" ? Object.keys(eventMappings) : [event]; | ||
for (const eventName of eventsToListenFor) { | ||
const legacyName = eventMappings[eventName]; | ||
safeWindowToDesktop === null || safeWindowToDesktop === void 0 ? void 0 : safeWindowToDesktop.on(legacyName, callback); | ||
safeWindowToDesktop === null || safeWindowToDesktop === void 0 ? void 0 : safeWindowToDesktop.on(legacyName, callback, eventId); | ||
} | ||
const off = setupRemoveEventListener(eventMappings); | ||
return async () => off(event, callback); | ||
return async () => off(event, callback, eventId); | ||
}; | ||
@@ -26,7 +28,7 @@ // TODO: Remove legacy event support in 2.0 | ||
*/ | ||
export const setupRemoveEventListener = (eventMappings) => (event, callback) => { | ||
export const setupRemoveEventListener = (eventMappings) => (event, callback, eventId) => { | ||
const eventsToListenFor = event === "*" ? Object.keys(eventMappings) : [event]; | ||
for (const eventName of eventsToListenFor) { | ||
const legacyName = eventMappings[eventName]; | ||
safeWindowToDesktop === null || safeWindowToDesktop === void 0 ? void 0 : safeWindowToDesktop.off(legacyName, callback); | ||
safeWindowToDesktop === null || safeWindowToDesktop === void 0 ? void 0 : safeWindowToDesktop.off(legacyName, callback, eventId); | ||
} | ||
@@ -33,0 +35,0 @@ }; |
{ | ||
"name": "@todesktop/client-util", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Utility functions for ToDesktop client libraries", | ||
@@ -29,4 +29,5 @@ "main": "index.js", | ||
"@todesktop/client-electron-types": "^16.0.9", | ||
"@todesktop/client-electron-updater-types": "^5.2.3" | ||
"@todesktop/client-electron-updater-types": "^5.2.3", | ||
"nanoid": "^4.0.2" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24476
576
3
+ Addednanoid@^4.0.2
+ Addednanoid@4.0.2(transitive)