nostr-dev-kit / ndk

Nostr Development Kit with outbox-model support
https://nostr-dev-kit.github.io/ndk/
MIT License
366 stars 99 forks source link

NDK-Svelte Store Subscription should support fine-grained event handling #227

Open Pleb5 opened 6 months ago

Pleb5 commented 6 months ago

It is impossible to reliably attach a fine-grained callback (subscription.on(...)=>{} ) on ndk svelte store subscriptions.

Purpose: storeSubscriptions are great to handle a collection of Svelte components but sometimes it is necessary to react with extra logic on the newest unique event that arrived. E.g. render a list of components of which one might belong to the user and the app has to react based on this one event that may be present or not.

Cause: store.subscription is undefined unitl startSubscription() is called. It is not possible to attach a callback before the subsciption start therefore. If the callback is attached AFTER startSubscription() then some events will be missed by the callback by that time.

Fix: Everything is already given in the storeSubscribe() method to assign value to store.subscription (filters, opts). The assignment code in startSubscription() should be moved in this method.