Skip to content

Commit 467f644

Browse files
authored
AddListener/RemoveListener/ListenToAll/RemoveAllListeners (#23)
1 parent 70cd49d commit 467f644

File tree

10 files changed

+178456
-1062
lines changed

10 files changed

+178456
-1062
lines changed

Assets/Plugin/thirdweb.jslib

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,30 @@ var plugin = {
2424
dynCall_viii(cb, idPtr, null, buffer);
2525
});
2626
},
27+
ThirdwebInvokeListener: function (taskId, route, payload, action) {
28+
// execute bridge call
29+
window.bridge.invokeListener(
30+
UTF8ToString(taskId),
31+
UTF8ToString(route),
32+
UTF8ToString(payload),
33+
action,
34+
(jsAction, jsTaskId, jsResult) => {
35+
// Handle Task ID
36+
var jsId = jsTaskId;
37+
var jsIdSize = lengthBytesUTF8(jsId) + 1;
38+
var jsIdBuffer = _malloc(jsIdSize);
39+
stringToUTF8(jsId, jsIdBuffer, jsIdSize);
40+
41+
// Handle Result
42+
var jsRes = jsResult;
43+
var jsResSize = lengthBytesUTF8(jsRes) + 1;
44+
var jsResBuffer = _malloc(jsResSize);
45+
stringToUTF8(jsRes, jsResBuffer, jsResSize);
46+
47+
dynCall_vii(jsAction, jsIdBuffer, jsResBuffer);
48+
}
49+
);
50+
},
2751
ThirdwebInitialize: function (chain, options) {
2852
window.bridge.initialize(UTF8ToString(chain), UTF8ToString(options));
2953
},

Assets/Thirdweb/Core/Scripts/Bridge.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

Assets/Thirdweb/Core/Scripts/Events.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using System.Threading.Tasks;
3+
using System;
34

45
namespace Thirdweb
56
{
@@ -33,5 +34,36 @@ public async Task<List<ContractEvent<object>>> GetAll(EventQueryOptions eventQue
3334
{
3435
return await Bridge.InvokeRoute<List<ContractEvent<object>>>(getRoute("getAllEvents"), Utils.ToJsonStringArray(eventQueryOptions));
3536
}
37+
38+
/// <summary>
39+
/// Listens an event and executes callback
40+
/// </summary>
41+
/// <param name="eventName">Event name filter</param>
42+
/// <param name="action">Callback action</param>
43+
/// <typeparam name="T">Action return type</typeparam>
44+
/// <returns>Task ID string</returns>
45+
public string AddListener<T>(string eventName, Action<T> action)
46+
{
47+
return Bridge.InvokeListener<T>(getRoute("addEventListener"), Utils.ToJsonStringArray(eventName), action);
48+
}
49+
50+
/// <summary>
51+
/// Listens to all events and executes callback every time
52+
/// </summary>
53+
/// <param name="action">Callback action</param>
54+
/// <typeparam name="T">Action return type</typeparam>
55+
/// <returns>Task ID string</returns>
56+
public string ListenToAll<T>(Action<T> action)
57+
{
58+
return Bridge.InvokeListener(getRoute("listenToAllEvents"), new string[] { }, action);
59+
}
60+
61+
/// <summary>
62+
/// Removes all event listeners
63+
/// </summary>
64+
public async Task<string> RemoveAllListeners()
65+
{
66+
return await Bridge.InvokeRoute<string>(getRoute("removeAllListeners"), new string[] { });
67+
}
3668
}
3769
}

0 commit comments

Comments
 (0)