Created
February 17, 2021 02:40
-
-
Save Sasino97/a2a1bcbaa595afc878dd7b4a837e8d31 to your computer and use it in GitHub Desktop.
TriggerEventAsyncInternal<T>
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
using CitizenFX.Core; | |
using System; | |
using System.Threading.Tasks; | |
using static CitizenFX.Core.Native.API; | |
public abstract class ClientScript : BaseScript | |
{ | |
private async Task<T> TriggerEventAsyncInternal<T>(string eventName, int timeout, bool server, params object[] args) | |
{ | |
var responseEventName = $"{eventName}:response"; | |
Action<T> eventHandler = null; | |
bool hasResult = false; | |
T result = default; | |
eventHandler = new Action<T>((T tResult) => | |
{ | |
EventHandlers[responseEventName] -= eventHandler; | |
result = tResult; | |
hasResult = true; | |
}); | |
EventHandlers[responseEventName] += eventHandler; | |
if (server) | |
TriggerServerEvent(eventName, args); | |
else | |
TriggerEvent(eventName, args); | |
var t = DateTime.Now; | |
while (!hasResult && (DateTime.Now - t).TotalMilliseconds < timeout) | |
await Delay(0); | |
return result; | |
} | |
protected async Task<T> TriggerEventAsync<T>(string eventName, params object[] args) | |
=> await TriggerEventAsyncInternal<T>(eventName, 250, false, args); | |
protected async Task<T> TriggerServerEventAsync<T>(string eventName, params object[] args) | |
=> await TriggerEventAsyncInternal<T>(eventName, 2500, true, args); | |
} |
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
using CitizenFX.Core; | |
using System; | |
using System.Threading.Tasks; | |
using static CitizenFX.Core.Native.API; | |
public abstract class ServerScript : BaseScript | |
{ | |
private async Task<T> TriggerEventAsyncInternal<T>(string eventName, int timeout, bool server, Player target, params object[] args) | |
{ | |
var responseEventName = $"{eventName}:response"; | |
Action<T> eventHandler = null; | |
bool hasResult = false; | |
T result = default; | |
eventHandler = new Action<T>((T tResult) => | |
{ | |
EventHandlers[responseEventName] -= eventHandler; | |
result = tResult; | |
hasResult = true; | |
}); | |
EventHandlers[responseEventName] += eventHandler; | |
if (server) | |
TriggerEvent(eventName, args); | |
else | |
{ | |
if (target != null) | |
target.TriggerEvent(eventName, args); | |
else | |
TriggerClientEvent(eventName, args); | |
} | |
var t = DateTime.Now; | |
while (!hasResult && (DateTime.Now - t).TotalMilliseconds < timeout) | |
await Delay(1); | |
return result; | |
} | |
protected async Task<T> TriggerEventAsync<T>(string eventName, params object[] args) | |
=> await TriggerEventAsyncInternal<T>(eventName, 250, true, null, args); | |
protected async Task<T> TriggerClientEventAsync<T>(string eventName, params object[] args) | |
=> await TriggerEventAsyncInternal<T>(eventName, 2500, false, null, args); | |
protected async Task<T> TriggerClientEventAsync<T>(Player target, string eventName, params object[] args) | |
=> await TriggerEventAsyncInternal<T>(eventName, 2500, false, target, args); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment