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

Asyncify initial load

This commit is contained in:
smoogipoo 2020-09-08 17:59:38 +09:00
parent 06328e0000
commit 070704cba7

View File

@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Threading;
@ -15,6 +16,7 @@ using osu.Framework.Logging;
using osu.Framework.Platform;
using osu.Game.Beatmaps;
using osu.Game.IO.Legacy;
using osu.Game.Overlays.Notifications;
namespace osu.Game.Collections
{
@ -46,17 +48,46 @@ namespace osu.Game.Collections
[BackgroundDependencyLoader]
private void load()
{
Collections.CollectionChanged += collectionsChanged;
loadDatabase();
}
private void collectionsChanged(object sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
foreach (var c in e.NewItems.Cast<BeatmapCollection>())
c.Changed += backgroundSave;
break;
case NotifyCollectionChangedAction.Remove:
foreach (var c in e.OldItems.Cast<BeatmapCollection>())
c.Changed -= backgroundSave;
break;
case NotifyCollectionChangedAction.Replace:
foreach (var c in e.OldItems.Cast<BeatmapCollection>())
c.Changed -= backgroundSave;
foreach (var c in e.NewItems.Cast<BeatmapCollection>())
c.Changed += backgroundSave;
break;
}
backgroundSave();
}
private void loadDatabase() => Task.Run(async () =>
{
if (storage.Exists(database_name))
{
using (var stream = storage.GetStream(database_name))
importCollections(readCollections(stream));
await import(stream);
}
});
foreach (var c in Collections)
c.Changed += backgroundSave;
Collections.CollectionChanged += (_, __) => backgroundSave();
}
/// <summary>
/// Set an endpoint for notifications to be posted to.
/// </summary>