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:
- Accept a
publisher (instance or ID), a configName (string), and a callback fn.
- Internally resolve the
publisher to an instance.
- Retrieve the
Neo.core.Config controller for the specified configName on the publisher.
- Call
Neo.core.Config#subscribe using this.id (the subscribing Base instance's ID) as the subscription id.
- Store the returned cleanup function in a new private array (
#configSubscriptionCleanups) on this (the subscribing Base instance).
- 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.
Problem: Manually managing
Neo.core.Configsubscriptions withinNeo.core.Baseinstances 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)toNeo.core.Base. This method will:publisher(instance or ID), aconfigName(string), and a callbackfn.publisherto an instance.Neo.core.Configcontroller for the specifiedconfigNameon thepublisher.Neo.core.Config#subscribeusingthis.id(the subscribingBaseinstance's ID) as the subscriptionid.#configSubscriptionCleanups) onthis(the subscribingBaseinstance).#configSubscriptionCleanupsintoNeo.core.Base#destroy(), ensuring automatic unsubscription.Benefits:
Baseinstance is destroyed, eliminating a common source of leaks.Base, reducing boilerplate and potential errors in subclasses.afterSet<ConfigName>()hooks for internal reactions.Impact: Replaces direct calls to
Neo.core.Config#subscribeinNeo.core.Basesubclasses with the newobserveConfigmethod.