@@ -26,7 +26,20 @@ public RequestMessageBody(string[] arguments)
26
26
public string [ ] arguments ;
27
27
}
28
28
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
+
29
41
private static Dictionary < string , TaskCompletionSource < string > > taskMap = new Dictionary < string , TaskCompletionSource < string > > ( ) ;
42
+ private static Dictionary < string , GenericAction > taskActionMap = new Dictionary < string , GenericAction > ( ) ;
30
43
31
44
[ AOT . MonoPInvokeCallback ( typeof ( Action < string , string , string > ) ) ]
32
45
private static void jsCallback ( string taskId , string result , string error )
@@ -45,6 +58,16 @@ private static void jsCallback(string taskId, string result, string error)
45
58
}
46
59
}
47
60
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
+
48
71
public static void Initialize ( string chainOrRPC , ThirdwebSDK . Options options )
49
72
{
50
73
if ( Application . isEditor )
@@ -115,6 +138,21 @@ public static async Task<T> InvokeRoute<T>(string route, string[] body)
115
138
return JsonConvert . DeserializeObject < Result < T > > ( result ) . result ;
116
139
}
117
140
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
+
118
156
public static async Task FundWallet ( FundWalletOptions payload )
119
157
{
120
158
if ( Application . isEditor )
@@ -133,6 +171,8 @@ public static async Task FundWallet(FundWalletOptions payload)
133
171
[ DllImport ( "__Internal" ) ]
134
172
private static extern string ThirdwebInvoke ( string taskId , string route , string payload , Action < string , string , string > cb ) ;
135
173
[ DllImport ( "__Internal" ) ]
174
+ private static extern string ThirdwebInvokeListener ( string taskId , string route , string payload , Action < string , string > action ) ;
175
+ [ DllImport ( "__Internal" ) ]
136
176
private static extern string ThirdwebInitialize ( string chainOrRPC , string options ) ;
137
177
[ DllImport ( "__Internal" ) ]
138
178
private static extern string ThirdwebConnect ( string taskId , string wallet , int chainId , Action < string , string , string > cb ) ;
0 commit comments