Throttle imagelist update events

This commit is contained in:
Ben Olden-Cooligan 2020-01-29 11:24:02 -05:00
parent 8d1e1d9e5f
commit 3d90047898
3 changed files with 69 additions and 8 deletions

View File

@ -1,28 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NAPS2.Images.Storage;
using NAPS2.Util;
namespace NAPS2.Images
{
public class ScannedImageList
{
private readonly ImageContext imageContext;
private readonly TimedThrottle runUpdateEventsThrottle;
private Memento savedState = Memento.Empty;
private ListSelection<ScannedImage> selection;
public ScannedImageList(ImageContext imageContext)
: this(imageContext, new List<ScannedImage>())
{
this.imageContext = imageContext;
Images = new List<ScannedImage>();
Selection = ListSelection.Empty<ScannedImage>();
}
public ScannedImageList(ImageContext imageContext, List<ScannedImage> images)
{
this.imageContext = imageContext;
runUpdateEventsThrottle = new TimedThrottle(RunUpdateEvents, TimeSpan.FromMilliseconds(100));
Images = images;
Selection = ListSelection.Empty<ScannedImage>();
}
public ThumbnailRenderer ThumbnailRenderer { get; set; }
@ -54,15 +56,13 @@ namespace NAPS2.Images
public void Mutate(ListMutation<ScannedImage> mutation, ListSelection<ScannedImage> selectionToMutate = null)
{
MutateInternal(mutation, selectionToMutate);
UpdateImageMetadata();
ImagesUpdated?.Invoke(this, EventArgs.Empty);
runUpdateEventsThrottle.RunAction(SynchronizationContext.Current);
}
public async Task MutateAsync(ListMutation<ScannedImage> mutation, ListSelection<ScannedImage> selectionToMutate = null)
{
await Task.Run(() => MutateInternal(mutation, selectionToMutate));
UpdateImageMetadata();
ImagesUpdated?.Invoke(this, EventArgs.Empty);
runUpdateEventsThrottle.RunAction(SynchronizationContext.Current);
}
private void MutateInternal(ListMutation<ScannedImage> mutation, ListSelection<ScannedImage> selectionToMutate)
@ -77,6 +77,12 @@ namespace NAPS2.Images
}
}
private void RunUpdateEvents()
{
UpdateImageMetadata();
ImagesUpdated?.Invoke(this, EventArgs.Empty);
}
private void UpdateImageMetadata()
{
int i = 0;

View File

@ -309,6 +309,7 @@
<Compile Include="Platform\MonoRuntimeCompat.cs" />
<Compile Include="Util\ExpFallback.cs" />
<Compile Include="Util\NativeLibrary.cs" />
<Compile Include="Util\TimedThrottle.cs" />
<Compile Include="WinForms\ImageClipboard.cs" />
<Compile Include="Remoting\RemotingHelper.cs" />
<Compile Include="Util\IInvoker.cs" />

View File

@ -0,0 +1,54 @@
using System;
using System.Threading;
namespace NAPS2.Util
{
public class TimedThrottle
{
private readonly Action action;
private readonly TimeSpan interval;
private Timer timer;
private DateTime lastRun = DateTime.MinValue;
public TimedThrottle(Action action, TimeSpan interval)
{
this.action = action;
this.interval = interval;
}
public void RunAction(SynchronizationContext syncContext)
{
bool doRunAction = false;
lock (this)
{
if (timer == null && lastRun < DateTime.Now - interval)
{
doRunAction = true;
lastRun = DateTime.Now;
}
else if (timer == null)
{
timer = new Timer(Tick, syncContext, interval, TimeSpan.FromMilliseconds(-1));
}
}
if (doRunAction)
{
action();
}
}
private void Tick(object state)
{
var syncContext = (SynchronizationContext) state;
lock (this)
{
timer?.Dispose();
timer = null;
lastRun = DateTime.Now;
}
syncContext.Post(_ => action(), null);
}
}
}