diff options
Diffstat (limited to 'lua/mason-core/async')
| -rw-r--r-- | lua/mason-core/async/control.lua | 58 | ||||
| -rw-r--r-- | lua/mason-core/async/init.lua | 12 |
2 files changed, 46 insertions, 24 deletions
diff --git a/lua/mason-core/async/control.lua b/lua/mason-core/async/control.lua index 57aa88db..2388bce5 100644 --- a/lua/mason-core/async/control.lua +++ b/lua/mason-core/async/control.lua @@ -4,8 +4,12 @@ local a = require "mason-core.async" local Condvar = {} Condvar.__index = Condvar -function Condvar.new() - return setmetatable({ handles = {} }, Condvar) +function Condvar:new() + ---@type Condvar + local instance = {} + setmetatable(instance, self) + instance.handles = {} + return instance end ---@async @@ -31,8 +35,12 @@ end local Permit = {} Permit.__index = Permit -function Permit.new(semaphore) - return setmetatable({ semaphore = semaphore }, Permit) +function Permit:new(semaphore) + ---@type Permit + local instance = {} + setmetatable(instance, self) + instance.semaphore = semaphore + return instance end function Permit:forget() @@ -42,7 +50,7 @@ function Permit:forget() if semaphore.permits > 0 and #semaphore.handles > 0 then semaphore.permits = semaphore.permits - 1 local release = table.remove(semaphore.handles, 1) - release(Permit.new(semaphore)) + release(Permit:new(semaphore)) end end @@ -51,8 +59,13 @@ local Semaphore = {} Semaphore.__index = Semaphore ---@param permits integer -function Semaphore.new(permits) - return setmetatable({ permits = permits, handles = {} }, Semaphore) +function Semaphore:new(permits) + ---@type Semaphore + local instance = {} + setmetatable(instance, self) + instance.permits = permits + instance.handles = {} + return instance end ---@async @@ -65,7 +78,7 @@ function Semaphore:acquire() end) end - return Permit.new(self) + return Permit:new(self) end ---@class OneShotChannel @@ -75,12 +88,14 @@ end local OneShotChannel = {} OneShotChannel.__index = OneShotChannel -function OneShotChannel.new() - return setmetatable({ - has_sent = false, - value = nil, - condvar = Condvar.new(), - }, OneShotChannel) +function OneShotChannel:new() + ---@type OneShotChannel + local instance = {} + setmetatable(instance, self) + instance.has_sent = false + instance.value = nil + instance.condvar = Condvar:new() + return instance end function OneShotChannel:is_closed() @@ -108,12 +123,15 @@ end ---@field is_closed boolean local Channel = {} Channel.__index = Channel -function Channel.new() - return setmetatable({ - condvar = Condvar.new(), - buffer = nil, - is_closed = false, - }, Channel) + +function Channel:new() + ---@type Channel + local instance = {} + setmetatable(instance, self) + instance.condvar = Condvar:new() + instance.buffer = nil + instance.is_closed = false + return instance end function Channel:close() diff --git a/lua/mason-core/async/init.lua b/lua/mason-core/async/init.lua index 63d4ec94..56d86170 100644 --- a/lua/mason-core/async/init.lua +++ b/lua/mason-core/async/init.lua @@ -6,8 +6,12 @@ local exports = {} local Promise = {} Promise.__index = Promise -function Promise.new(resolver) - return setmetatable({ resolver = resolver, has_resolved = false }, Promise) +function Promise:new(resolver) + local instance = {} + setmetatable(instance, self) + instance.resolver = resolver + instance.has_resolved = false + return instance end ---@param success boolean @@ -27,7 +31,7 @@ function Promise:__call(callback) end local function await(resolver) - local ok, value = co.yield(Promise.new(resolver)) + local ok, value = co.yield(Promise:new(resolver)) if not ok then error(value[1], 0) end @@ -156,7 +160,7 @@ end ---@param suspend_fns async fun()[] ---@param mode '"first"' | '"all"' local function wait(suspend_fns, mode) - local channel = require("mason-core.async.control").OneShotChannel.new() + local channel = require("mason-core.async.control").OneShotChannel:new() if #suspend_fns == 0 then return end |
