This commit is contained in:
aaron 2023-08-01 22:33:35 +01:00
parent 5b270f0f36
commit f7ce5f024d
4 changed files with 32 additions and 43 deletions

View file

@ -5,6 +5,7 @@
if not game then
script = (require :: any) "test/wrap-require"
Instance = require("test/mock").Instance
typeof = require("test/mock").typeof
end
local throw = require(script.Parent.throw)

View file

@ -21,6 +21,18 @@ local Instance = {} do
Type: "Instance"
}
local function deepclone<T>(template: T & {}): T
local t = table.clone(template :: {}) :: {}
for i, v in next, t do
if type(v) == "table" then
t[i] = deepclone(v)
end
end
return t :: T & {}
end
--[[
attempt to mimic roblox engine's method of userdata proxy to actual instance data
proxy can gc independantly of actual instance data
@ -115,10 +127,10 @@ local Instance = {} do
local clone_userdata = (Instance.new("") :: any) :: userdata
local clone_data = getdata(clone_userdata)
table.clear(clone_data)
for i, v in next, data do
clone_data[i] = type(v) == "table" and table.clone(v) or v
for i, v in next, deepclone(data) do
clone_data[i] = v
end
return clone_userdata
end

View file

@ -949,58 +949,32 @@ TEST("spring()", function()
end
end)
TEST("Event", function()
TEST("Events", function()
local create = vide.create
local source = vide.source
local function Thing(props)
local instance = Instance.new("Thing") :: any
instance.Signal = (Signal.new() :: any) :: RBXScriptSignal & { Fire: any }
testkit.print2(getmetatable(instance))
return create(instance)(props)
end
do CASE "Connect event"
local connected = false
local event = (Signal.new() :: any) :: RBXScriptSignal & { Fire: any }
local val = create "IntValue" {
Changed = event,
[Event.Changed] = function(newval)
local val = Thing {
Signal = function(newval)
connected = true
CHECK(newval == 1)
end
} :: IntValue
}
testkit.print2(getmetatable(val))
CHECK(not connected)
val.Value = 1; event:Fire(val.Value)
vide.step(1/60)
val.Value = 1; val.Signal:Fire(val.Value)
CHECK(connected)
end
do CASE "Bind connection to state"
local countA = 0
local countB = 0
local listener, set = source(function() countA += 1 end :: () -> ()?)
local event = (Signal.new() :: any) :: RBXScriptSignal & { Fire: any }
local int = create "IntValue" {
Changed = event,
[Event.Changed] = listener
} :: IntValue
int.Value = 1; event:Fire(int.Value)
int.Value = 2; event:Fire(int.Value)
CHECK(countA == 2)
set(function() return function() countB += 1 end end)
int.Value = 3; event:Fire(int.Value)
int.Value = 4; event:Fire(int.Value)
CHECK(countA == 2)
CHECK(countB == 2)
set(nil)
int.Value = 5; event:Fire(int.Value)
CHECK(countA == 2)
CHECK(countB == 2)
end
do CASE "Always return same object for a given index"
CHECK(Event.Test == Event.Test)
end
end)
TEST("Changed", function()

View file

@ -37,6 +37,8 @@ untrack
batch
async/loading/suspense
define order with nested properties
## version 1
```lua