Skip to content

Commit d87d886

Browse files
committed
Leverage synchronous cache adapter to load events in one go in useSubscribe hook
1 parent f255a07 commit d87d886

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

.changeset/fresh-pugs-jump.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@nostr-dev-kit/ndk-mobile": patch
3+
---
4+
5+
Leverage synchronous cache adapter to load events in one go in useSubscribe hook

ndk-mobile/src/hooks/subscribe.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import '@bacons/text-decoder/install';
22
import { createStore } from 'zustand/vanilla';
33
import { useStore } from 'zustand';
4-
import { NDKEvent, NDKFilter, NDKRelaySet, NDKSubscription, NDKSubscriptionOptions, wrapEvent } from '@nostr-dev-kit/ndk';
4+
import { NDKEvent, NDKFilter, NDKRelaySet, NDKSubscription, NDKSubscriptionOptions } from '@nostr-dev-kit/ndk';
55
import { useCallback, useEffect, useMemo, useRef } from 'react';
66
import { useNDK } from './ndk.js';
77
import { useNDKSession } from '../stores/session/index.js';
8-
98
/**
109
* Extends NDKEvent with a 'from' method to wrap events with a kind-specific handler
1110
*/
@@ -229,16 +228,10 @@ export const useSubscribe = <T extends NDKEvent>(
229228
return;
230229
}
231230

232-
// If we need to convert the event, we do so
233-
if (opts?.wrap) event = wrapEvent<T>(event);
234-
235231
event.once("deleted", () => {
236232
storeInstance.removeEventId(id);
237233
});
238234

239-
// If conversion failed, we bail
240-
if (!event) return;
241-
242235
storeInstance.addEvent(event as T);
243236
eventIds.current.set(id, event.created_at!);
244237
},
@@ -268,7 +261,10 @@ export const useSubscribe = <T extends NDKEvent>(
268261
subscription.on('closed', handleClosed);
269262

270263
storeInstance.setSubscription(subscription);
271-
subscription.start();
264+
const cachedEvents = subscription.start(false);
265+
if (cachedEvents) {
266+
for (const event of cachedEvents) handleEvent(event);
267+
}
272268

273269
return () => {
274270
if (storeInstance.subscriptionRef) {

0 commit comments

Comments
 (0)