Skip to content

Commit d85e56b

Browse files
committed
Пофиксил оффсеты на localstorage
1 parent c6c356a commit d85e56b

3 files changed

Lines changed: 24 additions & 14 deletions

File tree

examples/advanced/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ services:
2323
# env_file: .env
2424
environment:
2525
- LUA_ENV=development
26-
- DATA_PROVIDER=redis
26+
# - DATA_PROVIDER=redis
2727
- REDIS_HOST=redis
2828
# - REDIS_PORT=6379
2929
# - REDIS_PASS=qweqwe

examples/advanced/server.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,10 @@ app:use(function(err, _, res, next)
109109
return_error(res, 500, "Internal Server Error")
110110
end)
111111

112-
app:listen(3000)
112+
app:listen(3000, function()
113+
-- for i = 1, 40 do
114+
-- longpolling:publish_new("test", json_encode({incr = i}))
115+
-- end
116+
117+
print("Сервер запущен на порту 3000")
118+
end)

lua/long-polling/dataproviders/localtable.lua

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,33 @@ local MT = {}
22
MT.__index = MT
33

44
function MT:get_updates(channel, offset)
5-
local storage = self.storage
6-
local total = storage[channel] and #storage[channel] or 0
5+
local storage = self.storages[channel] or {}
6+
if not storage.total then return {}, 0 end -- несуществующий channel, данные не добавлялись
7+
8+
local sub_last_n = storage.total - offset
79
local updates = {}
8-
for i = offset + 1, total do
9-
updates[#updates + 1] = storage[channel][i]
10+
for i = #storage - sub_last_n + 1, #storage do
11+
updates[#updates + 1] = storage[i]
1012
end
11-
return updates, total
13+
return updates, storage.total
1214
end
1315

1416
function MT:add_update(channel, data)
15-
local storage = self.storage
16-
if not storage[channel] then storage[channel] = {} end
17-
storage[channel][#storage[channel] + 1] = data
18-
if #storage[channel] > self.opts.max_updates then
19-
table.remove(storage[channel], 1)
17+
self.storages[channel] = self.storages[channel] or {}
18+
19+
local storage = self.storages[channel]
20+
storage[#storage + 1] = data
21+
storage.total = (storage.total or 0) + 1
22+
if #storage > self.opts.max_updates then
23+
table.remove(storage, 1)
2024
end
21-
return #storage[channel]
25+
return storage.total
2226
end
2327

2428
function MT.new(opts)
2529
opts = opts or {}
2630
opts.max_updates = opts.max_updates or tonumber(os.getenv("CHANNEL_STORAGE_MAXSIZE")) or 30
27-
return setmetatable({storage = {}, opts = opts}, MT)
31+
return setmetatable({storages = {}, opts = opts}, MT)
2832
end
2933

3034
return MT

0 commit comments

Comments
 (0)