diff options
-rw-r--r-- | server/src/vm/source.lua | 16 | ||||
-rw-r--r-- | server/src/vm/value.lua | 32 | ||||
-rw-r--r-- | server/src/vm/vm.lua | 9 |
3 files changed, 32 insertions, 25 deletions
diff --git a/server/src/vm/source.lua b/server/src/vm/source.lua index 22132166..9528a67f 100644 --- a/server/src/vm/source.lua +++ b/server/src/vm/source.lua @@ -66,15 +66,15 @@ function mt:setVM() self.vm = vm end -function mt:isRemoved() - if not self.vm then - return true - end - if self.vm._removed then - self.vm = nil - return true +function mt:subscribe(value) + if self.uri == value.uri then + return end - return false + self.vm._subscribted[value] = true +end + +function mt:isRemoved() + return self.vm:isRemoved() end function mt:set(name, v) diff --git a/server/src/vm/value.lua b/server/src/vm/value.lua index 3cbccdd0..d747f6ce 100644 --- a/server/src/vm/value.lua +++ b/server/src/vm/value.lua @@ -23,7 +23,6 @@ local function create (tp, source, literal) _type = {}, _literal = literal, _info = {}, - _infoCheck = 10, }, mt) if type(tp) == 'table' then for i = 1, #tp do @@ -224,6 +223,8 @@ function mt:mergeValue(value) if not self._info[info.source] then self._info[#self._info+1] = info self._info[info.source] = true + + info.source:subscribe(self) end end if value._meta then @@ -247,7 +248,7 @@ function mt:addInfo(tp, source, ...) if self._info[source] then return end - if not source or not source.isRemoved then + if not source or not source.subscribe then return end local info = { @@ -258,21 +259,7 @@ function mt:addInfo(tp, source, ...) self._info[#self._info+1] = info self._info[info.source] = true - -- 清除无效的info - --if #self._info > self._infoCheck then - -- for i = #self._info, 1, -1 do - -- local info = self._info[i] - -- local src = info.source - -- if src:isRemoved() then - -- table.remove(self._info, i) - -- self._info[src] = nil - -- end - -- end - -- self._infoCheck = #self._info * 2 - -- if self._infoCheck < 10 then - -- self._infoCheck = 10 - -- end - --end + source:subscribe(self) end function mt:eachInfo(callback) @@ -285,6 +272,17 @@ function mt:eachInfo(callback) return nil end +function mt:cleanInfo() + for i = #self._info, 1, -1 do + local info = self._info[i] + local source = info.source + if source:isRemoved() then + self._info[source] = nil + table.remove(self._info, i) + end + end +end + function mt:setFunction(func) self._func = func end diff --git a/server/src/vm/vm.lua b/server/src/vm/vm.lua index e6343b4b..841b09ca 100644 --- a/server/src/vm/vm.lua +++ b/server/src/vm/vm.lua @@ -1137,8 +1137,16 @@ function mt:eachSource(callback) end end +function mt:isRemoved() + return self._removed == true +end + function mt:remove() self._removed = true + for value in pairs(self._subscribted) do + self._subscribted[value] = nil + value:cleanInfo() + end end local function compile(ast, lsp, uri) @@ -1149,6 +1157,7 @@ local function compile(ast, lsp, uri) env = nil, lsp = lsp, uri = uri or '', + _subscribted = {}, }, mt) -- 创建初始环境 |