-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscribe.luau
46 lines (42 loc) · 1.12 KB
/
subscribe.luau
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
local FENV_KEY = "74709086-5669-43dc-a7d1-195d71ae0464"
local function callBody(cellData, body, recompute)
cellData.seen = {}
getfenv(0)[FENV_KEY] = cellData
body()
getfenv(0)[FENV_KEY] = nil
cellData.seen = nil
for root in cellData.roots do
root.subscriptions[cellData] = true
table.insert(cellData.unsubscribes, function()
root.subscriptions[cellData] = nil
end)
end
end
local function recompute(d)
for _, unsubscribe in d.unsubscribes do
unsubscribe()
end
table.clear(d.roots)
table.clear(d.unsubscribes)
callBody(d, d.body, recompute)
end
local function subscribe<T>(body: () -> ())
local subscription = newproxy(true)
local cellData = {
proxy = subscription, -- Store a reference to the proxy so it doesn't get garbage collected.
body = body,
recompute = recompute,
dependents = setmetatable({}, { __newindex = function() end }),
unsubscribes = {},
roots = {},
}
getmetatable(subscription)._data = cellData
callBody(cellData, body, recompute)
return function()
for _, unsubscribe in cellData.unsubscribes do
unsubscribe()
end
table.clear(cellData.unsubscribes)
end
end
return subscribe