|
1 | 1 | namespace ElectronNET.API |
2 | 2 | { |
| 3 | + using System; |
3 | 4 | using System.Collections.Concurrent; |
| 5 | + using System.Diagnostics; |
4 | 6 | using System.Runtime.CompilerServices; |
| 7 | + using System.Threading.Tasks; |
5 | 8 | using ElectronNET.Common; |
6 | 9 |
|
7 | 10 | public abstract class ApiBase |
8 | 11 | { |
9 | 12 | internal const int PropertyTimeout = 1000; |
10 | 13 |
|
11 | 14 | private readonly string objectName; |
| 15 | + private readonly ConcurrentDictionary<string, PropertyGetter> propertyGetters = new ConcurrentDictionary<string, PropertyGetter>(); |
| 16 | + private readonly ConcurrentDictionary<string, string> propertyEventNames = new ConcurrentDictionary<string, string>(); |
| 17 | + private readonly ConcurrentDictionary<string, string> propertyMessageNames = new ConcurrentDictionary<string, string>(); |
12 | 18 | private readonly ConcurrentDictionary<string, string> methodMessageNames = new ConcurrentDictionary<string, string>(); |
13 | 19 |
|
14 | 20 | public virtual int Id |
@@ -82,5 +88,80 @@ protected void CallMethod3(object val1, object val2, object val3, [CallerMemberN |
82 | 88 | BridgeConnector.Socket.Emit(messageName, val1, val2, val3); |
83 | 89 | } |
84 | 90 | } |
| 91 | + |
| 92 | + protected Task<T> GetPropertyAsync<T>([CallerMemberName] string callerName = null) |
| 93 | + { |
| 94 | + Debug.Assert(callerName != null, nameof(callerName) + " != null"); |
| 95 | + |
| 96 | + return this.propertyGetters.GetOrAdd(callerName, _ => |
| 97 | + { |
| 98 | + var getter = new PropertyGetter<T>(this, callerName, PropertyTimeout); |
| 99 | + |
| 100 | + getter.Task<T>().ContinueWith(_ => this.propertyGetters.TryRemove(callerName, out var _)); |
| 101 | + |
| 102 | + return getter; |
| 103 | + }).Task<T>(); |
| 104 | + } |
| 105 | + |
| 106 | + internal abstract class PropertyGetter |
| 107 | + { |
| 108 | + public abstract Task<T> Task<T>(); |
| 109 | + } |
| 110 | + |
| 111 | + internal class PropertyGetter<T> : PropertyGetter |
| 112 | + { |
| 113 | + private readonly Task<T> tcsTask; |
| 114 | + private TaskCompletionSource<T> tcs; |
| 115 | + |
| 116 | + public PropertyGetter(ApiBase apiBase, string callerName, int timeoutMs) |
| 117 | + { |
| 118 | + this.tcs = new TaskCompletionSource<T>(TaskCreationOptions.RunContinuationsAsynchronously); |
| 119 | + this.tcsTask = this.tcs.Task; |
| 120 | + |
| 121 | + var eventName = apiBase.propertyEventNames.GetOrAdd(callerName, s => $"{apiBase.objectName}-{s.StripAsync().LowerFirst()}{apiBase.SocketEventCompleteSuffix}"); |
| 122 | + var messageName = apiBase.propertyMessageNames.GetOrAdd(callerName, s => apiBase.objectName + s.StripAsync()); |
| 123 | + |
| 124 | + BridgeConnector.Socket.On<T>(eventName, (result) => |
| 125 | + { |
| 126 | + BridgeConnector.Socket.Off(eventName); |
| 127 | + |
| 128 | + lock (this) |
| 129 | + { |
| 130 | + this.tcs?.SetResult(result); |
| 131 | + this.tcs = null; |
| 132 | + } |
| 133 | + }); |
| 134 | + |
| 135 | + if (apiBase.Id >= 0) |
| 136 | + { |
| 137 | + BridgeConnector.Socket.Emit(messageName, apiBase.Id); |
| 138 | + } |
| 139 | + else |
| 140 | + { |
| 141 | + BridgeConnector.Socket.Emit(messageName); |
| 142 | + } |
| 143 | + |
| 144 | + System.Threading.Tasks.Task.Delay(ApiBase.PropertyTimeout).ContinueWith(_ => |
| 145 | + { |
| 146 | + if (this.tcs != null) |
| 147 | + { |
| 148 | + lock (this) |
| 149 | + { |
| 150 | + if (this.tcs != null) |
| 151 | + { |
| 152 | + var ex = new TimeoutException($"No response after {timeoutMs:D}ms trying to retrieve value {apiBase.objectName}.{callerName}()"); |
| 153 | + this.tcs.TrySetException(ex); |
| 154 | + this.tcs = null; |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + }); |
| 159 | + } |
| 160 | + |
| 161 | + public override Task<T1> Task<T1>() |
| 162 | + { |
| 163 | + return this.tcsTask as Task<T1>; |
| 164 | + } |
| 165 | + } |
85 | 166 | } |
86 | 167 | } |
0 commit comments