Skip to content

Fix shared effect ownership and gradient disposal - #894

Merged
jonobr1 merged 4 commits into
devfrom
codex/fix-shared-effect-disposal
Nov 17, 2026
Merged

jonobr1 merged 4 commits into
devfrom
codex/fix-shared-effect-disposal

Conversation

@jonobr1

@jonobr1 jonobr1 commented Oct 31, 2026 •

Copy link
Copy Markdown
Owner

Disposing a shape can currently destroy a gradient or texture still used by another shape, and identical fill/stroke assignments dispose the same effect twice. Gradient disposal also leaves renderer references and external listeners behind.

Track effect assignments independently of scene membership. Shape disposal detaches that shape's listeners and disposes each effect once, only when its last assignment is released. Apply the same tracking to image/sprite textures and image-sequence collections. Two.release() preserves other shapes' effect listeners without disposing effect resources. Recursive release defers factory-created scene effects until shapes have released their assignments, and skips effects that still have users. SVG gradients and textures can move out of disposed renderers into surviving definitions. Image-sequence collection callbacks are detached before their ownership records are released, so later collection edits cannot restore released listeners.

Gradient disposal now calls parent cleanup, clears cached renderer and stop elements, and preserves internal change tracking so subsequent rendering recreates the gradient correctly. Update API documentation and TypeScript comments with the ownership contract.

Ownership details:

  • Unrendered shapes and shapes removed from a scene retain their assignments until disposal or release.
  • Replacing an effect assignment does not dispose the old effect; the caller can dispose it when no longer needed.
  • Direct effect.dispose() is explicit cleanup and should be called only when all users are finished.

Validation:

  • Build, ESLint, and TypeScript checks pass.
  • Browser disposal regressions pass, including nested factory-created gradients shared across instances, release followed by disposal, texture collection edits after release, and gradient reuse. With WebGL contexts disabled, all 393 disposal assertions pass; WebGL variants detect unavailable contexts.
  • Full browser suite: 1,309 passing assertions and 10 image-comparison failures matching the baseline failure names and messages.
  • New regression coverage against the original implementation produces 53 failing assertions.
  • Documentation generation and site build pass.
  • Generated bundles and unrelated documentation changes are excluded.

Closes #847.

Comment thread tests/suite/dispose.js Fixed

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Unresolved disposal bugs can disrupt gradients shared across instances and reattach released texture listeners, while the unguarded WebGL test fails in browsers without WebGL support.

3 open issues
What changed in this PR

This PR addresses #847 by tracking shared effect ownership across Two.js shapes and making gradient disposal safe to repeat while preserving reuse.

Changes:

  • Track effect assignments independently of scene membership and dispose effects only after their last assignment is released.
  • Clear gradient renderer caches and external listeners while preserving internal change tracking.
  • Add disposal regression coverage and update API documentation and TypeScript comments.
File Description
wiki/​docs/​two/​README.md Document shared-effect release behavior.
wiki/​docs/​text/​README.md Document text effect ownership.
wiki/​docs/​shapes/​points/​README.md Document points effect ownership.
wiki/​docs/​path/​README.md Document path effect ownership.
wiki/​docs/​effects/​sprite/​README.md Clarify sprite texture release.
wiki/​docs/​effects/​image/​README.md Clarify image texture release.
wiki/​docs/​effects/​image-sequence/​README.md Document shared texture disposal.
wiki/​docs/​effects/​gradient/​README.md Document repeatable disposal and reuse.
tests/​suite/​dispose.js Cover shared effects, release, and gradient reuse.
src/​utils/​effects.js Centralize assignment tracking and effect cleanup.
src/​two.js Scope effect release to individual objects.
src/​two.d.ts Update release ownership comments.
src/​text.js Track and release text effect assignments.
src/​text.d.ts Update text disposal comments.
src/​shapes/​points.js Track and release points effect assignments.
src/​shapes/​points.d.ts Update points disposal comments.
src/​path.js Track and release path effect assignments.
src/​path.d.ts Update path disposal comments.
src/​effects/​sprite.js Track sprite texture ownership.
src/​effects/​sprite.d.ts Update sprite disposal comments.
src/​effects/​image.js Track image texture ownership.
src/​effects/​image.d.ts Update image disposal comments.
src/​effects/​image-sequence.js Track texture ownership through collection edits.
src/​effects/​image-sequence.d.ts Update sequence disposal comments.
src/​effects/​gradient.js Perform parent cleanup and reset rendering state for reuse.
src/​effects/​gradient.d.ts Update gradient disposal contract comments.

Review effort: Balanced

Comment thread src/two.js Resolved
Comment thread src/two.js Resolved
Comment thread tests/suite/dispose.js Resolved
@jonobr1
jonobr1 requested a review from jonofyi November 17, 2026 05:41
@jonobr1
jonobr1 marked this pull request as ready for review November 17, 2026 05:43
@jonobr1
jonobr1 merged commit cf1748f into dev Nov 17, 2026
6 checks passed
@jonobr1
jonobr1 deleted the codex/fix-shared-effect-disposal branch November 17, 2026 05:43
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.

Gradient disposal is not idempotent and shared fill and stroke can throw

4 participants