LearnNewsExamplesServices
Frontmatter
id6957
titleIntroduce `Neo.core.Base#observeConfig()` for Lifecycle-Aware Config Subscriptions
stateClosed
labels
enhancement
assigneestobiu
createdAtJul 6, 2025, 2:23 AM
updatedAtJul 6, 2025, 2:27 AM
githubUrlhttps://github.com/neomjs/neo/issues/6957
authortobiu
commentsCount0
parentIssuenull
subIssues[]
subIssuesCompleted0
subIssuesTotal0
blockedBy[]
blocking[]
closedAtJul 6, 2025, 2:27 AM

Introduce Neo.core.Base#observeConfig() for Lifecycle-Aware Config Subscriptions

Closed v10.0.0-beta.5 enhancement
tobiu
tobiu commented on Jul 6, 2025, 2:23 AM

Problem: Manually managing Neo.core.Config subscriptions within Neo.core.Base instances is error-prone. Developers must remember to explicitly unsubscribe, leading to potential memory leaks if cleanup is missed.

Solution: Add a new public method observeConfig(publisher, configName, fn) to Neo.core.Base. This method will:

  1. Accept a publisher (instance or ID), a configName (string), and a callback fn.
  2. Internally resolve the publisher to an instance.
  3. Retrieve the Neo.core.Config controller for the specified configName on the publisher.
  4. Call Neo.core.Config#subscribe using this.id (the subscribing Base instance's ID) as the subscription id.
  5. Store the returned cleanup function in a new private array (#configSubscriptionCleanups) on this (the subscribing Base instance).
  6. Integrate the cleanup of #configSubscriptionCleanups into Neo.core.Base#destroy(), ensuring automatic unsubscription.

Benefits:

  • Automatic Memory Leak Prevention: Subscriptions are automatically cleaned up when the subscribing Base instance is destroyed, eliminating a common source of leaks.
  • Simplified Developer Experience: Provides a single, intuitive, and consistent API for subscribing to configs on other instances.
  • Centralized Management: Consolidates subscription logic within Base, reducing boilerplate and potential errors in subclasses.
  • Discourages Self-Observation: Includes a JSDoc warning example, guiding developers towards using afterSet<ConfigName>() hooks for internal reactions.

Impact: Replaces direct calls to Neo.core.Config#subscribe in Neo.core.Base subclasses with the new observeConfig method.

tobiu assigned to @tobiu on Jul 6, 2025, 2:23 AM
tobiu added the enhancement label on Jul 6, 2025, 2:23 AM
tobiu referenced in commit b2c2e3f - "Introduce Neo.core.Base#observeConfig() for Lifecycle-Aware Config Subscriptions #6957" on Jul 6, 2025, 2:25 AM
tobiu closed this issue on Jul 6, 2025, 2:27 AM
tobiu cross-referenced by #6958 on Jul 6, 2025, 2:40 AM