File tree Expand file tree Collapse file tree 8 files changed +38
-11
lines changed Expand file tree Collapse file tree 8 files changed +38
-11
lines changed Original file line number Diff line number Diff line change 1
- import { RpcScaffold } from '../interfaces' ;
1
+ import { PromiseScaffold , RpcScaffold } from '../interfaces' ;
2
2
3
3
const channels : string [ ] = [ ] ;
4
4
@@ -11,13 +11,13 @@ export abstract class Channel<T extends RpcScaffold<T>> {
11
11
12
12
protected abstract createReceiver ( ...args : any [ ] ) : any ;
13
13
14
- protected abstract createInvoker ( ...args : any [ ] ) : T ;
14
+ protected abstract createInvoker ( ...args : any [ ] ) : T | PromiseScaffold < T > ;
15
15
16
16
public abstract getReceiver ( ...args : any [ ] ) : any ;
17
17
18
- public abstract getInvoker ( ...args : any [ ] ) : T ;
19
-
20
18
public destroy ( ) {
21
19
channels . splice ( channels . indexOf ( this . name ) , 1 ) ;
22
20
}
21
+
22
+ public abstract getInvoker ( ...args : any [ ] ) : T | PromiseScaffold < T > ;
23
23
}
Original file line number Diff line number Diff line change 1
- import { RpcScaffold } from '..' ;
1
+ import { PromiseScaffold , RpcScaffold } from '..' ;
2
2
import { Channel } from './channel' ;
3
3
4
4
export abstract class MultiReceiverChannel <
@@ -12,7 +12,7 @@ export abstract class MultiReceiverChannel<
12
12
return this . receivers . get ( args ) ;
13
13
}
14
14
15
- public getInvoker ( ...args : any [ ] ) : T {
15
+ public getInvoker ( ...args : any [ ] ) : PromiseScaffold < T > {
16
16
return this . createInvoker ( ...args ) ;
17
17
}
18
18
Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ export abstract class SingleReceiverChannel<
20
20
public getInvoker ( ...args : any [ ] ) : T {
21
21
if ( this . isReceiver ( ) && process ?. env ?. TEST !== 'true' )
22
22
throw new Error ( 'This context cannot invoke remote methods.' ) ;
23
- return this . createInvoker ( ...args ) ;
23
+ return this . createInvoker ( ...args ) as T ;
24
24
}
25
25
26
26
public abstract isReceiver ( ) : boolean ;
Original file line number Diff line number Diff line change 1
1
export * from './rpc' ;
2
- export * from './utils' ;
2
+ export * from './utils' ;
3
+ export * from './promise' ;
Original file line number Diff line number Diff line change
1
+ export type UnwrapPromise < T > = T extends PromiseLike < infer U > ? U : T ;
2
+
3
+ export type OptionalPromise < T > = Promise < T > | T ;
Original file line number Diff line number Diff line change
1
+ import { OptionalPromise , UnwrapPromise } from './promise' ;
1
2
import { AnyFunction } from './utils' ;
2
3
3
4
export type RpcScaffold < T > = {
@@ -35,3 +36,21 @@ export interface ServiceCaller {
35
36
cb : ( obj : any ) => Promise < any > ;
36
37
method : string ;
37
38
}
39
+
40
+ export type OptionalPromiseScaffold < T extends RpcScaffold < T > > = {
41
+ [ K in keyof T ] : (
42
+ ...args : Parameters < T [ K ] >
43
+ ) => OptionalPromise < UnwrapPromise < ReturnType < T [ K ] > > > ;
44
+ } ;
45
+
46
+ export type PromiseScaffold < T extends RpcScaffold < T > > = {
47
+ [ K in keyof T ] : (
48
+ ...args : Parameters < T [ K ] >
49
+ ) => Promise < UnwrapPromise < ReturnType < T [ K ] > > > ;
50
+ } ;
51
+
52
+ export type SyncScaffold < T extends RpcScaffold < T > > = {
53
+ [ K in keyof T ] : (
54
+ ...args : Parameters < T [ K ] >
55
+ ) => UnwrapPromise < ReturnType < T [ K ] > > ;
56
+ } ;
Original file line number Diff line number Diff line change 1
1
import {
2
+ OptionalPromiseScaffold ,
3
+ PromiseScaffold ,
2
4
RpcEventBase ,
3
5
RpcHandler ,
4
6
RpcObserver ,
5
7
RpcScaffold ,
8
+ SyncScaffold ,
6
9
} from '@wexond/rpc-core' ;
7
10
8
11
export type RpcWorkerHandler < T extends RpcScaffold < T > > = RpcHandler <
9
- T ,
12
+ OptionalPromiseScaffold < T > ,
10
13
RpcWorkerEvent
11
14
> ;
12
15
Original file line number Diff line number Diff line change @@ -4,18 +4,19 @@ import {
4
4
createServiceProxy ,
5
5
makeRandomId ,
6
6
RpcScaffold ,
7
+ PromiseScaffold ,
7
8
} from '@wexond/rpc-core' ;
8
9
import { WorkerReceiver } from './worker-receiver' ;
9
10
import { RpcWorkerRequest , RpcWorkerResponse } from '../interfaces' ;
10
11
11
12
export declare interface WorkerChannel < T > {
12
13
getReceiver ( port ?: MessagePort | Worker ) : WorkerReceiver < T > ;
13
- getInvoker ( port ?: MessagePort | Worker ) : T ;
14
+ getInvoker ( port ?: MessagePort | Worker ) : PromiseScaffold < T > ;
14
15
}
15
16
export class WorkerChannel <
16
17
T extends RpcScaffold < T >
17
18
> extends MultiReceiverChannel < T > {
18
- protected createInvoker ( port ?: MessagePort | Worker ) : T {
19
+ protected createInvoker ( port ?: MessagePort | Worker ) {
19
20
if ( ! port && ! parentPort ) throw new Error ( 'Invalid MessagePort.' ) ;
20
21
21
22
return createServiceProxy < T > ( ( method , ...args : any [ ] ) => {
You can’t perform that action at this time.
0 commit comments