Skip to content

Commit

Permalink
use function overloading for better types
Browse files Browse the repository at this point in the history
  • Loading branch information
Florent-Bouisset committed Jul 31, 2024
1 parent c10b367 commit 9fd3962
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
9 changes: 6 additions & 3 deletions src/compat/eme/eme-api-implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import getMozMediaKeysCallbacks, {
import getOldKitWebKitMediaKeyCallbacks, {
isOldWebkitMediaElement,
} from "./custom_media_keys/old_webkit_media_keys";
import type { ICustomMediaKeys } from "./custom_media_keys/types";
import type {
ICustomMediaEncryptedEvent,
ICustomMediaKeys,
} from "./custom_media_keys/types";
import getWebKitMediaKeysCallbacks from "./custom_media_keys/webkit_media_keys";
import { WebKitMediaKeysConstructor } from "./custom_media_keys/webkit_media_keys_constructor";

Expand Down Expand Up @@ -64,7 +67,7 @@ export interface IEmeApiImplementation {
*/
onEncrypted: (
target: IEventTargetLike,
listener: (evt: unknown) => void,
listener: (evt: ICustomMediaEncryptedEvent) => void,
cancelSignal: CancellationSignal,
) => void;

Expand Down Expand Up @@ -286,7 +289,7 @@ function createOnEncryptedForWebkit(): IEmeApiImplementation["onEncrypted"] {
);
const onEncrypted = (
target: IEventTargetLike,
listener: (event?: Event) => void,
listener: (event: ICustomMediaEncryptedEvent) => void,
cancelSignal: CancellationSignal,
) => {
compatibleEventListener(
Expand Down
20 changes: 20 additions & 0 deletions src/compat/event_listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import type {
ICompatHTMLMediaElement,
ICompatPictureInPictureWindow,
} from "./browser_compatibility_types";
import type { ICustomMediaEncryptedEvent } from "./eme/custom_media_keys/types";

const BROWSER_PREFIXES = ["", "webkit", "moz", "ms"];

Expand Down Expand Up @@ -104,13 +105,32 @@ export type IEventTargetLike = HTMLElement | IEventEmitterLike | IEventEmitter<u
* @returns {Function} - Returns function allowing to easily add a callback to
* be triggered when that event is emitted on a given event target.
*/

function createCompatibleEventListener(
eventNames: Array<"needkey" | "encrypted">,
prefixes?: string[],
): (
element: IEventTargetLike,
listener: (event: ICustomMediaEncryptedEvent) => void,
cancelSignal: CancellationSignal,
) => void;

function createCompatibleEventListener(
eventNames: string[],
prefixes?: string[],
): (
element: IEventTargetLike,
listener: (event?: Event) => void,
cancelSignal: CancellationSignal,
) => void;

function createCompatibleEventListener(
eventNames: string[] | Array<"needkey" | "encrypted">,
prefixes?: string[],
): (
element: IEventTargetLike,
listener: (event?: Event | MediaEncryptedEvent) => void,
cancelSignal: CancellationSignal,
) => void {
let mem: string | undefined;
const prefixedEvents = eventPrefixed(eventNames, prefixes);
Expand Down
3 changes: 1 addition & 2 deletions src/main_thread/decrypt/content_decryptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import type { ICustomMediaKeys, ICustomMediaKeySystemAccess } from "../../compat/eme";
import eme, { getInitData } from "../../compat/eme";
import type { ICustomMediaEncryptedEvent } from "../../compat/eme/custom_media_keys/types";
import config from "../../config";
import { EncryptedMediaError, OtherError } from "../../errors";
import log from "../../log";
Expand Down Expand Up @@ -169,7 +168,7 @@ export default class ContentDecryptor extends EventEmitter<IContentDecryptorEven
mediaElement,
(evt) => {
log.debug("DRM: Encrypted event received from media element.");
const initData = getInitData(evt as ICustomMediaEncryptedEvent);
const initData = getInitData(evt);
if (initData !== null) {
this.onInitializationData(initData);
}
Expand Down

0 comments on commit 9fd3962

Please sign in to comment.