Skip to content

Commit c0bf8dd

Browse files
multi callbacks working
1 parent 7ce5346 commit c0bf8dd

File tree

6 files changed

+60
-33
lines changed

6 files changed

+60
-33
lines changed

Assets/ButtonBehavior.cs.meta

Lines changed: 0 additions & 11 deletions
This file was deleted.

Assets/Plugin/thirdweb.jslib

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
mergeInto(LibraryManager.library, {
2-
ThirdwebInvoke: function (route, payload, cb) {
2+
ThirdwebInvoke: function (taskId, route, payload, cb) {
3+
// convert taskId from pointer to str and allocate it to keep in memory
4+
var id = UTF8ToString(taskId);
5+
var idSize = lengthBytesUTF8(id) + 1;
6+
var idPtr = _malloc(idSize);
7+
stringToUTF8(id, idPtr, idSize);
8+
// execute bridge call
39
window.bridge
410
.invoke(UTF8ToString(route), UTF8ToString(payload))
511
.then((returnStr) => {
612
var bufferSize = lengthBytesUTF8(returnStr) + 1;
713
var buffer = _malloc(bufferSize);
814
stringToUTF8(returnStr, buffer, bufferSize);
9-
dynCall_vi(cb, buffer);
15+
// callback into unity
16+
dynCall_vii(cb, idPtr, buffer);
1017
});
1118
},
1219
});

Assets/SDKTest.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ void Update()
1818

1919
}
2020

21+
int count;
22+
2123
public async void OnLoginClick()
2224
{
2325
Debug.Log("Button clicked ");
24-
string result = await SDK.Initialize();
25-
Debug.Log("result" + result);
26+
count++;
27+
NFT result = await SDK.GetNFT(count.ToString());
28+
Debug.Log("name: " + result.metadata.name);
29+
Debug.Log("owner: " + result.owner);
2630
}
2731
}

Assets/Thirdweb/Scripts/Bridge.cs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ public class Bridge
1212
[System.Serializable]
1313
private struct Result<T>
1414
{
15-
public string ack_id;
1615
public T result;
1716
}
1817

@@ -26,36 +25,43 @@ public RequestMessageBody(string[] arguments)
2625
public string[] arguments;
2726
}
2827

29-
private static TaskCompletionSource<string> utcs;
28+
private static Dictionary<string, TaskCompletionSource<string>> taskMap = new Dictionary<string, TaskCompletionSource<string>>();
3029

31-
[AOT.MonoPInvokeCallback(typeof(Action<string>))]
32-
private static void testFuncCB(string result)
30+
[AOT.MonoPInvokeCallback(typeof(Action<string, string>))]
31+
private static void jsCallback(string taskId, string result)
3332
{
34-
utcs.TrySetResult(result);
33+
if (taskMap.ContainsKey(taskId))
34+
{
35+
taskMap[taskId].TrySetResult(result);
36+
taskMap.Remove(taskId);
37+
}
3538
}
3639

3740
public static async Task<T> InvokeRoute<T>(string route, string[] body)
3841
{
3942
var msg = JsonUtility.ToJson(new RequestMessageBody(body));
40-
var ack_id = ThirdwebInvoke(route, msg, testFuncCB);
41-
var tr = new TaskCompletionSource<string>();
42-
string result = await tr.Task;
43+
string taskId = System.Guid.NewGuid().ToString();
44+
var task = new TaskCompletionSource<string>();
45+
taskMap[taskId] = task;
46+
ThirdwebInvoke(taskId, route, msg, jsCallback);
47+
string result = await task.Task;
4348
// Debug.LogFormat("Result from {0}: {1}", route, result);
4449
return JsonUtility.FromJson<Result<T>>(result).result;
4550
}
4651

4752
public static async Task<string> InvokeRouteRaw(string route, string[] body)
4853
{
4954
var msg = JsonUtility.ToJson(new RequestMessageBody(body));
50-
Debug.LogFormat("Calling JS");
51-
utcs = new TaskCompletionSource<string>();
52-
ThirdwebInvoke(route, msg, testFuncCB);
53-
var result = await utcs.Task;
55+
string taskId = System.Guid.NewGuid().ToString();
56+
var task = new TaskCompletionSource<string>();
57+
taskMap[taskId] = task;
58+
ThirdwebInvoke(taskId, route, msg, jsCallback);
59+
string result = await task.Task;
5460
// Debug.LogFormat("Result from {0}: {1}", route, result);
5561
return result;
5662
}
5763

5864
[DllImport("__Internal")]
59-
private static extern string ThirdwebInvoke(string route, string payload, Action<string> cb);
65+
private static extern string ThirdwebInvoke(string taskId, string route, string payload, Action<string, string> cb);
6066
}
6167
}

Assets/Thirdweb/Scripts/SDK.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,30 @@
44

55
namespace Thirdweb
66
{
7+
8+
[System.Serializable]
9+
public struct NFTMetadata
10+
{
11+
public string id;
12+
public string uri;
13+
public string description;
14+
public string image;
15+
public string name;
16+
// TODO: support properties;
17+
}
18+
19+
[System.Serializable]
20+
public struct NFT
21+
{
22+
public NFTMetadata metadata;
23+
public string owner;
24+
}
25+
726
public class SDK
827
{
9-
public static async Task<string> Initialize()
28+
public static async Task<NFT> GetNFT(string id)
1029
{
11-
var result = await Bridge.InvokeRouteRaw("initialize", new string[] { });
30+
var result = await Bridge.InvokeRoute<NFT>("erc721.get", new string[] { id });
1231
return result;
1332
}
1433

Assets/WebGLTemplates/Better2020/index.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,17 @@
7878
loadingCover.style.display = "";
7979

8080
import { ThirdwebSDK } from "https://esm.sh/@thirdweb-dev/sdk@nightly?bundle"
81+
let id = 0;
8182
const w = window;
8283
w.bridge = {};
8384
w.bridge.invoke = async (route, payload) => {
8485
console.log("invoke called", route, payload);
8586
const sdk = new ThirdwebSDK("goerli");
8687
const contract = await sdk.getContract("0x2e01763fA0e15e07294D74B63cE4b526B321E389")
87-
const all = await contract.erc721.getAll();
88-
console.log("all", all);
89-
return JSON.stringify(all);
88+
const tokenId = JSON.parse(payload).arguments[0];
89+
console.log("tokenId", tokenId);
90+
const all = await contract.erc721.get(tokenId);
91+
return JSON.stringify({ result: all });
9092
};
9193

9294
const script = document.createElement("script");

0 commit comments

Comments
 (0)