@@ -8,49 +8,44 @@ local effectScope = core.effectScope
88local test = suite .test
99
1010--- Creates a weak reference to a value.
11- --- @return Function to dereference the value.
12- local function weakRef <T >(value : T ): () -> T ?
13- local ref = setmetatable ({ value = value }, { __mode = "v" })
14- return function ()
15- return ref .value
16- end
11+ local function weakRef <T >(value : T ): { value : T ? }
12+ return setmetatable ({ value = value }, { __mode = "v" }) :: any
1713end
1814
19- --- Triggers garbage collection by allocating excessive memory .
15+ --- Triggers garbage collection by creating a large number of tables .
2016local function gc ()
2117 for _ = 1 , 1000 do
2218 local _ = table.create (100 , 0 )
2319 end
2420end
2521
2622--- In alien-signals, a computed value unlinks from dependencies when its last
27- --- parent scope is disposed. Calling a computed value outside of a scope links
28- --- it to dependencies, but the dependencies hold strong references to the
29- --- computed value, so it won't GC until called in a scope and disposed .
23+ --- parent scope is disposed. However, calling a computed value outside of a
24+ --- scope still links it to dependencies, which act as strong references.
25+ --- Computed values called in this way won't GC until unlinked from a scope.
3026---
3127--- This function releases a computed value by linking it to a scope and then
3228--- immediately closing it, causing alien-signals to unlink the computed value
33- --- and allow it to be garbage collected.
34- local function release (getter : () -> ())
29+ --- from its dependencies and allow it to be garbage collected.
30+ local function unlink (getter : () -> ())
3531 effectScope (function ()
3632 getter ()
3733 end )()
3834end
3935
4036test ("should release computed cache" , function ()
4137 local srcRef = weakRef ({})
42- local src = atom (srcRef ())
43-
38+ local src = atom (srcRef .value )
4439 local c = computed (function ()
4540 return src ()
4641 end )
4742
4843 c () -- cache src value
4944 src (nil ) -- release value
50- c = release (c ) -- release computed
45+ c = unlink (c ) -- release computed
5146
5247 gc ()
53- assert (srcRef () == nil , "should release computed cache" )
48+ assert (srcRef . value == nil , "should release computed cache" )
5449end )
5550
5651test ("should not release effect for atom" , function ()
@@ -61,7 +56,6 @@ test("should not release effect for atom", function()
6156 effectCalls += 1
6257 src ()
6358 end )
64-
6559 assert (effectCalls == 1 , "should call effect once" )
6660
6761 gc ()
0 commit comments