summaryrefslogtreecommitdiffstats
path: root/aconf/persistence/defer.lua
blob: d7f33d8e2eaebdbfa56d284d33f0ddfe0fa23482 (plain)
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 address = require('aconf.path.address')


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 = address.parent(path)
      end
   end

   self:commit()
end

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


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