@nativescript/windows-hermes 1.0.0-beta.12 → 1.0.0-beta.14

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.
@@ -1,4 +1,5 @@
1
1
  using System;
2
+ using System.Runtime.InteropServices;
2
3
  using System.Text.Json;
3
4
  using Windows.ApplicationModel;
4
5
  using Microsoft.UI.Dispatching;
@@ -31,7 +32,8 @@ namespace __PROJECT_NAME__
31
32
  Shown = 3,
32
33
  Hidden = 4,
33
34
  UncaughtError = 5,
34
- Exit = 6
35
+ Exit = 6,
36
+ ExitSupported = 7
35
37
  }
36
38
 
37
39
  public App()
@@ -72,6 +74,8 @@ namespace __PROJECT_NAME__
72
74
  try
73
75
  {
74
76
  _runtimeHost.RunMainScript();
77
+ // Tells the app it may end itself (Application.Exit()) once its last window closes.
78
+ _runtimeHost.NotifyAppEvent((int)AppEventKind.ExitSupported, null);
75
79
  jsError = _runtimeHost.GetLastJsError();
76
80
  if (!string.IsNullOrEmpty(jsError))
77
81
  {
@@ -94,6 +98,9 @@ namespace __PROJECT_NAME__
94
98
  void ShowMainWindow()
95
99
  {
96
100
  StartPump();
101
+ #if DEBUG
102
+ _runtimeHost.WatchLiveSync(DispatcherQueue.GetForCurrentThread());
103
+ #endif
97
104
 
98
105
  try
99
106
  {
@@ -117,13 +124,35 @@ namespace __PROJECT_NAME__
117
124
  }
118
125
  }
119
126
 
127
+ // This window is never shown, so it only closes when the app exits. JavaScript ends the app
128
+ // itself (Application.Exit()) once its last window closes, from inside a callback: the
129
+ // runtime is still on the stack then, so it is disposed after the current dispatcher work
130
+ // item has unwound.
120
131
  private void OnWindowClosed(object sender, WindowEventArgs e)
121
132
  {
122
133
  StopPump();
123
134
  _runtimeHost.NotifyAppEvent((int)AppEventKind.Exit, null);
124
135
  try { ApplicationData.Current.LocalSettings.Values[LastLaunchArgsKey] = string.Empty; }
125
136
  catch (InvalidOperationException) { } // unpackaged: no ApplicationData
137
+
138
+ var dispatcherQueue = DispatcherQueue.GetForCurrentThread();
139
+ if (dispatcherQueue == null || !dispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, Shutdown))
140
+ {
141
+ Shutdown();
142
+ }
143
+ }
144
+
145
+ [DllImport("user32.dll")]
146
+ private static extern void PostQuitMessage(int exitCode);
147
+
148
+ private void Shutdown()
149
+ {
126
150
  _runtimeHost.Dispose();
151
+
152
+ // WinUI ignores Application.Exit() while no activated window is open
153
+ // (microsoft-ui-xaml#5931), which is the case once the app's windows have all closed,
154
+ // so the message loop is ended directly.
155
+ PostQuitMessage(0);
127
156
  }
128
157
 
129
158
  private void OnWindowActivated(object sender, WindowActivatedEventArgs e)
@@ -123,8 +123,60 @@ namespace __PROJECT_NAME__
123
123
  private const string InspectorMarker = "ns-inspector";
124
124
  private const string DebugBreakMarker = "ns-debugbreak";
125
125
  private const string DebuggerStartedMarker = "ns-debugger-started";
126
+ // The CLI writes LiveSyncMarker after syncing files into app\ (webpack HMR), but only
127
+ // while LiveSyncReadyMarker says this host is watching for it.
128
+ private const string LiveSyncMarker = "ns-livesync";
129
+ private const string LiveSyncReadyMarker = "ns-livesync-ready";
126
130
 
127
131
  private bool _waitForDebugger;
132
+ private FileSystemWatcher _liveSyncWatcher;
133
+
134
+ /// Applies CLI file syncs while the app runs, by calling global.__onLiveSync() on the UI
135
+ /// thread, as the Android and iOS runtimes do after a LiveSync.
136
+ public void WatchLiveSync(Microsoft.UI.Dispatching.DispatcherQueue dispatcherQueue)
137
+ {
138
+ if (!_initialized || _liveSyncWatcher != null || dispatcherQueue == null) return;
139
+ try
140
+ {
141
+ DeleteMarker(LiveSyncMarker);
142
+ var watcher = new FileSystemWatcher(LocalFolderPath, LiveSyncMarker)
143
+ {
144
+ NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite,
145
+ };
146
+ FileSystemEventHandler onMarker = (_, _) => dispatcherQueue.TryEnqueue(ApplyLiveSync);
147
+ watcher.Created += onMarker;
148
+ watcher.Changed += onMarker;
149
+ watcher.EnableRaisingEvents = true;
150
+ _liveSyncWatcher = watcher;
151
+ WriteMarker(LiveSyncReadyMarker, "");
152
+ }
153
+ catch (Exception ex)
154
+ {
155
+ System.Diagnostics.Debug.WriteLine($"[NativeScript LiveSync] Watch failed: {ex.Message}");
156
+ }
157
+ }
158
+
159
+ private void ApplyLiveSync()
160
+ {
161
+ // One write raises several events; the first one consumes the marker.
162
+ if (!_initialized || !ConsumeMarker(LiveSyncMarker)) return;
163
+ try
164
+ {
165
+ runtime_runscript(_runtime, "typeof __onLiveSync === 'function' && __onLiveSync();", "ns-livesync.js");
166
+ }
167
+ catch (Exception ex)
168
+ {
169
+ System.Diagnostics.Debug.WriteLine($"[NativeScript LiveSync] Apply failed: {ex.Message}");
170
+ }
171
+ }
172
+
173
+ private void StopWatchingLiveSync()
174
+ {
175
+ if (_liveSyncWatcher == null) return;
176
+ _liveSyncWatcher.Dispose();
177
+ _liveSyncWatcher = null;
178
+ DeleteMarker(LiveSyncReadyMarker);
179
+ }
128
180
 
129
181
  public string DevtoolsFrontendUrl { get; private set; }
130
182
 
@@ -235,6 +287,7 @@ namespace __PROJECT_NAME__
235
287
  }
236
288
  catch { }
237
289
  DeleteMarker(DebuggerStartedMarker);
290
+ DeleteMarker(LiveSyncReadyMarker);
238
291
  var debugBreak = ConsumeMarker(DebugBreakMarker);
239
292
  if (ConsumeMarker(InspectorMarker) || debugBreak)
240
293
  {
@@ -476,6 +529,9 @@ namespace __PROJECT_NAME__
476
529
  public void Dispose()
477
530
  {
478
531
  if (!_initialized) return;
532
+ #if DEBUG
533
+ StopWatchingLiveSync();
534
+ #endif
479
535
  runtime_deinit(_runtime);
480
536
  _runtime = 0;
481
537
  _initialized = false;
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nativescript/windows-hermes",
3
- "version": "1.0.0-beta.12",
3
+ "version": "1.0.0-beta.14",
4
4
  "description": "NativeScript Windows runtime (Hermes engine) with a WinUI 3 app template",
5
5
  "keywords": [
6
6
  "NativeScript",