1- using System . Collections . Concurrent ;
2- using System . Linq ;
1+ using System . Linq ;
32using System . Threading ;
43using System . Threading . Tasks ;
54
@@ -8,37 +7,21 @@ namespace Microsoft.Azure.Experiments
87 public static class ResourceOperations
98 {
109 public static async Task < IState > CreateAsync < Config > (
11- this IResourceConfig < Config > config , IClient client , IState current , IState parameters )
10+ this IResourceConfig < Config > config ,
11+ IClient client ,
12+ IState current ,
13+ IState parameters ,
14+ CancellationToken cancellationToken )
1215 where Config : class
1316 {
14- var visitor = new CreateAsyncVisitor ( client , current , parameters ) ;
17+ var visitor = new CreateAsyncVisitor ( client , current , parameters , cancellationToken ) ;
1518 await visitor . GetOrAdd ( config ) ;
1619 return visitor . Result ;
1720 }
1821
19- sealed class CreateAsyncVisitor : IResourceConfigVisitor < Task < object > >
22+ sealed class CreateAsyncVisitor : AsyncOperation
2023 {
21- public async Task < object > GetOrAddUntyped ( IResourceConfig config )
22- => await TaskMap . GetOrAdd (
23- config ,
24- async _ =>
25- {
26- var info = await config . Apply ( this ) ;
27- if ( info != null )
28- {
29- Result . GetOrAddUntyped ( config , ( ) => info ) ;
30- }
31- return info ;
32- } ) ;
33-
34- public async Task < Config > GetOrAdd < Config > ( IResourceConfig < Config > config )
35- where Config : class
36- {
37- var result = await GetOrAddUntyped ( config ) ;
38- return result as Config ;
39- }
40-
41- public async Task < object > Visit < Config > ( ResourceConfig < Config > config ) where Config : class
24+ public override async Task < object > Visit < Config > ( ResourceConfig < Config > config )
4225 {
4326 var current = Current . GetOrNull ( config ) ;
4427 if ( current != null )
@@ -52,98 +35,30 @@ public async Task<object> Visit<Config>(ResourceConfig<Config> config) where Con
5235 config . ResourceGroupName ,
5336 config . Name ,
5437 Parameters . GetOrNull ( config ) ,
55- new CancellationToken ( ) ) ) ;
38+ CancellationToken ) ) ;
5639 }
5740
58- public async Task < object > Visit < Config , ParentConfig > (
41+ public override async Task < object > Visit < Config , ParentConfig > (
5942 NestedResourceConfig < Config , ParentConfig > config )
60- where Config : class
61- where ParentConfig : class
6243 {
6344 var parent = await GetOrAdd ( config . Parent ) ;
6445 return config . Policy . Get ( parent ) ;
6546 }
6647
67- public CreateAsyncVisitor ( IClient client , IState current , IState parameters )
48+ public CreateAsyncVisitor (
49+ IClient client ,
50+ IState current ,
51+ IState parameters ,
52+ CancellationToken cancellationToken )
53+ : base ( client , cancellationToken )
6854 {
69- Client = client ;
7055 Current = current ;
7156 Parameters = parameters ;
7257 }
7358
74- public State Result { get ; } = new State ( ) ;
75-
76- IClient Client { get ; }
77-
7859 IState Current { get ; }
7960
8061 IState Parameters { get ; }
81-
82- ConcurrentDictionary < IResourceConfig , Task < object > > TaskMap { get ; }
83- = new ConcurrentDictionary < IResourceConfig , Task < object > > ( ) ;
84- }
85- }
86-
87- /*
88- public sealed class CreateOperation
89- {
90- public Func<IClient, IStateSet, Task> CreateAsync { get; }
91-
92- public IEnumerable<CreateOperation> Dependencies { get; }
93-
94- public CreateOperation(
95- Func<IClient, Task> createAsync,
96- IEnumerable<CreateOperation> dependencies)
97- {
98- CreateAsync = createAsync;
99- Dependencies = dependencies;
100- }
101-
102- public static CreateOperation Create<Config>(IState state, IResourceConfig<Config> config)
103- where Config : class
104- => new Visitor(state).Get(config);
105-
106- sealed class Visitor : IResourceConfigVisitor<CreateOperation>
107- {
108- public CreateOperation Get(IResourceConfig config)
109- => Map.GetOrAdd(config, _ => config.Apply(this));
110-
111- public CreateOperation Visit<Config>(ResourceConfig<Config> config) where Config : class
112- {
113- var info = State.Get(config);
114- return info == null
115- ? new CreateOperation(
116- async (client, state) =>
117- {
118- var p = config.CreateConfig(client.Context.SubscriptionId);
119- var i = await config.Policy.CreateOrUpdateAsync(new CreateOrUpdateAsyncParams<IClient, Config>(
120- client,
121- config.ResourceGroupName,
122- config.Name,
123- p,
124- new CancellationToken()));
125- state.Set(config, i);
126- },
127- config.Dependencies.Select(d => Get(d)).Where(d => d != null))
128- : null;
129- }
130-
131- public CreateOperation Visit<Config, ParentConfig>(
132- NestedResourceConfig<Config, ParentConfig> config)
133- where Config : class
134- where ParentConfig : class
135- => Get(config.Parent);
136-
137- public Visitor(IStateGet state)
138- {
139- State = state;
140- }
141-
142- IStateGet State { get; }
143-
144- ConcurrentDictionary<IResourceConfig, CreateOperation> Map { get; }
145- = new ConcurrentDictionary<IResourceConfig, CreateOperation>();
14662 }
14763 }
148- */
14964}
0 commit comments