Skip to content

Commit

Permalink
Fixes async / await usage (#6921)
Browse files Browse the repository at this point in the history
  • Loading branch information
jtkech authored Aug 23, 2020
1 parent 75a793a commit 3ba0002
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public async Task<dynamic> UpdateTypeEditorAsync(ContentTypeDefinition contentTy

var layout = await _layoutAccessor.GetLayoutAsync();

_contentDefinitionManager.AlterTypeDefinition(contentTypeDefinition.Name, typeBuilder =>
await _contentDefinitionManager.AlterTypeDefinitionAsync(contentTypeDefinition.Name, async typeBuilder =>
{
var typeContext = new UpdateTypeEditorContext(
typeBuilder,
Expand All @@ -94,9 +94,9 @@ public async Task<dynamic> UpdateTypeEditorAsync(ContentTypeDefinition contentTy
updater
);
BindPlacementAsync(typeContext).GetAwaiter().GetResult();
await BindPlacementAsync(typeContext);
_handlers.InvokeAsync((handler, contentTypeDefinition, typeContext) => handler.UpdateTypeEditorAsync(contentTypeDefinition, typeContext), contentTypeDefinition, typeContext, _logger).GetAwaiter().GetResult();
await _handlers.InvokeAsync((handler, contentTypeDefinition, typeContext) => handler.UpdateTypeEditorAsync(contentTypeDefinition, typeContext), contentTypeDefinition, typeContext, _logger);
});

return contentTypeDefinitionShape;
Expand Down Expand Up @@ -140,7 +140,7 @@ public async Task<dynamic> UpdatePartEditorAsync(ContentPartDefinition contentPa
UpdatePartEditorContext partContext = null;
var layout = await _layoutAccessor.GetLayoutAsync();

_contentDefinitionManager.AlterPartDefinition(contentPartDefinition.Name, partBuilder =>
await _contentDefinitionManager.AlterPartDefinitionAsync(contentPartDefinition.Name, async partBuilder =>
{
partContext = new UpdatePartEditorContext(
partBuilder,
Expand All @@ -152,9 +152,9 @@ public async Task<dynamic> UpdatePartEditorAsync(ContentPartDefinition contentPa
updater
);
BindPlacementAsync(partContext).GetAwaiter().GetResult();
await BindPlacementAsync(partContext);
_handlers.InvokeAsync((handler, contentPartDefinition, partContext) => handler.UpdatePartEditorAsync(contentPartDefinition, partContext), contentPartDefinition, partContext, _logger).GetAwaiter().GetResult();
await _handlers.InvokeAsync((handler, contentPartDefinition, partContext) => handler.UpdatePartEditorAsync(contentPartDefinition, partContext), contentPartDefinition, partContext, _logger);
});

return contentPartDefinitionShape;
Expand Down Expand Up @@ -197,9 +197,9 @@ public async Task<dynamic> UpdateTypePartEditorAsync(ContentTypePartDefinition c
dynamic typePartDefinitionShape = await CreateContentShapeAsync("ContentTypePartDefinition_Edit");
var layout = await _layoutAccessor.GetLayoutAsync();

_contentDefinitionManager.AlterTypeDefinition(contentTypePartDefinition.ContentTypeDefinition.Name, typeBuilder =>
await _contentDefinitionManager.AlterTypeDefinitionAsync(contentTypePartDefinition.ContentTypeDefinition.Name, typeBuilder =>
{
typeBuilder.WithPart(contentTypePartDefinition.Name, async typePartBuilder =>
return typeBuilder.WithPartAsync(contentTypePartDefinition.Name, async typePartBuilder =>
{
typePartDefinitionShape.ContentPart = contentTypePartDefinition;
Expand Down Expand Up @@ -261,9 +261,9 @@ public async Task<dynamic> UpdatePartFieldEditorAsync(ContentPartFieldDefinition

var layout = await _layoutAccessor.GetLayoutAsync();

_contentDefinitionManager.AlterPartDefinition(contentPartDefinition.Name, partBuilder =>
await _contentDefinitionManager.AlterPartDefinitionAsync(contentPartDefinition.Name, partBuilder =>
{
partBuilder.WithField(contentPartFieldDefinition.Name, async partFieldBuilder =>
return partBuilder.WithFieldAsync(contentPartFieldDefinition.Name, async partFieldBuilder =>
{
partFieldDefinitionShape.ContentField = contentPartFieldDefinition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ public static void AlterTypeDefinition(this IContentDefinitionManager manager, s
alteration(builder);
manager.StoreTypeDefinition(builder.Build());
}

public static async Task AlterTypeDefinitionAsync(this IContentDefinitionManager manager, string name, Func<ContentTypeDefinitionBuilder, Task> alterationAsync)
{
var typeDefinition = manager.LoadTypeDefinition(name) ?? new ContentTypeDefinition(name, name.CamelFriendly());
var builder = new ContentTypeDefinitionBuilder(typeDefinition);
await alterationAsync(builder);
manager.StoreTypeDefinition(builder.Build());
}

public static void AlterPartDefinition(this IContentDefinitionManager manager, string name, Action<ContentPartDefinitionBuilder> alteration)
{
var partDefinition = manager.LoadPartDefinition(name) ?? new ContentPartDefinition(name);
Expand All @@ -60,6 +69,14 @@ public static void AlterPartDefinition(this IContentDefinitionManager manager, s
manager.StorePartDefinition(builder.Build());
}

public static async Task AlterPartDefinitionAsync(this IContentDefinitionManager manager, string name, Func<ContentPartDefinitionBuilder, Task> alterationAsync)
{
var partDefinition = manager.LoadPartDefinition(name) ?? new ContentPartDefinition(name);
var builder = new ContentPartDefinitionBuilder(partDefinition);
await alterationAsync(builder);
manager.StorePartDefinition(builder.Build());
}

/// <summary>
/// Migrate existing ContentPart settings to WithSettings<typeparamref name="TSettings"/>
/// This method will be removed in a future release.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.ContentManagement.Utilities;
Expand Down Expand Up @@ -137,6 +138,32 @@ public ContentPartDefinitionBuilder WithField(string fieldName, Action<ContentPa
return this;
}

public async Task<ContentPartDefinitionBuilder> WithFieldAsync(string fieldName, Func<ContentPartFieldDefinitionBuilder, Task> configurationAsync)
{
var existingField = _fields.FirstOrDefault(x => x.Name == fieldName);

if (existingField != null)
{
var toRemove = _fields.Where(x => x.Name == fieldName).ToArray();
foreach (var remove in toRemove)
{
_fields.Remove(remove);
}
}
else
{
existingField = new ContentPartFieldDefinition(null, fieldName, new JObject());
}

var configurer = new FieldConfigurerImpl(existingField, _part);

await configurationAsync(configurer);

_fields.Add(configurer.Build());

return this;
}

private class FieldConfigurerImpl : ContentPartFieldDefinitionBuilder
{
private ContentFieldDefinition _fieldDefinition;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.ContentManagement.Utilities;
Expand Down Expand Up @@ -155,6 +156,39 @@ public ContentTypeDefinitionBuilder WithPart(string name, ContentPartDefinition
return this;
}

public Task<ContentTypeDefinitionBuilder> WithPartAsync(string name, string partName, Func<ContentTypePartDefinitionBuilder, Task> configurationAsync)
{
return WithPartAsync(name, new ContentPartDefinition(partName), configurationAsync);
}

public Task<ContentTypeDefinitionBuilder> WithPartAsync(string partName, Func<ContentTypePartDefinitionBuilder, Task> configurationAsync)
{
return WithPartAsync(partName, new ContentPartDefinition(partName), configurationAsync);
}

public async Task<ContentTypeDefinitionBuilder> WithPartAsync(string name, ContentPartDefinition partDefinition, Func<ContentTypePartDefinitionBuilder, Task> configurationAsync)
{
var existingPart = _parts.FirstOrDefault(x => x.Name == name);

if (existingPart != null)
{
_parts.Remove(existingPart);
}
else
{
existingPart = new ContentTypePartDefinition(name, partDefinition, new JObject());
existingPart.ContentTypeDefinition = Current;
}

var configurer = new PartConfigurerImpl(existingPart);

await configurationAsync(configurer);

_parts.Add(configurer.Build());

return this;
}

private class PartConfigurerImpl : ContentTypePartDefinitionBuilder
{
private readonly ContentPartDefinition _partDefinition;
Expand Down

0 comments on commit 3ba0002

Please sign in to comment.