@zimi/remote 0.2.1 → 0.2.4
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/LICENSE.md +21 -21
- package/README.md +289 -285
- package/dist/adaptor.d.mts +33 -0
- package/dist/adaptor.d.ts +5 -4
- package/dist/adaptor.js +1 -1
- package/dist/adaptor.js.map +1 -1
- package/dist/adaptor.mjs +1 -0
- package/dist/adaptor.mjs.map +1 -0
- package/dist/adaptors/dao3/client.d.mts +39 -0
- package/dist/adaptors/dao3/client.d.ts +5 -3
- package/dist/adaptors/dao3/client.js +1 -44
- package/dist/adaptors/dao3/client.js.map +1 -1
- package/dist/adaptors/dao3/client.mjs +2 -0
- package/dist/adaptors/dao3/client.mjs.map +1 -0
- package/dist/adaptors/dao3/server.d.mts +82 -0
- package/dist/adaptors/dao3/server.d.ts +5 -3
- package/dist/adaptors/dao3/server.js +1 -104
- package/dist/adaptors/dao3/server.js.map +1 -1
- package/dist/adaptors/dao3/server.mjs +2 -0
- package/dist/adaptors/dao3/server.mjs.map +1 -0
- package/dist/adaptors/electron/constants.d.mts +5 -0
- package/dist/adaptors/electron/constants.d.ts +5 -0
- package/dist/adaptors/electron/constants.js +2 -0
- package/dist/adaptors/electron/constants.js.map +1 -0
- package/dist/adaptors/electron/constants.mjs +2 -0
- package/dist/adaptors/electron/constants.mjs.map +1 -0
- package/dist/adaptors/electron/main.d.mts +10 -0
- package/dist/adaptors/electron/main.d.ts +10 -0
- package/dist/adaptors/electron/main.js +2 -0
- package/dist/adaptors/electron/main.js.map +1 -0
- package/dist/adaptors/electron/main.mjs +2 -0
- package/dist/adaptors/electron/main.mjs.map +1 -0
- package/dist/adaptors/electron/messenger.d.mts +8 -0
- package/dist/adaptors/electron/messenger.d.ts +8 -0
- package/dist/adaptors/electron/messenger.js +2 -0
- package/dist/adaptors/electron/messenger.js.map +1 -0
- package/dist/adaptors/electron/messenger.mjs +2 -0
- package/dist/adaptors/electron/messenger.mjs.map +1 -0
- package/dist/adaptors/electron/preload.d.mts +6 -0
- package/dist/adaptors/electron/preload.d.ts +6 -0
- package/dist/adaptors/electron/preload.js +2 -0
- package/dist/adaptors/electron/preload.js.map +1 -0
- package/dist/adaptors/electron/preload.mjs +2 -0
- package/dist/adaptors/electron/preload.mjs.map +1 -0
- package/dist/adaptors/electron/renderer.d.mts +9 -0
- package/dist/adaptors/electron/renderer.d.ts +9 -0
- package/dist/adaptors/electron/renderer.js +2 -0
- package/dist/adaptors/electron/renderer.js.map +1 -0
- package/dist/adaptors/electron/renderer.mjs +2 -0
- package/dist/adaptors/electron/renderer.mjs.map +1 -0
- package/dist/adaptors/http.d.mts +15 -0
- package/dist/adaptors/http.d.ts +6 -4
- package/dist/adaptors/http.js +1 -26
- package/dist/adaptors/http.js.map +1 -1
- package/dist/adaptors/http.mjs +2 -0
- package/dist/adaptors/http.mjs.map +1 -0
- package/dist/adaptors/iframe.d.mts +7 -0
- package/dist/adaptors/iframe.d.ts +5 -2
- package/dist/adaptors/iframe.js +1 -38
- package/dist/adaptors/iframe.js.map +1 -1
- package/dist/adaptors/iframe.mjs +2 -0
- package/dist/adaptors/iframe.mjs.map +1 -0
- package/dist/index.d.mts +16 -0
- package/dist/index.d.ts +16 -10
- package/dist/index.js +1 -9
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -0
- package/dist/remote.d.mts +81 -0
- package/dist/remote.d.ts +6 -4
- package/dist/remote.js +1 -196
- package/dist/remote.js.map +1 -1
- package/dist/remote.mjs +2 -0
- package/dist/remote.mjs.map +1 -0
- package/dist/remoteValue/exposeToRemote.d.mts +43 -0
- package/dist/remoteValue/exposeToRemote.d.ts +7 -5
- package/dist/remoteValue/exposeToRemote.js +1 -52
- package/dist/remoteValue/exposeToRemote.js.map +1 -1
- package/dist/remoteValue/exposeToRemote.mjs +2 -0
- package/dist/remoteValue/exposeToRemote.mjs.map +1 -0
- package/dist/remoteValue/remoteValue.d.mts +29 -0
- package/dist/remoteValue/remoteValue.d.ts +6 -4
- package/dist/remoteValue/remoteValue.js +1 -67
- package/dist/remoteValue/remoteValue.js.map +1 -1
- package/dist/remoteValue/remoteValue.mjs +2 -0
- package/dist/remoteValue/remoteValue.mjs.map +1 -0
- package/dist/remoteValue/type.d.mts +67 -0
- package/dist/remoteValue/type.d.ts +20 -19
- package/dist/remoteValue/type.js +1 -1
- package/dist/remoteValue/type.js.map +1 -1
- package/dist/remoteValue/type.mjs +1 -0
- package/dist/remoteValue/type.mjs.map +1 -0
- package/dist/response.d.mts +105 -0
- package/dist/response.d.ts +6 -5
- package/dist/response.js +1 -154
- package/dist/response.js.map +1 -1
- package/dist/response.mjs +2 -0
- package/dist/response.mjs.map +1 -0
- package/package.json +31 -24
- package/src/adaptor.ts +34 -34
- package/src/adaptors/dao3/client.ts +53 -53
- package/src/adaptors/dao3/server.ts +136 -136
- package/src/adaptors/electron/constants.ts +3 -0
- package/src/adaptors/electron/main.ts +59 -0
- package/src/adaptors/electron/messenger.ts +13 -0
- package/src/adaptors/electron/preload.ts +21 -0
- package/src/adaptors/electron/renderer.ts +52 -0
- package/src/adaptors/http.ts +31 -31
- package/src/adaptors/iframe.ts +47 -47
- package/src/index.ts +17 -12
- package/src/remote.ts +263 -260
- package/src/remoteValue/exposeToRemote.ts +102 -102
- package/src/remoteValue/remoteValue.ts +94 -94
- package/src/remoteValue/type.ts +124 -124
- package/src/response.ts +170 -170
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
interface RawRemoteError {
|
|
2
|
+
/**
|
|
3
|
+
* code > 0 才是合法的 Error;
|
|
4
|
+
* name 和 message 都是 human readable 的字符串,
|
|
5
|
+
* 仅用于调试和展示,
|
|
6
|
+
* 程序逻辑判断请使用 code
|
|
7
|
+
*/
|
|
8
|
+
code: number;
|
|
9
|
+
/**
|
|
10
|
+
* name 和 message 都是 human readable 的字符串,
|
|
11
|
+
* 仅用于调试和展示,
|
|
12
|
+
* 程序逻辑判断请使用 code
|
|
13
|
+
*/
|
|
14
|
+
name: string;
|
|
15
|
+
/**
|
|
16
|
+
* name 和 message 都是 human readable 的字符串,
|
|
17
|
+
* 仅用于调试和展示,
|
|
18
|
+
* 程序逻辑判断请使用 code
|
|
19
|
+
*/
|
|
20
|
+
message: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* name 和 message 都是 human readable 的字符串,
|
|
24
|
+
* 仅用于调试和展示,
|
|
25
|
+
* 程序逻辑判断请使用 code
|
|
26
|
+
*/
|
|
27
|
+
declare class RemoteError extends Error {
|
|
28
|
+
/**
|
|
29
|
+
* code > 0 才是合法的 Error;
|
|
30
|
+
* name 和 message 都是 human readable 的字符串,
|
|
31
|
+
* 仅用于调试和展示,
|
|
32
|
+
* 程序逻辑判断请使用 code
|
|
33
|
+
*/
|
|
34
|
+
code: number;
|
|
35
|
+
/**
|
|
36
|
+
* name 和 message 都是 human readable 的字符串,
|
|
37
|
+
* 仅用于调试和展示,
|
|
38
|
+
* 程序逻辑判断请使用 code
|
|
39
|
+
*/
|
|
40
|
+
constructor(message: string);
|
|
41
|
+
toJson(): RawRemoteError;
|
|
42
|
+
toString(): string;
|
|
43
|
+
valueOf(): string;
|
|
44
|
+
static fromError(err: unknown): RemoteError;
|
|
45
|
+
static isRemoteError(data: unknown): data is RawRemoteError;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* name 和 message 都是 human readable 的字符串,
|
|
49
|
+
* 仅用于调试和展示,
|
|
50
|
+
* 程序逻辑判断请使用 code
|
|
51
|
+
*/
|
|
52
|
+
declare class RemoteTimeoutError extends RemoteError {
|
|
53
|
+
/**
|
|
54
|
+
* name 和 message 都是 human readable 的字符串,
|
|
55
|
+
* 仅用于调试和展示,
|
|
56
|
+
* 程序逻辑判断请使用 code
|
|
57
|
+
*/
|
|
58
|
+
static code: number;
|
|
59
|
+
/**
|
|
60
|
+
* name 和 message 都是 human readable 的字符串,
|
|
61
|
+
* 仅用于调试和展示,
|
|
62
|
+
* 程序逻辑判断请使用 code
|
|
63
|
+
*/
|
|
64
|
+
code: number;
|
|
65
|
+
/**
|
|
66
|
+
* name 和 message 都是 human readable 的字符串,
|
|
67
|
+
* 仅用于调试和展示,
|
|
68
|
+
* 程序逻辑判断请使用 code
|
|
69
|
+
*/
|
|
70
|
+
constructor(message: string);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* name 和 message 都是 human readable 的字符串,
|
|
74
|
+
* 仅用于调试和展示,
|
|
75
|
+
* 程序逻辑判断请使用 code
|
|
76
|
+
*/
|
|
77
|
+
declare class RemoteNotFoundError extends RemoteError {
|
|
78
|
+
/**
|
|
79
|
+
* name 和 message 都是 human readable 的字符串,
|
|
80
|
+
* 仅用于调试和展示,
|
|
81
|
+
* 程序逻辑判断请使用 code
|
|
82
|
+
*/
|
|
83
|
+
static code: number;
|
|
84
|
+
/**
|
|
85
|
+
* name 和 message 都是 human readable 的字符串,
|
|
86
|
+
* 仅用于调试和展示,
|
|
87
|
+
* 程序逻辑判断请使用 code
|
|
88
|
+
*/
|
|
89
|
+
code: number;
|
|
90
|
+
/**
|
|
91
|
+
* name 和 message 都是 human readable 的字符串,
|
|
92
|
+
* 仅用于调试和展示,
|
|
93
|
+
* 程序逻辑判断请使用 code
|
|
94
|
+
*/
|
|
95
|
+
constructor(message: string);
|
|
96
|
+
}
|
|
97
|
+
declare const response: {
|
|
98
|
+
success<T>(data: T): {
|
|
99
|
+
readonly code: 0;
|
|
100
|
+
readonly data: T;
|
|
101
|
+
};
|
|
102
|
+
error(error: RemoteError): RawRemoteError;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
export { RemoteError, RemoteNotFoundError, RemoteTimeoutError, response };
|
package/dist/response.d.ts
CHANGED
|
@@ -24,7 +24,7 @@ interface RawRemoteError {
|
|
|
24
24
|
* 仅用于调试和展示,
|
|
25
25
|
* 程序逻辑判断请使用 code
|
|
26
26
|
*/
|
|
27
|
-
|
|
27
|
+
declare class RemoteError extends Error {
|
|
28
28
|
/**
|
|
29
29
|
* code > 0 才是合法的 Error;
|
|
30
30
|
* name 和 message 都是 human readable 的字符串,
|
|
@@ -49,7 +49,7 @@ export declare class RemoteError extends Error {
|
|
|
49
49
|
* 仅用于调试和展示,
|
|
50
50
|
* 程序逻辑判断请使用 code
|
|
51
51
|
*/
|
|
52
|
-
|
|
52
|
+
declare class RemoteTimeoutError extends RemoteError {
|
|
53
53
|
/**
|
|
54
54
|
* name 和 message 都是 human readable 的字符串,
|
|
55
55
|
* 仅用于调试和展示,
|
|
@@ -74,7 +74,7 @@ export declare class RemoteTimeoutError extends RemoteError {
|
|
|
74
74
|
* 仅用于调试和展示,
|
|
75
75
|
* 程序逻辑判断请使用 code
|
|
76
76
|
*/
|
|
77
|
-
|
|
77
|
+
declare class RemoteNotFoundError extends RemoteError {
|
|
78
78
|
/**
|
|
79
79
|
* name 和 message 都是 human readable 的字符串,
|
|
80
80
|
* 仅用于调试和展示,
|
|
@@ -94,11 +94,12 @@ export declare class RemoteNotFoundError extends RemoteError {
|
|
|
94
94
|
*/
|
|
95
95
|
constructor(message: string);
|
|
96
96
|
}
|
|
97
|
-
|
|
97
|
+
declare const response: {
|
|
98
98
|
success<T>(data: T): {
|
|
99
99
|
readonly code: 0;
|
|
100
100
|
readonly data: T;
|
|
101
101
|
};
|
|
102
102
|
error(error: RemoteError): RawRemoteError;
|
|
103
103
|
};
|
|
104
|
-
|
|
104
|
+
|
|
105
|
+
export { RemoteError, RemoteNotFoundError, RemoteTimeoutError, response };
|
package/dist/response.js
CHANGED
|
@@ -1,155 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
* name 和 message 都是 human readable 的字符串,
|
|
3
|
-
* 仅用于调试和展示,
|
|
4
|
-
* 程序逻辑判断请使用 code
|
|
5
|
-
*/
|
|
6
|
-
export class RemoteError extends Error {
|
|
7
|
-
/**
|
|
8
|
-
* name 和 message 都是 human readable 的字符串,
|
|
9
|
-
* 仅用于调试和展示,
|
|
10
|
-
* 程序逻辑判断请使用 code
|
|
11
|
-
*/
|
|
12
|
-
constructor(message) {
|
|
13
|
-
super(message);
|
|
14
|
-
/**
|
|
15
|
-
* code > 0 才是合法的 Error;
|
|
16
|
-
* name 和 message 都是 human readable 的字符串,
|
|
17
|
-
* 仅用于调试和展示,
|
|
18
|
-
* 程序逻辑判断请使用 code
|
|
19
|
-
*/
|
|
20
|
-
Object.defineProperty(this, "code", {
|
|
21
|
-
enumerable: true,
|
|
22
|
-
configurable: true,
|
|
23
|
-
writable: true,
|
|
24
|
-
value: 1
|
|
25
|
-
});
|
|
26
|
-
this.name = 'RemoteError';
|
|
27
|
-
}
|
|
28
|
-
toJson() {
|
|
29
|
-
return {
|
|
30
|
-
code: this.code,
|
|
31
|
-
name: this.name,
|
|
32
|
-
message: this.message,
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
toString() {
|
|
36
|
-
return JSON.stringify(this.toJson());
|
|
37
|
-
}
|
|
38
|
-
valueOf() {
|
|
39
|
-
return `${this.name} [${this.code}]: ${this.message}`;
|
|
40
|
-
}
|
|
41
|
-
static fromError(err) {
|
|
42
|
-
if (typeof err === 'string') {
|
|
43
|
-
return new RemoteError(err);
|
|
44
|
-
}
|
|
45
|
-
if (!err) {
|
|
46
|
-
return new RemoteError('Unknown error');
|
|
47
|
-
}
|
|
48
|
-
const json = err;
|
|
49
|
-
const error = new RemoteError(typeof json.message === 'string' ? json.message : 'Unknown error');
|
|
50
|
-
if (typeof json.code === 'number' && json.code > 0) {
|
|
51
|
-
error.code = json.code;
|
|
52
|
-
}
|
|
53
|
-
if (json.name && typeof json.name === 'string') {
|
|
54
|
-
error.name = json.name;
|
|
55
|
-
}
|
|
56
|
-
return error;
|
|
57
|
-
}
|
|
58
|
-
static isRemoteError(data) {
|
|
59
|
-
const json = data;
|
|
60
|
-
return (!!json &&
|
|
61
|
-
typeof json === 'object' &&
|
|
62
|
-
typeof json.code === 'number' &&
|
|
63
|
-
json.code > 0 &&
|
|
64
|
-
typeof json.name === 'string' &&
|
|
65
|
-
typeof json.message === 'string');
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* name 和 message 都是 human readable 的字符串,
|
|
70
|
-
* 仅用于调试和展示,
|
|
71
|
-
* 程序逻辑判断请使用 code
|
|
72
|
-
*/
|
|
73
|
-
export class RemoteTimeoutError extends RemoteError {
|
|
74
|
-
/**
|
|
75
|
-
* name 和 message 都是 human readable 的字符串,
|
|
76
|
-
* 仅用于调试和展示,
|
|
77
|
-
* 程序逻辑判断请使用 code
|
|
78
|
-
*/
|
|
79
|
-
constructor(message) {
|
|
80
|
-
super(message);
|
|
81
|
-
/**
|
|
82
|
-
* name 和 message 都是 human readable 的字符串,
|
|
83
|
-
* 仅用于调试和展示,
|
|
84
|
-
* 程序逻辑判断请使用 code
|
|
85
|
-
*/
|
|
86
|
-
Object.defineProperty(this, "code", {
|
|
87
|
-
enumerable: true,
|
|
88
|
-
configurable: true,
|
|
89
|
-
writable: true,
|
|
90
|
-
value: 2
|
|
91
|
-
});
|
|
92
|
-
this.name = 'RemoteTimeoutError';
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* name 和 message 都是 human readable 的字符串,
|
|
97
|
-
* 仅用于调试和展示,
|
|
98
|
-
* 程序逻辑判断请使用 code
|
|
99
|
-
*/
|
|
100
|
-
Object.defineProperty(RemoteTimeoutError, "code", {
|
|
101
|
-
enumerable: true,
|
|
102
|
-
configurable: true,
|
|
103
|
-
writable: true,
|
|
104
|
-
value: 2
|
|
105
|
-
});
|
|
106
|
-
/**
|
|
107
|
-
* name 和 message 都是 human readable 的字符串,
|
|
108
|
-
* 仅用于调试和展示,
|
|
109
|
-
* 程序逻辑判断请使用 code
|
|
110
|
-
*/
|
|
111
|
-
export class RemoteNotFoundError extends RemoteError {
|
|
112
|
-
/**
|
|
113
|
-
* name 和 message 都是 human readable 的字符串,
|
|
114
|
-
* 仅用于调试和展示,
|
|
115
|
-
* 程序逻辑判断请使用 code
|
|
116
|
-
*/
|
|
117
|
-
constructor(message) {
|
|
118
|
-
super(message);
|
|
119
|
-
/**
|
|
120
|
-
* name 和 message 都是 human readable 的字符串,
|
|
121
|
-
* 仅用于调试和展示,
|
|
122
|
-
* 程序逻辑判断请使用 code
|
|
123
|
-
*/
|
|
124
|
-
Object.defineProperty(this, "code", {
|
|
125
|
-
enumerable: true,
|
|
126
|
-
configurable: true,
|
|
127
|
-
writable: true,
|
|
128
|
-
value: 3
|
|
129
|
-
});
|
|
130
|
-
this.name = 'RemoteNotFoundError';
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* name 和 message 都是 human readable 的字符串,
|
|
135
|
-
* 仅用于调试和展示,
|
|
136
|
-
* 程序逻辑判断请使用 code
|
|
137
|
-
*/
|
|
138
|
-
Object.defineProperty(RemoteNotFoundError, "code", {
|
|
139
|
-
enumerable: true,
|
|
140
|
-
configurable: true,
|
|
141
|
-
writable: true,
|
|
142
|
-
value: 3
|
|
143
|
-
});
|
|
144
|
-
export const response = {
|
|
145
|
-
success(data) {
|
|
146
|
-
return {
|
|
147
|
-
code: 0,
|
|
148
|
-
data,
|
|
149
|
-
};
|
|
150
|
-
},
|
|
151
|
-
error(error) {
|
|
152
|
-
return error.toJson();
|
|
153
|
-
},
|
|
154
|
-
};
|
|
1
|
+
"use strict";var a=Object.defineProperty;var c=Object.getOwnPropertyDescriptor;var i=Object.getOwnPropertyNames;var m=Object.prototype.hasOwnProperty;var u=(t,e)=>{for(var r in e)a(t,r,{get:e[r],enumerable:!0})},d=(t,e,r,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of i(e))!m.call(t,n)&&n!==r&&a(t,n,{get:()=>e[n],enumerable:!(s=c(e,n))||s.enumerable});return t};var g=t=>d(a({},"__esModule",{value:!0}),t);var w={};u(w,{RemoteError:()=>o,RemoteNotFoundError:()=>p,RemoteTimeoutError:()=>f,response:()=>R});module.exports=g(w);class o extends Error{code=1;constructor(e){super(e),this.name="RemoteError"}toJson(){return{code:this.code,name:this.name,message:this.message}}toString(){return JSON.stringify(this.toJson())}valueOf(){return`${this.name} [${this.code}]: ${this.message}`}static fromError(e){if(typeof e=="string")return new o(e);if(!e)return new o("Unknown error");const r=e,s=new o(typeof r.message=="string"?r.message:"Unknown error");return typeof r.code=="number"&&r.code>0&&(s.code=r.code),r.name&&typeof r.name=="string"&&(s.name=r.name),s}static isRemoteError(e){const r=e;return!!r&&typeof r=="object"&&typeof r.code=="number"&&r.code>0&&typeof r.name=="string"&&typeof r.message=="string"}}class f extends o{static code=2;code=2;constructor(e){super(e),this.name="RemoteTimeoutError"}}class p extends o{static code=3;code=3;constructor(e){super(e),this.name="RemoteNotFoundError"}}const R={success(t){return{code:0,data:t}},error(t){return t.toJson()}};0&&(module.exports={RemoteError,RemoteNotFoundError,RemoteTimeoutError,response});
|
|
155
2
|
//# sourceMappingURL=response.js.map
|
package/dist/response.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":["../src/response.ts"],"sourcesContent":["/* eslint-disable max-classes-per-file */\r\ninterface RawRemoteError {\r\n /**\r\n * code > 0 才是合法的 Error;\r\n * name 和 message 都是 human readable 的字符串,\r\n * 仅用于调试和展示,\r\n * 程序逻辑判断请使用 code\r\n */\r\n code: number\r\n /**\r\n * name 和 message 都是 human readable 的字符串,\r\n * 仅用于调试和展示,\r\n * 程序逻辑判断请使用 code\r\n */\r\n name: string\r\n /**\r\n * name 和 message 都是 human readable 的字符串,\r\n * 仅用于调试和展示,\r\n * 程序逻辑判断请使用 code\r\n */\r\n message: string\r\n}\r\n\r\n/**\r\n * name 和 message 都是 human readable 的字符串,\r\n * 仅用于调试和展示,\r\n * 程序逻辑判断请使用 code\r\n */\r\nexport class RemoteError extends Error {\r\n /**\r\n * code > 0 才是合法的 Error;\r\n * name 和 message 都是 human readable 的字符串,\r\n * 仅用于调试和展示,\r\n * 程序逻辑判断请使用 code\r\n */\r\n code = 1\r\n\r\n /**\r\n * name 和 message 都是 human readable 的字符串,\r\n * 仅用于调试和展示,\r\n * 程序逻辑判断请使用 code\r\n */\r\n constructor(message: string) {\r\n super(message)\r\n this.name = 'RemoteError'\r\n }\r\n\r\n toJson(): RawRemoteError {\r\n return {\r\n code: this.code,\r\n name: this.name,\r\n message: this.message,\r\n }\r\n }\r\n\r\n toString() {\r\n return JSON.stringify(this.toJson())\r\n }\r\n\r\n valueOf() {\r\n return `${this.name} [${this.code}]: ${this.message}`\r\n }\r\n\r\n static fromError(err: unknown) {\r\n if (typeof err === 'string') {\r\n return new RemoteError(err)\r\n }\r\n if (!err) {\r\n return new RemoteError('Unknown error')\r\n }\r\n const json = err as Partial<RawRemoteError>\r\n const error = new RemoteError(\r\n typeof json.message === 'string' ? json.message : 'Unknown error'\r\n )\r\n if (typeof json.code === 'number' && json.code > 0) {\r\n error.code = json.code\r\n }\r\n if (json.name && typeof json.name === 'string') {\r\n error.name = json.name\r\n }\r\n return error\r\n }\r\n\r\n static isRemoteError(data: unknown): data is RawRemoteError {\r\n const json = data as Partial<RawRemoteError>\r\n return (\r\n !!json &&\r\n typeof json === 'object' &&\r\n typeof json.code === 'number' &&\r\n json.code > 0 &&\r\n typeof json.name === 'string' &&\r\n typeof json.message === 'string'\r\n )\r\n }\r\n}\r\n\r\n/**\r\n * name 和 message 都是 human readable 的字符串,\r\n * 仅用于调试和展示,\r\n * 程序逻辑判断请使用 code\r\n */\r\nexport class RemoteTimeoutError extends RemoteError {\r\n /**\r\n * name 和 message 都是 human readable 的字符串,\r\n * 仅用于调试和展示,\r\n * 程序逻辑判断请使用 code\r\n */\r\n static code = 2\r\n\r\n /**\r\n * name 和 message 都是 human readable 的字符串,\r\n * 仅用于调试和展示,\r\n * 程序逻辑判断请使用 code\r\n */\r\n code = 2\r\n\r\n /**\r\n * name 和 message 都是 human readable 的字符串,\r\n * 仅用于调试和展示,\r\n * 程序逻辑判断请使用 code\r\n */\r\n constructor(message: string) {\r\n super(message)\r\n this.name = 'RemoteTimeoutError'\r\n }\r\n}\r\n\r\n/**\r\n * name 和 message 都是 human readable 的字符串,\r\n * 仅用于调试和展示,\r\n * 程序逻辑判断请使用 code\r\n */\r\nexport class RemoteNotFoundError extends RemoteError {\r\n /**\r\n * name 和 message 都是 human readable 的字符串,\r\n * 仅用于调试和展示,\r\n * 程序逻辑判断请使用 code\r\n */\r\n static code = 3\r\n\r\n /**\r\n * name 和 message 都是 human readable 的字符串,\r\n * 仅用于调试和展示,\r\n * 程序逻辑判断请使用 code\r\n */\r\n code = 3\r\n\r\n /**\r\n * name 和 message 都是 human readable 的字符串,\r\n * 仅用于调试和展示,\r\n * 程序逻辑判断请使用 code\r\n */\r\n constructor(message: string) {\r\n super(message)\r\n this.name = 'RemoteNotFoundError'\r\n }\r\n}\r\n\r\nexport const response = {\r\n success<T>(data: T) {\r\n return {\r\n code: 0,\r\n data,\r\n } as const\r\n },\r\n\r\n error(error: RemoteError) {\r\n return error.toJson()\r\n },\r\n}\r\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,iBAAAE,EAAA,wBAAAC,EAAA,uBAAAC,EAAA,aAAAC,IAAA,eAAAC,EAAAN,GA4BO,MAAME,UAAoB,KAAM,CAOrC,KAAO,EAOP,YAAYK,EAAiB,CAC3B,MAAMA,CAAO,EACb,KAAK,KAAO,aACd,CAEA,QAAyB,CACvB,MAAO,CACL,KAAM,KAAK,KACX,KAAM,KAAK,KACX,QAAS,KAAK,OAChB,CACF,CAEA,UAAW,CACT,OAAO,KAAK,UAAU,KAAK,OAAO,CAAC,CACrC,CAEA,SAAU,CACR,MAAO,GAAG,KAAK,IAAI,KAAK,KAAK,IAAI,MAAM,KAAK,OAAO,EACrD,CAEA,OAAO,UAAUC,EAAc,CAC7B,GAAI,OAAOA,GAAQ,SACjB,OAAO,IAAIN,EAAYM,CAAG,EAE5B,GAAI,CAACA,EACH,OAAO,IAAIN,EAAY,eAAe,EAExC,MAAMO,EAAOD,EACPE,EAAQ,IAAIR,EAChB,OAAOO,EAAK,SAAY,SAAWA,EAAK,QAAU,eACpD,EACA,OAAI,OAAOA,EAAK,MAAS,UAAYA,EAAK,KAAO,IAC/CC,EAAM,KAAOD,EAAK,MAEhBA,EAAK,MAAQ,OAAOA,EAAK,MAAS,WACpCC,EAAM,KAAOD,EAAK,MAEbC,CACT,CAEA,OAAO,cAAcC,EAAuC,CAC1D,MAAMF,EAAOE,EACb,MACE,CAAC,CAACF,GACF,OAAOA,GAAS,UAChB,OAAOA,EAAK,MAAS,UACrBA,EAAK,KAAO,GACZ,OAAOA,EAAK,MAAS,UACrB,OAAOA,EAAK,SAAY,QAE5B,CACF,CAOO,MAAML,UAA2BF,CAAY,CAMlD,OAAO,KAAO,EAOd,KAAO,EAOP,YAAYK,EAAiB,CAC3B,MAAMA,CAAO,EACb,KAAK,KAAO,oBACd,CACF,CAOO,MAAMJ,UAA4BD,CAAY,CAMnD,OAAO,KAAO,EAOd,KAAO,EAOP,YAAYK,EAAiB,CAC3B,MAAMA,CAAO,EACb,KAAK,KAAO,qBACd,CACF,CAEO,MAAMF,EAAW,CACtB,QAAWM,EAAS,CAClB,MAAO,CACL,KAAM,EACN,KAAAA,CACF,CACF,EAEA,MAAMD,EAAoB,CACxB,OAAOA,EAAM,OAAO,CACtB,CACF","names":["response_exports","__export","RemoteError","RemoteNotFoundError","RemoteTimeoutError","response","__toCommonJS","message","err","json","error","data"]}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
class t extends Error{code=1;constructor(r){super(r),this.name="RemoteError"}toJson(){return{code:this.code,name:this.name,message:this.message}}toString(){return JSON.stringify(this.toJson())}valueOf(){return`${this.name} [${this.code}]: ${this.message}`}static fromError(r){if(typeof r=="string")return new t(r);if(!r)return new t("Unknown error");const e=r,s=new t(typeof e.message=="string"?e.message:"Unknown error");return typeof e.code=="number"&&e.code>0&&(s.code=e.code),e.name&&typeof e.name=="string"&&(s.name=e.name),s}static isRemoteError(r){const e=r;return!!e&&typeof e=="object"&&typeof e.code=="number"&&e.code>0&&typeof e.name=="string"&&typeof e.message=="string"}}class n extends t{static code=2;code=2;constructor(r){super(r),this.name="RemoteTimeoutError"}}class a extends t{static code=3;code=3;constructor(r){super(r),this.name="RemoteNotFoundError"}}const c={success(o){return{code:0,data:o}},error(o){return o.toJson()}};export{t as RemoteError,a as RemoteNotFoundError,n as RemoteTimeoutError,c as response};
|
|
2
|
+
//# sourceMappingURL=response.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/response.ts"],"sourcesContent":["/* eslint-disable max-classes-per-file */\r\ninterface RawRemoteError {\r\n /**\r\n * code > 0 才是合法的 Error;\r\n * name 和 message 都是 human readable 的字符串,\r\n * 仅用于调试和展示,\r\n * 程序逻辑判断请使用 code\r\n */\r\n code: number\r\n /**\r\n * name 和 message 都是 human readable 的字符串,\r\n * 仅用于调试和展示,\r\n * 程序逻辑判断请使用 code\r\n */\r\n name: string\r\n /**\r\n * name 和 message 都是 human readable 的字符串,\r\n * 仅用于调试和展示,\r\n * 程序逻辑判断请使用 code\r\n */\r\n message: string\r\n}\r\n\r\n/**\r\n * name 和 message 都是 human readable 的字符串,\r\n * 仅用于调试和展示,\r\n * 程序逻辑判断请使用 code\r\n */\r\nexport class RemoteError extends Error {\r\n /**\r\n * code > 0 才是合法的 Error;\r\n * name 和 message 都是 human readable 的字符串,\r\n * 仅用于调试和展示,\r\n * 程序逻辑判断请使用 code\r\n */\r\n code = 1\r\n\r\n /**\r\n * name 和 message 都是 human readable 的字符串,\r\n * 仅用于调试和展示,\r\n * 程序逻辑判断请使用 code\r\n */\r\n constructor(message: string) {\r\n super(message)\r\n this.name = 'RemoteError'\r\n }\r\n\r\n toJson(): RawRemoteError {\r\n return {\r\n code: this.code,\r\n name: this.name,\r\n message: this.message,\r\n }\r\n }\r\n\r\n toString() {\r\n return JSON.stringify(this.toJson())\r\n }\r\n\r\n valueOf() {\r\n return `${this.name} [${this.code}]: ${this.message}`\r\n }\r\n\r\n static fromError(err: unknown) {\r\n if (typeof err === 'string') {\r\n return new RemoteError(err)\r\n }\r\n if (!err) {\r\n return new RemoteError('Unknown error')\r\n }\r\n const json = err as Partial<RawRemoteError>\r\n const error = new RemoteError(\r\n typeof json.message === 'string' ? json.message : 'Unknown error'\r\n )\r\n if (typeof json.code === 'number' && json.code > 0) {\r\n error.code = json.code\r\n }\r\n if (json.name && typeof json.name === 'string') {\r\n error.name = json.name\r\n }\r\n return error\r\n }\r\n\r\n static isRemoteError(data: unknown): data is RawRemoteError {\r\n const json = data as Partial<RawRemoteError>\r\n return (\r\n !!json &&\r\n typeof json === 'object' &&\r\n typeof json.code === 'number' &&\r\n json.code > 0 &&\r\n typeof json.name === 'string' &&\r\n typeof json.message === 'string'\r\n )\r\n }\r\n}\r\n\r\n/**\r\n * name 和 message 都是 human readable 的字符串,\r\n * 仅用于调试和展示,\r\n * 程序逻辑判断请使用 code\r\n */\r\nexport class RemoteTimeoutError extends RemoteError {\r\n /**\r\n * name 和 message 都是 human readable 的字符串,\r\n * 仅用于调试和展示,\r\n * 程序逻辑判断请使用 code\r\n */\r\n static code = 2\r\n\r\n /**\r\n * name 和 message 都是 human readable 的字符串,\r\n * 仅用于调试和展示,\r\n * 程序逻辑判断请使用 code\r\n */\r\n code = 2\r\n\r\n /**\r\n * name 和 message 都是 human readable 的字符串,\r\n * 仅用于调试和展示,\r\n * 程序逻辑判断请使用 code\r\n */\r\n constructor(message: string) {\r\n super(message)\r\n this.name = 'RemoteTimeoutError'\r\n }\r\n}\r\n\r\n/**\r\n * name 和 message 都是 human readable 的字符串,\r\n * 仅用于调试和展示,\r\n * 程序逻辑判断请使用 code\r\n */\r\nexport class RemoteNotFoundError extends RemoteError {\r\n /**\r\n * name 和 message 都是 human readable 的字符串,\r\n * 仅用于调试和展示,\r\n * 程序逻辑判断请使用 code\r\n */\r\n static code = 3\r\n\r\n /**\r\n * name 和 message 都是 human readable 的字符串,\r\n * 仅用于调试和展示,\r\n * 程序逻辑判断请使用 code\r\n */\r\n code = 3\r\n\r\n /**\r\n * name 和 message 都是 human readable 的字符串,\r\n * 仅用于调试和展示,\r\n * 程序逻辑判断请使用 code\r\n */\r\n constructor(message: string) {\r\n super(message)\r\n this.name = 'RemoteNotFoundError'\r\n }\r\n}\r\n\r\nexport const response = {\r\n success<T>(data: T) {\r\n return {\r\n code: 0,\r\n data,\r\n } as const\r\n },\r\n\r\n error(error: RemoteError) {\r\n return error.toJson()\r\n },\r\n}\r\n"],"mappings":"AA4BO,MAAMA,UAAoB,KAAM,CAOrC,KAAO,EAOP,YAAYC,EAAiB,CAC3B,MAAMA,CAAO,EACb,KAAK,KAAO,aACd,CAEA,QAAyB,CACvB,MAAO,CACL,KAAM,KAAK,KACX,KAAM,KAAK,KACX,QAAS,KAAK,OAChB,CACF,CAEA,UAAW,CACT,OAAO,KAAK,UAAU,KAAK,OAAO,CAAC,CACrC,CAEA,SAAU,CACR,MAAO,GAAG,KAAK,IAAI,KAAK,KAAK,IAAI,MAAM,KAAK,OAAO,EACrD,CAEA,OAAO,UAAUC,EAAc,CAC7B,GAAI,OAAOA,GAAQ,SACjB,OAAO,IAAIF,EAAYE,CAAG,EAE5B,GAAI,CAACA,EACH,OAAO,IAAIF,EAAY,eAAe,EAExC,MAAMG,EAAOD,EACPE,EAAQ,IAAIJ,EAChB,OAAOG,EAAK,SAAY,SAAWA,EAAK,QAAU,eACpD,EACA,OAAI,OAAOA,EAAK,MAAS,UAAYA,EAAK,KAAO,IAC/CC,EAAM,KAAOD,EAAK,MAEhBA,EAAK,MAAQ,OAAOA,EAAK,MAAS,WACpCC,EAAM,KAAOD,EAAK,MAEbC,CACT,CAEA,OAAO,cAAcC,EAAuC,CAC1D,MAAMF,EAAOE,EACb,MACE,CAAC,CAACF,GACF,OAAOA,GAAS,UAChB,OAAOA,EAAK,MAAS,UACrBA,EAAK,KAAO,GACZ,OAAOA,EAAK,MAAS,UACrB,OAAOA,EAAK,SAAY,QAE5B,CACF,CAOO,MAAMG,UAA2BN,CAAY,CAMlD,OAAO,KAAO,EAOd,KAAO,EAOP,YAAYC,EAAiB,CAC3B,MAAMA,CAAO,EACb,KAAK,KAAO,oBACd,CACF,CAOO,MAAMM,UAA4BP,CAAY,CAMnD,OAAO,KAAO,EAOd,KAAO,EAOP,YAAYC,EAAiB,CAC3B,MAAMA,CAAO,EACb,KAAK,KAAO,qBACd,CACF,CAEO,MAAMO,EAAW,CACtB,QAAWH,EAAS,CAClB,MAAO,CACL,KAAM,EACN,KAAAA,CACF,CACF,EAEA,MAAMD,EAAoB,CACxB,OAAOA,EAAM,OAAO,CACtB,CACF","names":["RemoteError","message","err","json","error","data","RemoteTimeoutError","RemoteNotFoundError","response"]}
|
package/package.json
CHANGED
|
@@ -1,24 +1,31 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@zimi/remote",
|
|
3
|
-
"license": "MIT",
|
|
4
|
-
"version": "0.2.
|
|
5
|
-
"author": "xiaomingTang",
|
|
6
|
-
"description": "call remote functions as local",
|
|
7
|
-
"private": false,
|
|
8
|
-
"sideEffects": false,
|
|
9
|
-
"main": "dist/index.js",
|
|
10
|
-
"module": "dist/index.
|
|
11
|
-
"types": "dist/index.d.ts",
|
|
12
|
-
"repository": "https://github.com/xiaomingTang/xiaoming/tree/master/%40zimi/remote",
|
|
13
|
-
"files": [
|
|
14
|
-
"src",
|
|
15
|
-
"dist"
|
|
16
|
-
],
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
"
|
|
23
|
-
|
|
24
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@zimi/remote",
|
|
3
|
+
"license": "MIT",
|
|
4
|
+
"version": "0.2.4",
|
|
5
|
+
"author": "xiaomingTang",
|
|
6
|
+
"description": "call remote functions as local",
|
|
7
|
+
"private": false,
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"main": "dist/index.js",
|
|
10
|
+
"module": "dist/index.mjs",
|
|
11
|
+
"types": "dist/index.d.ts",
|
|
12
|
+
"repository": "https://github.com/xiaomingTang/xiaoming/tree/master/%40zimi/remote",
|
|
13
|
+
"files": [
|
|
14
|
+
"src",
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsup",
|
|
19
|
+
"build:watch": "tsup --watch"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"electron": "^39.2.7",
|
|
23
|
+
"eventemitter3": "^5.0.1"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"tsup": "^8.5.1"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"electron": "^39.2.7"
|
|
30
|
+
}
|
|
31
|
+
}
|
package/src/adaptor.ts
CHANGED
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
export interface AdaptorPackageData<D = unknown> {
|
|
2
|
-
/**
|
|
3
|
-
* 自身设备 id,应确保唯一性(对方能凭借该 deviceId 找到该设备)
|
|
4
|
-
*/
|
|
5
|
-
deviceId: string
|
|
6
|
-
/**
|
|
7
|
-
* 对方的设备 id
|
|
8
|
-
*/
|
|
9
|
-
targetDeviceId: string
|
|
10
|
-
/**
|
|
11
|
-
* 远程调用的对方的方法名
|
|
12
|
-
*/
|
|
13
|
-
name: string
|
|
14
|
-
data: D
|
|
15
|
-
/**
|
|
16
|
-
* 所需回调的方法名(如果需要回调的话)
|
|
17
|
-
*/
|
|
18
|
-
callbackName?: string
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
type Func<D = unknown, R = unknown> = (data: D) => R
|
|
22
|
-
|
|
23
|
-
export type AdaptorCallback = Func<AdaptorPackageData, void>
|
|
24
|
-
|
|
25
|
-
export interface Adaptor {
|
|
26
|
-
every: (callback: AdaptorCallback) => void
|
|
27
|
-
/**
|
|
28
|
-
* off 用于移除 once 注册的事件,当事件超时后,需要主动 off
|
|
29
|
-
*/
|
|
30
|
-
off: (name: string, callback: AdaptorCallback) => void
|
|
31
|
-
on: (name: string, callback: AdaptorCallback) => void
|
|
32
|
-
once: (name: string, callback: AdaptorCallback) => void
|
|
33
|
-
emit: (data: AdaptorPackageData) => void
|
|
34
|
-
}
|
|
1
|
+
export interface AdaptorPackageData<D = unknown> {
|
|
2
|
+
/**
|
|
3
|
+
* 自身设备 id,应确保唯一性(对方能凭借该 deviceId 找到该设备)
|
|
4
|
+
*/
|
|
5
|
+
deviceId: string
|
|
6
|
+
/**
|
|
7
|
+
* 对方的设备 id
|
|
8
|
+
*/
|
|
9
|
+
targetDeviceId: string
|
|
10
|
+
/**
|
|
11
|
+
* 远程调用的对方的方法名
|
|
12
|
+
*/
|
|
13
|
+
name: string
|
|
14
|
+
data: D
|
|
15
|
+
/**
|
|
16
|
+
* 所需回调的方法名(如果需要回调的话)
|
|
17
|
+
*/
|
|
18
|
+
callbackName?: string
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
type Func<D = unknown, R = unknown> = (data: D) => R
|
|
22
|
+
|
|
23
|
+
export type AdaptorCallback = Func<AdaptorPackageData, void>
|
|
24
|
+
|
|
25
|
+
export interface Adaptor {
|
|
26
|
+
every: (callback: AdaptorCallback) => void
|
|
27
|
+
/**
|
|
28
|
+
* off 用于移除 once 注册的事件,当事件超时后,需要主动 off
|
|
29
|
+
*/
|
|
30
|
+
off: (name: string, callback: AdaptorCallback) => void
|
|
31
|
+
on: (name: string, callback: AdaptorCallback) => void
|
|
32
|
+
once: (name: string, callback: AdaptorCallback) => void
|
|
33
|
+
emit: (data: AdaptorPackageData) => void
|
|
34
|
+
}
|
|
@@ -1,53 +1,53 @@
|
|
|
1
|
-
import EventEmitter from 'eventemitter3'
|
|
2
|
-
import { isRemoteAdaptorData } from '../../remote'
|
|
3
|
-
import { type AdaptorPackageData } from '../../adaptor'
|
|
4
|
-
|
|
5
|
-
interface RemoteChannel {
|
|
6
|
-
onClientEvent(fn: (e: unknown) => void): void
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* 这是 [dao3 游戏](https://dao3.fun/) 代码客户端适配器。
|
|
11
|
-
* ```ts
|
|
12
|
-
* const remoteManager = new ClientSideRemoteChannelEventManager(remoteChannel)
|
|
13
|
-
*
|
|
14
|
-
* export const remoteAdaptor = {
|
|
15
|
-
* every: remoteManager.onEvery.bind(remoteManager),
|
|
16
|
-
* off: remoteManager.off.bind(remoteManager),
|
|
17
|
-
* on: remoteManager.on.bind(remoteManager),
|
|
18
|
-
* once: remoteManager.once.bind(remoteManager),
|
|
19
|
-
* emit: (e) => {
|
|
20
|
-
* remoteChannel.sendServerEvent(e)
|
|
21
|
-
* },
|
|
22
|
-
* } satisfies Adaptor
|
|
23
|
-
*
|
|
24
|
-
* export const remote = new Remote<RemoteFuncsFromClient, RemoteFuncsFromServer>(
|
|
25
|
-
* remoteAdaptor,
|
|
26
|
-
* {
|
|
27
|
-
* deviceId: `client-${Math.random().toString(36).slice(2)}`,
|
|
28
|
-
* }
|
|
29
|
-
* )
|
|
30
|
-
*
|
|
31
|
-
* void remote._.initConnection()
|
|
32
|
-
* ```
|
|
33
|
-
*/
|
|
34
|
-
export class ClientSideRemoteChannelEventManager extends EventEmitter<{
|
|
35
|
-
[key: string]: [AdaptorPackageData]
|
|
36
|
-
}> {
|
|
37
|
-
constructor(remoteChannel: RemoteChannel) {
|
|
38
|
-
super()
|
|
39
|
-
remoteChannel.onClientEvent((e) => {
|
|
40
|
-
if (!isRemoteAdaptorData(e)) {
|
|
41
|
-
return
|
|
42
|
-
}
|
|
43
|
-
if (!e.name.startsWith('__REMOTE_VALUE_REQ__')) {
|
|
44
|
-
this.emit('__remote_every__', e)
|
|
45
|
-
}
|
|
46
|
-
this.emit(e.name, e)
|
|
47
|
-
})
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
onEvery(fn: (args: AdaptorPackageData) => void): void {
|
|
51
|
-
this.on('__remote_every__', fn)
|
|
52
|
-
}
|
|
53
|
-
}
|
|
1
|
+
import EventEmitter from 'eventemitter3'
|
|
2
|
+
import { isRemoteAdaptorData } from '../../remote'
|
|
3
|
+
import { type AdaptorPackageData } from '../../adaptor'
|
|
4
|
+
|
|
5
|
+
interface RemoteChannel {
|
|
6
|
+
onClientEvent(fn: (e: unknown) => void): void
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 这是 [dao3 游戏](https://dao3.fun/) 代码客户端适配器。
|
|
11
|
+
* ```ts
|
|
12
|
+
* const remoteManager = new ClientSideRemoteChannelEventManager(remoteChannel)
|
|
13
|
+
*
|
|
14
|
+
* export const remoteAdaptor = {
|
|
15
|
+
* every: remoteManager.onEvery.bind(remoteManager),
|
|
16
|
+
* off: remoteManager.off.bind(remoteManager),
|
|
17
|
+
* on: remoteManager.on.bind(remoteManager),
|
|
18
|
+
* once: remoteManager.once.bind(remoteManager),
|
|
19
|
+
* emit: (e) => {
|
|
20
|
+
* remoteChannel.sendServerEvent(e)
|
|
21
|
+
* },
|
|
22
|
+
* } satisfies Adaptor
|
|
23
|
+
*
|
|
24
|
+
* export const remote = new Remote<RemoteFuncsFromClient, RemoteFuncsFromServer>(
|
|
25
|
+
* remoteAdaptor,
|
|
26
|
+
* {
|
|
27
|
+
* deviceId: `client-${Math.random().toString(36).slice(2)}`,
|
|
28
|
+
* }
|
|
29
|
+
* )
|
|
30
|
+
*
|
|
31
|
+
* void remote._.initConnection()
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export class ClientSideRemoteChannelEventManager extends EventEmitter<{
|
|
35
|
+
[key: string]: [AdaptorPackageData]
|
|
36
|
+
}> {
|
|
37
|
+
constructor(remoteChannel: RemoteChannel) {
|
|
38
|
+
super()
|
|
39
|
+
remoteChannel.onClientEvent((e) => {
|
|
40
|
+
if (!isRemoteAdaptorData(e)) {
|
|
41
|
+
return
|
|
42
|
+
}
|
|
43
|
+
if (!e.name.startsWith('__REMOTE_VALUE_REQ__')) {
|
|
44
|
+
this.emit('__remote_every__', e)
|
|
45
|
+
}
|
|
46
|
+
this.emit(e.name, e)
|
|
47
|
+
})
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
onEvery(fn: (args: AdaptorPackageData) => void): void {
|
|
51
|
+
this.on('__remote_every__', fn)
|
|
52
|
+
}
|
|
53
|
+
}
|