summaryrefslogtreecommitdiff
path: root/lua/clever_f/acquisition_service.lua
blob: 5f15fa1ef2a8254eafabe36c472ed113d7ddbb08 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
local domain = require("clever_f.domain")

local M = {}
local AcquisitionRequest = {}
local AcquisitionService = {}
local TemporaryResourceScope = {}
AcquisitionService.__index = AcquisitionService
M.AcquisitionRequest = AcquisitionRequest
M.AcquisitionService = AcquisitionService
M.TemporaryResourceScope = TemporaryResourceScope
M.RepeatedDirection = {
  SAME = "same",
}

local request_records = setmetatable({}, { __mode = "k" })
local service_records = setmetatable({}, { __mode = "k" })
local scope_records = setmetatable({}, { __mode = "k" })

local function fail(message, level)
  error(message, (level or 1) + 1)
end

local function normalize_macro_state(value)
  if type(value) == "table" and not domain.MacroState.is(value) then
    value = value.register
  end
  return domain.MacroState.new(value)
end

local request_metatable = {
  __index = function(request, key)
    local method = AcquisitionRequest[key]
    if method ~= nil then
      return method
    end
    return request_records[request][key]
  end,
  __newindex = function()
    fail("AcquisitionRequest values are immutable", 2)
  end,
  __tostring = function(request)
    return "acquisition-request:" .. request_records[request].descriptor.value
  end,
  __metatable = "clever_f.acquisition_service.AcquisitionRequest",
}

function AcquisitionRequest.new(descriptor, context, position, count, macro_state)
  if AcquisitionRequest.is(descriptor) then
    return descriptor
  end
  if type(descriptor) == "table" and not domain.Descriptor.is(descriptor) then
    local options = descriptor
    descriptor = options.descriptor
    context = options.context
    position = options.position or options.origin
    count = options.count
    macro_state = options.macro_state
  end

  local request = setmetatable({}, request_metatable)
  request_records[request] = {
    descriptor = domain.Descriptor.from_string(descriptor),
    context = domain.ModeContext.from_full_mode(context),
    position = domain.Position.coerce(position),
    count = domain.Count.new(count),
    macro_state = normalize_macro_state(macro_state),
    repeated_direction = M.RepeatedDirection.SAME,
  }
  return request
end

function AcquisitionRequest.is(value)
  return type(value) == "table" and request_records[value] ~= nil
end

function AcquisitionRequest:to_table()
  return {
    descriptor = self.descriptor.value,
    context = self.context.key,
    position = self.position:to_table(),
    count = self.count.value,
    macro_register = self.macro_state.register,
    repeated_direction = self.repeated_direction,
  }
end

local scope_metatable = {
  __index = function(scope, key)
    local method = TemporaryResourceScope[key]
    if method ~= nil then
      return method
    end
    return scope_records[scope][key]
  end,
  __newindex = function()
    fail("TemporaryResourceScope values are read-only", 2)
  end,
  __metatable = "clever_f.acquisition_service.TemporaryResourceScope",
}

function TemporaryResourceScope.new(request)
  if not AcquisitionRequest.is(request) then
    fail("temporary resource scope requires an AcquisitionRequest", 2)
  end
  local scope = setmetatable({}, scope_metatable)
  scope_records[scope] = {
    request = request,
    active = true,
  }
  return scope
end

function TemporaryResourceScope.is(value)
  return type(value) == "table" and scope_records[value] ~= nil
end

function TemporaryResourceScope:release()
  local record = scope_records[self]
  if record == nil then
    fail("temporary resource scope is invalid", 2)
  end
  record.active = false
end

local function normalize_options(options, dependencies)
  if AcquisitionService.is(options) and dependencies == nil then
    return options
  end
  if type(options) ~= "table" then
    fail("AcquisitionService options must be a table", 3)
  end
  if options.host ~= nil then
    if dependencies ~= nil then
      fail("AcquisitionService dependencies must be part of its options", 3)
    end
    return options
  end
  local result = {}
  for key, value in pairs(dependencies or {}) do
    result[key] = value
  end
  result.host = options
  return result
end

function AcquisitionService.new(options, dependencies)
  options = normalize_options(options, dependencies)
  if AcquisitionService.is(options) then
    return options
  end
  if type(options.host) ~= "table" then
    fail("AcquisitionService host must be a table", 2)
  end
  local service = setmetatable({}, AcquisitionService)
  service_records[service] = {
    host = options.host,
    last_scope = nil,
    started_scope_count = 0,
  }
  return service
end

function AcquisitionService.is(value)
  return type(value) == "table" and service_records[value] ~= nil
end

function AcquisitionService:request(descriptor, context, position, count, macro_state)
  return AcquisitionRequest.new(descriptor, context, position, count, macro_state)
end

function AcquisitionService:start_temporary_scope(request)
  request = AcquisitionRequest.new(request)
  local record = service_records[self]
  local scope = TemporaryResourceScope.new(request)
  record.last_scope = scope
  record.started_scope_count = record.started_scope_count + 1
  return scope
end

function AcquisitionService:last_temporary_scope()
  return service_records[self].last_scope
end

function AcquisitionService:started_scope_count()
  return service_records[self].started_scope_count
end

function AcquisitionService:acquire(descriptor, context, position, count, macro_state)
  local request = self:request(descriptor, context, position, count, macro_state)
  self:start_temporary_scope(request)
  return request
end

function M.new(options, dependencies)
  return AcquisitionService.new(options, dependencies)
end

setmetatable(M, {
  __call = function(_, options, dependencies)
    return AcquisitionService.new(options, dependencies)
  end,
})

return M