mirror of
https://github.com/purrgrammer/grimoire.git
synced 2026-04-12 16:37:06 +02:00
613bf8e2d7a7dc434b9baeffc372c32b8a413d0a
This commit implements a progressive batching strategy for connecting to
inbox relays, preventing the browser from being overwhelmed by concurrent
AUTH requests and relay subscriptions.
## Problem
When enabling inbox sync, the app would:
- Connect to ALL inbox relays simultaneously (could be 5-10+ relays)
- Each relay potentially requires AUTH (signing an event)
- All AUTH requests happen concurrently
- Browser becomes unresponsive during initial sync
- Poor UX with hanging/freezing during connection phase
Example: User with 8 inbox relays → 8 concurrent connections → 8 AUTH
signatures at once → browser hangs for several seconds
## Solution
### Progressive Batched Connection Strategy
**Initial Batch** (Immediate):
- Connect to first 3 relays right away
- Get messages flowing quickly from primary relays
- Users see messages within seconds
**Subsequent Batches** (Delayed):
- Connect to remaining relays 2 at a time
- 1.5 second delay between batches
- Allows AUTH requests to complete before next batch
- Browser stays responsive throughout
**Example Timeline**:
```
T+0s: Connect to relays 1-3 (batch 1)
T+1.5s: Connect to relays 4-5 (batch 2)
T+3.0s: Connect to relays 6-7 (batch 3)
T+4.5s: Connect to relay 8 (batch 4)
```
### Configuration
```typescript
const INITIAL_BATCH_SIZE = 3; // First batch (immediate)
const BATCH_SIZE = 2; // Subsequent batches
const BATCH_DELAY_MS = 1500; // 1.5s between batches
```
### Implementation Details
**Before**:
```typescript
// Connected to ALL relays at once
pool.subscription(allRelays, [filter], { eventStore })
```
**After**:
```typescript
// Batch 1: First 3 relays (immediate)
pool.subscription(firstBatch, [filter], { eventStore })
// Batch 2+: Remaining relays (delayed)
setTimeout(() => {
pool.subscription(nextBatch, [filter], { eventStore })
}, batchNumber * BATCH_DELAY_MS)
```
## Performance Impact
**Before**:
- ❌ Browser freezes during AUTH burst
- ❌ Delayed initial message display (waiting for all relays)
- ❌ Poor perceived performance
**After**:
- ✅ Browser stays responsive
- ✅ Messages appear within seconds (from first batch)
- ✅ Smooth progressive loading
- ✅ AUTH requests spread over time
- ✅ Better perceived performance
## Additional Improvements
- Converted remaining console.log to dmDebug/dmInfo/dmWarn
- More consistent debug logging throughout gift-wrap service
- Better visibility into batching progress
## Testing
- ✅ All 864 tests pass
- ✅ Build succeeds with no errors
- ✅ Batching logic verified
- ✅ Progressive connection strategy confirmed
## Files Changed
- `src/services/gift-wrap.ts` - Implemented progressive batching
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
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+Kto 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
Languages
TypeScript
98.9%
CSS
0.8%
JavaScript
0.3%