diff options
| author | ruki <[email protected]> | 2022-07-23 00:41:45 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-07-23 00:41:45 +0800 |
| commit | 08f1f473ba31ac3720c3ecf3cc5afac04947eab6 (patch) | |
| tree | bc35b7bdcb7147b2c820203a51a08d6d5b879a2c | |
| parent | 8cb0768ed29c7ea78cd27fdd3febc66c2d2ddce9 (diff) | |
add tests
| -rw-r--r-- | tests/modules/fwatcher/watchdir.lua | 11 | ||||
| -rw-r--r-- | xmake/core/base/fwatcher.lua | 38 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/base/fwatcher.lua | 9 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/base/process.lua | 8 |
4 files changed, 54 insertions, 12 deletions
diff --git a/tests/modules/fwatcher/watchdir.lua b/tests/modules/fwatcher/watchdir.lua new file mode 100644 index 000000000..d73d7ff9f --- /dev/null +++ b/tests/modules/fwatcher/watchdir.lua @@ -0,0 +1,11 @@ +import("core.base.fwatcher") + +function main(watchdir) + fwatcher.add(watchdir) + while true do + local ok, event = fwatcher.wait(-1) + if ok > 0 then + print(event) + end + end +end diff --git a/xmake/core/base/fwatcher.lua b/xmake/core/base/fwatcher.lua index 00ee35718..17ee5cafe 100644 --- a/xmake/core/base/fwatcher.lua +++ b/xmake/core/base/fwatcher.lua @@ -37,7 +37,7 @@ fwatcher._close = fwatcher._close or fwatcher.close -- get cdata of fwatcher function _instance:cdata() local cdata = self._CDATA - if not cdata then + if not cdata and not self._CLOSED then cdata = fwatcher._open() self._CDATA = cdata end @@ -51,17 +51,37 @@ end -- add watch directory, e.g. {recursion = true} function _instance:add(watchdir, opt) + + -- ensure opened + local ok, errors = self:_ensure_opened() + if not ok then + return false, errors + end + + -- add watchdir + opt = opt or {} + return fwatcher._add(self:cdata(), watchdir, opt.recursion or false) end -- remove watch directory function _instance:remove(watchdir) + + -- ensure opened + local ok, errors = self:_ensure_opened() + if not ok then + return false, errors + end + + -- remove watchdir + opt = opt or {} + return fwatcher._remove(self:cdata(), watchdir) end -- wait event -- -- @param timeout the timeout -- --- @return ok, status +-- @return ok, event -- function _instance:wait(timeout) @@ -73,16 +93,16 @@ function _instance:wait(timeout) -- wait events local result = -1 - local status_or_errors = nil + local event_or_errors = nil if scheduler:co_running() then - result, status_or_errors = scheduler:poller_waitproc(self, timeout or -1) + result, event_or_errors = scheduler:poller_waitfs(self, timeout or -1) else - result, status_or_errors = fwatcher._wait(self:cdata(), timeout or -1) + result, event_or_errors = fwatcher._wait(self:cdata(), timeout or -1) end - if result < 0 and status_or_errors then - status_or_errors = string.format("%s: %s", self, status_or_errors) + if result < 0 and event_or_errors then + event_or_errors = string.format("%s: %s", self, event_or_errors) end - return result, status_or_errors + return result, event_or_errors end -- close instance @@ -106,6 +126,7 @@ function _instance:close() ok = fwatcher._close(self:cdata()) if ok then self._CDATA = nil + self._CLOSED = true end return ok end @@ -125,6 +146,7 @@ setmetatable(_instance, { __gc = function(self) if self._CDATA and fwatcher._close(self._CDATA) then self._CDATA = nil + self._CLOSED = true end end }) diff --git a/xmake/core/sandbox/modules/import/core/base/fwatcher.lua b/xmake/core/sandbox/modules/import/core/base/fwatcher.lua index 910c033a4..bf8710b00 100644 --- a/xmake/core/sandbox/modules/import/core/base/fwatcher.lua +++ b/xmake/core/sandbox/modules/import/core/base/fwatcher.lua @@ -41,6 +41,15 @@ function sandbox_core_base_fwatcher.remove(watchdir) end end +-- wait event +function sandbox_core_base_fwatcher.wait(timeout) + local ok, event_or_errors = fwatcher.wait(timeout) + if ok < 0 and event_or_errors then + raise(event_or_errors) + end + return ok, event_or_errors +end + -- return module return sandbox_core_base_fwatcher diff --git a/xmake/core/sandbox/modules/import/core/base/process.lua b/xmake/core/sandbox/modules/import/core/base/process.lua index 2f9141fc1..884ae9ebe 100644 --- a/xmake/core/sandbox/modules/import/core/base/process.lua +++ b/xmake/core/sandbox/modules/import/core/base/process.lua @@ -31,11 +31,11 @@ sandbox_core_base_process._subprocess = sandbox_core_base_process._subprocess or -- wait subprocess function sandbox_core_base_instance.wait(proc, timeout) - local ok, status, errors = proc:_wait(timeout) - if errors then - raise(errors) + local ok, status_or_errors = proc:_wait(timeout) + if ok < 0 and status_or_errors then + raise(status_or_errors) end - return ok, status + return ok, status_or_errors end -- kill subprocess |
