@@ -33,6 +33,11 @@ public IJavaScriptEngine Engine
33
33
public ReactUnityWebGLCompat WebGLCompat { get ; } = new ReactUnityWebGLCompat ( ) ;
34
34
private Callback WebGLCompatDispatchEventCallback { get ; set ; }
35
35
36
+
37
+ private List < Action > beforeStartCallbacks = new List < Action > ( ) { } ;
38
+ private List < Action < Exception > > afterStartCallbacks = new List < Action < Exception > > ( ) { } ;
39
+
40
+
36
41
public ScriptContext ( ReactContext context , JavascriptEngineType engineType , bool debug = false , bool awaitDebugger = false )
37
42
{
38
43
Context = context ;
@@ -48,23 +53,13 @@ public void RunMainScript(string script, Action beforeStart = null, Action after
48
53
{
49
54
if ( string . IsNullOrWhiteSpace ( script ) ) return ;
50
55
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 ( ) ) ;
60
58
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" ) ) ;
65
60
}
66
61
67
- public void Initialize ( Action callback )
62
+ public void Initialize ( Func < Exception > callback )
68
63
{
69
64
if ( Initialized )
70
65
{
@@ -112,9 +107,18 @@ public void Initialize(Action callback)
112
107
var dispatchWebGLCompatCallback = engine . GetGlobal ( "dispatchWebGLCompatEvent" ) ;
113
108
WebGLCompatDispatchEventCallback = new Callback ( dispatchWebGLCompatCallback , Context ) ;
114
109
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
+
115
117
EngineInitialized = true ;
116
118
117
- callback ? . Invoke ( ) ;
119
+ beforeStartCallbacks . ForEach ( x => x ? . Invoke ( ) ) ;
120
+ var error = callback ? . Invoke ( ) ;
121
+ afterStartCallbacks . ForEach ( x => x ? . Invoke ( error ) ) ;
118
122
} ) ;
119
123
}
120
124
else callback ? . Invoke ( ) ;
0 commit comments