mirror of
https://github.com/ppy/osu.git
synced 2025-01-13 16:32:54 +08:00
Split out unmanaged implementation of RealmLive
into its own class
This commit is contained in:
parent
9d85beddbe
commit
a9dbcd92a1
@ -24,20 +24,16 @@ namespace osu.Game.Database
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly T data;
|
private readonly T data;
|
||||||
|
|
||||||
private readonly RealmContextFactory? realmFactory;
|
private readonly RealmContextFactory realmFactory;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Construct a new instance of live realm data.
|
/// Construct a new instance of live realm data.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="data">The realm data.</param>
|
/// <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>
|
/// <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;
|
this.data = data;
|
||||||
|
|
||||||
if (IsManaged && realmFactory == null)
|
|
||||||
throw new ArgumentException(@"Realm factory must be provided for a managed instance", nameof(realmFactory));
|
|
||||||
|
|
||||||
this.realmFactory = realmFactory;
|
this.realmFactory = realmFactory;
|
||||||
|
|
||||||
ID = data.ID;
|
ID = data.ID;
|
||||||
@ -55,9 +51,6 @@ namespace osu.Game.Database
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (realmFactory == null)
|
|
||||||
throw new ArgumentException(@"Realm factory must be provided for a managed instance", nameof(realmFactory));
|
|
||||||
|
|
||||||
using (var realm = realmFactory.CreateContext())
|
using (var realm = realmFactory.CreateContext())
|
||||||
perform(realm.Find<T>(ID));
|
perform(realm.Find<T>(ID));
|
||||||
}
|
}
|
||||||
@ -74,9 +67,6 @@ namespace osu.Game.Database
|
|||||||
if (!IsManaged)
|
if (!IsManaged)
|
||||||
return perform(data);
|
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())
|
using (var realm = realmFactory.CreateContext())
|
||||||
return perform(realm.Find<T>(ID));
|
return perform(realm.Find<T>(ID));
|
||||||
}
|
}
|
||||||
@ -108,7 +98,7 @@ namespace osu.Game.Database
|
|||||||
if (!ThreadSafety.IsUpdateThread)
|
if (!ThreadSafety.IsUpdateThread)
|
||||||
throw new InvalidOperationException($"Can't use {nameof(Value)} on managed objects from non-update threads");
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
44
osu.Game/Database/RealmLiveUnmanaged.cs
Normal file
44
osu.Game/Database/RealmLiveUnmanaged.cs
Normal 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; }
|
||||||
|
}
|
||||||
|
}
|
@ -56,13 +56,13 @@ namespace osu.Game.Database
|
|||||||
public static List<ILive<T>> ToLiveUnmanaged<T>(this IEnumerable<T> realmList)
|
public static List<ILive<T>> ToLiveUnmanaged<T>(this IEnumerable<T> realmList)
|
||||||
where T : RealmObject, IHasGuidPrimaryKey
|
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)
|
public static ILive<T> ToLiveUnmanaged<T>(this T realmObject)
|
||||||
where T : RealmObject, IHasGuidPrimaryKey
|
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)
|
public static List<ILive<T>> ToLive<T>(this IEnumerable<T> realmList, RealmContextFactory realmContextFactory)
|
||||||
|
Loading…
Reference in New Issue
Block a user