@lynx-js/web-worker-rpc 0.7.1 → 0.8.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/CHANGELOG.md +10 -0
- package/dist/Rpc.d.ts +8 -1
- package/dist/Rpc.js +13 -2
- package/dist/RpcEndpoint.d.ts +16 -3
- package/dist/RpcEndpoint.js +2 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# @lynx-js/web-worker-rpc
|
|
2
2
|
|
|
3
|
+
## 0.8.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- feat: `createRpcEndpoint` adds a new parameter: `hasReturnTransfer`. ([#194](https://github.com/lynx-family/lynx-stack/pull/194))
|
|
8
|
+
|
|
9
|
+
When `isSync`: false, `hasReturn`: true, you can add `transfer` to the callback postMessage created.
|
|
10
|
+
|
|
11
|
+
At this time, the return value structure of register-handler is changed: `{ data: unknown; transfer: Transferable[]; } | Promise<{ data: unknown; transfer: Transferable[];}>`.
|
|
12
|
+
|
|
3
13
|
## 0.7.1
|
|
4
14
|
|
|
5
15
|
### Patch Changes
|
package/dist/Rpc.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { RpcEndpoint, RpcEndpointAsync, RpcEndpointAsyncVoid, RpcEndpointBase, RpcEndpointSync, RpcEndpointSyncVoid } from './RpcEndpoint.js';
|
|
1
|
+
import type { RpcEndpoint, RpcEndpointAsync, RpcEndpointAsyncVoid, RpcEndpointAsyncWithTransfer, RpcEndpointBase, RpcEndpointSync, RpcEndpointSyncVoid } from './RpcEndpoint.js';
|
|
2
2
|
type RetEndpoint<Return> = RpcEndpointBase<[
|
|
3
3
|
Return,
|
|
4
4
|
boolean
|
|
@@ -38,6 +38,13 @@ export declare class Rpc {
|
|
|
38
38
|
*/
|
|
39
39
|
registerHandler<T extends RetEndpoint<any>>(endpoint: T, handler: (...args: T['_TypeParameters']) => void): void;
|
|
40
40
|
registerHandler<T extends RpcEndpoint<any[], any>>(endpoint: T, handler: ((...args: T['_TypeParameters']) => T['_TypeReturn']) | ((...args: T['_TypeParameters']) => Promise<T['_TypeReturn']>)): void;
|
|
41
|
+
registerHandler<T extends RpcEndpointAsyncWithTransfer<any[], any>>(endpoint: T, handler: ((...args: T['_TypeParameters']) => {
|
|
42
|
+
data: T['_TypeReturn'];
|
|
43
|
+
transfer: Transferable[];
|
|
44
|
+
}) | ((...args: T['_TypeParameters']) => Promise<{
|
|
45
|
+
data: T['_TypeReturn'];
|
|
46
|
+
transfer: Transferable[];
|
|
47
|
+
}>)): void;
|
|
41
48
|
/**
|
|
42
49
|
* register a property of an object as a handler
|
|
43
50
|
* @param endpoint
|
package/dist/Rpc.js
CHANGED
|
@@ -44,7 +44,17 @@ export class Rpc {
|
|
|
44
44
|
? Rpc.createRetEndpoint(message.retId)
|
|
45
45
|
: undefined;
|
|
46
46
|
try {
|
|
47
|
-
const
|
|
47
|
+
const result = await handler(...message.data);
|
|
48
|
+
let retData = undefined, transfer = [];
|
|
49
|
+
if (message.sync) {
|
|
50
|
+
retData = result;
|
|
51
|
+
}
|
|
52
|
+
else if (message.hasTransfer) {
|
|
53
|
+
({ data: retData, transfer } = (result || {}));
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
retData = result;
|
|
57
|
+
}
|
|
48
58
|
if (message.sync) {
|
|
49
59
|
if (message.buf) {
|
|
50
60
|
const retStr = JSON.stringify(retData);
|
|
@@ -63,7 +73,7 @@ export class Rpc {
|
|
|
63
73
|
this.invoke(replyTempEndpoint, [
|
|
64
74
|
retData,
|
|
65
75
|
false,
|
|
66
|
-
]);
|
|
76
|
+
], transfer || []);
|
|
67
77
|
}
|
|
68
78
|
}
|
|
69
79
|
}
|
|
@@ -185,6 +195,7 @@ export class Rpc {
|
|
|
185
195
|
data: parameters,
|
|
186
196
|
sync: false,
|
|
187
197
|
retId: retHandler?.name,
|
|
198
|
+
hasTransfer: endpoint.hasReturnTransfer,
|
|
188
199
|
};
|
|
189
200
|
this.port.postMessage(message, { transfer });
|
|
190
201
|
return promise;
|
package/dist/RpcEndpoint.d.ts
CHANGED
|
@@ -2,7 +2,12 @@ export type RpcEndpointSyncVoid<Parameters extends any[]> = RpcEndpointBase<Para
|
|
|
2
2
|
export interface RpcEndpointSync<Parameters extends any[], Return> extends RpcEndpointBase<Parameters, Return, true, true> {
|
|
3
3
|
readonly bufferSize: number;
|
|
4
4
|
}
|
|
5
|
-
export
|
|
5
|
+
export interface RpcEndpointAsync<Parameters extends any[], Return> extends RpcEndpointBase<Parameters, Return, false, true> {
|
|
6
|
+
readonly hasReturnTransfer: false;
|
|
7
|
+
}
|
|
8
|
+
export interface RpcEndpointAsyncWithTransfer<Parameters extends any[], Return> extends RpcEndpointBase<Parameters, Return, true, true> {
|
|
9
|
+
readonly hasReturnTransfer: true;
|
|
10
|
+
}
|
|
6
11
|
export type RpcEndpointAsyncVoid<Parameters extends any[]> = RpcEndpointBase<Parameters, void, false, false>;
|
|
7
12
|
export interface RpcEndpointBase<Parameters extends any[], Return, IsSync extends boolean, HasReturn extends boolean> {
|
|
8
13
|
/**
|
|
@@ -40,9 +45,17 @@ export interface RpcEndpointBase<Parameters extends any[], Return, IsSync extend
|
|
|
40
45
|
* So you should ensure this size is enough for your stringified return value.
|
|
41
46
|
*/
|
|
42
47
|
readonly bufferSize: never | number;
|
|
48
|
+
/**
|
|
49
|
+
* @public
|
|
50
|
+
* Make the message invoke created by hasReturn support transfer.
|
|
51
|
+
* Only valid for async and hasReturn endpoints
|
|
52
|
+
*/
|
|
53
|
+
readonly hasReturnTransfer: never | boolean;
|
|
43
54
|
}
|
|
44
|
-
export type RpcEndpoint<Parameters extends any[], Return> = RpcEndpointSyncVoid<Parameters> | RpcEndpointSync<Parameters, Return> | RpcEndpointAsync<Parameters, Return> | RpcEndpointAsyncVoid<Parameters>;
|
|
55
|
+
export type RpcEndpoint<Parameters extends any[], Return> = RpcEndpointSyncVoid<Parameters> | RpcEndpointSync<Parameters, Return> | RpcEndpointAsync<Parameters, Return> | RpcEndpointAsyncVoid<Parameters> | RpcEndpointAsyncWithTransfer<Parameters, Return>;
|
|
45
56
|
export declare function createRpcEndpoint<Parameters extends any[], Return = void>(name: string, isSync: false, hasReturn: false): RpcEndpointAsyncVoid<Parameters>;
|
|
46
57
|
export declare function createRpcEndpoint<Parameters extends any[], Return = void>(name: string, isSync: false, hasReturn: true): RpcEndpointAsync<Parameters, Return>;
|
|
58
|
+
export declare function createRpcEndpoint<Parameters extends any[], Return = void>(name: string, isSync: false, hasReturn: true, hasReturnTransfer: false): RpcEndpointAsync<Parameters, Return>;
|
|
59
|
+
export declare function createRpcEndpoint<Parameters extends any[], Return = void>(name: string, isSync: false, hasReturn: true, hasReturnTransfer: true): RpcEndpointAsyncWithTransfer<Parameters, Return>;
|
|
47
60
|
export declare function createRpcEndpoint<Parameters extends any[]>(name: string, isSync: true, hasReturn: false): RpcEndpointSyncVoid<Parameters>;
|
|
48
|
-
export declare function createRpcEndpoint<Parameters extends any[], Return>(name: string, isSync: true, hasReturn: true, bufferSize: number): RpcEndpointSync<Parameters, Return>;
|
|
61
|
+
export declare function createRpcEndpoint<Parameters extends any[], Return>(name: string, isSync: true, hasReturn: true, hasReturnTransfer: false, bufferSize: number): RpcEndpointSync<Parameters, Return>;
|
package/dist/RpcEndpoint.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
export function createRpcEndpoint(name, isSync, hasReturn = true, bufferSize) {
|
|
1
|
+
export function createRpcEndpoint(name, isSync, hasReturn = true, hasReturnTransfer = false, bufferSize) {
|
|
2
2
|
return {
|
|
3
3
|
name,
|
|
4
4
|
isSync,
|
|
5
5
|
hasReturn,
|
|
6
|
+
hasReturnTransfer,
|
|
6
7
|
bufferSize,
|
|
7
8
|
};
|
|
8
9
|
}
|