Skip to content

Commit

Permalink
get_unique_identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
luozhiya committed Feb 5, 2025
1 parent a0c6899 commit 914fbc4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 21 deletions.
60 changes: 39 additions & 21 deletions lua/fittencode/concurrency/promise.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,35 @@ function Promise.new(executor)
return self
end

-- 获取原始唯一标识符的方法
local function get_unique_identifier(tbl)
if type(tbl) ~= 'table' then
return
end
local mt = getmetatable(tbl)
local __tostring = mt and mt.__tostring
if __tostring then
mt.__tostring = nil -- 临时移除 __tostring 方法
end
local unique_id = tostring(tbl)
if __tostring then
mt.__tostring = __tostring -- 恢复 __tostring 方法
end
unique_id = unique_id:match('table: (0x.*)')
return unique_id
end

-- 输出格式如下
--[[```
Pending 状态
Promise {
State: <pending>
}
Fulfilled 状态
Promise {
State: <fulfilled>
Value: 42
}
Rejected 状态
Promise {
State: <rejected>
Reason: Error: Something went wrong
promise_reactions = { {}, {} },
state = "<fulfilled>",
value = {
a = {
q = 1
},
b = true
}
}
--]]
function Promise:__tostring()
Expand All @@ -87,13 +99,19 @@ function Promise:__tostring()
[PromiseState.FULFILLED] = '<fulfilled>',
[PromiseState.REJECTED] = '<rejected>'
}
local details = ''
if self.state == PromiseState.FULFILLED then
details = '\n Value: ' .. tostring(self.value)
elseif self.state == PromiseState.REJECTED then
details = '\n Reason: ' .. tostring(self.reason)
end
return string.format('Promise {\n State: %s%s\n}', assert(states[self.state]), details)
return 'Promise<' .. get_unique_identifier(self) .. '> = ' .. vim.inspect(self, {
process = function(item, path)
if type(item) ~= 'function' and item ~= getmetatable(self) then
-- print(vim.inspect(path), vim.inspect(item))
-- { "state", inspect.KEY } "state"
-- { "state" } 1
if #path == 1 and path[1] == 'state' then
return assert(states[self.state], 'Invalid state')
end
return item
end
end
})
end

-- Manually resolve the Promise with a value.
Expand Down
19 changes: 19 additions & 0 deletions lua/fittencode/fn.lua
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,24 @@ local function clamp(value, min, max)
return math.max(min, math.min(value, max))
end

-- 获取原始唯一标识符的方法
local function get_unique_identifier(tbl)
if type(tbl) ~= 'table' then
return
end
local mt = getmetatable(tbl)
local __tostring = mt and mt.__tostring
if __tostring then
mt.__tostring = nil -- 临时移除 __tostring 方法
end
local unique_id = tostring(tbl)
if __tostring then
mt.__tostring = __tostring -- 恢复 __tostring 方法
end
unique_id = unique_id:match('table: (0x.*)')
return unique_id
end

return {
clamp = clamp,
debounce = debounce,
Expand All @@ -285,4 +303,5 @@ return {
encode_uri_component = encode_uri_component,
math_type = math_type,
simple_math_type = simple_math_type,
get_unique_identifier = get_unique_identifier,
}

0 comments on commit 914fbc4

Please sign in to comment.