Skip to content

Commit c8ec684

Browse files
committed
Revamped type system for v0.2.0.
1 parent 809bb6f commit c8ec684

File tree

69 files changed

+599
-260
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+599
-260
lines changed
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { Command } from "../../../command/command";
22
import { CommandStructure } from "../../../schema/command-structure";
33
import { CommandSocket } from "../../../command-socket/command-socket";
4-
export declare class TimedResponseCommand implements Command<TimedResponseCommandStructure, "commandsocket debug timed-response"> {
4+
export declare class TimedResponseCommand implements Command<TimedResponseCommandStructure> {
55
getName(): "commandsocket debug timed-response";
6-
execute(params: TimedResponseCommandStructure["params"], context: CommandSocket): Promise<TimedResponseCommandStructure["return"]>;
6+
execute(params: TimedResponseCommandStructure["parameter"], context: CommandSocket): Promise<TimedResponseCommandStructure["return"]>;
77
}
8-
export interface TimedResponseCommandStructure extends CommandStructure {
9-
params: number;
10-
return: string;
8+
export interface TimedResponseCommandStructure extends CommandStructure<number, string, "commandsocket debug timed-response"> {
119
}
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { Command } from "../../command/command";
22
import { CommandSocket } from "../../command-socket/command-socket";
3-
import { CommandStructure } from "../../schema/command-structure";
3+
import { CommandStructure, CommandStructureParameterType, CommandStructureReturnType } from "../../schema/command-structure";
44
import { CommandSocketIdentity } from "../../schema/command-socket-identity";
5-
export declare class IdentifyCommand implements Command<IdentifyCommandStructure, "commandsocket identify"> {
5+
export declare class IdentifyCommand implements Command<IdentifyCommandStructure> {
66
getName(): "commandsocket identify";
7-
execute(params: IdentifyCommandStructure["params"], context: CommandSocket): Promise<IdentifyCommandStructure["return"]>;
7+
execute(params: CommandStructureParameterType<IdentifyCommandStructure>, context: CommandSocket): Promise<CommandStructureReturnType<IdentifyCommandStructure>>;
88
}
9-
export interface IdentifyCommandStructure extends CommandStructure {
10-
params: void;
11-
return: CommandSocketIdentity;
9+
export interface IdentifyCommandStructure extends CommandStructure<void, CommandSocketIdentity, "commandsocket identify"> {
1210
}
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import { CommandStructure } from "../../schema/command-structure";
1+
import { CommandStructure, CommandStructureParameterType, CommandStructureReturnType } from "../../schema/command-structure";
22
import { Command } from "../../command/command";
33
import { CommandSocket } from "../../command-socket/command-socket";
4-
export declare class PingCommand implements Command<PingCommandStructure, "commandsocket ping"> {
4+
export declare class PingCommand implements Command<PingCommandStructure> {
55
getName(): "commandsocket ping";
6-
execute(params: PingCommandStructure["params"], context: CommandSocket): Promise<PingCommandStructure["return"]>;
6+
execute(params: CommandStructureParameterType<PingCommandStructure>, context: CommandSocket): Promise<CommandStructureReturnType<PingCommandStructure>>;
77
}
8-
export interface PingCommandStructure extends CommandStructure {
9-
params: "Ping!";
10-
return: "Pong!";
8+
export interface PingCommandStructure extends CommandStructure<"Ping!", "Pong!", "commandsocket ping"> {
119
}

.d.ts/command-socket/command-socket.d.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
import { CommandSetStructure, FullCommandSet, CommandIn, ParamTypeFor, ReturnTypeFor } from "../schema/command-set-structure";
1+
import { CommandInCommandSet, CommandNameInCommandSet, CommandSetStructure } from "../schema/command-set-structure";
22
import { CommandRegistry } from "../command/command-registry";
33
import { CommandSocketRequestMessage, CommandSocketResponseMessage } from "../schema/message/command-socket-message";
44
import { CommandSocketIdentity } from "../schema/command-socket-identity";
55
import { BuiltinCommandSet } from "../builtin/builtin-command-set";
66
import { ISocket } from "../socket/i-socket";
77
import { CommandSocketEvents } from "./command-socket-events";
88
import { CommandSocketState } from "./command-socket-state";
9+
import { IfAny } from "../util/any-types";
10+
import { CommandStructureParameterType, CommandStructureReturnType } from "../schema/command-structure";
11+
export declare type FullCommandSet<CS extends CommandSetStructure> = CS & BuiltinCommandSet;
12+
declare type ParameterOf<CS extends CommandSetStructure, CN extends keyof CS = CommandNameInCommandSet<CS>> = CommandStructureParameterType<CommandInCommandSet<FullCommandSet<CS>, CN>>;
13+
declare type ReturnTypeOf<CS extends CommandSetStructure, CN extends keyof CS = CommandNameInCommandSet<CS>> = CommandStructureReturnType<CommandInCommandSet<FullCommandSet<CS>, CN>>;
914
export declare abstract class CommandSocket<LCS extends CommandSetStructure = any, RCS extends CommandSetStructure = any> {
1015
static readonly ID_LENGTH: number;
1116
private static readonly BUILTIN_COMMANDS;
@@ -16,9 +21,9 @@ export declare abstract class CommandSocket<LCS extends CommandSetStructure = an
1621
private messageType;
1722
private outstandingRequests;
1823
private events;
19-
constructor(socket: ISocket, commandRegistry?: CommandRegistry<FullCommandSet<LCS>>);
20-
rawRequest<C extends CommandIn<RCS> = CommandIn<RCS>>(command: C, params?: ParamTypeFor<RCS, C>): Promise<CommandSocketResponseMessage>;
21-
invoke<C extends CommandIn<RCS> = CommandIn<RCS>>(command: C, params?: ParamTypeFor<RCS, C>): Promise<ReturnTypeFor<RCS, C>>;
24+
constructor(socket: ISocket, commandRegistry?: CommandRegistry<LCS>);
25+
rawRequest<CommandName extends IfAny<RCS, string, CommandNameInCommandSet<FullCommandSet<RCS>>>>(command: CommandName, params: ParameterOf<RCS, CommandName>): Promise<CommandSocketResponseMessage<CommandInCommandSet<FullCommandSet<RCS>, CommandName>>>;
26+
invoke<CommandName extends IfAny<RCS, string, CommandNameInCommandSet<FullCommandSet<RCS>>>>(command: CommandName, params: ParameterOf<RCS, CommandName>): Promise<ReturnTypeOf<RCS, CommandName>>;
2227
ping(): Promise<number>;
2328
close(): Promise<void>;
2429
protected handleMessage(data: any): void;
@@ -30,3 +35,4 @@ export declare abstract class CommandSocket<LCS extends CommandSetStructure = an
3035
getState(): CommandSocketState;
3136
getEvents(): CommandSocketEvents;
3237
}
38+
export {};

.d.ts/command/command-registry.d.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import { Command } from "./command";
2-
import { CommandSetStructure } from "../schema/command-set-structure";
32
import { CommandSocket } from "../command-socket/command-socket";
4-
export declare class CommandRegistry<CS extends CommandSetStructure = any> {
3+
import { CommandInCommandSet, CommandNameInCommandSet, CommandSetStructure } from "../schema/command-set-structure";
4+
import { IfAny } from "../util/any-types";
5+
import { CommandStructureParameterType, CommandStructureReturnType } from "../schema/command-structure";
6+
declare type ExecuteFunctionType<CS extends CommandSetStructure, CN extends keyof CS> = (params: CommandStructureParameterType<CommandInCommandSet<CS, CN>>, context: CommandSocket) => Promise<CommandStructureReturnType<CommandInCommandSet<CS, CN>>>;
7+
export declare class CommandRegistry<CommandSet extends CommandSetStructure = any> {
58
private commandMap;
6-
constructor(...commands: Array<Command<CS[keyof CS]>>);
7-
addCommands(...commands: Array<Command<CS[keyof CS]>>): void;
9+
constructor(...commands: Array<Command<CommandInCommandSet<CommandSet>>>);
10+
addCommands(...commands: Array<Command<CommandInCommandSet<CommandSet>>>): void;
11+
addInlineCommand<CommandName extends IfAny<CommandSet, string, CommandNameInCommandSet<CommandSet>>>(commandName: CommandName, implementation: ExecuteFunctionType<CommandSet, CommandName>): void;
812
hasCommand(command: string): boolean;
9-
getCommand<C extends keyof CS = keyof CS>(command: C): Command<CS[C]> | undefined;
10-
execute<C extends keyof CS = keyof CS>(command: C, params: CS[C]["params"], context: CommandSocket<any, any>): Promise<CS[C]["return"]>;
13+
getCommand<C extends IfAny<CommandSet, string, CommandNameInCommandSet<CommandSet>>>(command: C): IfAny<CommandSet, Command<any>, Command<CommandSet[C]>>;
14+
execute<CommandName extends IfAny<CommandSet, string, CommandNameInCommandSet<CommandSet>>>(commandName: CommandName, params: CommandStructureParameterType<CommandInCommandSet<CommandSet, CommandName>>, context: CommandSocket): Promise<IfAny<CommandSet, any, CommandStructureReturnType<CommandInCommandSet<CommandSet, CommandName>>>>;
1115
}
16+
export {};

.d.ts/command/command.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { CommandStructure } from "../schema/command-structure";
1+
import { CommandStructure, CommandStructureName, CommandStructureParameterType, CommandStructureReturnType } from "../schema/command-structure";
22
import { CommandSocket } from "../command-socket/command-socket";
3-
export interface Command<C extends CommandStructure, N extends string = string> {
4-
getName(): N;
5-
execute(params: C["params"], context: CommandSocket): Promise<C["return"]>;
3+
export interface Command<C extends CommandStructure> {
4+
getName(): CommandStructureName<C>;
5+
execute(params: CommandStructureParameterType<C>, context: CommandSocket): Promise<CommandStructureReturnType<C>>;
66
}

.d.ts/error/command-socket-error.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { CommandSocketRequestMessage, CommandSocketResponseMessage } from "../schema/message/command-socket-message";
22
import { CommandSocket } from "../command-socket/command-socket";
3+
import { CommandStructure } from "../schema/command-structure";
34
export declare class CommandSocketError extends Error {
45
constructor(message: string, name?: string);
5-
toMessage<P = any, R = any>(request: CommandSocketRequestMessage<P, R>, context: CommandSocket<any, any>): Promise<CommandSocketResponseMessage<P, R>>;
6+
toMessage<C extends CommandStructure>(request: CommandSocketRequestMessage<C>, context: CommandSocket): Promise<CommandSocketResponseMessage<C>>;
67
}

.d.ts/main.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
export { CommandSocket } from "./command-socket/command-socket";
1+
export { CommandSocket, FullCommandSet } from "./command-socket/command-socket";
22
export { ISocket, SocketEvents } from "./socket/i-socket";
3-
export { CommandSetStructure, FullCommandSet, CommandIn, ParamTypeFor, ReturnTypeFor } from "./schema/command-set-structure";
3+
export { CommandSetStructure } from "./schema/command-set-structure";
44
export { CommandStructure } from "./schema/command-structure";
55
export { CommandSocketIdentity } from "./schema/command-socket-identity";
66
export { CommandSocketMessage, CommandSocketResponseMessage, CommandSocketRequestMessage, CommandSocketSerializedError, CORRESPONDENCE_ID_LENGTH } from "./schema/message/command-socket-message";
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { CommandStructure as CSCommandStructure } from "./command-structure";
2-
import { BuiltinCommandSet } from "../builtin/builtin-command-set";
1+
import { CommandStructure } from "./command-structure";
2+
import { KnownKeys } from "../util/known-keys";
33
export interface CommandSetStructure {
4-
[commandName: string]: CSCommandStructure;
4+
[commandName: string]: CommandStructure;
55
}
6-
export declare type FullCommandSet<CS> = CS & BuiltinCommandSet;
7-
export declare type CommandIn<CS extends CommandSetStructure> = keyof FullCommandSet<CS>;
8-
export declare type ParamTypeFor<CS extends CommandSetStructure, C extends keyof CS> = FullCommandSet<CS>[C]["params"];
9-
export declare type ReturnTypeFor<CS extends CommandSetStructure, C extends keyof CS> = FullCommandSet<CS>[C]["return"];
6+
export declare type CommandNameInCommandSet<CS extends CommandSetStructure> = keyof CS & KnownKeys<CS>;
7+
export declare type CommandInCommandSet<CS extends CommandSetStructure, CN extends keyof CS = CommandNameInCommandSet<CS>> = CS[CN];
8+
export declare type CommandSetWithCommand<C extends CommandStructure> = C extends CommandStructure<infer P, infer R, infer N> ? Record<N, C> : never;

.d.ts/schema/command-structure.d.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
export interface CommandStructure {
2-
readonly params: any;
3-
readonly return: any;
1+
import { CommandSetWithCommand } from "./command-set-structure";
2+
export interface CommandStructure<ParameterType = any, ReturnType = any, Name extends string = string> {
3+
readonly name: Name;
4+
readonly parameter: ParameterType;
5+
readonly return: ReturnType;
46
}
7+
export declare type CommandStructureParameterType<C extends CommandStructure> = C extends CommandStructure<infer P> ? P : never;
8+
export declare type CommandStructureReturnType<C extends CommandStructure> = C extends CommandStructure<infer P, infer R> ? R : never;
9+
export declare type CommandStructureName<C extends CommandStructure> = C extends CommandStructure<infer P, infer R, infer N> ? N & keyof CommandSetWithCommand<C> : never;

0 commit comments

Comments
 (0)