forked from Azure/azure-powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
95173ef
commit ca51bc7
Showing
3 changed files
with
74 additions
and
141 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
experiments/Azure.Experiments/Azure.Experiments/AsyncOperation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System.Collections.Concurrent; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Azure.Experiments | ||
{ | ||
abstract class AsyncOperation : IResourceConfigVisitor<Task<object>> | ||
{ | ||
public AsyncOperation(IClient client, CancellationToken cancellationToken) | ||
{ | ||
Client = client; | ||
CancellationToken = cancellationToken; | ||
} | ||
|
||
public async Task<object> GetOrAddUntyped(IResourceConfig config) | ||
=> await TaskMap.GetOrAdd( | ||
config, | ||
async _ => | ||
{ | ||
var info = await config.Apply(this); | ||
Result.GetOrAddUntyped(config, () => info); | ||
return info; | ||
}); | ||
|
||
public async Task<Config> GetOrAdd<Config>(IResourceConfig<Config> config) | ||
where Config : class | ||
{ | ||
var result = await GetOrAddUntyped(config); | ||
return result as Config; | ||
} | ||
|
||
public abstract Task<object> Visit<Config>(ResourceConfig<Config> config) where Config : class; | ||
|
||
public abstract Task<object> Visit<Config, ParentConfig>(NestedResourceConfig<Config, ParentConfig> config) | ||
where Config : class | ||
where ParentConfig : class; | ||
|
||
public IClient Client { get; } | ||
|
||
public CancellationToken CancellationToken { get; } | ||
|
||
public State Result { get; } = new State(); | ||
|
||
ConcurrentDictionary<IResourceConfig, Task<object>> TaskMap { get; } | ||
= new ConcurrentDictionary<IResourceConfig, Task<object>>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters