Skip to content

Commit

Permalink
Merge pull request #64 from nezuo/no-yield-before-callbacks
Browse files Browse the repository at this point in the history
Throw error if beforeSave or beforeClose yield
  • Loading branch information
nezuo authored Aug 21, 2024
2 parents d1c85de + 3f489b1 commit 324e1bb
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
## Unreleased Changes
* Files now use the `.luau` extension instead of `.lua`. ([#61])
* Switched wally realm to `shared`. This means Lapis can be used as a shared or server dependency. ([#62])
* `beforeClose` and `beforeSave` now throw an error if they yield. For more information, see the PR. ([#64])

[#61]: https://github.com/nezuo/lapis/pull/61
[#62]: https://github.com/nezuo/lapis/pull/62
[#64]: https://github.com/nezuo/lapis/pull/64

## 0.3.2 - August 6, 2024
* Added `Collection:read` to view a document's data without editing or session locking it. ([#59])
Expand Down
7 changes: 4 additions & 3 deletions src/Document.luau
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local freezeDeep = require(script.Parent.freezeDeep)
local Promise = require(script.Parent.Parent.Promise)
local noYield = require(script.Parent.noYield)

local function runCallback(document, name, callback)
if callback == nil then
Expand All @@ -9,7 +10,7 @@ local function runCallback(document, name, callback)
document.callingCallback = name

return Promise.new(function(resolve, reject)
local ok, message = pcall(callback)
local ok, message = pcall(noYield, callback)

document.callingCallback = nil

Expand Down Expand Up @@ -123,7 +124,7 @@ end
:::
:::warning
If the beforeSave callback errors, the returned promise will reject and the data will not be saved.
If the beforeSave callback yields or errors, the returned promise will reject and the data will not be saved.
:::
@return Promise<()>
Expand Down Expand Up @@ -165,7 +166,7 @@ end
If called again, it will return the promise from the original call.
:::warning
If the beforeSave or beforeClose callbacks error, the returned promise will reject and the data will not be saved.
If the beforeSave or beforeClose callbacks yield or error, the returned promise will reject and the data will not be saved.
:::
@return Promise<()>
Expand Down
24 changes: 24 additions & 0 deletions src/Document.test.luau
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,18 @@ return function(x)
end)

x.nested("Document:beforeSave", function()
x.test("throws when yielding", function(context)
local document = context.lapis.createCollection("collection", defaultOptions()):load("document"):expect()

document:beforeSave(function()
task.wait()
end)

shouldThrow(function()
document:save():expect()
end, "beforeSave callback threw error: thread is not yieldable")
end)

x.test("throws when setting twice", function(context)
local document = context.lapis.createCollection("collection", defaultOptions()):load("document"):expect()

Expand Down Expand Up @@ -299,6 +311,18 @@ return function(x)
end)

x.nested("Document:beforeClose", function()
x.test("throws when yielding", function(context)
local document = context.lapis.createCollection("collection", defaultOptions()):load("document"):expect()

document:beforeClose(function()
task.wait()
end)

shouldThrow(function()
document:close():expect()
end, "beforeClose callback threw error: thread is not yieldable")
end)

x.test("throws when setting twice", function(context)
local document = context.lapis.createCollection("collection", defaultOptions()):load("document"):expect()

Expand Down
11 changes: 11 additions & 0 deletions src/noYield.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
local function noYield(callback)
for _ in
function()
callback()
end
do
break
end
end

return noYield

0 comments on commit 324e1bb

Please sign in to comment.