summaryrefslogtreecommitdiffstatshomepage
path: root/plugins/coro-wrapper/init.lua
blob: aab2683baf372572f2f7f5ef665b9d088c0c7ce7 (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
local exports = {}
exports.name = "creationix/coro-wrapper"
exports.version = "1.0.0-1"
exports.homepage = "https://github.com/luvit/lit/blob/master/deps/coro-wrapper.lua"
exports.description = "An adapter for applying decoders to coro-streams."
exports.tags = {"coro", "decoder", "adapter"}
exports.license = "MIT"
exports.author = { name = "Tim Caswell" }

function exports.reader(read, decode)
  local buffer = ""
  return function ()
    while true do
      local item, extra = decode(buffer)
      if item then
        buffer = extra
        return item
      end
      local chunk = read()
      if not chunk then return end
      buffer = buffer .. chunk
    end
  end,
  function (newDecode)
    decode = newDecode
  end
end

function exports.writer(write, encode)
  return function (item)
    if not item then
      return write()
    end
    return write(encode(item))
  end,
  function (newEncode)
    encode = newEncode
  end
end

return exports