Skip to content

Commit

Permalink
Reuse Task instances where possible (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomachristian authored and dennisdoomen committed Jan 23, 2018
1 parent 2bef488 commit 107050e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
8 changes: 5 additions & 3 deletions Src/LiquidProjections/Dispatcher.cs
Expand Up @@ -11,6 +11,8 @@ namespace LiquidProjections
/// </summary>
public class Dispatcher
{
private static readonly Task<ExceptionResolution> AbortExceptionResolutionTask = Task.FromResult(ExceptionResolution.Abort);

private readonly CreateSubscription createSubscription;

public Dispatcher(CreateSubscription createSubscription)
Expand Down Expand Up @@ -43,9 +45,9 @@ public Dispatcher(CreateSubscription createSubscription)
/// Configures the behavior of the dispatcher in case an exception is thrown by a subscriber. The default
/// behavior is to dispose of the subscription.
/// </summary>
public HandleException ExceptionHandler { get; set; } = (e, attempts, info) => Task.FromResult(ExceptionResolution.Abort);
public HandleException ExceptionHandler { get; set; } = (e, attempts, info) => AbortExceptionResolutionTask;

public HandleSuccess SuccessHandler { get; set; } = _ => Task.FromResult(false);
public HandleSuccess SuccessHandler { get; set; } = _ => SpecializedTasks.FalseTask;

private async Task HandleTransactions(IReadOnlyList<Transaction> transactions, Func<IReadOnlyList<Transaction>, SubscriptionInfo, Task> handler, SubscriptionInfo info)
{
Expand Down Expand Up @@ -176,6 +178,6 @@ public class SubscriptionOptions
/// If restarting is enabled through <see cref="RestartWhenAhead"/>, this property can be used to run some
/// clean-up code before the dispatcher will restart at the first transaction.
/// </summary>
public Func<Task> BeforeRestarting { get; set; } = () => Task.FromResult(0);
public Func<Task> BeforeRestarting { get; set; } = () => SpecializedTasks.ZeroTask;
}
}
24 changes: 12 additions & 12 deletions Src/LiquidProjections/EventMapBuilder.cs
Expand Up @@ -562,7 +562,7 @@ public static class EventMapBuilderExtensions
eventMappingBuilder.As((anEvent, context) =>
{
action(anEvent, context);
return Task.FromResult(0);
return SpecializedTasks.ZeroTask;
});
}

Expand Down Expand Up @@ -592,7 +592,7 @@ public static class EventMapBuilderExtensions
eventMappingBuilder.As((anEvent, context) =>
{
action(anEvent);
return Task.FromResult(0);
return SpecializedTasks.ZeroTask;
});
}

Expand Down Expand Up @@ -837,7 +837,7 @@ public static class EventMapBuilderExtensions
eventActionBuilder.Using((projection, anEvent, context) =>
{
projector(projection, anEvent, context);
return Task.FromResult(false);
return SpecializedTasks.FalseTask;
});
}

Expand Down Expand Up @@ -867,7 +867,7 @@ public static class EventMapBuilderExtensions
eventActionBuilder.Using((projection, anEvent, context) =>
{
projector(projection, anEvent);
return Task.FromResult(false);
return SpecializedTasks.FalseTask;
});
}

Expand Down Expand Up @@ -926,7 +926,7 @@ public static class EventMapBuilderExtensions
eventActionBuilder.Using((projection, anEvent, context) =>
{
projector(projection, anEvent, context);
return Task.FromResult(false);
return SpecializedTasks.FalseTask;
});
}

Expand Down Expand Up @@ -959,7 +959,7 @@ public static class EventMapBuilderExtensions
eventActionBuilder.Using((projection, anEvent, context) =>
{
projector(projection, anEvent);
return Task.FromResult(false);
return SpecializedTasks.FalseTask;
});
}

Expand Down Expand Up @@ -1020,7 +1020,7 @@ public static class EventMapBuilderExtensions
eventActionBuilder.Using((projection, anEvent, context) =>
{
projector(projection, anEvent, context);
return Task.FromResult(false);
return SpecializedTasks.FalseTask;
});
}

Expand Down Expand Up @@ -1052,7 +1052,7 @@ public static class EventMapBuilderExtensions
eventActionBuilder.Using((projection, anEvent, context) =>
{
projector(projection, anEvent);
return Task.FromResult(false);
return SpecializedTasks.FalseTask;
});
}

Expand Down Expand Up @@ -1113,7 +1113,7 @@ public static class EventMapBuilderExtensions
eventActionBuilder.Using((projection, anEvent, context) =>
{
projector(projection, anEvent, context);
return Task.FromResult(false);
return SpecializedTasks.FalseTask;
});
}

Expand Down Expand Up @@ -1146,7 +1146,7 @@ public static class EventMapBuilderExtensions
eventActionBuilder.Using((projection, anEvent, context) =>
{
projector(projection, anEvent);
return Task.FromResult(false);
return SpecializedTasks.FalseTask;
});
}

Expand Down Expand Up @@ -1207,7 +1207,7 @@ public static class EventMapBuilderExtensions
eventActionBuilder.Using((projection, anEvent, context) =>
{
projector(projection, anEvent, context);
return Task.FromResult(false);
return SpecializedTasks.FalseTask;
});
}

Expand Down Expand Up @@ -1239,7 +1239,7 @@ public static class EventMapBuilderExtensions
eventActionBuilder.Using((projection, anEvent, context) =>
{
projector(projection, anEvent);
return Task.FromResult(false);
return SpecializedTasks.FalseTask;
});
}

Expand Down
2 changes: 1 addition & 1 deletion Src/LiquidProjections/Projector.cs
Expand Up @@ -9,7 +9,7 @@ public class Projector
{
private readonly IEventMap<ProjectionContext> map;
private readonly IReadOnlyList<Projector> children;
private ShouldRetry shouldRetry = (exception, count) => Task.FromResult(false);
private ShouldRetry shouldRetry = (exception, count) => SpecializedTasks.FalseTask;

public Projector(IEventMapBuilder<ProjectionContext> eventMapBuilder, IEnumerable<Projector> children = null)
: this(BuildMap(eventMapBuilder), children)
Expand Down
10 changes: 10 additions & 0 deletions Src/LiquidProjections/SpecializedTasks.cs
@@ -0,0 +1,10 @@
using System.Threading.Tasks;

namespace LiquidProjections
{
internal static class SpecializedTasks
{
internal static readonly Task<bool> FalseTask = Task.FromResult(false);
internal static readonly Task<int> ZeroTask = Task.FromResult(0);
}
}

0 comments on commit 107050e

Please sign in to comment.