Repository navigation
let effects return a cleanup, and take an external scheduler - #52
Conversation
Two additions to `createEffect`: An effect may return a function, which runs before the next run of the body and once more on dispose. Subscriptions set up in an effect previously had to be torn down by tracking the dispose separately. A cleanup that errors is reported and swallowed, so it cannot stop the effect re-running. `scheduleWork` hands the timing of every run to the caller, including the first, which is otherwise immediate. A host with its own work queue needs its effects ordered against the rest of that queue rather than run the moment they are created. `isInitialized` tracks whether the body has run at all, since a deferred first run reaches `processNotification` with no sources to compare versions against. Neither argument changes behaviour when it is absent. Co-authored-by: Cursor <cursoragent@cursor.com>
MisterUncloaked
left a comment
There was a problem hiding this comment.
Drafted by my agent and reviewed by me.
One must-fix and a few should-fix items inline.
* let a host time signal, computed and effect work A runtime driving these signals wants to know which computeds and effects its frame time went into, and it cannot see that from outside. `setHooks` takes the measurements it cares about, so nothing here needs to know about the profiler reading them. The `begin` hooks return whatever the host measures in and it comes back to the matching `end`, so this file never decides what a timestamp is. They are bound as upvalues rather than read out of a table per call, so a hook that is not installed costs what a constant did. Effects take a debug name, which reaches the profiler and the cleanup error report -- both of which could otherwise only say that some effect was slow or broken. Co-authored-by: Cursor <cursoragent@cursor.com> * Avoid per-observer profiling checks and cover hook lifecycle --------- Co-authored-by: Cursor <cursoragent@cursor.com>
MisterUncloaked
left a comment
There was a problem hiding this comment.
Drafted by my agent and reviewed by me.
This branch now also carries #49 (Signals.setHooks and the effect debugName), so it lands on main through this PR. The #49 feedback looks addressed here. Can we update the description to cover the hooks, and add debugName to the README signature at modules/signals/README.md:200?
MisterUncloaked
left a comment
There was a problem hiding this comment.
Drafted by my agent and reviewed by me.
Blocker: analyze still fails, now on the return nil lines added to the signals-experimental tests (createProxy.test.lua:44,87,108,159,195,227 and onDisposed.test.lua:25,55): "Expected this to be '() -> ()', but got 'nil'". The variadic type already accepts a body that returns nothing, so can we drop those 8 lines? With them removed, luau-lsp analyze is clean locally. The two asks from my last review are also still open: debugName in the README signature at modules/signals/README.md:200, and a description that covers Signals.setHooks and the effect debugName.
`Signals.lua` has required `Packages.SignalsFlags` since #52 gated effect cleanup behind `SignalsEffectCleanup`, but the package's manifest never said so. Inside this repo's flat test place every package is a sibling, so nothing noticed. Installed by Rotriever, a package only sees the dependencies it declares, so a consumer pinning Signals by git revision fails at that require. Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Co-authored-by: Hugh Collins <65657661+hcollinsblox@users.noreply.github.com>
createEffectcan return a cleanup function. WhenSignalsEffectCleanupis enabled, the cleanup runs before the next effect run and on disposal. A cleanup that errors is reported with a traceback and does not stop the effect from rerunning. With the flag disabled, returned values retain their previous behavior.An optional
scheduleWorkcallback controls the timing of every effect run, including the first. It must defer its work until after the current notification pass. This lets a host order effects within its own queue.isInitializedtracks the deferred first run, which has no sources to compare yet.The merged diagnostics work adds
Signals.setHooksfor signal set and notification counts and for timing computed and effect runs. The optional effectdebugNameis passed to the effect timing hook and included in cleanup error warnings.Existing effect behavior is preserved when the new arguments and cleanup flag are absent.