@rbxts/replion 1.0.10 → 1.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/package.json +1 -1
- package/src/Client.lua +61 -39
- package/src/Server.lua +46 -35
- package/src/Shared.lua +5 -0
- package/src/index.d.ts +8 -0
package/README.md
CHANGED
package/package.json
CHANGED
package/src/Client.lua
CHANGED
|
@@ -58,7 +58,7 @@ function Client.new(channel: string, data: Shared.GenericDataTable)
|
|
|
58
58
|
return self;
|
|
59
59
|
end;
|
|
60
60
|
|
|
61
|
-
function Client:get(key: (string | {string})?)
|
|
61
|
+
function Client:get(key: (string | { string })?)
|
|
62
62
|
if not key then return self.data; end;
|
|
63
63
|
if typeof(key) == 'table' then
|
|
64
64
|
return Shared.getNestedValue(self.data, key);
|
|
@@ -67,51 +67,73 @@ function Client:get(key: (string | {string})?)
|
|
|
67
67
|
return self.data[key];
|
|
68
68
|
end;
|
|
69
69
|
|
|
70
|
-
function Client:observe(key: string?, callback: Observer)
|
|
71
|
-
|
|
72
|
-
|
|
70
|
+
function Client:observe(key: (string | { string })?, callback: Observer)
|
|
71
|
+
if not key then
|
|
72
|
+
task.spawn(callback, self.data, nil);
|
|
73
|
+
return self._allSignal:Connect(callback);
|
|
74
|
+
end;
|
|
73
75
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
+
local signalKey = Shared.getSignalKey(key);
|
|
77
|
+
if not self._signals[signalKey] then
|
|
78
|
+
self._signals[signalKey] = Signal.new();
|
|
79
|
+
end;
|
|
76
80
|
|
|
77
|
-
|
|
78
|
-
self._signals[key] = Signal.new();
|
|
79
|
-
end;
|
|
81
|
+
local initialValue = if typeof(key) == 'table' then Shared.getNestedValue(self.data, key) else self.data[key];
|
|
80
82
|
|
|
81
|
-
|
|
83
|
+
task.spawn(callback, initialValue, nil);
|
|
84
|
+
return self._signals[signalKey]:Connect(callback);
|
|
85
|
+
end;
|
|
82
86
|
|
|
83
|
-
|
|
87
|
+
function Client:_recursiveSignalCheck(currentPath: { string }, updateTree: any)
|
|
88
|
+
for k, v in updateTree do
|
|
89
|
+
local nextPath = table.clone(currentPath);
|
|
90
|
+
table.insert(nextPath, k);
|
|
91
|
+
|
|
92
|
+
local signalKey = Shared.getSignalKey(nextPath);
|
|
93
|
+
local signal = self._signals[signalKey];
|
|
94
|
+
|
|
95
|
+
if signal then
|
|
96
|
+
local newValue = Shared.getNestedValue(self.data, nextPath);
|
|
97
|
+
signal:Fire(newValue, newValue); -- Warning: Sends improper 'old' argument due to shallow copy optimization.
|
|
98
|
+
end;
|
|
99
|
+
|
|
100
|
+
if typeof(v) == 'table' then
|
|
101
|
+
self:_recursiveSignalCheck(nextPath, v)
|
|
102
|
+
end;
|
|
103
|
+
end;
|
|
84
104
|
end;
|
|
85
105
|
|
|
86
106
|
function Client:_applyUpdates(updates: Shared.GenericDataTable)
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
-- If it's a table, we assume it changed if it's in the
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
107
|
+
local oldData = table.clone(self.data); -- Note: Shallow copy instead of deep copy.
|
|
108
|
+
|
|
109
|
+
deepMergeAndApply(self.data, updates);
|
|
110
|
+
|
|
111
|
+
local anyChanged = false;
|
|
112
|
+
|
|
113
|
+
for key, newValue in updates do
|
|
114
|
+
local oldValue = oldData[key];
|
|
115
|
+
|
|
116
|
+
-- If it's a table, we assume it changed if it's in the updates list.
|
|
117
|
+
if typeof(newValue) == 'table' then
|
|
118
|
+
anyChanged = true
|
|
119
|
+
local signal = self._signals[key];
|
|
120
|
+
if signal then
|
|
121
|
+
signal:Fire(self.data[key], self.data[key]); -- Warning: Sends improper 'old' argument due to shallow copy optimization.
|
|
122
|
+
end;
|
|
123
|
+
|
|
124
|
+
self:_recursiveSignalCheck({key}, newValue)
|
|
125
|
+
elseif self.data[key] ~= oldValue then
|
|
126
|
+
anyChanged = true;
|
|
127
|
+
local signal = self._signals[key];
|
|
128
|
+
if signal then
|
|
129
|
+
signal:Fire(self.data[key], oldValue);
|
|
130
|
+
end;
|
|
131
|
+
end;
|
|
132
|
+
end;
|
|
133
|
+
|
|
134
|
+
if anyChanged then
|
|
135
|
+
self._allSignal:Fire(self.data, updates);
|
|
136
|
+
end;
|
|
115
137
|
end;
|
|
116
138
|
|
|
117
139
|
function Client:destroy()
|
package/src/Server.lua
CHANGED
|
@@ -99,46 +99,57 @@ function Server.new(config: ServerConfig)
|
|
|
99
99
|
return self;
|
|
100
100
|
end;
|
|
101
101
|
|
|
102
|
-
function Server:observe(key: string?, callback: Observer)
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
102
|
+
function Server:observe(key: (string | {string})?, callback: Observer)
|
|
103
|
+
if not key then
|
|
104
|
+
task.spawn(callback, self.data, nil);
|
|
105
|
+
return self._allSignal:Connect(callback);
|
|
106
|
+
end
|
|
107
107
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
108
|
+
local signalKey = Shared.getSignalKey(key);
|
|
109
|
+
if not self._signals[signalKey] then
|
|
110
|
+
self._signals[signalKey] = Signal.new();
|
|
111
|
+
end;
|
|
112
|
+
|
|
113
|
+
local initialValue = if typeof(key) == 'table' then Shared.getNestedValue(self.data, key) else self.data[key];
|
|
111
114
|
|
|
112
|
-
|
|
113
|
-
|
|
115
|
+
task.spawn(callback, initialValue, nil);
|
|
116
|
+
return self._signals[signalKey]:Connect(callback);
|
|
114
117
|
end;
|
|
115
118
|
|
|
116
119
|
function Server:set(key: string | {string}, value: any)
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
120
|
+
if self._destroyed then return; end;
|
|
121
|
+
|
|
122
|
+
local path = if typeof(key) == 'table' then key else {key};
|
|
123
|
+
local topKey = path[1];
|
|
124
|
+
|
|
125
|
+
local oldValue = Shared.getNestedValue(self.data, path);
|
|
126
|
+
local newValue = if typeof(value) == 'function' then value(oldValue) else value;
|
|
127
|
+
if oldValue == newValue then return; end;
|
|
128
|
+
|
|
129
|
+
local oldTop = self.data[topKey];
|
|
130
|
+
|
|
131
|
+
setNestedValue(self.data, path, newValue);
|
|
132
|
+
queueNestedUpdate(self._queuedUpdates, path, newValue);
|
|
133
|
+
self:_scheduleUpdatesFlush();
|
|
134
|
+
|
|
135
|
+
do
|
|
136
|
+
local topSignal = self._signals[topKey];
|
|
137
|
+
if topSignal then
|
|
138
|
+
topSignal:Fire(self.data[topKey], oldTop);
|
|
139
|
+
end;
|
|
140
|
+
|
|
141
|
+
local signalKey = Shared.getSignalKey(key);
|
|
142
|
+
if signalKey ~= topKey then
|
|
143
|
+
local pathSignal = self._signals[signalKey];
|
|
144
|
+
if pathSignal then
|
|
145
|
+
pathSignal:Fire(newValue, oldValue)
|
|
146
|
+
end;
|
|
147
|
+
end;
|
|
148
|
+
|
|
149
|
+
local thisUpdate = {};
|
|
150
|
+
setNestedValue(thisUpdate, path, newValue);
|
|
151
|
+
self._allSignal:Fire(self.data, thisUpdate);
|
|
152
|
+
end;
|
|
142
153
|
end;
|
|
143
154
|
|
|
144
155
|
function Server:get(key: (string | {string})?)
|
package/src/Shared.lua
CHANGED
|
@@ -15,4 +15,9 @@ function Shared.getNestedValue(root: any, path: { string })
|
|
|
15
15
|
return current;
|
|
16
16
|
end;
|
|
17
17
|
|
|
18
|
+
function Shared.getSignalKey(key: string | {string})
|
|
19
|
+
if typeof(key) == 'string' then return key; end;
|
|
20
|
+
return table.concat(key, '\0');
|
|
21
|
+
end;
|
|
22
|
+
|
|
18
23
|
return Shared;
|
package/src/index.d.ts
CHANGED
|
@@ -22,6 +22,10 @@ declare namespace Replion {
|
|
|
22
22
|
key: K,
|
|
23
23
|
callback: (newValue: T[K], oldValue: T[K] | undefined) => void,
|
|
24
24
|
): Connection;
|
|
25
|
+
observe(
|
|
26
|
+
path: string[],
|
|
27
|
+
callback: (newValue: any, oldValue: any) => void,
|
|
28
|
+
): Connection;
|
|
25
29
|
|
|
26
30
|
destroy(): void;
|
|
27
31
|
}
|
|
@@ -38,6 +42,10 @@ declare namespace Replion {
|
|
|
38
42
|
key: K,
|
|
39
43
|
callback: (newValue: T[K], oldValue: T[K] | undefined) => void,
|
|
40
44
|
): Connection;
|
|
45
|
+
observe(
|
|
46
|
+
path: string[],
|
|
47
|
+
callback: (newValue: any, oldValue: any) => void,
|
|
48
|
+
): Connection;
|
|
41
49
|
|
|
42
50
|
destroy(): void;
|
|
43
51
|
}
|