Skip to content

Code Quality: Only execute actions once #13397

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/Files.App/Actions/DebounceAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Files.App.Actions
{
public abstract class DebouncedAction : IAction
{
private DateTime lastExecutedTime = DateTime.MinValue;

private readonly TimeSpan debounceTimeSpan;
public abstract string Label { get; }
public abstract string Description { get; }
public abstract RichGlyph Glyph { get; }
public abstract HotKey HotKey { get; }
public abstract HotKey SecondHotKey { get; }
public abstract HotKey ThirdHotKey { get; }
public abstract HotKey MediaHotKey { get; }
public abstract bool IsExecutable { get; }

protected DebouncedAction(TimeSpan debounceDuration)
{
debounceTimeSpan = debounceDuration;
}

public virtual bool CanExecuteNow()
{
return DateTime.Now - lastExecutedTime > debounceTimeSpan;
}

public virtual void MarkLastExecutionTime()
{
lastExecutedTime = DateTime.Now;
}

public abstract Task ExecuteAsync();
}
}
43 changes: 43 additions & 0 deletions src/Files.App/Actions/DebouncedActionDecorator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Files.App.Actions
{
public class DebouncedActionDecorator : DebouncedAction
{
public override string Label => _innerAction.Label;
public override string Description => _innerAction.Description;
public override RichGlyph Glyph => _innerAction.Glyph;
public override HotKey HotKey => _innerAction.HotKey;
public override HotKey SecondHotKey => _innerAction.SecondHotKey;
public override HotKey ThirdHotKey => _innerAction.ThirdHotKey;
public override HotKey MediaHotKey => _innerAction.MediaHotKey;
public override bool IsExecutable => _innerAction.IsExecutable;

private readonly IAction _innerAction;

/// <summary>
/// Initializes a new instance of the DebouncedActionDecorator class.
/// </summary>
/// <param name="innerAction">The IAction instance to be wrapped.</param>
/// <param name="debounceDuration">The debounce duration. Default is 800 milliseconds.</param>
public DebouncedActionDecorator(IAction innerAction, TimeSpan? debounceDuration = null)
: base(debounceDuration ?? TimeSpan.FromMilliseconds(800))
{
_innerAction = innerAction;
}

public override async Task ExecuteAsync()
{
if (!CanExecuteNow())
return;

MarkLastExecutionTime();

await _innerAction.ExecuteAsync();
}
}
}
Loading