@nativescript/windows 0.1.0-alpha.62 → 0.1.0-alpha.63

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -161,12 +161,7 @@ public static partial class Bridge
161
161
  }
162
162
  }
163
163
 
164
- // Helper invoked from JS runtime to schedule a previously-registered
165
- // JS callback id to run on the application's UI thread. This method
166
- // attempts several common dispatcher abstractions (SynchronizationContext,
167
- // WPF Dispatcher, UWP/CoreDispatcher, WinUI DispatcherQueue) and falls
168
- // back to running the callback on the current thread if none are found.
169
- public static object? RunJsCallbackOnUIThread(int callbackId)
164
+ public static object? RunOnUIThread(int callbackId)
170
165
  {
171
166
  var action = new Action(() =>
172
167
  {
@@ -180,43 +175,6 @@ public static partial class Bridge
180
175
  }
181
176
  });
182
177
 
183
- // 1) SynchronizationContext (WinForms/WPF/UWP if present)
184
- var sc = SynchronizationContext.Current;
185
- if (sc != null)
186
- {
187
- var mre = new ManualResetEventSlim(false);
188
- sc.Post(_ => { try { action(); } finally { mre.Set(); } }, null);
189
- mre.Wait();
190
- return null;
191
- }
192
-
193
- // 2) WPF Dispatcher.CurrentDispatcher
194
- try
195
- {
196
- var wpfDispatcherType = AppDomain.CurrentDomain.GetAssemblies()
197
- .Select(a => a.GetType("System.Windows.Threading.Dispatcher"))
198
- .FirstOrDefault(t => t != null);
199
- if (wpfDispatcherType != null)
200
- {
201
- var currentDispatcherProp = wpfDispatcherType.GetProperty("CurrentDispatcher", BindingFlags.Public | BindingFlags.Static);
202
- var dispatcher = currentDispatcherProp?.GetValue(null);
203
- if (dispatcher != null)
204
- {
205
- var beginInvoke = dispatcher.GetType().GetMethod("BeginInvoke", new[] { typeof(Action) })
206
- ?? dispatcher.GetType().GetMethods().FirstOrDefault(m => m.Name == "BeginInvoke" && m.GetParameters().Length == 1);
207
- if (beginInvoke != null)
208
- {
209
- var mre = new ManualResetEventSlim(false);
210
- beginInvoke.Invoke(dispatcher, new object[] { new Action(() => { try { action(); } finally { mre.Set(); } }) });
211
- mre.Wait();
212
- return null;
213
- }
214
- }
215
- }
216
- }
217
- catch { }
218
-
219
- // 3) Try CoreDispatcher (UWP) via reflection
220
178
  try
221
179
  {
222
180
  foreach (var asm in AppDomain.CurrentDomain.GetAssemblies())
@@ -229,29 +187,37 @@ public static partial class Bridge
229
187
  var dispatcherProp = mainView.GetType().GetProperty("Dispatcher", BindingFlags.Public | BindingFlags.Instance);
230
188
  var dispatcher = dispatcherProp?.GetValue(mainView);
231
189
  if (dispatcher == null) continue;
232
- var runAsyncMethods = dispatcher.GetType().GetMethods().Where(m => m.Name == "RunAsync");
233
- foreach (var m in runAsyncMethods)
190
+
191
+ var hasAccessProp = dispatcher.GetType().GetProperty("HasThreadAccess", BindingFlags.Public | BindingFlags.Instance);
192
+ if (hasAccessProp?.GetValue(dispatcher) is true)
193
+ {
194
+ action();
195
+ return null;
196
+ }
197
+
198
+ foreach (var m in dispatcher.GetType().GetMethods().Where(m => m.Name == "RunAsync"))
234
199
  {
235
200
  var parameters = m.GetParameters();
236
201
  if (parameters.Length != 2) continue;
237
202
  var enumType = parameters[0].ParameterType;
238
203
  object priority = enumType.IsEnum ? Enum.ToObject(enumType, 0) : Activator.CreateInstance(enumType)!;
239
204
  var handlerType = parameters[1].ParameterType;
240
- var d = Delegate.CreateDelegate(handlerType, action.Target, action.Method);
241
- var result = m.Invoke(dispatcher, new object[] { priority, d });
242
- var asTask = result?.GetType().GetMethod("AsTask");
243
- if (asTask != null)
205
+ var mre = new ManualResetEventSlim(false);
206
+ var wrapped = new Action(() => { try { action(); } finally { mre.Set(); } });
207
+ try
244
208
  {
245
- var t = (Task)asTask.Invoke(result, null)!;
246
- t.GetAwaiter().GetResult();
209
+ var d = Delegate.CreateDelegate(handlerType, wrapped.Target, wrapped.Method);
210
+ m.Invoke(dispatcher, new object[] { priority, d });
211
+ mre.Wait();
212
+ return null;
247
213
  }
248
- return null;
214
+ catch { }
249
215
  }
216
+ break;
250
217
  }
251
218
  }
252
219
  catch { }
253
220
 
254
- // 4) Try Microsoft.UI.Dispatching.DispatcherQueue
255
221
  try
256
222
  {
257
223
  foreach (var asm in AppDomain.CurrentDomain.GetAssemblies())
@@ -261,19 +227,48 @@ public static partial class Bridge
261
227
  var getForCurrent = dqType.GetMethod("GetForCurrentThread", BindingFlags.Public | BindingFlags.Static);
262
228
  var dq = getForCurrent?.Invoke(null, null);
263
229
  if (dq == null) continue;
264
- var tryEnqueueMethods = dq.GetType().GetMethods().Where(m => m.Name == "TryEnqueue" && m.GetParameters().Length == 1);
265
- foreach (var m in tryEnqueueMethods)
230
+
231
+ if (dq.GetType().GetProperty("HasThreadAccess")?.GetValue(dq) is true)
266
232
  {
267
- var paramType = m.GetParameters()[0].ParameterType;
268
- var d = Delegate.CreateDelegate(paramType, action.Target, action.Method);
269
- var ok = m.Invoke(dq, new object[] { d });
233
+ action();
270
234
  return null;
271
235
  }
236
+ break;
237
+ }
238
+ }
239
+ catch { }
240
+
241
+ try
242
+ {
243
+ var wpfDispatcherType = AppDomain.CurrentDomain.GetAssemblies()
244
+ .Select(a => a.GetType("System.Windows.Threading.Dispatcher"))
245
+ .FirstOrDefault(t => t != null);
246
+ if (wpfDispatcherType != null)
247
+ {
248
+ var currentDispatcherProp = wpfDispatcherType.GetProperty("CurrentDispatcher", BindingFlags.Public | BindingFlags.Static);
249
+ var dispatcher = currentDispatcherProp?.GetValue(null);
250
+ if (dispatcher != null)
251
+ {
252
+ var checkAccess = dispatcher.GetType().GetMethod("CheckAccess");
253
+ if (checkAccess?.Invoke(dispatcher, null) is true)
254
+ {
255
+ action();
256
+ return null;
257
+ }
258
+ var beginInvoke = dispatcher.GetType().GetMethod("BeginInvoke", new[] { typeof(Action) })
259
+ ?? dispatcher.GetType().GetMethods().FirstOrDefault(m => m.Name == "BeginInvoke" && m.GetParameters().Length == 1);
260
+ if (beginInvoke != null)
261
+ {
262
+ var mre = new ManualResetEventSlim(false);
263
+ beginInvoke.Invoke(dispatcher, new object[] { new Action(() => { try { action(); } finally { mre.Set(); } }) });
264
+ mre.Wait();
265
+ return null;
266
+ }
267
+ }
272
268
  }
273
269
  }
274
270
  catch { }
275
271
 
276
- // Fallback: run directly on current thread.
277
272
  action();
278
273
  return null;
279
274
  }
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nativescript/windows",
3
- "version": "0.1.0-alpha.62",
3
+ "version": "0.1.0-alpha.63",
4
4
  "description": "NativeScript for using Windows v8",
5
5
  "repository": {
6
6
  "type": "git",