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 if not game then
script = (require :: any) "test/wrap-require" script = (require :: any) "test/wrap-require"
Instance = require("test/mock").Instance Instance = require("test/mock").Instance
typeof = require("test/mock").typeof
end end
local throw = require(script.Parent.throw) local throw = require(script.Parent.throw)

View file

@ -21,6 +21,18 @@ local Instance = {} do
Type: "Instance" 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 attempt to mimic roblox engine's method of userdata proxy to actual instance data
proxy can gc independantly of 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_userdata = (Instance.new("") :: any) :: userdata
local clone_data = getdata(clone_userdata) local clone_data = getdata(clone_userdata)
table.clear(clone_data) for i, v in next, deepclone(data) do
for i, v in next, data do clone_data[i] = v
clone_data[i] = type(v) == "table" and table.clone(v) or v
end end
return clone_userdata return clone_userdata
end end

View file

@ -949,58 +949,32 @@ TEST("spring()", function()
end end
end) end)
TEST("Event", function() TEST("Events", function()
local create = vide.create 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" do CASE "Connect event"
local connected = false local connected = false
local event = (Signal.new() :: any) :: RBXScriptSignal & { Fire: any } local val = Thing {
Signal = function(newval)
local val = create "IntValue" {
Changed = event,
[Event.Changed] = function(newval)
connected = true connected = true
CHECK(newval == 1) CHECK(newval == 1)
end end
} :: IntValue }
testkit.print2(getmetatable(val))
CHECK(not connected) CHECK(not connected)
val.Value = 1; event:Fire(val.Value) val.Value = 1; val.Signal:Fire(val.Value)
vide.step(1/60)
CHECK(connected) CHECK(connected)
end 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) end)
TEST("Changed", function() TEST("Changed", function()

View file

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