Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debounce config saves #1388

Merged
merged 1 commit into from
Feb 13, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 24 additions & 6 deletions osu.Framework/Configuration/ConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using osu.Framework.Configuration.Tracking;

namespace osu.Framework.Configuration
Expand Down Expand Up @@ -119,7 +121,7 @@ public Bindable<U> Set<U>(T lookup, U value)
protected virtual void AddBindable<TBindable>(T lookup, Bindable<TBindable> bindable)
{
ConfigStore[lookup] = bindable;
bindable.ValueChanged += _ => Save();
bindable.ValueChanged += _ => backgroundSave();
}

private Bindable<U> set<U>(T lookup, U value)
Expand Down Expand Up @@ -154,15 +156,14 @@ protected Bindable<U> GetOriginalBindable<U>(T lookup)

#region IDisposable Support

private bool disposedValue; // To detect redundant calls
private bool isDisposed;

protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
if (!isDisposed)
{
Save();

disposedValue = true;
isDisposed = true;
}
}

Expand Down Expand Up @@ -204,10 +205,27 @@ public void Load()
hasLoaded = true;
}

private int lastBackgroundSave;

/// <summary>
/// Perform a save with debounce.
/// </summary>
private void backgroundSave()
{
var current = Interlocked.Increment(ref lastBackgroundSave);
Task.Delay(100).ContinueWith(task =>
{
if (current == lastBackgroundSave) Save();
});
}

private readonly object saveLock = new object();

public bool Save()
{
if (!hasLoaded) return false;
return PerformSave();
lock (saveLock)
return PerformSave();
}

protected abstract void PerformLoad();
Expand Down