@mingzey/typedrpc 1.0.2 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api.d.ts +2 -2
- package/dist/api.d.ts.map +1 -1
- package/dist/client.d.ts +1 -1
- package/dist/connection.d.ts +1 -1
- package/dist/connection.js +1 -1
- package/dist/{connecitons → connections}/basic.d.ts +1 -0
- package/dist/connections/basic.d.ts.map +1 -0
- package/dist/{connecitons → connections}/basic.js.map +1 -1
- package/dist/{connecitons → connections}/http.d.ts.map +1 -1
- package/dist/{connecitons → connections}/http.js.map +1 -1
- package/dist/{connecitons → connections}/socket.d.ts.map +1 -1
- package/dist/{connecitons → connections}/socket.js +10 -10
- package/dist/{connecitons → connections}/socket.js.map +1 -1
- package/dist/{connecitons → connections}/socketio.d.ts.map +1 -1
- package/dist/{connecitons → connections}/socketio.js +10 -10
- package/dist/{connecitons → connections}/socketio.js.map +1 -1
- package/dist/context.d.ts +1 -1
- package/dist/core.d.ts +5 -2
- package/dist/core.d.ts.map +1 -1
- package/dist/core.js +20 -13
- package/dist/core.js.map +1 -1
- package/dist/handler.d.ts +1 -1
- package/dist/index.d.ts +5 -5
- package/dist/index.js +5 -5
- package/dist/server.d.ts +6 -8
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +3 -1
- package/dist/server.js.map +1 -1
- package/package.json +28 -29
- package/dist/connecitons/basic.d.ts.map +0 -1
- package/dist/test/TestCase.d.ts +0 -6
- package/dist/test/TestCase.d.ts.map +0 -1
- package/dist/test/TestCase.js +0 -5
- package/dist/test/TestCase.js.map +0 -1
- package/dist/test/authorization.d.ts +0 -8
- package/dist/test/authorization.d.ts.map +0 -1
- package/dist/test/authorization.js +0 -169
- package/dist/test/authorization.js.map +0 -1
- package/dist/test/basic.d.ts +0 -7
- package/dist/test/basic.d.ts.map +0 -1
- package/dist/test/basic.js +0 -35
- package/dist/test/basic.js.map +0 -1
- package/dist/test/context.d.ts +0 -7
- package/dist/test/context.d.ts.map +0 -1
- package/dist/test/context.js +0 -55
- package/dist/test/context.js.map +0 -1
- package/dist/test/expressmix.d.ts +0 -7
- package/dist/test/expressmix.d.ts.map +0 -1
- package/dist/test/expressmix.js +0 -63
- package/dist/test/expressmix.js.map +0 -1
- package/dist/test/full-duplex.d.ts +0 -12
- package/dist/test/full-duplex.d.ts.map +0 -1
- package/dist/test/full-duplex.js +0 -77
- package/dist/test/full-duplex.js.map +0 -1
- package/dist/test/socket.d.ts +0 -8
- package/dist/test/socket.d.ts.map +0 -1
- package/dist/test/socket.js +0 -71
- package/dist/test/socket.js.map +0 -1
- package/dist/test/socketio.d.ts +0 -12
- package/dist/test/socketio.d.ts.map +0 -1
- package/dist/test/socketio.js +0 -77
- package/dist/test/socketio.js.map +0 -1
- package/dist/test.d.ts +0 -2
- package/dist/test.d.ts.map +0 -1
- package/dist/test.js +0 -33
- package/dist/test.js.map +0 -1
- package/src/api.ts +0 -72
- package/src/client.ts +0 -77
- package/src/connecitons/basic.ts +0 -49
- package/src/connecitons/http.ts +0 -184
- package/src/connecitons/socket.ts +0 -247
- package/src/connecitons/socketio.ts +0 -200
- package/src/connection.ts +0 -10
- package/src/context.ts +0 -18
- package/src/core.ts +0 -130
- package/src/define.ts +0 -63
- package/src/handler.ts +0 -202
- package/src/index.ts +0 -41
- package/src/packet.ts +0 -77
- package/src/server.ts +0 -82
- package/src/test/TestCase.ts +0 -6
- package/src/test/authorization.ts +0 -197
- package/src/test/basic.ts +0 -44
- package/src/test/context.ts +0 -64
- package/src/test/expressmix.ts +0 -81
- package/src/test/socket.ts +0 -90
- package/src/test/socketio.ts +0 -96
- package/src/test.ts +0 -35
- package/src/utils.ts +0 -95
- /package/dist/{connecitons → connections}/basic.js +0 -0
- /package/dist/{connecitons → connections}/http.d.ts +0 -0
- /package/dist/{connecitons → connections}/http.js +0 -0
- /package/dist/{connecitons → connections}/socket.d.ts +0 -0
- /package/dist/{connecitons → connections}/socketio.d.ts +0 -0
package/src/test/context.ts
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { TypedRPCClient } from "../index.js";
|
|
2
|
-
import { TypedRPCContextSymbol, type TypedRPCContext, type TypedRPCContextAware } from "../index.js";
|
|
3
|
-
import { TypedRPCAPIDefine } from "../index.js";
|
|
4
|
-
import { TypedRPCServer } from "../index.js";
|
|
5
|
-
import { TestCase } from "./TestCase.js";
|
|
6
|
-
|
|
7
|
-
interface MathServiceInterface{
|
|
8
|
-
add(a:number,b:number):number,
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
const serverAPIDefine = new TypedRPCAPIDefine<{
|
|
12
|
-
math:MathServiceInterface,
|
|
13
|
-
}>();
|
|
14
|
-
|
|
15
|
-
const server = new TypedRPCServer({
|
|
16
|
-
local:serverAPIDefine,
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
class MathService implements MathServiceInterface, TypedRPCContextAware{
|
|
20
|
-
|
|
21
|
-
/** 通过 TypedRPCContextAware 接口,将上下文注入到服务中,允许在方法内访问RPC调用时的上下文 */
|
|
22
|
-
[TypedRPCContextSymbol]: TypedRPCContext | null = null;
|
|
23
|
-
|
|
24
|
-
/** 使用hookService时,必须声明为TypedRPC方法,否则不予注册为hook,避免外部非法调用方法*/
|
|
25
|
-
@TypedRPCAPIDefine.method()
|
|
26
|
-
add(a: number, b: number): number {
|
|
27
|
-
const context = this[TypedRPCContextSymbol]!;
|
|
28
|
-
if(!context){
|
|
29
|
-
throw new Error('context is null');
|
|
30
|
-
}
|
|
31
|
-
return a+b;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
server.hookService('math',new MathService());
|
|
36
|
-
|
|
37
|
-
const client = new TypedRPCClient({
|
|
38
|
-
remote:serverAPIDefine,
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
export default class TestBasic extends TestCase{
|
|
45
|
-
name(): string {
|
|
46
|
-
return 'Context';
|
|
47
|
-
}
|
|
48
|
-
async run(): Promise<boolean> {
|
|
49
|
-
await server.listen({
|
|
50
|
-
port:3698,
|
|
51
|
-
})
|
|
52
|
-
const connection = await client.connect("localhost:3698");
|
|
53
|
-
const api = client.getAPI(connection);
|
|
54
|
-
const result = await api.math.add.call(1,2);
|
|
55
|
-
if(result == 3){
|
|
56
|
-
return true;
|
|
57
|
-
}
|
|
58
|
-
return false;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
public async finally(): Promise<void> {
|
|
62
|
-
await server.close();
|
|
63
|
-
}
|
|
64
|
-
}
|
package/src/test/expressmix.ts
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { TypedRPCAPIDefine, TypedRPCClient, TypedRPCConnectionProviderHTTP, TypedRPCServer } from "../index.js";
|
|
2
|
-
import http from 'http';
|
|
3
|
-
import express from 'express';
|
|
4
|
-
import { TestCase } from "./TestCase.js";
|
|
5
|
-
|
|
6
|
-
const ServerAPIDefine = new TypedRPCAPIDefine<{
|
|
7
|
-
math:{
|
|
8
|
-
add:(a:number,b:number)=>number,
|
|
9
|
-
},
|
|
10
|
-
}>();
|
|
11
|
-
|
|
12
|
-
const httpServer = http.createServer();
|
|
13
|
-
/**
|
|
14
|
-
* TypedRPCConnectionProviderHTTP 是 TypedRPCConnectionProvider 的默认实现
|
|
15
|
-
* 你也可以自定义实现 TypedRPCConnectionProvider 与 TypedRPCConnection 来支持其他协议
|
|
16
|
-
* 例如基于IPC协议的连接、基于Websocket协议的连接、基于udp协议的连接
|
|
17
|
-
* */
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const provider = new TypedRPCConnectionProviderHTTP({
|
|
21
|
-
server:httpServer,
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
const server = new TypedRPCServer({
|
|
25
|
-
local:ServerAPIDefine,
|
|
26
|
-
connection:{
|
|
27
|
-
provider:provider,
|
|
28
|
-
}
|
|
29
|
-
})
|
|
30
|
-
server.hook('math','add',{
|
|
31
|
-
handler:(a,b)=>a+b,
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const app = express();
|
|
36
|
-
app.use("/test",(req,res) => {
|
|
37
|
-
res.end("hello world");
|
|
38
|
-
})
|
|
39
|
-
/**
|
|
40
|
-
* 通过provider注入express中间件
|
|
41
|
-
* provider会将无法识别的请求转发到express中间件
|
|
42
|
-
*/
|
|
43
|
-
provider.use(app)
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const client = new TypedRPCClient({
|
|
48
|
-
remote:ServerAPIDefine,
|
|
49
|
-
})
|
|
50
|
-
|
|
51
|
-
export default class TextExpressMix extends TestCase{
|
|
52
|
-
name(): string {
|
|
53
|
-
return "ExpressMix";
|
|
54
|
-
}
|
|
55
|
-
async run(): Promise<boolean> {
|
|
56
|
-
await server.listen({
|
|
57
|
-
port:3698
|
|
58
|
-
});
|
|
59
|
-
await new Promise(resolve => setTimeout(resolve,100));
|
|
60
|
-
|
|
61
|
-
// RPC测试
|
|
62
|
-
const connection = await client.connect("localhost:3698");
|
|
63
|
-
const result = await client.getAPI(connection).math.add.call(1,2);
|
|
64
|
-
if(result !== 3){
|
|
65
|
-
throw new Error("RPC request result should be 3");
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// express测试
|
|
69
|
-
const response = await fetch("http://localhost:3698/test");
|
|
70
|
-
const text = await response.text();
|
|
71
|
-
if(text !== "hello world"){
|
|
72
|
-
throw new Error("Express request result should be 'hello world'");
|
|
73
|
-
}
|
|
74
|
-
return true;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
public async finally(): Promise<void> {
|
|
78
|
-
await server.close();
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
}
|
package/src/test/socket.ts
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import { TestCase } from "./TestCase.js";
|
|
2
|
-
import { TypedRPCAPIDefine, TypedRPCClient, TypedRPCConnectionProviderSocket, TypedRPCServer } from "../index.js";
|
|
3
|
-
|
|
4
|
-
const ServerAPIDefine = new TypedRPCAPIDefine<{
|
|
5
|
-
math: {
|
|
6
|
-
add: (a: number, b: number) => number,
|
|
7
|
-
}
|
|
8
|
-
}>
|
|
9
|
-
|
|
10
|
-
const ClientAPIDefine = new TypedRPCAPIDefine<{
|
|
11
|
-
status: {
|
|
12
|
-
time: () => number,
|
|
13
|
-
}
|
|
14
|
-
}>
|
|
15
|
-
|
|
16
|
-
const server = new TypedRPCServer({
|
|
17
|
-
local: ServerAPIDefine,
|
|
18
|
-
remote: ClientAPIDefine,
|
|
19
|
-
connection: {
|
|
20
|
-
provider: new TypedRPCConnectionProviderSocket(),
|
|
21
|
-
}
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
server.hook('math', 'add', {
|
|
25
|
-
handler: (a, b) => {
|
|
26
|
-
return a + b;
|
|
27
|
-
}
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
const client = new TypedRPCClient({
|
|
31
|
-
local: ClientAPIDefine,
|
|
32
|
-
remote: ServerAPIDefine,
|
|
33
|
-
connection: {
|
|
34
|
-
provider: new TypedRPCConnectionProviderSocket(),
|
|
35
|
-
}
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
client.hook('status', 'time', {
|
|
39
|
-
handler: () => {
|
|
40
|
-
return Date.now();
|
|
41
|
-
}
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
class TestSocket extends TestCase {
|
|
46
|
-
name(): string {
|
|
47
|
-
return 'Socket';
|
|
48
|
-
}
|
|
49
|
-
async run(): Promise<boolean> {
|
|
50
|
-
let requestToServer: Promise<boolean>;
|
|
51
|
-
let requestToClient: Promise<boolean>;
|
|
52
|
-
|
|
53
|
-
requestToClient = new Promise<boolean>((resolve) => {
|
|
54
|
-
server.emitter.on('connection', async (connectionToClient) => {
|
|
55
|
-
const apiToClient = server.getAPI(connectionToClient);
|
|
56
|
-
const result = await apiToClient.status.time.call();
|
|
57
|
-
if (result != null) {
|
|
58
|
-
resolve(true);
|
|
59
|
-
} else {
|
|
60
|
-
resolve(false);
|
|
61
|
-
}
|
|
62
|
-
})
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
await server.listen({
|
|
66
|
-
port: 3698,
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
requestToServer = new Promise<boolean>(async (resolve) => {
|
|
70
|
-
const connectionToServer = await client.connect("localhost:3698");
|
|
71
|
-
const apiToServer = client.getAPI(connectionToServer);
|
|
72
|
-
const result = await apiToServer.math.add.call(1, 2);
|
|
73
|
-
if (result == 3) {
|
|
74
|
-
resolve(true);
|
|
75
|
-
} else {
|
|
76
|
-
resolve(false);
|
|
77
|
-
}
|
|
78
|
-
})
|
|
79
|
-
|
|
80
|
-
const resultToClient = await requestToClient;
|
|
81
|
-
const resultToServer = await requestToServer;
|
|
82
|
-
return resultToClient && resultToServer;
|
|
83
|
-
}
|
|
84
|
-
public async finally(): Promise<void> {
|
|
85
|
-
await server.close();
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
export default TestSocket;
|
package/src/test/socketio.ts
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 测试全双工通信实现双向调用
|
|
3
|
-
* TypedRPC的默认实现是基于http的,全双工必须实现自定义的Provider
|
|
4
|
-
* 这里使用socket.io作为Provider的底层通讯
|
|
5
|
-
*/
|
|
6
|
-
import { TestCase } from "./TestCase.js";
|
|
7
|
-
import { TypedRPCAPIDefine, TypedRPCConnectionProviderSocketIO } from "../index.js";
|
|
8
|
-
import { TypedRPCServer } from "../index.js";
|
|
9
|
-
import { TypedRPCClient } from "../index.js";
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const ServerAPIDefine = new TypedRPCAPIDefine<{
|
|
14
|
-
math:{
|
|
15
|
-
add:(a:number,b:number) => number,
|
|
16
|
-
}
|
|
17
|
-
}>
|
|
18
|
-
|
|
19
|
-
const ClientAPIDefine = new TypedRPCAPIDefine<{
|
|
20
|
-
status:{
|
|
21
|
-
time:() => number,
|
|
22
|
-
}
|
|
23
|
-
}>
|
|
24
|
-
|
|
25
|
-
const server = new TypedRPCServer({
|
|
26
|
-
local:ServerAPIDefine,
|
|
27
|
-
remote:ClientAPIDefine,
|
|
28
|
-
connection:{
|
|
29
|
-
provider:new TypedRPCConnectionProviderSocketIO(),
|
|
30
|
-
}
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
server.hook('math','add',{
|
|
34
|
-
handler:(a,b) => {
|
|
35
|
-
return a + b;
|
|
36
|
-
}
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
const client = new TypedRPCClient({
|
|
40
|
-
local:ClientAPIDefine,
|
|
41
|
-
remote:ServerAPIDefine,
|
|
42
|
-
connection:{
|
|
43
|
-
provider:new TypedRPCConnectionProviderSocketIO(),
|
|
44
|
-
}
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
client.hook('status','time',{
|
|
48
|
-
handler:() => {
|
|
49
|
-
return Date.now();
|
|
50
|
-
}
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
export default class TestSocketIO extends TestCase{
|
|
54
|
-
name(): string {
|
|
55
|
-
return "SocketIO";
|
|
56
|
-
}
|
|
57
|
-
async run(): Promise<boolean> {
|
|
58
|
-
let requestToServer:Promise<boolean>;
|
|
59
|
-
let requestToClient:Promise<boolean>;
|
|
60
|
-
|
|
61
|
-
requestToClient = new Promise<boolean>((resolve) => {
|
|
62
|
-
server.emitter.on('connection',async (connectionToClient) => {
|
|
63
|
-
const apiToClient = server.getAPI(connectionToClient);
|
|
64
|
-
const result = await apiToClient.status.time.call();
|
|
65
|
-
if(result != null){
|
|
66
|
-
resolve(true);
|
|
67
|
-
}else{
|
|
68
|
-
resolve(false);
|
|
69
|
-
}
|
|
70
|
-
})
|
|
71
|
-
})
|
|
72
|
-
|
|
73
|
-
await server.listen({
|
|
74
|
-
port:3698,
|
|
75
|
-
})
|
|
76
|
-
|
|
77
|
-
requestToServer = new Promise<boolean>(async (resolve) => {
|
|
78
|
-
const connectionToServer = await client.connect("localhost:3698");
|
|
79
|
-
const apiToServer = client.getAPI(connectionToServer);
|
|
80
|
-
const result = await apiToServer.math.add.call(1,2);
|
|
81
|
-
if(result == 3){
|
|
82
|
-
resolve(true);
|
|
83
|
-
}else{
|
|
84
|
-
resolve(false);
|
|
85
|
-
}
|
|
86
|
-
})
|
|
87
|
-
|
|
88
|
-
const resultToClient = await requestToClient;
|
|
89
|
-
const resultToServer = await requestToServer;
|
|
90
|
-
return resultToClient && resultToServer;
|
|
91
|
-
}
|
|
92
|
-
public async finally(): Promise<void> {
|
|
93
|
-
await server.close();
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
package/src/test.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import type { TestCase } from './test/TestCase.js';
|
|
2
|
-
|
|
3
|
-
const testList = [
|
|
4
|
-
import('./test/basic.js'),
|
|
5
|
-
import('./test/context.js'),
|
|
6
|
-
import('./test/socketio.js'),
|
|
7
|
-
import('./test/authorization.js'),
|
|
8
|
-
import('./test/expressmix.js'),
|
|
9
|
-
import('./test/socket.js'),
|
|
10
|
-
]
|
|
11
|
-
|
|
12
|
-
async function main() {
|
|
13
|
-
for(let i of testList){
|
|
14
|
-
let exp = await i;
|
|
15
|
-
let test = new (exp.default)() as TestCase;
|
|
16
|
-
let result = await test.run().catch((e) => {
|
|
17
|
-
console.log(`Test:${test.name()} failed. ${e}`);
|
|
18
|
-
throw e;
|
|
19
|
-
})
|
|
20
|
-
await test.finally();
|
|
21
|
-
if(result){
|
|
22
|
-
console.log(`\x1b[32m[PASS]\x1b[0m ${test.name()}`);
|
|
23
|
-
}else{
|
|
24
|
-
console.log(`\x1b[31m[FAIL]\x1b[0m ${test.name()}`);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
main().then(() => {
|
|
31
|
-
console.log('TypedRPC test finished.');
|
|
32
|
-
}).catch((e) => {
|
|
33
|
-
console.log(`TypedRPC test failed. ${e}`);
|
|
34
|
-
throw e;
|
|
35
|
-
})
|
package/src/utils.ts
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
import type { TypedRPCAPIDefine } from "./define.js";
|
|
2
|
-
import type { TypedRPCRequestPacket, TypedRPCResponsePacket } from "./packet.js";
|
|
3
|
-
|
|
4
|
-
export type FunctionTypeToPromiseFunctionType<T extends (...args: any[]) => any> =
|
|
5
|
-
T extends (...args: infer P) => Promise<infer R> ?
|
|
6
|
-
(...args: P) => Promise<R | undefined>
|
|
7
|
-
: T extends (...args: infer P) => infer R ?
|
|
8
|
-
(...args: P) => Promise<R | undefined>
|
|
9
|
-
: never
|
|
10
|
-
;
|
|
11
|
-
|
|
12
|
-
export type TypedRPCDefineToTypedRPCAPI<T extends TypedRPCAPIDefine<any>> = T extends TypedRPCAPIDefine<infer U> ?
|
|
13
|
-
{
|
|
14
|
-
[S in keyof U]:{
|
|
15
|
-
[M in keyof U[S]]:{
|
|
16
|
-
/** 调用该方法 */
|
|
17
|
-
call:FunctionTypeToPromiseFunctionType<U[S][M]>,
|
|
18
|
-
/** 获取方法id */
|
|
19
|
-
request:(config:{
|
|
20
|
-
args:Parameters<U[S][M]>,
|
|
21
|
-
callback?:(result:ReturnType<U[S][M]>,req:TypedRPCRequestPacket,res:TypedRPCResponsePacket) => void,
|
|
22
|
-
error?:(error:any,req:TypedRPCRequestPacket,res:TypedRPCResponsePacket) => void,
|
|
23
|
-
}) => string,
|
|
24
|
-
id:string,
|
|
25
|
-
/** 获取方法路径 */
|
|
26
|
-
path:string,
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
: never;
|
|
31
|
-
|
|
32
|
-
export type TypedRPCDefineServiceName<T extends TypedRPCAPIDefine<any>> = T extends TypedRPCAPIDefine<infer U> ? keyof U & string : never;
|
|
33
|
-
export type TypedRPCDefineMethodName<T extends TypedRPCAPIDefine<any>,S extends TypedRPCDefineServiceName<T>> = T extends TypedRPCAPIDefine<infer U> ? keyof U[S] & string: never;
|
|
34
|
-
export type TypedRPCDefineMethodBody<T extends TypedRPCAPIDefine<any>,S extends TypedRPCDefineServiceName<T>,M extends TypedRPCDefineMethodName<T,S>> = T extends TypedRPCAPIDefine<infer U> ? U[S][M] : never;
|
|
35
|
-
export type TypedRPCDefineServiceInstance<T extends TypedRPCAPIDefine<any>,S extends TypedRPCDefineServiceName<T>> = T extends TypedRPCAPIDefine<infer U> ? U[S] : never;
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
export type TypedEmitterEvents = {
|
|
39
|
-
[key:string]:(...args:any[]) => void
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export class TypedEmitter<T extends TypedEmitterEvents> {
|
|
43
|
-
|
|
44
|
-
private record:Map<keyof T,Set<T[keyof T]>> = new Map();
|
|
45
|
-
|
|
46
|
-
on<K extends keyof T>(event:K,callback:T[K]){
|
|
47
|
-
if(!this.record.has(event)){
|
|
48
|
-
this.record.set(event,new Set());
|
|
49
|
-
}
|
|
50
|
-
this.record.get(event)!.add(callback);
|
|
51
|
-
return () => {
|
|
52
|
-
this.off(event,callback);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
off<K extends keyof T>(event:K,callback:T[K]){
|
|
57
|
-
if(!this.record.has(event)){
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
this.record.get(event)!.delete(callback);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
emit<K extends keyof T>(event:K,...args:Parameters<T[K]>){
|
|
64
|
-
if(!this.record.has(event)){
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
for(const callback of this.record.get(event)!){
|
|
68
|
-
callback(...args);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
once<K extends keyof T>(event:K,callback:T[K]){
|
|
73
|
-
const wrapper = (...args:Parameters<T[K]>) => {
|
|
74
|
-
callback(...args);
|
|
75
|
-
this.off(event,wrapper as T[K]);
|
|
76
|
-
}
|
|
77
|
-
this.on(event,wrapper as T[K]);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export class IdMaker{
|
|
82
|
-
|
|
83
|
-
public static instance = new IdMaker();
|
|
84
|
-
|
|
85
|
-
static makeId():string{
|
|
86
|
-
return IdMaker.instance.makeId();
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
public makeId():string{
|
|
90
|
-
// 生成12位随机字符串
|
|
91
|
-
const suffix = Math.random().toString(36).substring(2,10);
|
|
92
|
-
const timestamp = Date.now().toString(36);
|
|
93
|
-
return `${timestamp}${suffix}`;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|