Skip to content

refactor: lazily initialize singleton instance#133

Closed
OutdatedGuy wants to merge 1 commit into
mainfrom
refactor/lazy-singleton
Closed

refactor: lazily initialize singleton instance#133
OutdatedGuy wants to merge 1 commit into
mainfrom
refactor/lazy-singleton

Conversation

@OutdatedGuy

Copy link
Copy Markdown
Owner

No description provided.

@OutdatedGuy OutdatedGuy requested a review from Copilot May 22, 2026 13:11
@OutdatedGuy OutdatedGuy self-assigned this May 22, 2026
@OutdatedGuy OutdatedGuy added the enhancement New feature or request label May 22, 2026

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.

Pull request overview

Refactors InternetConnection’s singleton initialization from eager static initialization to a lazy initialization approach, so the instance is created only when first accessed.

Changes:

  • Updated factory InternetConnection() to lazily create and cache the singleton instance.
  • Replaced the eagerly initialized static final _instance with a nullable static backing field.

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

Comment on lines 139 to +140
/// The singleton instance of [InternetConnection].
static final _instance = InternetConnection.createInstance();
static InternetConnection? _instance;
Comment on lines 65 to +71
/// Returns an instance of [InternetConnection].
///
/// This is a singleton class, meaning that there is only one instance of it.
factory InternetConnection() => _instance;
factory InternetConnection() {
_instance ??= InternetConnection.createInstance();
return _instance!;
}
@OutdatedGuy

Copy link
Copy Markdown
Owner Author

Dart's static and top-level variables are naturally lazy, hence this is useless!

@OutdatedGuy OutdatedGuy deleted the refactor/lazy-singleton branch May 22, 2026 13:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants