@@ -66,40 +66,60 @@ function rawr({ transport, timeout = 0, handlers = {}, methods, idGenerator }) {
6666 addHandler ( m , methods [ m ] ) ;
6767 } ) ;
6868
69- const methodsProxy = new Proxy ( { } , {
70- get : ( target , name ) => {
71- return ( ...args ) => {
72- const id = idGenerator ? idGenerator ( ) : ++ callId ;
73- const msg = {
74- jsonrpc : '2.0' ,
75- method : name ,
76- params : args ,
77- id
78- } ;
69+ function sendMessage ( method , params , config ) {
70+ const id = idGenerator ? idGenerator ( ) : ++ callId ;
71+ const msg = {
72+ jsonrpc : '2.0' ,
73+ method,
74+ params,
75+ id
76+ } ;
7977
80- let timeoutId ;
81- if ( timeout ) {
82- timeoutId = setTimeout ( ( ) => {
83- if ( pendingCalls [ id ] ) {
84- const err = new Error ( 'RPC timeout' ) ;
85- err . code = 504 ;
86- pendingCalls [ id ] . reject ( err ) ;
87- delete pendingCalls [ id ] ;
88- }
89- } , timeout ) ;
78+ let timeoutId ;
79+ if ( config . timeout || timeout ) {
80+ timeoutId = setTimeout ( ( ) => {
81+ if ( pendingCalls [ id ] ) {
82+ const err = new Error ( 'RPC timeout' ) ;
83+ err . code = 504 ;
84+ pendingCalls [ id ] . reject ( err ) ;
85+ delete pendingCalls [ id ] ;
9086 }
87+ } , config . timeout || timeout ) ;
88+ }
9189
92- const response = new Promise ( ( resolve , reject ) => {
93- pendingCalls [ id ] = { resolve, reject, timeoutId } ;
94- } ) ;
90+ const response = new Promise ( ( resolve , reject ) => {
91+ pendingCalls [ id ] = { resolve, reject, timeoutId } ;
92+ } ) ;
9593
96- transport . send ( msg ) ;
94+ transport . send ( msg , config ) ;
9795
98- return response ;
96+ return response ;
97+ }
98+
99+ const methodsProxy = new Proxy ( { } , {
100+ get : ( target , name ) => {
101+ return ( ...args ) => {
102+ return sendMessage ( name , args , { } ) ;
99103 } ;
100104 }
101105 } ) ;
102106
107+ const configurableMethodsProxy = new Proxy ( { } , {
108+ get : ( target , name ) => {
109+ return ( ...args ) => {
110+ let config ;
111+ if ( args . length ) {
112+ const testArg = args . pop ( ) ;
113+ if ( testArg && typeof testArg === 'object' && ! Array . isArray ( testArg ) ) {
114+ config = testArg ;
115+ }
116+ }
117+ return sendMessage ( name , args , config || { } ) ;
118+ } ;
119+ }
120+ } ) ;
121+
122+
103123 const notifiers = new Proxy ( { } , {
104124 get : ( target , name ) => {
105125 return ( ...args ) => {
@@ -125,6 +145,7 @@ function rawr({ transport, timeout = 0, handlers = {}, methods, idGenerator }) {
125145
126146 return {
127147 methods : methodsProxy ,
148+ methodsExt : configurableMethodsProxy ,
128149 addHandler,
129150 notifications,
130151 notifiers,
@@ -759,8 +780,8 @@ function dom(webWorker) {
759780 emitter . emit ( 'rpc' , data ) ;
760781 }
761782 } ) ;
762- emitter . send = ( msg ) => {
763- webWorker . postMessage ( msg ) ;
783+ emitter . send = ( msg , config ) => {
784+ webWorker . postMessage ( msg , config ? config . postMessageOptions : undefined ) ;
764785 } ;
765786 return emitter ;
766787}
@@ -773,8 +794,8 @@ function worker() {
773794 emitter . emit ( 'rpc' , data ) ;
774795 }
775796 } ;
776- emitter . send = ( msg ) => {
777- self . postMessage ( msg ) ;
797+ emitter . send = ( msg , config ) => {
798+ self . postMessage ( msg , config ? config . postMessageOptions : undefined ) ;
778799 } ;
779800 return emitter ;
780801}
0 commit comments