Skip to content

[Bug] Duplicate Zustand store subscriptions created on StrictMode double-invoke — listeners fire twice #638

Description

@RUKAYAT-CODER

Overview

src/services/syncService.ts and src/services/pushNotifications.ts register Zustand store subscriptions in constructors or module-level initialization code. In React development mode (StrictMode), effects and constructors may run twice. If these services are initialized inside React components or useEffects without proper cleanup, a second subscription is registered without the first being removed, causing event handlers to fire twice for every state change (double sync, double notification handling).

Specifications

Features:

  • Each service maintains a single active store subscription at any time
  • initialize() checks for existing subscription before registering new one
  • destroy() unsubscribes all listeners
  • StrictMode double-invoke does not result in duplicate listeners

Tasks:

  • In SyncService and PushNotificationService, store subscription return values
  • Add guard: if (this.subscription) return; this.subscription = store.subscribe(...)
  • Implement destroy() calling this.subscription?.(); this.subscription = null;
  • Ensure services initialized in useEffect return cleanup calling destroy()
  • Add unit test confirming single subscription after double initialize() call

Impacted Files:

  • src/services/syncService.ts
  • src/services/pushNotifications.ts
  • App.tsx or service initialization

Acceptance Criteria

  • Double `initialize()" call results in exactly 1 active subscription
  • Store state change triggers event handler exactly once
  • destroy() removes subscription cleanly
  • Unit test confirms event handler called once (not twice) after double init

Metadata

Metadata

Assignees

Labels

Stellar WaveIssues in the Stellar wave programbugSomething isn't working

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions