Skip to content

Commit 058c6e4

Browse files
committed
Use Rx event subscription instead of MessageBus to send information
1 parent ea1d438 commit 058c6e4

9 files changed

Lines changed: 62 additions & 39 deletions

File tree

v2rayN/ServiceLib/Enums/EMsgCommand.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.

v2rayN/ServiceLib/Handler/AppEvents.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,10 @@ namespace ServiceLib.Handler;
66
public static class AppEvents
77
{
88
public static readonly Subject<Unit> ProfilesRefreshRequested = new();
9+
10+
public static readonly Subject<string> SendSnackMsgRequested = new();
11+
12+
public static readonly Subject<string> SendMsgViewRequested = new();
13+
14+
public static readonly Subject<Unit> AppExitRequested = new();
915
}

v2rayN/ServiceLib/Manager/NoticeManager.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using ReactiveUI;
2-
31
namespace ServiceLib.Manager;
42

53
public class NoticeManager
@@ -13,7 +11,7 @@ public void Enqueue(string? content)
1311
{
1412
return;
1513
}
16-
MessageBus.Current.SendMessage(content, EMsgCommand.SendSnackMsg.ToString());
14+
AppEvents.SendSnackMsgRequested.OnNext(content);
1715
}
1816

1917
public void SendMessage(string? content)
@@ -22,7 +20,7 @@ public void SendMessage(string? content)
2220
{
2321
return;
2422
}
25-
MessageBus.Current.SendMessage(content, EMsgCommand.SendMsgView.ToString());
23+
AppEvents.SendMsgViewRequested.OnNext(content);
2624
}
2725

2826
public void SendMessageEx(string? content)

v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public async Task MyAppExitAsync(bool blWindowsShutDown)
300300
Logging.SaveLog("MyAppExitAsync Begin");
301301

302302
await SysProxyHandler.UpdateSysProxy(_config, true);
303-
MessageBus.Current.SendMessage("", EMsgCommand.AppExit.ToString());
303+
AppEvents.AppExitRequested.OnNext(Unit.Default);
304304

305305
await ConfigHandler.SaveConfig(_config);
306306
await ProfileExManager.Instance.SaveTo();

v2rayN/ServiceLib/ViewModels/MsgViewModel.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Concurrent;
2+
using System.Reactive.Linq;
23
using System.Text.RegularExpressions;
34
using ReactiveUI;
45
using ReactiveUI.Fody.Helpers;
@@ -34,12 +35,10 @@ public MsgViewModel(Func<EViewAction, object?, Task<bool>>? updateView)
3435
y => y == true)
3536
.Subscribe(c => { _config.MsgUIItem.AutoRefresh = AutoRefresh; });
3637

37-
MessageBus.Current.Listen<string>(EMsgCommand.SendMsgView.ToString()).Subscribe(OnNext);
38-
}
39-
40-
private async void OnNext(string x)
41-
{
42-
await AppendQueueMsg(x);
38+
AppEvents.SendMsgViewRequested
39+
.AsObservable()
40+
//.ObserveOn(RxApp.MainThreadScheduler)
41+
.Subscribe(async content => await AppendQueueMsg(content));
4342
}
4443

4544
private async Task AppendQueueMsg(string msg)

v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Reactive.Disposables;
2+
using System.Reactive.Linq;
23
using Avalonia;
34
using Avalonia.Controls;
45
using Avalonia.Controls.ApplicationLifetimes;
@@ -39,7 +40,6 @@ public MainWindow()
3940
menuBackupAndRestore.Click += MenuBackupAndRestore_Click;
4041
menuClose.Click += MenuClose_Click;
4142

42-
MessageBus.Current.Listen<string>(EMsgCommand.SendSnackMsg.ToString()).Subscribe(DelegateSnackMsg);
4343
ViewModel = new MainWindowViewModel(UpdateViewHandler);
4444
Locator.CurrentMutable.RegisterLazySingleton(() => ViewModel, typeof(MainWindowViewModel));
4545

@@ -136,6 +136,18 @@ public MainWindow()
136136
this.Bind(ViewModel, vm => vm.TabMainSelectedIndex, v => v.tabMain2.SelectedIndex).DisposeWith(disposables);
137137
break;
138138
}
139+
140+
AppEvents.SendSnackMsgRequested
141+
.AsObservable()
142+
.ObserveOn(RxApp.MainThreadScheduler)
143+
.Subscribe(async content => await DelegateSnackMsg(content))
144+
.DisposeWith(disposables);
145+
146+
AppEvents.AppExitRequested
147+
.AsObservable()
148+
.ObserveOn(RxApp.MainThreadScheduler)
149+
.Subscribe(_ => StorageUI())
150+
.DisposeWith(disposables);
139151
});
140152

141153
if (Utils.IsWindows())
@@ -156,7 +168,6 @@ public MainWindow()
156168
menuAddServerViaScan.IsVisible = false;
157169

158170
AddHelpMenuItem();
159-
MessageBus.Current.Listen<string>(EMsgCommand.AppExit.ToString()).Subscribe(StorageUI);
160171
}
161172

162173
#region Event
@@ -168,11 +179,9 @@ private void OnProgramStarted(object state, bool timeout)
168179
DispatcherPriority.Default);
169180
}
170181

171-
private void DelegateSnackMsg(string content)
182+
private async Task DelegateSnackMsg(string content)
172183
{
173-
Dispatcher.UIThread.Post(() =>
174-
_manager?.Show(new Notification(null, content, NotificationType.Information)),
175-
DispatcherPriority.Normal);
184+
_manager?.Show(new Notification(null, content, NotificationType.Information));
176185
}
177186

178187
private async Task<bool> UpdateViewHandler(EViewAction action, object? obj)
@@ -462,7 +471,7 @@ private void RestoreUI()
462471
}
463472
}
464473

465-
private void StorageUI(string? n = null)
474+
private void StorageUI()
466475
{
467476
ConfigHandler.SaveWindowSizeItem(_config, GetType().Name, Width, Height);
468477

v2rayN/v2rayN.Desktop/Views/ProfilesView.axaml.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Reactive.Disposables;
2+
using System.Reactive.Linq;
23
using Avalonia.Controls;
34
using Avalonia.Input;
45
using Avalonia.Interactivity;
@@ -96,11 +97,16 @@ public ProfilesView(Window window)
9697
this.BindCommand(ViewModel, vm => vm.Export2ClientConfigClipboardCmd, v => v.menuExport2ClientConfigClipboard).DisposeWith(disposables);
9798
this.BindCommand(ViewModel, vm => vm.Export2ShareUrlCmd, v => v.menuExport2ShareUrl).DisposeWith(disposables);
9899
this.BindCommand(ViewModel, vm => vm.Export2ShareUrlBase64Cmd, v => v.menuExport2ShareUrlBase64).DisposeWith(disposables);
100+
101+
AppEvents.AppExitRequested
102+
.AsObservable()
103+
.ObserveOn(RxApp.MainThreadScheduler)
104+
.Subscribe(_ => StorageUI())
105+
.DisposeWith(disposables);
99106
});
100107

101108
RestoreUI();
102109
ViewModel?.RefreshServers();
103-
MessageBus.Current.Listen<string>(EMsgCommand.AppExit.ToString()).Subscribe(StorageUI);
104110
}
105111

106112
private async void LstProfiles_Sorting(object? sender, DataGridColumnEventArgs e)
@@ -412,7 +418,7 @@ private void RestoreUI()
412418
}
413419
}
414420

415-
private void StorageUI(string? n = null)
421+
private void StorageUI()
416422
{
417423
List<ColumnItem> lvColumnItem = new();
418424
foreach (var item2 in lstProfiles.Columns)

v2rayN/v2rayN/Views/MainWindow.xaml.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.ComponentModel;
22
using System.Reactive.Disposables;
3+
using System.Reactive.Linq;
34
using System.Windows;
45
using System.Windows.Controls;
56
using System.Windows.Input;
@@ -36,7 +37,6 @@ public MainWindow()
3637
menuCheckUpdate.Click += MenuCheckUpdate_Click;
3738
menuBackupAndRestore.Click += MenuBackupAndRestore_Click;
3839

39-
MessageBus.Current.Listen<string>(EMsgCommand.SendSnackMsg.ToString()).Subscribe(DelegateSnackMsg);
4040
ViewModel = new MainWindowViewModel(UpdateViewHandler);
4141
Locator.CurrentMutable.RegisterLazySingleton(() => ViewModel, typeof(MainWindowViewModel));
4242

@@ -133,6 +133,18 @@ public MainWindow()
133133
this.Bind(ViewModel, vm => vm.TabMainSelectedIndex, v => v.tabMain2.SelectedIndex).DisposeWith(disposables);
134134
break;
135135
}
136+
137+
AppEvents.SendSnackMsgRequested
138+
.AsObservable()
139+
.ObserveOn(RxApp.MainThreadScheduler)
140+
.Subscribe(async content => await DelegateSnackMsg(content))
141+
.DisposeWith(disposables);
142+
143+
AppEvents.AppExitRequested
144+
.AsObservable()
145+
.ObserveOn(RxApp.MainThreadScheduler)
146+
.Subscribe(_ => StorageUI())
147+
.DisposeWith(disposables);
136148
});
137149

138150
this.Title = $"{Utils.GetVersion()} - {(Utils.IsAdministrator() ? ResUI.RunAsAdmin : ResUI.NotRunAsAdmin)}";
@@ -144,7 +156,6 @@ public MainWindow()
144156

145157
AddHelpMenuItem();
146158
WindowsManager.Instance.RegisterGlobalHotkey(_config, OnHotkeyHandler, null);
147-
MessageBus.Current.Listen<string>(EMsgCommand.AppExit.ToString()).Subscribe(StorageUI);
148159
}
149160

150161
#region Event
@@ -157,12 +168,9 @@ private void OnProgramStarted(object state, bool timeout)
157168
}));
158169
}
159170

160-
private void DelegateSnackMsg(string content)
171+
private async Task DelegateSnackMsg(string content)
161172
{
162-
Application.Current?.Dispatcher.Invoke((() =>
163-
{
164-
MainSnackbar.MessageQueue?.Enqueue(content);
165-
}), DispatcherPriority.Normal);
173+
MainSnackbar.MessageQueue?.Enqueue(content);
166174
}
167175

168176
private async Task<bool> UpdateViewHandler(EViewAction action, object? obj)
@@ -423,7 +431,7 @@ private void RestoreUI()
423431
}
424432
}
425433

426-
private void StorageUI(string? n = null)
434+
private void StorageUI()
427435
{
428436
ConfigHandler.SaveWindowSizeItem(_config, GetType().Name, Width, Height);
429437

v2rayN/v2rayN/Views/ProfilesView.xaml.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Reactive.Disposables;
2+
using System.Reactive.Linq;
23
using System.Windows;
34
using System.Windows.Controls;
45
using System.Windows.Controls.Primitives;
@@ -90,11 +91,16 @@ public ProfilesView()
9091
this.BindCommand(ViewModel, vm => vm.Export2ClientConfigClipboardCmd, v => v.menuExport2ClientConfigClipboard).DisposeWith(disposables);
9192
this.BindCommand(ViewModel, vm => vm.Export2ShareUrlCmd, v => v.menuExport2ShareUrl).DisposeWith(disposables);
9293
this.BindCommand(ViewModel, vm => vm.Export2ShareUrlBase64Cmd, v => v.menuExport2ShareUrlBase64).DisposeWith(disposables);
94+
95+
AppEvents.AppExitRequested
96+
.AsObservable()
97+
.ObserveOn(RxApp.MainThreadScheduler)
98+
.Subscribe(_ => StorageUI())
99+
.DisposeWith(disposables);
93100
});
94101

95102
RestoreUI();
96103
ViewModel?.RefreshServers();
97-
MessageBus.Current.Listen<string>(EMsgCommand.AppExit.ToString()).Subscribe(StorageUI);
98104
}
99105

100106
#region Event
@@ -369,7 +375,7 @@ private void RestoreUI()
369375
}
370376
}
371377

372-
private void StorageUI(string? n = null)
378+
private void StorageUI()
373379
{
374380
List<ColumnItem> lvColumnItem = new();
375381
foreach (var t in lstProfiles.Columns)

0 commit comments

Comments
 (0)