Skip to content

Commit 717c28e

Browse files
authored
Allow onUpdate to return cleanup method (#8)
1 parent 0a4a21a commit 717c28e

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

froact.lua

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ end
3939

4040
local function newTemplate(roact: any, unpureByDefault: boolean?)
4141
type CleanupMethod = () -> ()
42-
type UpdateMethod<Props> = (props: Props) -> ()
42+
type UpdateMethod<Props> = (props: Props) -> CleanupMethod
4343
type Constructor<Props> = (
4444
name: string,
4545
parent: Instance,
@@ -63,20 +63,28 @@ local function newTemplate(roact: any, unpureByDefault: boolean?)
6363
end
6464
end
6565
self.updateCallbacks = {}
66+
self.updateCleanups = {}
6667
self.cleanup = f(hostKey, hostParent, function(callback)
67-
callback(self.props)
68+
table.insert(self.updateCleanups, callback(self.props))
6869
table.insert(self.updateCallbacks, f)
6970
end)
7071
end
7172
function Component:render()
7273
return roact.createFragment()
7374
end
7475
function Component:didUpdate()
76+
for _, cleanup in self.updateCleanups do
77+
cleanup()
78+
end
79+
self.updateCleanups = {}
7580
for _, callback in self.updateCallbacks do
76-
callback(self.props)
81+
table.insert(self.updateCleanups, callback(self.props))
7782
end
7883
end
7984
function Component:willUnmount()
85+
for _, cleanup in self.updateCleanups do
86+
cleanup()
87+
end
8088
self.cleanup()
8189
end
8290
return function(props: Props, children)

0 commit comments

Comments
 (0)