@@ -26,7 +26,20 @@ public RequestMessageBody(string[] arguments)
2626 public string [ ] arguments ;
2727 }
2828
29+ private struct GenericAction
30+ {
31+ public Type t ;
32+ public Delegate d ;
33+
34+ public GenericAction ( Type t , Delegate d )
35+ {
36+ this . t = t ;
37+ this . d = d ;
38+ }
39+ }
40+
2941 private static Dictionary < string , TaskCompletionSource < string > > taskMap = new Dictionary < string , TaskCompletionSource < string > > ( ) ;
42+ private static Dictionary < string , GenericAction > taskActionMap = new Dictionary < string , GenericAction > ( ) ;
3043
3144 [ AOT . MonoPInvokeCallback ( typeof ( Action < string , string , string > ) ) ]
3245 private static void jsCallback ( string taskId , string result , string error )
@@ -45,6 +58,16 @@ private static void jsCallback(string taskId, string result, string error)
4558 }
4659 }
4760
61+ [ AOT . MonoPInvokeCallback ( typeof ( Action < string , string > ) ) ]
62+ private static void jsAction ( string taskId , string result )
63+ {
64+ if ( taskActionMap . ContainsKey ( taskId ) )
65+ {
66+ Type tempType = taskActionMap [ taskId ] . t ;
67+ taskActionMap [ taskId ] . d . DynamicInvoke ( tempType == typeof ( string ) ? result : JsonConvert . DeserializeObject ( result , tempType ) ) ;
68+ }
69+ }
70+
4871 public static void Initialize ( string chainOrRPC , ThirdwebSDK . Options options )
4972 {
5073 if ( Application . isEditor )
@@ -115,6 +138,21 @@ public static async Task<T> InvokeRoute<T>(string route, string[] body)
115138 return JsonConvert . DeserializeObject < Result < T > > ( result ) . result ;
116139 }
117140
141+ public static string InvokeListener < T > ( string route , string [ ] body , Action < T > action )
142+ {
143+ if ( Application . isEditor )
144+ {
145+ Debug . LogWarning ( "Interacting with the thirdweb SDK is not supported in the editor. Please build and run the app instead." ) ;
146+ return null ;
147+ }
148+
149+ string taskId = Guid . NewGuid ( ) . ToString ( ) ;
150+ taskActionMap [ taskId ] = new GenericAction ( typeof ( T ) , action ) ;
151+ var msg = Utils . ToJson ( new RequestMessageBody ( body ) ) ;
152+ ThirdwebInvokeListener ( taskId , route , msg , jsAction ) ;
153+ return taskId ;
154+ }
155+
118156 public static async Task FundWallet ( FundWalletOptions payload )
119157 {
120158 if ( Application . isEditor )
@@ -133,6 +171,8 @@ public static async Task FundWallet(FundWalletOptions payload)
133171 [ DllImport ( "__Internal" ) ]
134172 private static extern string ThirdwebInvoke ( string taskId , string route , string payload , Action < string , string , string > cb ) ;
135173 [ DllImport ( "__Internal" ) ]
174+ private static extern string ThirdwebInvokeListener ( string taskId , string route , string payload , Action < string , string > action ) ;
175+ [ DllImport ( "__Internal" ) ]
136176 private static extern string ThirdwebInitialize ( string chainOrRPC , string options ) ;
137177 [ DllImport ( "__Internal" ) ]
138178 private static extern string ThirdwebConnect ( string taskId , string wallet , int chainId , Action < string , string , string > cb ) ;
0 commit comments