@@ -24,66 +24,69 @@ export interface IRPCProtocol {
2424 get < T > ( identifier : ProxyIdentifier < T > ) : T ;
2525}
2626
27- const SEP = '||' ;
28- const SEP_LENGTH = SEP . length ;
29-
30- export function getRPCName ( serviceId : string , methodName : string ) {
31- return `${ serviceId } ${ SEP } ${ methodName } ` ;
32- }
33-
34- export function extractServiceAndMethod ( rpcId : string ) : [ string , string ] {
35- const idx = rpcId . indexOf ( SEP ) ;
36- return [ rpcId . substring ( 0 , idx ) , rpcId . substring ( idx + SEP_LENGTH ) ] ;
37- }
38-
3927/**
4028 * A connection multiplexer that allows to register multiple local RPC services and to create proxies for them.
4129 */
4230export class SumiConnectionMultiplexer extends SumiConnection implements IRPCProtocol {
43- private readonly _locals : Map < string , any > ;
44- private readonly _proxies : Map < string , any > ;
31+ protected static SEP = '/' ;
32+ protected static SEP_LENGTH = SumiConnectionMultiplexer . SEP . length ;
33+
34+ protected static getRPCName ( serviceId : string , methodName : string ) {
35+ return `${ serviceId } ${ SumiConnectionMultiplexer . SEP } ${ methodName } ` ;
36+ }
37+
38+ protected static extractServiceAndMethod ( rpcId : string ) : [ string , string ] {
39+ const idx = rpcId . indexOf ( SumiConnectionMultiplexer . SEP ) ;
40+ return [ rpcId . substring ( 0 , idx ) , rpcId . substring ( idx + SumiConnectionMultiplexer . SEP_LENGTH ) ] ;
41+ }
42+
43+ protected static normalizeServiceId ( serviceId : string ) {
44+ return serviceId . replace ( / \/ / g, '_' ) ;
45+ }
46+
47+ protected readonly _locals : Map < string , any > ;
48+ protected readonly _proxies : Map < string , any > ;
4549
4650 constructor ( protected socket : BaseConnection < Uint8Array > , protected options : ISumiConnectionOptions = { } ) {
4751 super ( socket , options ) ;
4852 this . _locals = new Map ( ) ;
4953 this . _proxies = new Map ( ) ;
5054
51- this . onRequestNotFound ( ( rpcName : string , args : any [ ] ) => {
52- const [ rpcId , methodName ] = extractServiceAndMethod ( rpcName ) ;
53- return this . _doInvokeHandler ( rpcId , methodName , args ) ;
54- } ) ;
55+ this . onRequestNotFound ( ( rpcName : string , args : any [ ] ) => this . _doInvokeHandler ( rpcName , args ) ) ;
5556
5657 // call `listen` implicitly
5758 // compatible behavior with the RPCProtocol
5859 this . listen ( ) ;
5960 }
6061
6162 public set < T > ( identifier : ProxyIdentifier < T > , instance : any ) {
62- this . _locals . set ( identifier . serviceId , instance ) ;
63+ this . _locals . set ( SumiConnectionMultiplexer . normalizeServiceId ( identifier . serviceId ) , instance ) ;
6364 return instance ;
6465 }
6566
6667 public get < T > ( identifier : ProxyIdentifier < T > ) {
67- return this . _locals . get ( identifier . serviceId ) ;
68+ return this . _locals . get ( SumiConnectionMultiplexer . normalizeServiceId ( identifier . serviceId ) ) ;
6869 }
6970
7071 public getProxy < T > ( proxyId : ProxyIdentifier < T > ) {
71- if ( ! this . _proxies . has ( proxyId . serviceId ) ) {
72- this . _proxies . set ( proxyId . serviceId , this . _createProxy ( proxyId . serviceId ) ) ;
72+ const serviceId = SumiConnectionMultiplexer . normalizeServiceId ( proxyId . serviceId ) ;
73+
74+ if ( ! this . _proxies . has ( serviceId ) ) {
75+ this . _proxies . set ( serviceId , this . _createProxy ( serviceId ) ) ;
7376 }
7477
75- return this . _proxies . get ( proxyId . serviceId ) ;
78+ return this . _proxies . get ( serviceId ) ;
7679 }
7780
78- private _createProxy ( rpcId : string ) {
81+ protected _createProxy ( rpcId : string ) {
7982 const handler = {
8083 get : ( target : any , name : string ) => {
8184 if ( typeof name === 'symbol' ) {
8285 return null ;
8386 }
8487 // charCodeAt(0) === 36 means starts with $
8588 if ( ! target [ name ] && name . charCodeAt ( 0 ) === 36 ) {
86- const rpcName = getRPCName ( rpcId , name ) ;
89+ const rpcName = SumiConnectionMultiplexer . getRPCName ( rpcId , name ) ;
8790 target [ name ] = ( ...args : any [ ] ) => this . sendRequest ( rpcName , ...args ) ;
8891 }
8992
@@ -94,7 +97,9 @@ export class SumiConnectionMultiplexer extends SumiConnection implements IRPCPro
9497 return new Proxy ( Object . create ( null ) , handler ) ;
9598 }
9699
97- private async _doInvokeHandler ( rpcId : string , methodName : string , args : any [ ] ) : Promise < any > {
100+ protected async _doInvokeHandler ( rpcName : string , args : any [ ] ) : Promise < any > {
101+ const [ rpcId , methodName ] = SumiConnectionMultiplexer . extractServiceAndMethod ( rpcName ) ;
102+
98103 const actor = this . _locals . get ( rpcId ) ;
99104 if ( ! actor ) {
100105 throw new Error ( 'Unknown actor ' + rpcId ) ;
0 commit comments