@@ -85,27 +85,52 @@ public void SetAutoRecycle(bool autoRecycle)
85
85
/// 根据组件类型获取对应的IComponentAgent
86
86
/// </summary>
87
87
/// <typeparam name="T">组件类型</typeparam>
88
+ /// <param name="isNew">是否当获取为空的时候默认创建,默认值为true</param>
88
89
/// <returns></returns>
89
- public async Task < T > GetComponentAgent < T > ( ) where T : IComponentAgent
90
+ public async Task < T > GetComponentAgent < T > ( bool isNew = true ) where T : IComponentAgent
90
91
{
91
- return ( T ) await GetComponentAgent ( typeof ( T ) ) ;
92
+ return ( T ) await GetComponentAgent ( typeof ( T ) , isNew ) ;
92
93
}
93
94
94
95
/// <summary>
95
96
/// 根据组件类型获取对应的IComponentAgent
96
97
/// </summary>
97
98
/// <param name="agentType">代理类型</param>
99
+ /// <param name="isNew">是否当获取为空的时候默认创建,默认值为true</param>
98
100
/// <returns></returns>
99
- public async Task < IComponentAgent > GetComponentAgent ( Type agentType )
101
+ public async Task < IComponentAgent > GetComponentAgent ( Type agentType , bool isNew = true )
100
102
{
101
103
var compType = agentType . BaseType . GetGenericArguments ( ) [ 0 ] ;
102
- var comp = _componentsMap . GetOrAdd ( compType , GetOrAddFactory ) ;
103
- var agent = comp . GetAgent ( agentType ) ;
104
- if ( ! comp . IsActive )
104
+ IComponentAgent agent ;
105
+ if ( isNew )
106
+ {
107
+ var comp = _componentsMap . GetOrAdd ( compType , GetOrAddFactory ) ;
108
+ agent = comp . GetAgent ( agentType ) ;
109
+ if ( ! comp . IsActive )
110
+ {
111
+ async Task Worker ( )
112
+ {
113
+ await comp . Active ( ) ;
114
+ agent . Active ( ) ;
115
+ }
116
+
117
+ await SendAsyncWithoutCheck ( Worker ) ;
118
+ }
119
+
120
+ return agent ;
121
+ }
122
+
123
+ if ( ! _componentsMap . TryGetValue ( compType , out var component ) )
124
+ {
125
+ return default ;
126
+ }
127
+
128
+ agent = component . GetAgent ( agentType ) ;
129
+ if ( ! component . IsActive )
105
130
{
106
131
async Task Worker ( )
107
132
{
108
- await comp . Active ( ) ;
133
+ await component . Active ( ) ;
109
134
agent . Active ( ) ;
110
135
}
111
136
0 commit comments