1
1
using System . Collections . Concurrent ;
2
- using System . Diagnostics ;
3
- using UniGetUI . Core . Logging ;
4
- using WinRT . Interop ;
5
2
6
3
namespace UniGetUI . Core . Classes ;
7
4
@@ -21,12 +18,10 @@ namespace UniGetUI.Core.Classes;
21
18
*/
22
19
public static class TaskRecycler < ReturnT >
23
20
{
24
- private static ConcurrentDictionary < int , Task < ReturnT > > _tasks = new ( ) ;
25
-
21
+ private static readonly ConcurrentDictionary < int , Task < ReturnT > > _tasks = new ( ) ;
26
22
27
23
// ---------------------------------------------------------------------------------------------------------------
28
24
29
-
30
25
/// Asynchronous entry point for 0 parameters
31
26
public static Task < ReturnT > RunOrAttachAsync ( Func < ReturnT > method , int cacheTimeSecs = 0 )
32
27
{
@@ -57,10 +52,8 @@ public static Task<ReturnT> RunOrAttachAsync<Param1T, Param2T, Param3T>(Func<Par
57
52
return _runTaskAndWait ( new Task < ReturnT > ( ( ) => method ( arg1 , arg2 , arg3 ) ) , hash , cacheTimeSecs ) ;
58
53
}
59
54
60
-
61
55
// ---------------------------------------------------------------------------------------------------------------
62
56
63
-
64
57
/// Synchronous entry point for 0 parameters
65
58
public static ReturnT RunOrAttach ( Func < ReturnT > method , int cacheTimeSecs = 0 )
66
59
=> RunOrAttachAsync ( method , cacheTimeSecs ) . GetAwaiter ( ) . GetResult ( ) ;
@@ -79,10 +72,8 @@ public static ReturnT RunOrAttach<Param1T, Param2T, Param3T>(Func<Param1T, Param
79
72
Param2T arg2 , Param3T arg3 , int cacheTimeSecs = 0 )
80
73
=> RunOrAttachAsync ( method , arg1 , arg2 , arg3 , cacheTimeSecs ) . GetAwaiter ( ) . GetResult ( ) ;
81
74
82
-
83
75
// ---------------------------------------------------------------------------------------------------------------
84
76
85
-
86
77
/// <summary>
87
78
/// Instantly removes a function call from the cache, even if the associated task has not
88
79
/// finished yet. Any previous call will finish as expected. New calls won't attach to any
@@ -93,10 +84,8 @@ public static ReturnT RunOrAttach<Param1T, Param2T, Param3T>(Func<Param1T, Param
93
84
public static void RemoveFromCache ( Func < ReturnT > method )
94
85
=> _removeFromCache ( method . GetHashCode ( ) , 0 ) ;
95
86
96
-
97
87
// ---------------------------------------------------------------------------------------------------------------
98
88
99
-
100
89
/// <summary>
101
90
/// Handles running the task if no such task was found on cache, and returning the cached task if it was found.
102
91
/// </summary>
@@ -134,15 +123,14 @@ private static async Task<ReturnT> _runTaskAndWait(Task<ReturnT> task, int hash,
134
123
/// </summary>
135
124
private static async void _removeFromCache ( int hash , int cacheTimeSecsSecs )
136
125
{
137
- if ( cacheTimeSecsSecs > 0 )
126
+ if ( cacheTimeSecsSecs > 0 )
138
127
await Task . Delay ( cacheTimeSecsSecs * 1000 ) ;
139
128
140
129
_tasks . Remove ( hash , out _ ) ;
141
130
}
142
131
}
143
132
144
-
145
133
public static class TaskRecyclerTelemetry
146
134
{
147
- public static int DeduplicatedCalls = 0 ;
135
+ public static int DeduplicatedCalls ;
148
136
}
0 commit comments