Skip to content

Commit

Permalink
Merge branch 'main' into features/ui
Browse files Browse the repository at this point in the history
  • Loading branch information
GeekyEggo committed Oct 21, 2024
2 parents 279a1d6 + b79f87e commit d09ee64
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 27 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@

# Change Log

## vNext
## 1.2.0

### ✨ New

- Add support for Chinese (Traditional).

### 🐞 Fix

- Fix types of `EventEmitter` event arguments.

## 1.1.0

### ✨ New
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@elgato/streamdeck",
"version": "1.1.0",
"version": "1.2.0",
"description": "The official Node.js SDK for creating Stream Deck plugins.",
"main": "./dist/index.js",
"type": "module",
Expand Down
58 changes: 57 additions & 1 deletion src/common/__tests__/event-emitter.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EventEmitter } from "../event-emitter";
import type { Expect, TypesAreEqual } from "../../../tests/utils";
import { type EventArgs, EventEmitter, type EventsOf } from "../event-emitter";

describe("EventEmitter", () => {
describe("adding listeners", () => {
Expand Down Expand Up @@ -363,10 +364,65 @@ describe("EventEmitter", () => {
expect(two).toHaveBeenCalledWith("Hello world");
});
});

/* eslint-disable @typescript-eslint/no-unused-vars */
describe("types", () => {
/**
* Event map
*/
test("event map", () => {
// @ts-expect-error: arguments of type `string` are not valid
const invalidArgs = new EventEmitter<{
invalid: string;
valid: [name: string];
}>();

// @ts-expect-error: key of type `Number` is not valid.
const invalidEventName = new EventEmitter<{
[1]: [name: string];
valid: [name: string];
}>();
});

/**
* Event names.
*/
test("event name", () => {
type eventName = Expect<
TypesAreEqual<EventsOf<EventMap>, "another" | "array" | "empty" | "message" | "other" | (string & {})>
>;

// @ts-expect-error: arguments of type `string` are not valid
type invalid = EventsOf<{
invalid: string;
valid: [name: string];
}>;
});

/**
* Event arguments.
*/
it("events args", () => {
type t = EventArgs<EventMap, "array">;

type empty = Expect<TypesAreEqual<EventArgs<EventMap, "empty">, []>>;
type single = Expect<TypesAreEqual<EventArgs<EventMap, "message">, [message: string]>>;
type multiple = Expect<TypesAreEqual<EventArgs<EventMap, "array">, [id: number, name: string]>>;

type invalid = EventArgs<
// @ts-expect-error: arguments of type `string` are not valid
{ invalid: string },
"invalid"
>;
});
});
/* eslint-enable @typescript-eslint/no-unused-vars */
});

type EventMap = {
message: [message: string];
other: [id: number];
another: [id: number];
empty: [];
array: [id: number, name: string];
};
27 changes: 5 additions & 22 deletions src/common/event-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,32 +240,15 @@ type EventMap<T> = {
/**
* Parsed {@link EventMap} whereby each property is a `string` that denotes an event name, and the associated value type defines the listener arguments.
*/
export type EventsOf<T> =
EventMap<unknown> extends T
? string
: keyof EventMap<
Pick<
T,
Extract<
{
[K in keyof T]: K extends string ? K : never;
}[keyof T],
{
[K in keyof T]: T[K] extends unknown[] ? K : never;
}[keyof T]
>
>
>;
export type EventsOf<TMap extends EventMap<TMap>> = keyof TMap | (string & {});

/**
* Parses the event arguments for the specified event from the event map.
*/
type EventArgs<TMap, TEvent extends string> = TMap extends {
[k in TEvent]: infer TArgs;
}
? TArgs extends [unknown]
? TArgs
: unknown[]
export type EventArgs<TMap extends EventMap<TMap>, TEvent extends EventsOf<TMap>> = TEvent extends keyof TMap
? TMap[TEvent] extends unknown[]
? TMap[TEvent]
: never
: unknown[];

/**
Expand Down

0 comments on commit d09ee64

Please sign in to comment.