-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathshared-table.lua
More file actions
250 lines (214 loc) · 5.55 KB
/
Copy pathshared-table.lua
File metadata and controls
250 lines (214 loc) · 5.55 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
local proxy
local MODE_K = { __mode = 'k' }
local MODE_V = { __mode = 'v' }
---@class SharedTable
local M = {}
M.__index = M
---@param default? table
---@return SharedTable
function M:init(default)
proxy = proxy or require 'proxy'
self.value = default or {}
---@type { [table]: integer }
self.bindMap = setmetatable({}, MODE_K)
self.bindId = 0
self.trap = proxy.new(self.value, {
recursive = true,
updateRaw = true,
anySetter = function (parent, raw, key, value)
local tp = type(value)
if tp ~= 'nil'
and tp ~= 'boolean'
and tp ~= 'number'
and tp ~= 'string'
and tp ~= 'table' then
local path = proxy.getPath(parent)
path[#path+1] = key
local pathStr = table.concat(path, '.')
error('不支持的值类型:' .. tp .. ',路径是:' .. pathStr)
return
end
if not self.touched then
self.touched = {}
end
if not self.touched[raw] then
self.touched[raw] = {}
end
self.touched[raw][key] = true
return value
end
})
local queue = { self.value }
while true do
local current = queue[#queue]
if not current then
break
end
queue[#queue] = nil
self.bindId = self.bindId + 1
self.bindMap[current] = self.bindId
for _, v in pairs(current) do
if type(v) == 'table' then
queue[#queue+1] = v
end
end
end
return self
end
---@alias SharedTable.DumpShape { value: table, bind: { [integer]: table } }
---@return SharedTable.DumpShape
function M:dump()
local bind = {}
for tbl, id in pairs(self.bindMap) do
bind[id] = tbl
end
return {
value = self.value,
bind = bind,
}
end
---@return boolean
function M:isTouched()
return self.touched ~= nil
end
---@alias SharedTable.ChangesShape {
--- set?: { [integer]: table<any, any> },
--- del?: { [integer]: any[] },
--- newBind?: { [table]: integer },
---}
---@return SharedTable.ChangesShape?
function M:exportChanges()
local touched = self.touched
if touched == nil then
return nil
end
self.touched = nil
local set, del
local newBind
local function getId(t)
if type(t) ~= 'table' then
return nil
end
local id = self.bindMap[t]
if id then
return id
end
if not newBind then
newBind = {}
end
local queue = { t }
while true do
local current = queue[#queue]
if not current then
break
end
queue[#queue] = nil
id = self.bindId + 1
self.bindId = id
self.bindMap[current] = id
newBind[current] = id
for _, v in pairs(current) do
if type(v) == 'table' and not self.bindMap[v] then
queue[#queue+1] = v
end
end
end
return self.bindMap[t]
end
local function updateResult(raw, key)
local value = raw[key]
local id = self.bindMap[raw]
if not id then
-- raw 本身是新插入但还没注册的子表,注册时已包含全部当前 kv
getId(raw)
return
end
if value == nil then
if not del then
del = {}
end
if not del[id] then
del[id] = {}
end
del[id][#del[id]+1] = key
return
end
getId(value)
if not set then
set = {}
end
if not set[id] then
set[id] = {}
end
set[id][key] = value
end
for raw, keys in pairs(touched) do
for key in pairs(keys) do
updateResult(raw, key)
end
end
return {
set = set,
del = del,
newBind = newBind,
}
end
---@class SharedTable.API
local API = {}
---@private
---@type table<table, table<integer, table>>
API.tableToBind = setmetatable({}, MODE_K)
function API.setTools(proxyLib)
proxy = proxyLib
end
---@param default? table
---@return SharedTable
function API.create(default)
local st = setmetatable({}, M)
st:init(default)
return st
end
---@param dump SharedTable.DumpShape
function API.load(dump)
API.tableToBind[dump.value] = setmetatable(dump.bind, MODE_V)
return dump.value
end
---@param t table
---@param changes SharedTable.ChangesShape
function API.importChanges(t, changes)
local bind = API.tableToBind[t]
if not bind then
error('table is not a shared table')
return
end
if changes.newBind then
for tbl, id in pairs(changes.newBind) do
bind[id] = tbl
end
end
if changes.set then
for id, kv in pairs(changes.set) do
local tbl = bind[id]
if not tbl then
error('invalid bind id: ' .. id)
return
end
for k, v in pairs(kv) do
tbl[k] = v
end
end
end
if changes.del then
for id, keys in pairs(changes.del) do
local tbl = bind[id]
if not tbl then
error('invalid bind id: ' .. id)
return
end
for _, k in ipairs(keys) do
tbl[k] = nil
end
end
end
end
return API