Created
June 30, 2023 10:48
-
-
Save Dubiy/2da3f1574f4233c45f80f342b6547305 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Jint; | |
using UnityEngine; | |
using System; | |
using System.IO; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
using System.Collections.Generic; | |
public class RunJs : MonoBehaviour | |
{ | |
void Start() | |
{ | |
Engine engine = new Engine(); | |
engine.SetValue("console",typeof(Debug)); | |
engine.Execute("window = {};"); | |
engine.Execute("global = {};"); | |
engine.SetValue("fetch", new Func<RequestObj, FetchResponse>(Fetch)); | |
engine.Execute(@" | |
(() => { | |
return new Promise(resolve => { | |
console.log('go promise'); | |
fetch({url: 'https://mainnet-public.mirrornode.hedera.com/api/v1/accounts/0.0.3333'}) | |
.then(response => { | |
console.log(`response.json(): ${response.json()}`); | |
// resolve(response.json()); | |
resolve('{fetchCallback: `fail`}'); | |
}) | |
.catch(error => console.error('Error:', error)); | |
// resolve('{outerResolver: `ok`}'); | |
}); | |
})() | |
.then(res => { | |
console.log(`Promise callback: ${res}`); | |
return res; | |
}); | |
"); | |
static FetchResponse Fetch(RequestObj request) | |
{ | |
var url = request.url; | |
var fetchResponse = new FetchResponse(); | |
Task.Run(async () => | |
{ | |
try | |
{ | |
using (var httpClient = new HttpClient()) | |
{ | |
var response = await httpClient.GetAsync(url); | |
var content = await response.Content.ReadAsStringAsync(); | |
var result = new | |
{ | |
ok = response.IsSuccessStatusCode, | |
status = (int)response.StatusCode, | |
statusText = response.StatusCode.ToString(), | |
text = new Func<string>(() => content), | |
json = new Func<string>(() => content) | |
}; | |
fetchResponse.Resolve(result); | |
} | |
} | |
catch (Exception e) | |
{ | |
var result = new | |
{ | |
ok = false, | |
status = 0, | |
statusText = e.Message, | |
text = new Func<string>(() => string.Empty), | |
json = new Func<string>(() => string.Empty) | |
}; | |
fetchResponse.Reject(result); | |
} | |
}); | |
return fetchResponse; | |
} | |
} | |
} | |
public class RequestObj | |
{ | |
public string url { get; set; } | |
public string method { get; set; } = "get"; | |
public override string ToString() | |
{ | |
return $"url: {url}, method: {method}"; | |
} | |
} | |
class FetchResponse | |
{ | |
private Action<object> resolveCallback; | |
private Action<object> rejectCallback; | |
public void Resolve(object result) | |
{ | |
resolveCallback?.Invoke(result); | |
} | |
public void Reject(object error) | |
{ | |
rejectCallback?.Invoke(error); | |
} | |
// set callbacks. | |
// TODO: chain callbacks | |
public FetchResponse Then(Action<object> onResolved) | |
{ | |
resolveCallback = onResolved; | |
return this; | |
} | |
public FetchResponse Catch(Action<object> onRejected) | |
{ | |
rejectCallback = onRejected; | |
return this; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment