diff --git a/src/Snap.Hutao/Snap.Hutao/Service/DailyNote/DailyNoteService.cs b/src/Snap.Hutao/Snap.Hutao/Service/DailyNote/DailyNoteService.cs index c307ba1c..68897c1f 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/DailyNote/DailyNoteService.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/DailyNote/DailyNoteService.cs @@ -38,11 +38,11 @@ internal sealed partial class DailyNoteService : IDailyNoteService, IRecipient - public async ValueTask AddDailyNoteAsync(UserAndUid userAndUid) + public async ValueTask AddDailyNoteAsync(UserAndUid userAndUid, CancellationToken token = default) { string roleUid = userAndUid.Uid.Value; - if (await dailyNoteDbService.ContainsUidAsync(roleUid).ConfigureAwait(false)) + if (await dailyNoteDbService.ContainsUidAsync(roleUid, token).ConfigureAwait(false)) { return; } @@ -57,7 +57,7 @@ internal sealed partial class DailyNoteService : IDailyNoteService, IRecipient - public async ValueTask> GetDailyNoteEntryCollectionAsync(bool forceRefresh = false) + public async ValueTask> GetDailyNoteEntryCollectionAsync(bool forceRefresh = false, CancellationToken token = default) { if (entries is null) { // IUserService.GetUserGameRoleByUid only usable after call IUserService.GetRoleCollectionAsync await userService.GetRoleCollectionAsync().ConfigureAwait(false); - await RefreshDailyNotesCoreAsync(forceRefresh).ConfigureAwait(false); + await RefreshDailyNotesCoreAsync(forceRefresh, token).ConfigureAwait(false); - List entryList = await dailyNoteDbService.GetDailyNoteEntryListIncludingUserAsync().ConfigureAwait(false); + List entryList = await dailyNoteDbService.GetDailyNoteEntryListIncludingUserAsync(token).ConfigureAwait(false); entryList.ForEach(entry => { entry.UserGameRole = userService.GetUserGameRoleByUid(entry.Uid); }); entries = entryList.ToObservableCollection(); } @@ -92,35 +92,35 @@ internal sealed partial class DailyNoteService : IDailyNoteService, IRecipient - public ValueTask RefreshDailyNotesAsync() + public ValueTask RefreshDailyNotesAsync(CancellationToken token = default) { - return RefreshDailyNotesCoreAsync(true); + return RefreshDailyNotesCoreAsync(true, token); } /// - public async ValueTask RemoveDailyNoteAsync(DailyNoteEntry entry) + public async ValueTask RemoveDailyNoteAsync(DailyNoteEntry entry, CancellationToken token = default) { await taskContext.SwitchToMainThreadAsync(); ArgumentNullException.ThrowIfNull(entries); entries.Remove(entry); await taskContext.SwitchToBackgroundAsync(); - await dailyNoteDbService.DeleteDailyNoteEntryByIdAsync(entry.InnerId).ConfigureAwait(false); + await dailyNoteDbService.DeleteDailyNoteEntryByIdAsync(entry.InnerId, token).ConfigureAwait(false); } - public async ValueTask UpdateDailyNoteAsync(DailyNoteEntry entry) + public async ValueTask UpdateDailyNoteAsync(DailyNoteEntry entry, CancellationToken token = default) { await taskContext.SwitchToBackgroundAsync(); - await dailyNoteDbService.UpdateDailyNoteEntryAsync(entry).ConfigureAwait(false); + await dailyNoteDbService.UpdateDailyNoteEntryAsync(entry, token).ConfigureAwait(false); } - private async ValueTask RefreshDailyNotesCoreAsync(bool forceRefresh) + private async ValueTask RefreshDailyNotesCoreAsync(bool forceRefresh, CancellationToken token = default) { using (IServiceScope scope = serviceProvider.CreateScope()) { DailyNoteWebhookOperation dailyNoteWebhookOperation = serviceProvider.GetRequiredService(); - foreach (DailyNoteEntry entry in await dailyNoteDbService.GetDailyNoteEntryListIncludingUserAsync().ConfigureAwait(false)) + foreach (DailyNoteEntry entry in await dailyNoteDbService.GetDailyNoteEntryListIncludingUserAsync(token).ConfigureAwait(false)) { if (!forceRefresh && entry.DailyNote is not null) { @@ -132,7 +132,7 @@ internal sealed partial class DailyNoteService : IDailyNoteService, IRecipient dailyNoteResponse = await gameRecordClient - .GetDailyNoteAsync(new(entry.User, entry.Uid)) + .GetDailyNoteAsync(new(entry.User, entry.Uid), token) .ConfigureAwait(false); if (dailyNoteResponse.IsOk()) @@ -141,19 +141,19 @@ internal sealed partial class DailyNoteService : IDailyNoteService, IRecipient e.UserId == entry.UserId && e.Uid == entry.Uid) is { } cachedEntry) { entry.CopyTo(cachedEntry); } - // database + // Database { // 发送通知必须早于数据库更新,否则会导致通知重复 await dailyNoteNotificationOperation.SendAsync(entry).ConfigureAwait(false); - await dailyNoteDbService.UpdateDailyNoteEntryAsync(entry).ConfigureAwait(false); - await dailyNoteWebhookOperation.TryPostDailyNoteToWebhookAsync(entry.Uid, dailyNote).ConfigureAwait(false); + await dailyNoteDbService.UpdateDailyNoteEntryAsync(entry, token).ConfigureAwait(false); + await dailyNoteWebhookOperation.TryPostDailyNoteToWebhookAsync(entry.Uid, dailyNote, token).ConfigureAwait(false); } } } diff --git a/src/Snap.Hutao/Snap.Hutao/Service/DailyNote/IDailyNoteService.cs b/src/Snap.Hutao/Snap.Hutao/Service/DailyNote/IDailyNoteService.cs index 392bc3db..7ecd8120 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/DailyNote/IDailyNoteService.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/DailyNote/IDailyNoteService.cs @@ -18,22 +18,22 @@ internal interface IDailyNoteService /// /// 角色 /// 任务 - ValueTask AddDailyNoteAsync(UserAndUid userAndUid); + ValueTask AddDailyNoteAsync(UserAndUid userAndUid, CancellationToken token = default); - ValueTask> GetDailyNoteEntryCollectionAsync(bool forceRefresh = false); + ValueTask> GetDailyNoteEntryCollectionAsync(bool forceRefresh = false, CancellationToken token = default); /// /// 异步刷新实时便笺 /// /// 任务 - ValueTask RefreshDailyNotesAsync(); + ValueTask RefreshDailyNotesAsync(CancellationToken token = default); /// /// 移除指定的实时便笺 /// /// 指定的实时便笺 /// 任务 - ValueTask RemoveDailyNoteAsync(DailyNoteEntry entry); + ValueTask RemoveDailyNoteAsync(DailyNoteEntry entry, CancellationToken token = default); - ValueTask UpdateDailyNoteAsync(DailyNoteEntry entry); + ValueTask UpdateDailyNoteAsync(DailyNoteEntry entry, CancellationToken token = default); } \ No newline at end of file