From f7ce5f024d7f78d9174e46b512f8a4ebf981ba79 Mon Sep 17 00:00:00 2001 From: aaron <83140718+centau@users.noreply.github.com> Date: Tue, 1 Aug 2023 22:33:35 +0100 Subject: [PATCH] --- src/create.lua | 1 + test/mock.luau | 18 ++++++++++++++--- test/tests.luau | 54 +++++++++++++------------------------------------ todo.md | 2 ++ 4 files changed, 32 insertions(+), 43 deletions(-) diff --git a/src/create.lua b/src/create.lua index 065c3e8..f502069 100644 --- a/src/create.lua +++ b/src/create.lua @@ -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) diff --git a/test/mock.luau b/test/mock.luau index 8971674..0139bdc 100644 --- a/test/mock.luau +++ b/test/mock.luau @@ -21,6 +21,18 @@ local Instance = {} do Type: "Instance" } + local function deepclone(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 diff --git a/test/tests.luau b/test/tests.luau index 226bc21..adb79e1 100644 --- a/test/tests.luau +++ b/test/tests.luau @@ -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() diff --git a/todo.md b/todo.md index 8d36520..b00be91 100644 --- a/todo.md +++ b/todo.md @@ -37,6 +37,8 @@ untrack batch async/loading/suspense +define order with nested properties + ## version 1 ```lua