Skip to content

Conversation

@mondoreale
Copy link
Contributor

@mondoreale mondoreale commented Jan 20, 2026

Summary

Resolve circular dependencies in the SDK's dependency injection (DI) container by using token-based injection instead of direct class references. This eliminates the need for delay() wrappers for Resends and Subscriber classes.

Important

This change is necessary for future ESM compatibility. The current delay(() => Class) approach relies on CommonJS module semantics where circular imports can be partially resolved at runtime. In ESM, module bindings are evaluated differently and delay() won't be sufficient to break circular dependency cycles.

Token-based injection provides a clean solution that works in both module systems.

Changes

  • Add tokens.ts with Symbol-based injection tokens for Resends and Subscriber
  • Update setupTsyringe.ts to register Resends and Subscriber classes with the DI container using tokens (with ContainerScoped lifecycle)
  • Replace @scoped(Lifecycle.ContainerScoped) decorators with @injectable() for Resends and Subscriber classes (lifecycle is now specified during token registration)
  • Update MessagePipelineFactory to use token injection for Resends instead of delay(() => Resends)
  • Update SubscriberKeyExchange to use token injection for Subscriber (previously injected directly without delay())
  • Update StreamrClient to resolve Subscriber and Resends using tokens instead of class references

Limitations and future improvements

  • Only Resends and Subscriber are currently registered via tokens; other classes with circular dependency potential could be migrated to this pattern in the future
  • The delay() wrapper is still used for StreamRegistry and GroupKeyManager in MessagePipelineFactory; these could be converted to token injection for consistency

@github-actions github-actions bot added the sdk label Jan 20, 2026
@mondoreale mondoreale force-pushed the resolve-circular-deps-in-sdk branch from 3f5566d to 1bb9784 Compare January 20, 2026 22:43
@mondoreale mondoreale marked this pull request as ready for review January 20, 2026 23:25
@mondoreale mondoreale requested a review from Copilot January 20, 2026 23:35
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR resolves circular dependencies in the SDK by implementing Symbol-based dependency injection tokens for the Resends and Subscriber classes. This approach eliminates the need for delay() wrappers and provides better ESM compatibility for future migration.

Changes:

  • Introduces Symbol-based injection tokens for Resends and Subscriber classes
  • Updates dependency injection to use tokens instead of delay() wrappers
  • Converts class imports to type-only imports where the class is now injected via token

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
packages/sdk/src/tokens.ts Defines Symbol-based injection tokens for Resends and Subscriber
packages/sdk/src/setupTsyringe.ts Registers Resends and Subscriber classes with their respective tokens using ContainerScoped lifecycle
packages/sdk/src/subscribe/MessagePipelineFactory.ts Replaces delay(() => Resends) with token-based injection and adds type-only import for Resends
packages/sdk/src/encryption/SubscriberKeyExchange.ts Updates Subscriber injection to use token instead of direct class reference and converts to type-only import
packages/sdk/src/StreamrClient.ts Updates resolution of Resends and Subscriber to use tokens instead of class references

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@mondoreale mondoreale merged commit f531e37 into main Jan 21, 2026
29 checks passed
@mondoreale mondoreale deleted the resolve-circular-deps-in-sdk branch January 21, 2026 09:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants