1-  // This file is part of the WinFormium project.
1+ // This file is part of the WinFormium project.
22// Copyright (c) 2025 Xuanchen Lin all rights reserved.
33// This project is licensed under the LGPL License.
44// See the LICENSE file in the project root for more information.
@@ -15,6 +15,37 @@ public class JavaScriptPromiseContext
1515 /// </summary>
1616 private string ? _remoteCallId = null ;
1717
18+ internal bool IsPromiseContextReady
19+ {
20+ get ;
21+ set
22+ {
23+ field = value ;
24+
25+ if ( _resolvesShouldBeCalledWhenContextReady is not null )
26+ {
27+
28+ _resolvesShouldBeCalledWhenContextReady . Invoke ( ) ;
29+ }
30+ else if ( _rejectsShouldBeCalledWhenContextReady is not null )
31+ {
32+ _rejectsShouldBeCalledWhenContextReady . Invoke ( ) ;
33+ }
34+ }
35+ }
36+
37+ /// <summary>
38+ /// Gets or sets the action to invoke when the promise is resolved, if the context is not yet ready.
39+ /// </summary>
40+ private Action ? _resolvesShouldBeCalledWhenContextReady ;
41+
42+ /// <summary>
43+ /// Gets or sets the action to invoke when the promise is rejected, if the context is not yet ready.
44+ /// </summary>
45+ private Action ? _rejectsShouldBeCalledWhenContextReady ;
46+
47+
48+
1849 /// <summary>
1950 /// Generates and returns a unique call identifier for the remote promise invocation.
2051 /// </summary>
@@ -52,13 +83,25 @@ internal JavaScriptPromiseContext(int objectId)
5283 ObjectId = objectId ;
5384 }
5485
86+
87+
5588 /// <summary>
5689 /// Resolves the promise with an optional return value in JSON format.
5790 /// </summary>
5891 /// <param name="returnValueAsJson">The return value as a JSON string, or <c>null</c> if not applicable.</param>
5992 public void Resolve ( string ? returnValueAsJson = null )
6093 {
61- CallRemoteResovle ? . Invoke ( returnValueAsJson ) ;
94+ if ( IsPromiseContextReady )
95+ {
96+ CallRemoteResovle ? . Invoke ( returnValueAsJson ) ;
97+ }
98+ else
99+ {
100+ _resolvesShouldBeCalledWhenContextReady = ( ) =>
101+ {
102+ CallRemoteResovle ? . Invoke ( returnValueAsJson ) ;
103+ } ;
104+ }
62105 }
63106
64107 /// <summary>
@@ -67,7 +110,17 @@ public void Resolve(string? returnValueAsJson = null)
67110 /// <param name="reason">The reason for rejection, or <c>null</c> if not specified.</param>
68111 public void Reject ( string ? reason = null )
69112 {
70- CallRemoteReject ? . Invoke ( reason ) ;
113+ if ( IsPromiseContextReady )
114+ {
115+ CallRemoteReject ? . Invoke ( reason ) ;
116+ }
117+ else
118+ {
119+ _rejectsShouldBeCalledWhenContextReady = ( ) =>
120+ {
121+ CallRemoteReject ? . Invoke ( reason ) ;
122+ } ;
123+ }
71124 }
72125
73126 /// <summary>
0 commit comments