-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlib.lua
58 lines (49 loc) · 1.75 KB
/
lib.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
local eventName = "netvar#" .. GetCurrentResourceName()
if IsDuplicityVersion() then
N = {__data = {}}
setmetatable(N, {
__index = function(self, key)
return self.__data[key]
end,
__newindex = function(self, key, value)
self.__data[key] = value
TriggerClientEvent(eventName, -1, key, value)
end
})
RegisterNetEvent(eventName, function(key)
TriggerClientEvent(eventName, source, key, N[key])
end)
else
N = {__data = {}, __hasBeenRequested = {}, __awaitingPromises = {}, __subscribers = {}}
setmetatable(N, {
__index = function(self, key)
-- Request the variable from the server if it hasn't been requested yet
if (not self.__hasBeenRequested[key]) then
self.__hasBeenRequested[key] = true
self.__awaitingPromises[key] = promise.new()
TriggerServerEvent(eventName, key)
Citizen.Await(self.__awaitingPromises[key])
end
return self.__data[key]
end,
__newindex = function(self, key, value)
if self.__subscribers[key] == nil then
self.__subscribers[key] = {}
end
table.insert(self.__subscribers[key], value)
end
})
-- Subscribe to variable changes
RegisterNetEvent(eventName, function(key, value)
if N.__subscribers[key] ~= nil then
for _, callback in pairs(N.__subscribers[key]) do
callback(value, N.__data[key])
end
end
N.__data[key] = value
if (N.__awaitingPromises[key]) then
N.__awaitingPromises[key]:resolve()
N.__awaitingPromises[key] = nil
end
end)
end