Convert dangerous events to IBindables

This commit is contained in:
Dean Herbert
2020-05-19 16:44:22 +09:00
parent 4dbe948397
commit 052ad79fc6
16 changed files with 234 additions and 164 deletions

View File

@ -11,6 +11,7 @@ using Humanizer;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore;
using osu.Framework;
using osu.Framework.Bindables;
using osu.Framework.Extensions;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Logging;
@ -56,13 +57,17 @@ namespace osu.Game.Database
/// Fired when a new <typeparamref name="TModel"/> becomes available in the database.
/// This is not guaranteed to run on the update thread.
/// </summary>
public event Action<TModel> ItemAdded;
public IBindable<WeakReference<TModel>> ItemAdded => itemAdded;
private readonly Bindable<WeakReference<TModel>> itemAdded = new Bindable<WeakReference<TModel>>();
/// <summary>
/// Fired when a <typeparamref name="TModel"/> is removed from the database.
/// This is not guaranteed to run on the update thread.
/// </summary>
public event Action<TModel> ItemRemoved;
public IBindable<WeakReference<TModel>> ItemRemoved => itemRemoved;
private readonly Bindable<WeakReference<TModel>> itemRemoved = new Bindable<WeakReference<TModel>>();
public virtual string[] HandledExtensions => new[] { ".zip" };
@ -82,8 +87,8 @@ namespace osu.Game.Database
ContextFactory = contextFactory;
ModelStore = modelStore;
ModelStore.ItemAdded += item => handleEvent(() => ItemAdded?.Invoke(item));
ModelStore.ItemRemoved += s => handleEvent(() => ItemRemoved?.Invoke(s));
ModelStore.ItemAdded += item => handleEvent(() => itemAdded.Value = new WeakReference<TModel>(item));
ModelStore.ItemRemoved += item => handleEvent(() => itemRemoved.Value = new WeakReference<TModel>(item));
Files = new FileStore(contextFactory, storage);

View File

@ -10,6 +10,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using osu.Framework.Bindables;
namespace osu.Game.Database
{
@ -23,9 +24,13 @@ namespace osu.Game.Database
where TModel : class, IHasFiles<TFileModel>, IHasPrimaryKey, ISoftDelete, IEquatable<TModel>
where TFileModel : class, INamedFileInfo, new()
{
public event Action<ArchiveDownloadRequest<TModel>> DownloadBegan;
public IBindable<WeakReference<ArchiveDownloadRequest<TModel>>> DownloadBegan => downloadBegan;
public event Action<ArchiveDownloadRequest<TModel>> DownloadFailed;
private readonly Bindable<WeakReference<ArchiveDownloadRequest<TModel>>> downloadBegan = new Bindable<WeakReference<ArchiveDownloadRequest<TModel>>>();
public IBindable<WeakReference<ArchiveDownloadRequest<TModel>>> DownloadFailed => downloadFailed;
private readonly Bindable<WeakReference<ArchiveDownloadRequest<TModel>>> downloadFailed = new Bindable<WeakReference<ArchiveDownloadRequest<TModel>>>();
private readonly IAPIProvider api;
@ -81,7 +86,7 @@ namespace osu.Game.Database
// for now a failed import will be marked as a failed download for simplicity.
if (!imported.Any())
DownloadFailed?.Invoke(request);
downloadFailed.Value = new WeakReference<ArchiveDownloadRequest<TModel>>(request);
currentDownloads.Remove(request);
}, TaskCreationOptions.LongRunning);
@ -100,14 +105,14 @@ namespace osu.Game.Database
api.PerformAsync(request);
DownloadBegan?.Invoke(request);
downloadBegan.Value = new WeakReference<ArchiveDownloadRequest<TModel>>(request);
return true;
void triggerFailure(Exception error)
{
currentDownloads.Remove(request);
DownloadFailed?.Invoke(request);
downloadFailed.Value = new WeakReference<ArchiveDownloadRequest<TModel>>(request);
notification.State = ProgressNotificationState.Cancelled;

View File

@ -1,8 +1,9 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using osu.Game.Online.API;
using System;
using osu.Game.Online.API;
using osu.Framework.Bindables;
namespace osu.Game.Database
{
@ -17,13 +18,13 @@ namespace osu.Game.Database
/// Fired when a <typeparamref name="TModel"/> download begins.
/// This is NOT run on the update thread and should be scheduled.
/// </summary>
event Action<ArchiveDownloadRequest<TModel>> DownloadBegan;
IBindable<WeakReference<ArchiveDownloadRequest<TModel>>> DownloadBegan { get; }
/// <summary>
/// Fired when a <typeparamref name="TModel"/> download is interrupted, either due to user cancellation or failure.
/// This is NOT run on the update thread and should be scheduled.
/// </summary>
event Action<ArchiveDownloadRequest<TModel>> DownloadFailed;
IBindable<WeakReference<ArchiveDownloadRequest<TModel>>> DownloadFailed { get; }
/// <summary>
/// Checks whether a given <typeparamref name="TModel"/> is already available in the local store.

View File

@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.
using System;
using osu.Framework.Bindables;
namespace osu.Game.Database
{
@ -9,11 +10,11 @@ namespace osu.Game.Database
/// Represents a model manager that publishes events when <typeparamref name="TModel"/>s are added or removed.
/// </summary>
/// <typeparam name="TModel">The model type.</typeparam>
public interface IModelManager<out TModel>
public interface IModelManager<TModel>
where TModel : class
{
event Action<TModel> ItemAdded;
IBindable<WeakReference<TModel>> ItemAdded { get; }
event Action<TModel> ItemRemoved;
IBindable<WeakReference<TModel>> ItemRemoved { get; }
}
}