Alejandro Gómez dc4345a64b perf: Make NIP-17 inbox sync on-demand for optimal login performance
This commit eliminates automatic inbox initialization on login, preventing
unwanted network requests and heavy I/O operations. Inbox sync now only
activates when users explicitly enable it.

## Problem

When logging in, the app would automatically:
- Initialize gift wrap service immediately
- Auto-enable inbox sync without user consent
- Load encrypted content from Dexie
- Wait up to 1 second for cache readiness
- Fetch inbox relay lists from network
- Subscribe to gift wrap events
- Open persistent relay connections

This caused:
- App hangs during login (network/IO blocking)
- Unwanted network activity before user opts in
- Poor performance on initial load
- Unnecessary resource consumption when DMs not needed

## Solution

### 1. On-Demand Initialization (useAccountSync.ts)
**Before**: Auto-init and auto-enable on every login
**After**: Watch settings$ and only init when user enables

```typescript
// Only initialize when user explicitly enables inbox sync
const settingsSub = giftWrapService.settings$.subscribe((settings) => {
  if (settings.enabled && giftWrapService.userPubkey !== pubkey) {
    giftWrapService.init(pubkey, signer);
  }
});
```

### 2. Early Exit for Disabled State (gift-wrap.ts)
**Before**: Always loaded cache and relays, then checked enabled flag
**After**: Check enabled FIRST, exit early if disabled

```typescript
async init(pubkey: string, signer: ISigner | null) {
  // Set basic properties
  this.userPubkey = pubkey;
  this.signer = signer;

  // Early exit if disabled (prevents expensive operations)
  if (!this.settings$.value.enabled) {
    return;
  }

  // Only do expensive operations when enabled
  await getStoredEncryptedContentIds();
  await this.waitForCacheReady();
  this.loadInboxRelays();
  // ...
}
```

### 3. Updated Documentation
- Clarified on-demand initialization flow
- Updated lifecycle documentation with performance notes
- Changed "auto-enable" section to "on-demand" section

## Performance Impact

**Login Performance**:
-  No automatic Dexie reads
-  No cache waiting (up to 1s saved)
-  No network requests for inbox relays
-  No relay subscriptions until needed
-  Instant login when DMs not needed

**User Control**:
- Users explicitly opt-in via InboxViewer toggle
- Clear UI feedback about enabling inbox sync
- No surprise network activity

**When Enabled**:
- Full functionality identical to before
- All optimizations from previous commits preserved

## Testing

-  All 864 tests pass
-  Build succeeds with no errors
-  Verified on-demand initialization flow
-  Confirmed no auto-init on login

## Files Changed

- `src/hooks/useAccountSync.ts` - Watch settings, init only when enabled
- `src/services/gift-wrap.ts` - Early exit if disabled, expose userPubkey
- `src/components/InboxViewer.tsx` - Updated comments
- `docs/gift-wrap-architecture.md` - Updated flow and lifecycle docs

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-16 17:04:05 +01:00
2025-12-18 15:46:02 +01:00
2025-12-19 12:49:29 +01:00
2025-12-13 15:06:05 +01:00
2025-12-22 20:40:16 +00:00
2025-12-20 14:25:40 +01:00
2026-01-14 19:24:37 +01:00
2025-12-14 16:50:16 +01:00

Grimoire

A Nostr protocol explorer and developer tool with a tiling window manager interface.

Features

  • Tiling Windows - Each window is a Nostr "app" (profile viewer, event feed, NIP docs, etc.)
  • Command Palette - Unix-style commands via Cmd+K to open apps and navigate
  • Multi-workspace - Virtual desktops with independent layouts
  • Real-time - Reactive event subscriptions with automatic updates

Stack

React 19, TypeScript, Vite, TailwindCSS, Jotai, Dexie, Applesauce

Getting Started

npm install
npm run dev

Scripts

Command Description
npm run dev Start dev server
npm run build Build for production
npm test Run tests in watch mode
npm run lint Lint code
npm run format Format code

License

MIT

Description
No description provided
Readme MIT 14 MiB
Languages
TypeScript 98.9%
CSS 0.8%
JavaScript 0.3%