1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-21 21:27:24 +08:00

Split out unmanaged implementation of RealmLive into its own class

This commit is contained in:
Dean Herbert 2021-12-16 15:11:48 +09:00
parent 9d85beddbe
commit a9dbcd92a1
3 changed files with 49 additions and 15 deletions

View File

@ -24,20 +24,16 @@ namespace osu.Game.Database
/// </summary>
private readonly T data;
private readonly RealmContextFactory? realmFactory;
private readonly RealmContextFactory realmFactory;
/// <summary>
/// Construct a new instance of live realm data.
/// </summary>
/// <param name="data">The realm data.</param>
/// <param name="realmFactory">The realm factory the data was sourced from. May be null for an unmanaged object.</param>
public RealmLive(T data, RealmContextFactory? realmFactory)
public RealmLive(T data, RealmContextFactory realmFactory)
{
this.data = data;
if (IsManaged && realmFactory == null)
throw new ArgumentException(@"Realm factory must be provided for a managed instance", nameof(realmFactory));
this.realmFactory = realmFactory;
ID = data.ID;
@ -55,9 +51,6 @@ namespace osu.Game.Database
return;
}
if (realmFactory == null)
throw new ArgumentException(@"Realm factory must be provided for a managed instance", nameof(realmFactory));
using (var realm = realmFactory.CreateContext())
perform(realm.Find<T>(ID));
}
@ -74,9 +67,6 @@ namespace osu.Game.Database
if (!IsManaged)
return perform(data);
if (realmFactory == null)
throw new ArgumentException(@"Realm factory must be provided for a managed instance", nameof(realmFactory));
using (var realm = realmFactory.CreateContext())
return perform(realm.Find<T>(ID));
}
@ -108,7 +98,7 @@ namespace osu.Game.Database
if (!ThreadSafety.IsUpdateThread)
throw new InvalidOperationException($"Can't use {nameof(Value)} on managed objects from non-update threads");
return realmFactory!.Context.Find<T>(ID);
return realmFactory.Context.Find<T>(ID);
}
}

View File

@ -0,0 +1,44 @@
// 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 System;
using Realms;
#nullable enable
namespace osu.Game.Database
{
/// <summary>
/// Provides a method of working with unmanaged realm objects.
/// Usually used for testing purposes where the instance is never required to be managed.
/// </summary>
/// <typeparam name="T">The underlying object type.</typeparam>
public class RealmLiveUnmanaged<T> : ILive<T> where T : RealmObjectBase, IHasGuidPrimaryKey
{
/// <summary>
/// Construct a new instance of live realm data.
/// </summary>
/// <param name="data">The realm data.</param>
public RealmLiveUnmanaged(T data)
{
Value = data;
}
public bool Equals(ILive<T>? other) => ID == other?.ID;
public Guid ID => Value.ID;
public void PerformRead(Action<T> perform) => perform(Value);
public TReturn PerformRead<TReturn>(Func<T, TReturn> perform) => perform(Value);
public void PerformWrite(Action<T> perform) => throw new InvalidOperationException(@"Can't perform writes on a non-managed underlying value");
public bool IsManaged => false;
/// <summary>
/// The original live data used to create this instance.
/// </summary>
public T Value { get; }
}
}

View File

@ -56,13 +56,13 @@ namespace osu.Game.Database
public static List<ILive<T>> ToLiveUnmanaged<T>(this IEnumerable<T> realmList)
where T : RealmObject, IHasGuidPrimaryKey
{
return realmList.Select(l => new RealmLive<T>(l, null)).Cast<ILive<T>>().ToList();
return realmList.Select(l => new RealmLiveUnmanaged<T>(l)).Cast<ILive<T>>().ToList();
}
public static ILive<T> ToLiveUnmanaged<T>(this T realmObject)
where T : RealmObject, IHasGuidPrimaryKey
{
return new RealmLive<T>(realmObject, null);
return new RealmLiveUnmanaged<T>(realmObject);
}
public static List<ILive<T>> ToLive<T>(this IEnumerable<T> realmList, RealmContextFactory realmContextFactory)