@@ -2122,6 +2122,47 @@ describe('Morph', function()
21222122 end )
21232123 end )
21242124
2125+ it (
2126+ ' executes do_after_render callbacks even when update is called during update phase' ,
2127+ function ()
2128+ with_buf ({}, function ()
2129+ local callback_executed = false
2130+ local capture_ctx
2131+
2132+ --- @param ctx morph.Ctx< {}, { open : boolean , updated : boolean } >
2133+ local function ComponentWithCallback (ctx )
2134+ if ctx .phase == ' mount' then
2135+ ctx .state = { open = false , updated = false }
2136+ capture_ctx = ctx
2137+ end
2138+ -- Only schedule callback and call update on the first update when open becomes true
2139+ if ctx .state .open and ctx .phase == ' update' and not ctx .state .updated then
2140+ ctx :do_after_render (function () callback_executed = true end )
2141+ -- Mark as updated to prevent infinite loop
2142+ ctx .state .updated = true
2143+ ctx :update (ctx .state )
2144+ end
2145+ return { ctx .state .open and ' open' or ' closed' }
2146+ end
2147+
2148+ local r = Morph .new (0 )
2149+ r :mount (h (ComponentWithCallback ))
2150+ assert .are .same (' closed' , get_text ())
2151+ assert .is_false (callback_executed )
2152+
2153+ -- Now trigger the update that causes the issue
2154+ capture_ctx .state .open = true
2155+ capture_ctx :refresh ()
2156+
2157+ -- The callback should have been executed
2158+ assert .is_true (
2159+ callback_executed ,
2160+ ' do_after_render callback should be executed even when ctx:update is called during update phase'
2161+ )
2162+ end )
2163+ end
2164+ )
2165+
21252166 it (' does not re-render when update called during mount phase' , function ()
21262167 with_buf ({}, function ()
21272168 local render_count = 0
0 commit comments