Skip to content

let effects return a cleanup, and take an external scheduler - #52

Merged
hcollinsblox merged 7 commits into
mainfrom
effect-cleanup-and-external-scheduler
Nov 3, 2026
Merged

hcollinsblox merged 7 commits into
mainfrom
effect-cleanup-and-external-scheduler

Conversation

@hcollinsblox

@hcollinsblox hcollinsblox commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

createEffect can return a cleanup function. When SignalsEffectCleanup is 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 scheduleWork callback 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. isInitialized tracks the deferred first run, which has no sources to compare yet.

The merged diagnostics work adds Signals.setHooks for signal set and notification counts and for timing computed and effect runs. The optional effect debugName is 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.

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 MisterUncloaked left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drafted by my agent and reviewed by me.

One must-fix and a few should-fix items inline.

Comment thread modules/signals/src/Signals.lua Resolved
Comment thread modules/signals/src/Signals.lua Resolved
Comment thread modules/signals/src/Signals.lua Resolved
Comment thread modules/signals/src/Signals.lua Resolved
Comment thread modules/signals/src/Signals.lua Resolved
* 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 MisterUncloaked left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Comment thread modules/signals/src/Signals.lua Resolved

@MisterUncloaked MisterUncloaked left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@hcollinsblox
hcollinsblox merged commit 3d22921 into main Nov 3, 2026
1 check passed
@hcollinsblox
hcollinsblox deleted the effect-cleanup-and-external-scheduler branch November 3, 2026 08:28
hcollinsblox added a commit that referenced this pull request Nov 10, 2026
`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>

This branch was successfully deployed

1 active deployment
integration — 0ea45905 Deployed Oct 27, 2026 by hcollinsblox via ci #113
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants