Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/binder/src/Shared/Binder.Loading.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@

local require = require(script.Parent.loader).load(script)

local Binder = require("Binder")
local BinderTestUtils = require("BinderTestUtils")
local Jest = require("Jest")
local JestUtils = require("JestUtils")
local Signal = require("Signal")

local describe = Jest.Globals.describe
local expect = Jest.Globals.expect
local it = Jest.Globals.it
local jest = Jest.Globals.jest

local NOT_STARTED_WARNING_DELAY_SECONDS = 30

describe("Binder load abandonment", function()
it("does not bind a class when the instance is removed while the constructor yields", function()
Expand Down Expand Up @@ -49,3 +54,27 @@ describe("Binder load abandonment", function()
controller:Destroy()
end)
end)

describe("Binder not started warning", function()
it("warns only once the binder has gone unstarted for the full delay", function()
local controller = BinderTestUtils.setup()
JestUtils.useFakeTimers()

local warnSpy = jest.spyOn(jest.globalEnv, "warn").mockImplementation(function() end)
controller.maid:GiveTask(function()
warnSpy.mockRestore()
end)

local binder = controller.maid:Add(Binder.new(controller.uniqueTag(), BinderTestUtils.noopConstructor))
binder:Init()
local warning = string.format("Binder %q is not loaded. Call :Start() on it!", binder:GetTag())

jest.advanceTimersByTime(5 * 1000)
expect(warnSpy).never.toHaveBeenCalledWith(warning)

jest.advanceTimersByTime((NOT_STARTED_WARNING_DELAY_SECONDS - 5) * 1000)
expect(warnSpy).toHaveBeenCalledWith(warning)

controller:Destroy()
end)
end)
4 changes: 3 additions & 1 deletion src/binder/src/Shared/Binder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ local Observable = require("Observable")
local Promise = require("Promise")
local Signal = require("Signal")

local NOT_STARTED_WARNING_DELAY_SECONDS = 30

local Binder = {}
Binder.__index = Binder
Binder.ClassName = "Binder"
Expand Down Expand Up @@ -180,7 +182,7 @@ function Binder:Init(...)
self._args = {}
end

self._maid._warning = task.delay(5, function()
self._maid._warning = task.delay(NOT_STARTED_WARNING_DELAY_SECONDS, function()
warn(string.format("Binder %q is not loaded. Call :Start() on it!", self._tagName))
end)
end
Expand Down
Loading