Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions protocol/messenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ type Messenger struct {

started bool
paused atomic.Bool
backgroundMode atomic.Bool // true when the app UI is not visible; gates background history syncs
quit chan struct{}
ctx context.Context
cancel context.CancelFunc
Expand Down Expand Up @@ -563,6 +564,22 @@ func (m *Messenger) ToBackground() {
}
}

// SetAppBackground is called by the Android/iOS layer when the app UI moves
// to background (background=true) or returns to foreground (background=false).
// It gates automatic mailserver history syncs: syncs triggered by connection
// change or storenode availability are deferred while backgrounded, and
// executed immediately on returning to foreground.
//
// Unlike SetPaused, this does NOT pause Waku transport or data-sync — Waku
// continues running so push notifications keep working.
func (m *Messenger) SetAppBackground(background bool) {

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.

@xAlisher just a few lines above we already have ToForeground()/ToBackground() functions. ToForeground already calls asyncRequestAllHistoricMessages() and as I see you're now adding a second, parallel lifecycle pair (via SetAppBackground). Why not reuse?

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.

Also we should think if shouldSync() should be called when doing m.asyncRequestAllHistoricMessages() in ToForeground().

m.backgroundMode.Store(background)
if !background {
// Returning to foreground: run any deferred history sync now.
m.asyncRequestAllHistoricMessages()
}
}

func (m *Messenger) SetPaused(paused bool) {
m.paused.Store(paused)
if m.pushNotificationClient != nil {
Expand Down Expand Up @@ -821,8 +838,10 @@ func (m *Messenger) handleConnectionChange(online bool) {
m.shouldPublishContactCode = false
}

// Start fetching messages from store nodes
if online {
// Start fetching messages from store nodes.
// Skip when backgrounded: the sync will run in SetAppBackground(false)
// when the app returns to foreground.
if online && !m.backgroundMode.Load() {
m.asyncRequestAllHistoricMessages()
}

Expand Down
6 changes: 5 additions & 1 deletion protocol/messenger_mailserver_cycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,11 @@ func (m *Messenger) checkForStorenodeCycleSignals() {
if ok {
signal.SendStoreNodeAvailable(&ms)
}
m.asyncRequestAllHistoricMessages()
// Skip history sync when backgrounded; SetAppBackground(false)
// will trigger it when the app returns to foreground.
if !m.backgroundMode.Load() {
Comment on lines +162 to +164

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Instead of skipping store node requests, why don't we just completely disconnect from storenodes and stop the StorenodeCycle?

m.asyncRequestAllHistoricMessages()
}
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions services/ext/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ext

import (
"context"
"errors"
"time"
Comment on lines 3 to 6

"github.com/libp2p/go-libp2p/core/peer"
Expand Down Expand Up @@ -1046,6 +1047,14 @@ func (api *PublicAPI) DeleteActivityCenterNotifications(ctx context.Context, ids
return err
}

func (api *PublicAPI) SetAppBackground(background bool) error {
if api.service.messenger == nil {
return errors.New("messenger not initialized")
}
api.service.messenger.SetAppBackground(background)
return nil
}

func (api *PublicAPI) RequestAllHistoricMessages() (*protocol.MessengerResponse, error) {
return api.service.messenger.RequestAllHistoricMessages(false)
}
Expand Down
Loading