Skip to content

Commit c80532c

Browse files
committed
improve addEventListener handling for running without main script
1 parent 4ff7b8d commit c80532c

File tree

2 files changed

+71
-17
lines changed

2 files changed

+71
-17
lines changed

Assets/Material Icons/Material Icons SDF - TMP.asset

Lines changed: 52 additions & 2 deletions
Large diffs are not rendered by default.

Runtime/Scripting/ScriptContext.cs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public IJavaScriptEngine Engine
3333
public ReactUnityWebGLCompat WebGLCompat { get; } = new ReactUnityWebGLCompat();
3434
private Callback WebGLCompatDispatchEventCallback { get; set; }
3535

36+
37+
private List<Action> beforeStartCallbacks = new List<Action>() { };
38+
private List<Action<Exception>> afterStartCallbacks = new List<Action<Exception>>() { };
39+
40+
3641
public ScriptContext(ReactContext context, JavascriptEngineType engineType, bool debug = false, bool awaitDebugger = false)
3742
{
3843
Context = context;
@@ -48,23 +53,13 @@ public void RunMainScript(string script, Action beforeStart = null, Action after
4853
{
4954
if (string.IsNullOrWhiteSpace(script)) return;
5055

51-
Initialize(() => {
52-
var beforeStartCallbacks = new List<Action>() { beforeStart };
53-
var afterStartCallbacks = new List<Action<Exception>>() { (success) => afterStart?.Invoke() };
54-
55-
engine.SetGlobal("addEventListener", new EventTarget.addEventListener((e, h, o) => GlobalEventTarget.AddEventListener(e, h)));
56-
engine.SetGlobal("removeEventListener", new EventTarget.removeEventListener((e, h, o) => GlobalEventTarget.RemoveEventListener(e, h)));
57-
engine.SetGlobal("dispatchEvent", new EventTarget.dispatchEvent((e, a) => GlobalEventTarget.DispatchEvent(e, Context, EventPriority.Unknown, a)));
58-
59-
afterStartCallbacks.Add((success) => GlobalEventTarget.DispatchEvent("DOMContentLoaded", Context, EventPriority.Discrete, success, this));
56+
beforeStartCallbacks.Add(beforeStart);
57+
afterStartCallbacks.Add((success) => afterStart?.Invoke());
6058

61-
beforeStartCallbacks.ForEach(x => x?.Invoke());
62-
var error = engine.TryExecute(script, "ReactUnity/main");
63-
afterStartCallbacks.ForEach(x => x?.Invoke(error));
64-
});
59+
Initialize(() => engine.TryExecute(script, "ReactUnity/main"));
6560
}
6661

67-
public void Initialize(Action callback)
62+
public void Initialize(Func<Exception> callback)
6863
{
6964
if (Initialized)
7065
{
@@ -112,9 +107,18 @@ public void Initialize(Action callback)
112107
var dispatchWebGLCompatCallback = engine.GetGlobal("dispatchWebGLCompatEvent");
113108
WebGLCompatDispatchEventCallback = new Callback(dispatchWebGLCompatCallback, Context);
114109

110+
111+
engine.SetGlobal("addEventListener", new EventTarget.addEventListener((e, h, o) => GlobalEventTarget.AddEventListener(e, h)));
112+
engine.SetGlobal("removeEventListener", new EventTarget.removeEventListener((e, h, o) => GlobalEventTarget.RemoveEventListener(e, h)));
113+
engine.SetGlobal("dispatchEvent", new EventTarget.dispatchEvent((e, a) => GlobalEventTarget.DispatchEvent(e, Context, EventPriority.Unknown, a)));
114+
115+
afterStartCallbacks.Add((success) => GlobalEventTarget.DispatchEvent("DOMContentLoaded", Context, EventPriority.Discrete, success, this));
116+
115117
EngineInitialized = true;
116118

117-
callback?.Invoke();
119+
beforeStartCallbacks.ForEach(x => x?.Invoke());
120+
var error = callback?.Invoke();
121+
afterStartCallbacks.ForEach(x => x?.Invoke(error));
118122
});
119123
}
120124
else callback?.Invoke();

0 commit comments

Comments
 (0)