Skip to content

Commit 29c9f73

Browse files
committed
changes callbacks to return ValueTask and adds Async suffix
initializes unused callbacks to null instead of noop since we're null-checking anyway
1 parent a0efff2 commit 29c9f73

File tree

4 files changed

+27
-27
lines changed

4 files changed

+27
-27
lines changed

src/ImageSharp.Web/Middleware/ImageContext.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ public void ComprehendRequestHeaders(DateTimeOffset lastModified, long length)
125125
/// <param name="statusCode">The status code.</param>
126126
/// <param name="metaData">The image metadata.</param>
127127
/// <returns>The <see cref="Task"/>.</returns>
128-
public async Task SendStatusAsync(int statusCode, ImageCacheMetadata metaData)
128+
public Task SendStatusAsync(int statusCode, ImageCacheMetadata metaData)
129129
{
130-
await this.ApplyResponseHeadersAsync(statusCode, metaData.ContentType, this.ComputeMaxAge(metaData));
130+
return this.ApplyResponseHeadersAsync(statusCode, metaData.ContentType, this.ComputeMaxAge(metaData));
131131

132132
// this.logger.LogHandled(statusCode, SubPath);
133133
}
@@ -192,9 +192,9 @@ private async Task ApplyResponseHeadersAsync(int statusCode, string contentType,
192192
MustRevalidate = true
193193
};
194194

195-
if (this.options.OnPrepareResponse != null)
195+
if (this.options.OnPrepareResponseAsync != null)
196196
{
197-
await this.options.OnPrepareResponse.Invoke(this.context);
197+
await this.options.OnPrepareResponseAsync.Invoke(this.context);
198198
}
199199
}
200200

src/ImageSharp.Web/Middleware/ImageSharpMiddleware.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ public async Task Invoke(HttpContext context)
167167
}
168168
}
169169

170-
if (this.options.OnParseCommands != null)
170+
if (this.options.OnParseCommandsAsync != null)
171171
{
172-
await this.options.OnParseCommands.Invoke(new ImageCommandContext(context, commands, CommandParser.Instance));
172+
await this.options.OnParseCommandsAsync.Invoke(new ImageCommandContext(context, commands, CommandParser.Instance));
173173
}
174174

175175
// Get the correct service for the request.
@@ -276,9 +276,9 @@ private async Task ProcessRequestAsync(HttpContext context, bool processRequest,
276276
using (var image = FormattedImage.Load(this.options.Configuration, inStream))
277277
{
278278
image.Process(this.logger, this.processors, commands);
279-
if (this.options.OnBeforeSave != null)
279+
if (this.options.OnBeforeSaveAsync != null)
280280
{
281-
await this.options.OnBeforeSave.Invoke(image);
281+
await this.options.OnBeforeSaveAsync.Invoke(image);
282282
}
283283

284284
image.Save(outStream);
@@ -305,9 +305,9 @@ private async Task ProcessRequestAsync(HttpContext context, bool processRequest,
305305
outStream.Position = 0;
306306
string contentType = cachedImageMetadata.ContentType;
307307
string extension = this.formatUtilities.GetExtensionFromContentType(contentType);
308-
if (this.options.OnProcessed != null)
308+
if (this.options.OnProcessedAsync != null)
309309
{
310-
await this.options.OnProcessed.Invoke(new ImageProcessingContext(context, outStream, commands, contentType, extension));
310+
await this.options.OnProcessedAsync.Invoke(new ImageProcessingContext(context, outStream, commands, contentType, extension));
311311
}
312312

313313
outStream.Position = 0;

src/ImageSharp.Web/Middleware/ImageSharpMiddlewareOptions.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class ImageSharpMiddlewareOptions
3939
/// Gets or sets the additional command parsing method that can be used to used to augment commands.
4040
/// This is called once the commands have been gathered and before an <see cref="IImageProvider"/> has been assigned.
4141
/// </summary>
42-
public Func<ImageCommandContext, Task> OnParseCommands { get; set; } = c =>
42+
public Func<ImageCommandContext, ValueTask> OnParseCommandsAsync { get; set; } = c =>
4343
{
4444
if (c.Commands.Count != 0)
4545
{
@@ -55,28 +55,28 @@ public class ImageSharpMiddlewareOptions
5555
}
5656
}
5757

58-
return Task.CompletedTask;
58+
return default;
5959
};
6060

6161
/// <summary>
6262
/// Gets or sets the additional method that can be used for final manipulation before the image is saved.
6363
/// This is called after image has been processed, but before the image has been saved to the output stream for caching.
6464
/// This can be used to alter the metadata of the resultant image.
6565
/// </summary>
66-
public Func<FormattedImage, Task> OnBeforeSave { get; set; } = _ => Task.CompletedTask;
66+
public Func<FormattedImage, ValueTask> OnBeforeSaveAsync { get; set; }
6767

6868
/// <summary>
6969
/// Gets or sets the additional processing method.
7070
/// This is called after image has been processed, but before the result has been cached.
7171
/// This can be used to further optimize the resultant image.
7272
/// </summary>
73-
public Func<ImageProcessingContext, Task> OnProcessed { get; set; } = _ => Task.CompletedTask;
73+
public Func<ImageProcessingContext, ValueTask> OnProcessedAsync { get; set; }
7474

7575
/// <summary>
7676
/// Gets or sets the additional response method.
7777
/// This is called after the status code and headers have been set, but before the body has been written.
7878
/// This can be used to add or change the response headers.
7979
/// </summary>
80-
public Func<HttpContext, Task> OnPrepareResponse { get; set; } = _ => Task.CompletedTask;
80+
public Func<HttpContext, ValueTask> OnPrepareResponseAsync { get; set; }
8181
}
8282
}

tests/ImageSharp.Web.Tests/ImageSharpTestServer.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ public static class ImageSharpTestServer
4343
options.MaxBrowserCacheDays = -1;
4444
options.MaxCacheDays = -1;
4545
options.CachedNameLength = 12;
46-
options.OnParseCommands = _ => Task.CompletedTask;
47-
options.OnBeforeSave = _ => Task.CompletedTask;
48-
options.OnProcessed = _ => Task.CompletedTask;
49-
options.OnPrepareResponse = _ => Task.CompletedTask;
46+
options.OnParseCommandsAsync = null;
47+
options.OnBeforeSaveAsync = null;
48+
options.OnProcessedAsync = null;
49+
options.OnPrepareResponseAsync = null;
5050
})
5151
.SetRequestParser<QueryCollectionRequestParser>()
5252
.Configure<PhysicalFileSystemCacheOptions>(_ => { })
@@ -74,10 +74,10 @@ public static class ImageSharpTestServer
7474
options.MaxBrowserCacheDays = -1;
7575
options.MaxCacheDays = -1;
7676
options.CachedNameLength = 12;
77-
options.OnParseCommands = _ => Task.CompletedTask;
78-
options.OnBeforeSave = _ => Task.CompletedTask;
79-
options.OnProcessed = _ => Task.CompletedTask;
80-
options.OnPrepareResponse = _ => Task.CompletedTask;
77+
options.OnParseCommandsAsync = null;
78+
options.OnBeforeSaveAsync = null;
79+
options.OnProcessedAsync = null;
80+
options.OnPrepareResponseAsync = null;
8181
})
8282
.SetRequestParser<QueryCollectionRequestParser>()
8383
.Configure<PhysicalFileSystemCacheOptions>(_ => { })
@@ -112,10 +112,10 @@ void ConfigureServices(IServiceCollection services)
112112
options.Configuration = Configuration.Default;
113113
options.MaxBrowserCacheDays = -1;
114114
options.MaxCacheDays = -1;
115-
options.OnParseCommands = onParseCommands;
116-
options.OnBeforeSave = onBeforeSave;
117-
options.OnProcessed = onProcessed;
118-
options.OnPrepareResponse = onPrepareResponse;
115+
options.OnParseCommandsAsync = null;
116+
options.OnBeforeSaveAsync = null;
117+
options.OnProcessedAsync = null;
118+
options.OnPrepareResponseAsync = null;
119119
})
120120
.SetRequestParser<QueryCollectionRequestParser>()
121121
.Configure<PhysicalFileSystemCacheOptions>(_ => { })

0 commit comments

Comments
 (0)