summaryrefslogtreecommitdiffstats
path: root/aconf/persistence/defer.lua
blob: fa10805477f138bec09938434428480fc4e0e65d (plain) (blame)
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
--[[
Copyright (c) 2012-2015 Kaarle Ritvanen
See LICENSE file for license details
--]]

local object = require('aconf.object')
local super = object.super

local pth = require('aconf.path')


local DeferringCommitter = object.class(
   require('aconf.transaction.base').Transaction
)

function DeferringCommitter:init(backend)
   super(self, DeferringCommitter):init(backend)
   self.defer_paths = {}
   self.committed = true
end

function DeferringCommitter:defer(path) self.defer_paths[path] = true end

function DeferringCommitter:_set_multiple(mods)
   super(self, DeferringCommitter):_set_multiple(mods)

   if not self.committed then return end
   self.committed = false

   for _, mod in ipairs(mods) do
      local path, value = table.unpack(mod)
      while path > '/' do
	 if self.defer_paths[path] then return end
	 path = pth.parent(path)
      end
   end

   self:commit()
end

function DeferringCommitter:commit()
   super(self, DeferringCommitter):commit()
   self.committed = true
end


return DeferringCommitter(require('aconf.persistence'))