Rename register methods to better explain their purpose

This commit is contained in:
Dean Herbert 2022-01-24 14:37:36 +09:00
parent d7a9c5fd41
commit 40aa873190
15 changed files with 27 additions and 27 deletions

View File

@ -46,7 +46,7 @@ namespace osu.Game.Tests.Database
{ {
bool callbackRan = false; bool callbackRan = false;
realmFactory.Register(realm => realmFactory.RegisterCustomSubscription(realm =>
{ {
var subscription = realm.All<BeatmapInfo>().QueryAsyncWithNotifications((sender, changes, error) => var subscription = realm.All<BeatmapInfo>().QueryAsyncWithNotifications((sender, changes, error) =>
{ {

View File

@ -239,7 +239,7 @@ namespace osu.Game.Tests.Database
{ {
int changesTriggered = 0; int changesTriggered = 0;
realmFactory.Register(outerRealm => realmFactory.RegisterCustomSubscription(outerRealm =>
{ {
outerRealm.All<BeatmapInfo>().QueryAsyncWithNotifications(gotChange); outerRealm.All<BeatmapInfo>().QueryAsyncWithNotifications(gotChange);
ILive<BeatmapInfo>? liveBeatmap = null; ILive<BeatmapInfo>? liveBeatmap = null;

View File

@ -28,7 +28,7 @@ namespace osu.Game.Tests.Database
{ {
realmFactory.Write(realm => realm.Add(TestResources.CreateTestBeatmapSetInfo())); realmFactory.Write(realm => realm.Add(TestResources.CreateTestBeatmapSetInfo()));
var registration = realmFactory.Register(realm => realm.All<BeatmapSetInfo>(), onChanged); var registration = realmFactory.RegisterForNotifications(realm => realm.All<BeatmapSetInfo>(), onChanged);
testEventsArriving(true); testEventsArriving(true);
@ -104,7 +104,7 @@ namespace osu.Game.Tests.Database
realmFactory.Write(realm => realm.Add(TestResources.CreateTestBeatmapSetInfo())); realmFactory.Write(realm => realm.Add(TestResources.CreateTestBeatmapSetInfo()));
var subscription = realmFactory.Register(realm => var subscription = realmFactory.RegisterCustomSubscription(realm =>
{ {
beatmapSetInfo = realm.All<BeatmapSetInfo>().First(); beatmapSetInfo = realm.All<BeatmapSetInfo>().First();

View File

@ -63,6 +63,10 @@ namespace osu.Game.Database
private readonly ThreadLocal<bool> currentThreadCanCreateContexts = new ThreadLocal<bool>(); private readonly ThreadLocal<bool> currentThreadCanCreateContexts = new ThreadLocal<bool>();
private readonly Dictionary<Func<Realm, IDisposable?>, IDisposable?> customSubscriptionActions = new Dictionary<Func<Realm, IDisposable?>, IDisposable?>();
private readonly Dictionary<Func<Realm, IDisposable?>, Action> realmSubscriptionsResetMap = new Dictionary<Func<Realm, IDisposable?>, Action>();
private static readonly GlobalStatistic<int> contexts_created = GlobalStatistics.Get<int>(@"Realm", @"Contexts (Created)"); private static readonly GlobalStatistic<int> contexts_created = GlobalStatistics.Get<int>(@"Realm", @"Contexts (Created)");
private readonly object contextLock = new object(); private readonly object contextLock = new object();
@ -233,20 +237,16 @@ namespace osu.Game.Database
} }
} }
private readonly Dictionary<Func<Realm, IDisposable?>, IDisposable?> customSubscriptionActions = new Dictionary<Func<Realm, IDisposable?>, IDisposable?>(); public IDisposable RegisterForNotifications<T>(Func<Realm, IQueryable<T>> query, NotificationCallbackDelegate<T> onChanged)
private readonly Dictionary<Func<Realm, IDisposable?>, Action> realmSubscriptionsResetMap = new Dictionary<Func<Realm, IDisposable?>, Action>();
public IDisposable Register<T>(Func<Realm, IQueryable<T>> query, NotificationCallbackDelegate<T> onChanged)
where T : RealmObjectBase where T : RealmObjectBase
{ {
if (!ThreadSafety.IsUpdateThread) if (!ThreadSafety.IsUpdateThread)
throw new InvalidOperationException(@$"{nameof(Register)} must be called from the update thread."); throw new InvalidOperationException(@$"{nameof(RegisterForNotifications)} must be called from the update thread.");
lock (contextLock) lock (contextLock)
{ {
realmSubscriptionsResetMap.Add(action, () => onChanged(new EmptyRealmSet<T>(), null, null)); realmSubscriptionsResetMap.Add(action, () => onChanged(new EmptyRealmSet<T>(), null, null));
return Register(action); return RegisterCustomSubscription(action);
} }
IDisposable? action(Realm realm) => query(realm).QueryAsyncWithNotifications(onChanged); IDisposable? action(Realm realm) => query(realm).QueryAsyncWithNotifications(onChanged);
@ -257,10 +257,10 @@ namespace osu.Game.Database
/// </summary> /// </summary>
/// <param name="action">The work to run. Return value should be an <see cref="IDisposable"/> from QueryAsyncWithNotifications, or an <see cref="InvokeOnDisposal"/> to clean up any bindings.</param> /// <param name="action">The work to run. Return value should be an <see cref="IDisposable"/> from QueryAsyncWithNotifications, or an <see cref="InvokeOnDisposal"/> to clean up any bindings.</param>
/// <returns>An <see cref="IDisposable"/> which should be disposed to unsubscribe any inner subscription.</returns> /// <returns>An <see cref="IDisposable"/> which should be disposed to unsubscribe any inner subscription.</returns>
public IDisposable Register(Func<Realm, IDisposable?> action) public IDisposable RegisterCustomSubscription(Func<Realm, IDisposable?> action)
{ {
if (!ThreadSafety.IsUpdateThread) if (!ThreadSafety.IsUpdateThread)
throw new InvalidOperationException(@$"{nameof(Register)} must be called from the update thread."); throw new InvalidOperationException(@$"{nameof(RegisterForNotifications)} must be called from the update thread.");
var syncContext = SynchronizationContext.Current; var syncContext = SynchronizationContext.Current;

View File

@ -272,7 +272,7 @@ namespace osu.Game.Database
where T : RealmObjectBase where T : RealmObjectBase
{ {
if (!RealmContextFactory.CurrentThreadSubscriptionsAllowed) if (!RealmContextFactory.CurrentThreadSubscriptionsAllowed)
throw new InvalidOperationException($"Make sure to call {nameof(RealmContextFactory)}.{nameof(RealmContextFactory.Register)}"); throw new InvalidOperationException($"Make sure to call {nameof(RealmContextFactory)}.{nameof(RealmContextFactory.RegisterForNotifications)}");
return collection.SubscribeForNotifications(callback); return collection.SubscribeForNotifications(callback);
} }

View File

@ -55,7 +55,7 @@ namespace osu.Game.Input.Bindings
protected override void LoadComplete() protected override void LoadComplete()
{ {
realmSubscription = realmFactory.Register(realm => queryRealmKeyBindings(), (sender, changes, error) => realmSubscription = realmFactory.RegisterForNotifications(realm => queryRealmKeyBindings(), (sender, changes, error) =>
{ {
// The first fire of this is a bit redundant as this is being called in base.LoadComplete, // The first fire of this is a bit redundant as this is being called in base.LoadComplete,
// but this is safest in case the subscription is restored after a context recycle. // but this is safest in case the subscription is restored after a context recycle.

View File

@ -42,7 +42,7 @@ namespace osu.Game.Online
// Used to interact with manager classes that don't support interface types. Will eventually be replaced. // Used to interact with manager classes that don't support interface types. Will eventually be replaced.
var beatmapSetInfo = new BeatmapSetInfo { OnlineID = TrackedItem.OnlineID }; var beatmapSetInfo = new BeatmapSetInfo { OnlineID = TrackedItem.OnlineID };
realmSubscription = realmContextFactory.Register(realm => realm.All<BeatmapSetInfo>().Where(s => s.OnlineID == TrackedItem.OnlineID && !s.DeletePending), (items, changes, ___) => realmSubscription = realmContextFactory.RegisterForNotifications(realm => realm.All<BeatmapSetInfo>().Where(s => s.OnlineID == TrackedItem.OnlineID && !s.DeletePending), (items, changes, ___) =>
{ {
if (items.Any()) if (items.Any())
Schedule(() => UpdateState(DownloadState.LocallyAvailable)); Schedule(() => UpdateState(DownloadState.LocallyAvailable));

View File

@ -78,7 +78,7 @@ namespace osu.Game.Online.Rooms
// handles changes to hash that didn't occur from the import process (ie. a user editing the beatmap in the editor, somehow). // handles changes to hash that didn't occur from the import process (ie. a user editing the beatmap in the editor, somehow).
realmSubscription?.Dispose(); realmSubscription?.Dispose();
realmSubscription = realmContextFactory.Register(realm => filteredBeatmaps(), (items, changes, ___) => realmSubscription = realmContextFactory.RegisterForNotifications(realm => filteredBeatmaps(), (items, changes, ___) =>
{ {
if (changes == null) if (changes == null)
return; return;

View File

@ -47,7 +47,7 @@ namespace osu.Game.Online
Downloader.DownloadBegan += downloadBegan; Downloader.DownloadBegan += downloadBegan;
Downloader.DownloadFailed += downloadFailed; Downloader.DownloadFailed += downloadFailed;
realmSubscription = realmContextFactory.Register(realm => realm.All<ScoreInfo>().Where(s => ((s.OnlineID > 0 && s.OnlineID == TrackedItem.OnlineID) || s.Hash == TrackedItem.Hash) && !s.DeletePending), (items, changes, ___) => realmSubscription = realmContextFactory.RegisterForNotifications(realm => realm.All<ScoreInfo>().Where(s => ((s.OnlineID > 0 && s.OnlineID == TrackedItem.OnlineID) || s.Hash == TrackedItem.Hash) && !s.DeletePending), (items, changes, ___) =>
{ {
if (items.Any()) if (items.Any())
Schedule(() => UpdateState(DownloadState.LocallyAvailable)); Schedule(() => UpdateState(DownloadState.LocallyAvailable));

View File

@ -94,7 +94,7 @@ namespace osu.Game.Overlays
foreach (var s in queryRealmBeatmapSets()) foreach (var s in queryRealmBeatmapSets())
beatmapSets.Add(s.Detach()); beatmapSets.Add(s.Detach());
beatmapSubscription = realmFactory.Register(realm => queryRealmBeatmapSets(), beatmapsChanged); beatmapSubscription = realmFactory.RegisterForNotifications(realm => queryRealmBeatmapSets(), beatmapsChanged);
} }
private void beatmapsChanged(IRealmCollection<BeatmapSetInfo> sender, ChangeSet changes, Exception error) private void beatmapsChanged(IRealmCollection<BeatmapSetInfo> sender, ChangeSet changes, Exception error)

View File

@ -83,7 +83,7 @@ namespace osu.Game.Overlays.Settings.Sections
skinDropdown.Current = dropdownBindable; skinDropdown.Current = dropdownBindable;
realmSubscription = realmFactory.Register(realm => queryRealmSkins(), (sender, changes, error) => realmSubscription = realmFactory.RegisterForNotifications(realm => queryRealmSkins(), (sender, changes, error) =>
{ {
// The first fire of this is a bit redundant due to the call below, // The first fire of this is a bit redundant due to the call below,
// but this is safest in case the subscription is restored after a context recycle. // but this is safest in case the subscription is restored after a context recycle.

View File

@ -190,13 +190,13 @@ namespace osu.Game.Screens.Select
{ {
base.LoadComplete(); base.LoadComplete();
subscriptionSets = realmFactory.Register(getBeatmapSets, beatmapSetsChanged); subscriptionSets = realmFactory.RegisterForNotifications(getBeatmapSets, beatmapSetsChanged);
subscriptionBeatmaps = realmFactory.Register(realm => realm.All<BeatmapInfo>().Where(b => !b.Hidden), beatmapsChanged); subscriptionBeatmaps = realmFactory.RegisterForNotifications(realm => realm.All<BeatmapInfo>().Where(b => !b.Hidden), beatmapsChanged);
// Can't use main subscriptions because we can't lookup deleted indices. // Can't use main subscriptions because we can't lookup deleted indices.
// https://github.com/realm/realm-dotnet/discussions/2634#discussioncomment-1605595. // https://github.com/realm/realm-dotnet/discussions/2634#discussioncomment-1605595.
subscriptionDeletedSets = realmFactory.Register(realm => realm.All<BeatmapSetInfo>().Where(s => s.DeletePending && !s.Protected), deletedBeatmapSetsChanged); subscriptionDeletedSets = realmFactory.RegisterForNotifications(realm => realm.All<BeatmapSetInfo>().Where(s => s.DeletePending && !s.Protected), deletedBeatmapSetsChanged);
subscriptionHiddenBeatmaps = realmFactory.Register(realm => realm.All<BeatmapInfo>().Where(b => b.Hidden), beatmapsChanged); subscriptionHiddenBeatmaps = realmFactory.RegisterForNotifications(realm => realm.All<BeatmapInfo>().Where(b => b.Hidden), beatmapsChanged);
} }
private void deletedBeatmapSetsChanged(IRealmCollection<BeatmapSetInfo> sender, ChangeSet changes, Exception error) private void deletedBeatmapSetsChanged(IRealmCollection<BeatmapSetInfo> sender, ChangeSet changes, Exception error)

View File

@ -48,7 +48,7 @@ namespace osu.Game.Screens.Select.Carousel
ruleset.BindValueChanged(_ => ruleset.BindValueChanged(_ =>
{ {
scoreSubscription?.Dispose(); scoreSubscription?.Dispose();
scoreSubscription = realmFactory.Register(realm => scoreSubscription = realmFactory.RegisterForNotifications(realm =>
realm.All<ScoreInfo>() realm.All<ScoreInfo>()
.Filter($"{nameof(ScoreInfo.User)}.{nameof(RealmUser.OnlineID)} == $0" .Filter($"{nameof(ScoreInfo.User)}.{nameof(RealmUser.OnlineID)} == $0"
+ $" && {nameof(ScoreInfo.BeatmapInfo)}.{nameof(BeatmapInfo.ID)} == $1" + $" && {nameof(ScoreInfo.BeatmapInfo)}.{nameof(BeatmapInfo.ID)} == $1"

View File

@ -113,7 +113,7 @@ namespace osu.Game.Screens.Select.Leaderboards
if (beatmapInfo == null) if (beatmapInfo == null)
return; return;
scoreSubscription = realmFactory.Register(realm => scoreSubscription = realmFactory.RegisterForNotifications(realm =>
realm.All<ScoreInfo>() realm.All<ScoreInfo>()
.Filter($"{nameof(ScoreInfo.BeatmapInfo)}.{nameof(BeatmapInfo.ID)} = $0", beatmapInfo.ID), .Filter($"{nameof(ScoreInfo.BeatmapInfo)}.{nameof(BeatmapInfo.ID)} = $0", beatmapInfo.ID),
(_, changes, ___) => (_, changes, ___) =>

View File

@ -80,7 +80,7 @@ namespace osu.Game.Screens.Spectate
playingUserStates.BindTo(spectatorClient.PlayingUserStates); playingUserStates.BindTo(spectatorClient.PlayingUserStates);
playingUserStates.BindCollectionChanged(onPlayingUserStatesChanged, true); playingUserStates.BindCollectionChanged(onPlayingUserStatesChanged, true);
realmSubscription = realmContextFactory.Register( realmSubscription = realmContextFactory.RegisterForNotifications(
realm => realm.All<BeatmapSetInfo>().Where(s => !s.DeletePending), beatmapsChanged); realm => realm.All<BeatmapSetInfo>().Where(s => !s.DeletePending), beatmapsChanged);
foreach ((int id, var _) in userMap) foreach ((int id, var _) in userMap)