Repository navigation
Expand file tree
/
Copy pathchanged_documents.lua
More file actions
64 lines (55 loc) · 1.68 KB
/
Copy pathchanged_documents.lua
File metadata and controls
64 lines (55 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
local DataStorage = require("datastorage")
local util = require("util")
local logger = require("logger")
local utils = require("utils")
local M = {}
function M.path()
return DataStorage:getDataDir() .. "/changed_documents.lua"
end
local function write(changed_docs)
local track_path = M.path()
local ok, err = util.writeToFile(utils.serialize_table(changed_docs), track_path, true, true, true)
if not ok then
logger.warn("AnnotationSync: Failed to write changed documents file: " .. track_path .. " (" .. tostring(err) .. ")")
end
end
function M.get_pending()
local count = 0
local track_path = M.path()
local ok, changed_docs = pcall(dofile, track_path)
if ok and type(changed_docs) == "table" then
for _ in pairs(changed_docs) do count = count + 1 end
end
return count, changed_docs
end
function M.has_pending()
local count, _ = M.get_pending()
return count > 0
end
function M.add(file)
local track_path = M.path()
-- Load existing table or create new
local changed_docs = {}
local ok, loaded = pcall(dofile, track_path)
if ok and type(loaded) == "table" then
changed_docs = loaded
end
if file and type(file) == "string" then
changed_docs[file] = true
write(changed_docs)
end
end
function M.remove_by_path(file)
if not file then return end
local track_path = M.path()
local ok, changed_docs = pcall(dofile, track_path)
if ok and type(changed_docs) == "table" and changed_docs[file] then
changed_docs[file] = nil
write(changed_docs)
end
end
function M.remove(document)
local file = document and document.file
M.remove_by_path(file)
end
return M