Skip to content

Commit 7d2156d

Browse files
committed
尽快cancel掉之前的请求
1 parent 2df623b commit 7d2156d

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

script/lclient.lua

+12-5
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,16 @@ function mt:remove()
162162
self._gc:remove()
163163
end
164164

165+
---@async
165166
function mt:notify(method, params)
166167
proto.doMethod {
167168
method = method,
168169
params = params,
169170
}
171+
await.sleep(0.1)
170172
end
171173

174+
---@async
172175
function mt:request(method, params, callback)
173176
local id = counter()
174177
self._waiting[id] = {
@@ -181,16 +184,20 @@ function mt:request(method, params, callback)
181184
method = method,
182185
params = params,
183186
}
187+
await.sleep(0.1)
184188
end
185189

186190
---@async
187191
function mt:awaitRequest(method, params)
188192
return await.wait(function (waker)
189-
self:request(method, params, function (result)
190-
if result == json.null then
191-
result = nil
192-
end
193-
waker(result)
193+
---@async
194+
await.call(function ()
195+
self:request(method, params, function (result)
196+
if result == json.null then
197+
result = nil
198+
end
199+
waker(result)
200+
end)
194201
end)
195202
end)
196203
end

script/proto/proto.lua

+27-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ local secretOption = {
156156
end
157157
}
158158

159-
function m.doMethod(proto)
159+
m.methodQueue = {}
160+
161+
function m.applyMethod(proto)
160162
logRecieve(proto)
161163
local method, optional = m.getMethodName(proto)
162164
local abil = m.ability[method]
@@ -202,6 +204,30 @@ function m.doMethod(proto)
202204
end)
203205
end
204206

207+
function m.applyMethodQueue()
208+
local queue = m.methodQueue
209+
m.methodQueue = {}
210+
local canceled = {}
211+
for _, proto in ipairs(queue) do
212+
if proto.method == '$/cancelRequest' then
213+
canceled[proto.params.id] = true
214+
end
215+
end
216+
for _, proto in ipairs(queue) do
217+
if not canceled[proto.id] then
218+
m.applyMethod(proto)
219+
end
220+
end
221+
end
222+
223+
function m.doMethod(proto)
224+
m.methodQueue[#m.methodQueue+1] = proto
225+
if #m.methodQueue > 1 then
226+
return
227+
end
228+
timer.wait(0, m.applyMethodQueue)
229+
end
230+
205231
function m.close(id, reason, message)
206232
local proto = m.holdon[id]
207233
if not proto then

0 commit comments

Comments
 (0)