在给一个调色板小玩具做 tile 选择器(移动端应用,接触屏 UI 之前先用 lua main.lua 加一串脚本化的决定做无界面测试)。有两处不对劲,我分不清是一个 bug 还是两个。
- 带
want = "amber"(调色板里第三个)的拒绝,下一个给我的是 x=2 color=green。普通的拒绝没问题。
- 最后的 history 输出里每一条的 color 和 x 都一样,都是最后一次 proposal 的(
x=3 color=green,一遍遍重复)。不过 grid 最终是对的。
resume 的打印显示决定都正确传进去了,所以我不觉得是 resume 差一位(见 coroutine.resume 文档)。是 history 读 grid 读晚了吗?
协程如下。
function Proposer.new(palette, start_x)
local proposal = { x = start_x, color = nil }
return coroutine.create(function()
local x = start_x
while true do
local c = palette:next()
proposal.x, proposal.color = x, c
local decision = coroutine.yield(proposal)
if decision.accept then
x = x + 1
elseif decision.want then
palette:seek(decision.want)
end
end
end)
end
再贴 Session:step 里记录 history 的那一段。
table.insert(self.history, { tile = self.pending, decision = decision })
if decision.accept then
self.grid[self.pending.x] = self.pending.color.name
end
amber 是在哪丢的?
从英语翻译·查看原文Building the tile picker for a palette toy (mobile app, testing headless with lua main.lua and a scripted list of decisions before the touch UI). Two things look wrong and I can't tell if it's one bug or two.
- A rejection with
want = "amber" (third in the palette) gets me x=2 color=green next. Plain rejections are fine.
- The history dump at the end prints the same color and x for every entry, the last proposal's (
x=3 color=green, over and over). The grid still ends up right.
The resume prints show the decisions arriving correctly, so I doubt it's an off-by-one resume (see the coroutine.resume docs). Is history reading the grid late?
The coroutine:
function Proposer.new(palette, start_x)
local proposal = { x = start_x, color = nil }
return coroutine.create(function()
local x = start_x
while true do
local c = palette:next()
proposal.x, proposal.color = x, c
local decision = coroutine.yield(proposal)
if decision.accept then
x = x + 1
elseif decision.want then
palette:seek(decision.want)
end
end
end)
end
And the part of Session:step that records history:
table.insert(self.history, { tile = self.pending, decision = decision })
if decision.accept then
self.grid[self.pending.x] = self.pending.color.name
end
Where does the amber get lost?