-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathremote-table.lua
299 lines (277 loc) · 8.15 KB
/
remote-table.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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
---@class remotetable
local mt = {}
mt.__name = 'remote-table'
local InterfaceMap = setmetatable({}, { __mode = 'k' })
local TypeMap = {}
local WaitingMap = {}
local enableMergeGet = true
local function getTypeMap(tp)
if not TypeMap[tp] then
TypeMap[tp] = {
onSet = nil,
onGet = nil,
}
end
return TypeMap[tp]
end
local function getMethod(rt, name)
local interface = InterfaceMap[rt]
if interface.type then
local typeInterface = TypeMap[interface.type]
return typeInterface[name]
else
return interface[name]
end
end
local function promiseSet(callback)
return function (key, value)
local token = callback(key, value)
if not token then
error('异步接口没有返回 token!')
end
if not coroutine.isyieldable() then
error('当前不可让出!')
end
WaitingMap[token] = {
thread = coroutine.running(),
}
return coroutine.yield()
end
end
local function mergedGet(interface, key)
if not enableMergeGet then
return false
end
if not interface.mergeThreads then
interface.mergeThreads = {}
end
if not interface.mergeThreads[key] then
interface.mergeThreads[key] = {}
return false
end
interface.mergeThreads[key][#interface.mergeThreads[key]+1] = coroutine.running()
return true
end
local function promiseGet(callback, interface)
return function (key)
if not coroutine.isyieldable() then
error('当前不可让出!')
end
if mergedGet(interface, key) then
return coroutine.yield()
end
local token = callback(key)
if not token then
error('异步接口没有返回 token!')
end
WaitingMap[token] = {
thread = coroutine.running(),
key = key,
interface = interface,
}
return coroutine.yield()
end
end
function mt:__index(key)
local cache = InterfaceMap[self].cache
if cache[key] ~= nil then
return cache[key]
end
local method = getMethod(self, 'onGet')
if not method then
error('没有设置远程读接口!')
end
local ext = InterfaceMap[self].ext
local value = method(key, ext)
if cache[key] ~= nil then
return cache[key]
end
cache[key] = value
return value
end
function mt:__newindex(key, value)
local cache = InterfaceMap[self].cache
local method = getMethod(self, 'onSet')
if not method then
error('没有设置远程写接口!')
end
local ext = InterfaceMap[self].ext
method(key, value, ext)
cache[key] = value
end
local m = {}
---创建一个远程表,读写数据时会调用远程的读写接口
---如果没有设置 `tp` 参数,那么你需要给这个表单独设置读写接口
---如果设置了 `tp` 参数,那么他会使用该类的读写接口
---@param tp? any # 使用同一个 tp 的表会使用同样的接口。
---@param ext? any # 额外参数,在调用类型接口时传入,方便类型接口区分是哪个对象
---@return remotetable
function m.create(tp, ext)
local rt = setmetatable({}, mt)
InterfaceMap[rt] = {
type = tp,
ext = ext,
cache = {},
onSet = nil,
onGet = nil,
}
return rt
end
---设置远程的读接口
---@param rt remotetable
---@param callback fun(key: any, ext: any): any
function m.onGet(rt, callback)
local interface = InterfaceMap[rt]
if not interface then
error('第1个参数不是remotetable!')
end
if type(callback) ~= 'function' then
error('第2个参数不是function!')
end
if interface.type then
error('不能给共享类型的remotetable设置单独的方法')
end
interface.onGet = callback
end
---设置远程的写接口
---@param rt remotetable
---@param callback fun(key: any, value: any, ext: any): any
function m.onSet(rt, callback)
local interface = InterfaceMap[rt]
if not interface then
error('第1个参数不是remotetable!')
end
if type(callback) ~= 'function' then
error('第2个参数不是function!')
end
if interface.type then
error('不能给共享类型的remotetable设置单独的方法')
end
interface.onSet = callback
end
---设置远程的异步读接口
---
---回调函数必须返回一个token,
---之后通过 m.resume 方法来延续
---@param rt remotetable
---@param callback fun(key: any, ext: any): any
function m.onAsyncGet(rt, callback)
local interface = InterfaceMap[rt]
if not interface then
error('第1个参数不是remotetable!')
end
if type(callback) ~= 'function' then
error('第2个参数不是function!')
end
if interface.type then
error('不能给共享类型的remotetable设置单独的方法')
end
interface.onGet = promiseGet(callback, interface)
end
---设置远程的异步写接口
---
---回调函数必须返回一个token,
---之后通过 m.resume 方法来延续
---@param rt remotetable
---@param callback fun(key: any, value: any, ext: any): any
function m.onAsyncSet(rt, callback)
local interface = InterfaceMap[rt]
if not interface then
error('第1个参数不是remotetable!')
end
if type(callback) ~= 'function' then
error('第2个参数不是function!')
end
if interface.type then
error('不能给共享类型的remotetable设置单独的方法')
end
interface.onSet = promiseSet(callback)
end
---设置类的远程读接口
---@param tp any
---@param callback fun(key: any, ext: any): any
function m.onTypeGet(tp, callback)
local typeInterface = getTypeMap(tp)
if type(callback) ~= 'function' then
error('第2个参数不是function!')
end
typeInterface.onGet = callback
end
---设置类的远程写接口
---@param tp any
---@param callback fun(key: any, value: any, ext: any): any
function m.onTypeSet(tp, callback)
local interface = getTypeMap(tp)
if type(callback) ~= 'function' then
error('第2个参数不是function!')
end
interface.onSet = callback
end
---设置类的远程读接口
---
---回调函数必须返回一个token,
---之后通过 m.resume 方法来延续
---@param tp any
---@param callback fun(key: any, ext: any): any
function m.onAsyncTypeGet(tp, callback)
local typeInterface = getTypeMap(tp)
if type(callback) ~= 'function' then
error('第2个参数不是function!')
end
typeInterface.onGet = promiseGet(callback, typeInterface)
end
---设置类的远程写接口
---
---回调函数必须返回一个token,
---之后通过 m.resume 方法来延续
---@param tp any
---@param callback fun(key: any, value: any, ext: any): any
function m.onAsyncTypeSet(tp, callback)
local interface = getTypeMap(tp)
if type(callback) ~= 'function' then
error('第2个参数不是function!')
end
interface.onSet = promiseSet(callback)
end
---延续之前让出的远程读写
---即使读写失败了也要调用一次这个函数
---@param token any
---@param value? any
function m.resume(token, value)
local waiting = WaitingMap[token]
if not waiting then
error(('无法根据 token 找到让出的线程:%s'):format(token))
end
WaitingMap[token] = nil
local thread = waiting.thread
local interface = waiting.interface
local mergedThreads
if interface then
local key = waiting.key
mergedThreads = interface.mergeThreads and interface.mergeThreads[key]
if mergedThreads then
interface.mergeThreads[key] = nil
end
end
coroutine.resume(thread, value)
if not mergedThreads then
return
end
for _, mergedThread in ipairs(mergedThreads) do
coroutine.resume(mergedThread, value)
end
end
---允许合并获取请求,默认是开启的
---@param enable boolean
function m.mergeGet(enable)
enableMergeGet = enable
end
---清点挂起的请求
function m.countHanging()
local c = 0
for _ in pairs(WaitingMap) do
c = c + 1
end
return c
end
return m