Skip to content

Commit

Permalink
Fix typing to try to properly support module augmentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Wundero committed Dec 4, 2024
1 parent 72d1a60 commit 965a16c
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 30 deletions.
2 changes: 1 addition & 1 deletion packages/core/jsr.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sinkr/core",
"license": "MIT",
"version": "0.1.0",
"version": "0.2.0",
"exports": "./mod.ts"
}
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sinkr/core",
"version": "0.1.0",
"version": "0.2.0",
"type": "module",
"main": "src/index.ts",
"exports": {
Expand Down
9 changes: 5 additions & 4 deletions packages/core/src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import type {
} from "@sinkr/validators";
import { ClientReceiveSchema } from "@sinkr/validators";

import type { EventMap, UserInfo } from "./index";
import type { RealEventMap } from "./event-fallback";
import type { UserInfo } from "./index";
import {
connectSymbol,
countEventSymbol,
Expand Down Expand Up @@ -47,7 +48,7 @@ type GenericMessageEvent<T> = Prettify<
>;

type MappedEvents = {
[K in keyof EventMap]: GenericMessageEvent<EventMap[K]>;
[K in keyof RealEventMap]: GenericMessageEvent<RealEventMap[K]>;
};

type _EventMapWithDefaults = MappedEvents & DefaultEvents;
Expand Down Expand Up @@ -156,7 +157,7 @@ interface DefaultChannelEventMap {
[countEventSymbol]: { count: number };
}

type ChannelEventMap = Prettify<DefaultChannelEventMap & EventMap>;
type ChannelEventMap = Prettify<DefaultChannelEventMap & RealEventMap>;

class ChannelSinker extends Emittery<ChannelEventMap> {
private _count = 0;
Expand Down Expand Up @@ -214,7 +215,7 @@ interface DefaultPresenceChannelEventMap {
}

type PresenceChannelEventMap = Prettify<
DefaultPresenceChannelEventMap & EventMap
DefaultPresenceChannelEventMap & RealEventMap
>;

class PresenceSinker extends Emittery<PresenceChannelEventMap> {
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/event-fallback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { EventMap } from "./index";

type DefaultMap = Record<string, unknown>;

type EventKey = keyof EventMap;

export type RealEventMap = EventKey extends never ? DefaultMap : EventMap;
9 changes: 1 addition & 8 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
/**
* Default event map which is used if no custom events are specified.
*
* Supports sending any event with a string key.
*/
export type DefaultEventMap = Record<string, unknown>;

/**
* Event map for customizing which events can be sent or received.
*
* Override this with module augmentation to specify your own types.
*/
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
export interface EventMap extends DefaultEventMap {}
export interface EventMap {}

/**
* User information for presence channels.
Expand Down
27 changes: 14 additions & 13 deletions packages/core/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import type {
StreamedServerEndpointSchema,
} from "@sinkr/validators";

import type { EventMap, UserInfo } from "./index";
import type { RealEventMap } from "./event-fallback";
import type { UserInfo } from "./index";

type SendDataParam =
| z.infer<typeof ServerEndpointSchema>
Expand Down Expand Up @@ -165,8 +166,8 @@ class Sourcerer {
* @returns The HTTP status code Sinkr returned.
*/
async sendToChannel<
TEvent extends keyof EventMap,
TData extends EventMap[TEvent],
TEvent extends keyof RealEventMap,
TData extends RealEventMap[TEvent],
>(channel: string, event: TEvent, message: TData): Promise<number> {
return await this.sendData({
route: "channel",
Expand All @@ -184,8 +185,8 @@ class Sourcerer {
* @returns The HTTP status code Sinkr returned. The function may return before the stream is finished.
*/
async streamToChannel<
TEvent extends keyof EventMap,
TData extends EventMap[TEvent],
TEvent extends keyof RealEventMap,
TData extends RealEventMap[TEvent],
>(
channel: string,
event: TEvent,
Expand All @@ -209,8 +210,8 @@ class Sourcerer {
* @returns The HTTP status code Sinkr returned.
*/
async directMessage<
TEvent extends keyof EventMap,
TData extends EventMap[TEvent],
TEvent extends keyof RealEventMap,
TData extends RealEventMap[TEvent],
>(userId: string, event: TEvent, message: TData): Promise<number> {
return await this.sendData({
route: "direct",
Expand All @@ -228,8 +229,8 @@ class Sourcerer {
* @returns The HTTP status code Sinkr returned. The function may return before the stream is finished.
*/
async streamDirectMessage<
TEvent extends keyof EventMap,
TData extends EventMap[TEvent],
TEvent extends keyof RealEventMap,
TData extends RealEventMap[TEvent],
>(
userId: string,
event: TEvent,
Expand All @@ -252,8 +253,8 @@ class Sourcerer {
* @returns The HTTP status code Sinkr returned.
*/
async broadcastMessage<
TEvent extends keyof EventMap,
TData extends EventMap[TEvent],
TEvent extends keyof RealEventMap,
TData extends RealEventMap[TEvent],
>(event: TEvent, message: TData): Promise<number> {
return await this.sendData({
route: "broadcast",
Expand All @@ -269,8 +270,8 @@ class Sourcerer {
* @returns The HTTP status code Sinkr returned. The function may return before the stream is finished.
*/
async streamBroadcastMessage<
TEvent extends keyof EventMap,
TData extends EventMap[TEvent],
TEvent extends keyof RealEventMap,
TData extends RealEventMap[TEvent],
>(event: TEvent, stream: ReadableStream<TData>): Promise<number> {
return await this.sendData(
{
Expand Down
2 changes: 1 addition & 1 deletion packages/react/jsr.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sinkr/react",
"version": "0.1.0",
"version": "0.2.0",
"license": "MIT",
"exports": "./mod.ts"
}
4 changes: 2 additions & 2 deletions packages/react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sinkr/react",
"version": "0.1.0",
"version": "0.2.0",
"type": "module",
"main": "src/index.ts",
"exports": {
Expand All @@ -25,7 +25,7 @@
},
"prettier": "@sinkr/prettier-config",
"dependencies": {
"@sinkr/core": "npm:@jsr/sinkr__core@0.1.0",
"@sinkr/core": "npm:@jsr/sinkr__core@0.2.0",
"react": "^18.3.1",
"react-dom": "^18.3.1"
}
Expand Down

0 comments on commit 965a16c

Please sign in to comment.