Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/Tests/PipelineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public async Task CanBuildDecoratingPipeline()
called = true;
return inner.HandleAsync(message, cancellation);
})
.Use(handler => WhatsAppHandler.Skip)
.Use(handler => WhatsAppHandler.Continue)
.Build();

await pipeline.HandleAsync(new ReactionMessage("1234", service, user, 0, "🗽"));
Expand Down
2 changes: 1 addition & 1 deletion src/WhatsApp/ConsoleHandlerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static WhatsAppHandlerBuilder UseConsole(this WhatsAppHandlerBuilder buil
{
// In production environments, we have ZERO impact since we're not even added to the pipeline.
if (services.GetRequiredService<IHostEnvironment>().IsProduction())
return WhatsAppHandler.Skip;
return WhatsAppHandler.Continue;

return new ConsoleHandler(inner);
});
Expand Down
22 changes: 12 additions & 10 deletions src/WhatsApp/WhatsAppHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,28 @@
public static class WhatsAppHandler
{
/// <summary>
/// An empty implementation of <see cref="IWhatsAppHandler"/> that does nothing and
/// can be used to shortcircuit the processing of WhatsApp messages.
/// An empty implementation of <see cref="IWhatsAppHandler"/> that is skipped
/// when building the processing pipeline so that normal processing continues
/// as if this handler was never added at all. It's useful to implement conditional
/// <c>Use(..)</c> logic that is dependent on runtime conditions, such as the
/// hosting environment or configuration settings, with zero impact on runtime
/// when the condition is not met.
/// </summary>
public static IWhatsAppHandler Empty { get; } = new EmptyWhatsAppHandler();
public static IWhatsAppHandler Continue { get; } = new ContinueWhatsAppHandler();

/// <summary>
/// An empty implementation of <see cref="IWhatsAppHandler"/> that is skipped
/// when building the processing pipeline. It's useful to implement conditional
/// <c>Use(..)</c> logic that is dependent on runtime conditions, such as the
/// hosting environment or configuration settings.
/// An empty implementation of <see cref="IWhatsAppHandler"/> that stops further
/// execution of the pipeline and generates no responses.
/// </summary>
public static IWhatsAppHandler Skip { get; } = new SKipWhatsAppHandler();
public static IWhatsAppHandler Stop { get; } = new StopWhatsAppHandler();

class EmptyWhatsAppHandler : IWhatsAppHandler
class StopWhatsAppHandler : IWhatsAppHandler
{
public IAsyncEnumerable<Response> HandleAsync(IEnumerable<IMessage> messages, CancellationToken cancellation = default)
=> AsyncEnumerable.Empty<Response>();
}

class SKipWhatsAppHandler : IWhatsAppHandler
class ContinueWhatsAppHandler : IWhatsAppHandler
{
public IAsyncEnumerable<Response> HandleAsync(IEnumerable<IMessage> messages, CancellationToken cancellation = default)
=> throw new NotSupportedException("This handler should never be invoked by the pipeline.");
Expand Down
4 changes: 2 additions & 2 deletions src/WhatsApp/WhatsAppHandlerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class WhatsAppHandlerBuilder
readonly Func<IServiceProvider, IWhatsAppHandler> handlerFactory;
List<Func<IWhatsAppHandler, IServiceProvider, IWhatsAppHandler>>? factories;

public WhatsAppHandlerBuilder() : this(_ => WhatsAppHandler.Empty)
public WhatsAppHandlerBuilder() : this(_ => WhatsAppHandler.Stop)
{
}

Expand Down Expand Up @@ -56,7 +56,7 @@ public IWhatsAppHandler Build(IServiceProvider? services = default)
}

// Only keep non-skipping handlers.
if (current != WhatsAppHandler.Skip)
if (current != WhatsAppHandler.Continue)
handler = factories[i](handler!, services);
}
}
Expand Down