@@ -4,16 +4,19 @@ using Uno.Threading;
44namespace Fuse . Scripting
55{
66 public delegate T ResultFactory < T > ( object [ ] args ) ;
7+ public delegate T ResultFactory2 < T > ( Context context , object [ ] args ) ;
78 public delegate Future < T > FutureFactory < T > ( object [ ] args ) ;
9+ public delegate Future < T > FutureFactory2 < T > ( Context context , object [ ] args ) ;
810 public delegate TJSResult ResultConverter < T , TJSResult > ( Context context , T result ) ;
911
1012 class FactoryClosure < T >
1113 {
12- ResultFactory < T > _factory ;
14+ ResultFactory2 < T > _factory ;
15+ Context _context ;
1316 object [ ] _args ;
1417 Promise < T > _promise ;
1518
16- public FactoryClosure ( ResultFactory < T > factory , object [ ] args , Promise < T > promise )
19+ public FactoryClosure ( ResultFactory2 < T > factory , Context context , object [ ] args , Promise < T > promise )
1720 {
1821 _factory = factory ;
1922 _args = args ;
@@ -25,7 +28,7 @@ namespace Fuse.Scripting
2528 T res = default ( T ) ;
2629 try
2730 {
28- res = _factory ( _args ) ;
31+ res = _factory ( _context , _args ) ;
2932 }
3033 catch ( Exception e )
3134 {
@@ -39,25 +42,38 @@ namespace Fuse.Scripting
3942
4043 public sealed class NativePromise < T , TJSResult > : NativeMember
4144 {
42- FutureFactory < T > _futureFactory ;
45+ FutureFactory2 < T > _futureFactory ;
4346 ResultConverter < T , TJSResult > _resultConverter ;
44- ResultFactory < T > _func ;
47+ ResultFactory2 < T > _func ;
4548
4649 public NativePromise ( string name , ResultFactory < T > func , ResultConverter < T , TJSResult > resultConverter = null ) : base ( name )
50+ {
51+ _func = ( context , args ) => func ( args ) ;
52+ _futureFactory = ( FutureFactory2 < T > ) Factory ;
53+ _resultConverter = resultConverter ;
54+ }
55+
56+ public NativePromise ( string name , ResultFactory2 < T > func , ResultConverter < T , TJSResult > resultConverter = null ) : base ( name )
4757 {
4858 _func = func ;
49- _futureFactory = ( FutureFactory < T > ) Factory ;
59+ _futureFactory = ( FutureFactory2 < T > ) Factory ;
5060 _resultConverter = resultConverter ;
5161 }
5262
53- Future < T > Factory ( object [ ] args )
63+ Future < T > Factory ( Context context , object [ ] args )
5464 {
5565 var future = new Promise < T > ( ) ;
56- new Thread ( new FactoryClosure < T > ( _func , args , future ) . Run ) . Start ( ) ;
66+ new Thread ( new FactoryClosure < T > ( _func , context , args , future ) . Run ) . Start ( ) ;
5767 return future ;
5868 }
5969
6070 public NativePromise ( string name , FutureFactory < T > futureFactory , ResultConverter < T , TJSResult > resultConverter = null ) : base ( name )
71+ {
72+ _futureFactory = ( context , args ) => futureFactory ( args ) ;
73+ _resultConverter = resultConverter ;
74+ }
75+
76+ public NativePromise ( string name , FutureFactory2 < T > futureFactory , ResultConverter < T , TJSResult > resultConverter = null ) : base ( name )
6177 {
6278 _futureFactory = futureFactory ;
6379 _resultConverter = resultConverter ;
@@ -70,18 +86,18 @@ namespace Fuse.Scripting
7086
7187 class ContextClosure
7288 {
73- FutureFactory < T > _factory ;
89+ FutureFactory2 < T > _factory ;
7490 ResultConverter < T , TJSResult > _converter ;
75- public ContextClosure ( FutureFactory < T > factory , ResultConverter < T , TJSResult > converter )
91+ public ContextClosure ( FutureFactory2 < T > factory , ResultConverter < T , TJSResult > converter )
7692 {
7793 _factory = factory ;
7894 _converter = converter ;
7995 }
8096
8197 internal object CreatePromise ( Context context , object [ ] args )
8298 {
83- var promise = ( Function ) context . GlobalObject [ "Promise" ] ; // HACK - TODO: get rid of this
84- var future = _factory ( args ) ;
99+ var promise = ( Function ) context . GlobalObject [ "Promise" ] ;
100+ var future = _factory ( context , args ) ;
85101 return promise . Construct ( context , ( Callback ) new PromiseClosure ( context . ThreadWorker , future , _converter ) . Run ) ;
86102 }
87103 }
@@ -123,7 +139,7 @@ namespace Fuse.Scripting
123139 _threadWorker . Invoke ( this . InternalResolve ) ;
124140 }
125141
126- void InternalResolve ( Scripting . Context context )
142+ void InternalResolve ( Context context )
127143 {
128144 if ( _converter != null )
129145 _resolve . Call ( context , _converter ( context , _result ) ) ;
@@ -138,7 +154,7 @@ namespace Fuse.Scripting
138154 _threadWorker . Invoke ( this . InternalReject ) ;
139155 }
140156
141- void InternalReject ( Scripting . Context context )
157+ void InternalReject ( Context context )
142158 {
143159 _reject . Call ( context , _reason . Message ) ;
144160 }
0 commit comments