Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor Filter to an event-based API #1682

Closed
wants to merge 8 commits into from

Conversation

danisharora099
Copy link
Collaborator

@danisharora099 danisharora099 commented Oct 24, 2023

Problem

For the Filter API, we currently rely on:

  • passing (and internally maintaining) callbacks
  • first createSusbcription, and then call await subscribe(Decoder, Callback) which is not the most intuitive API

Solution

  • switch to an event-listener based model which is more intuitive for subscription-based activities
  • dispatch errors using the same event listener
  • cleans up our protocol implementation class

Notes

new API:

const subscription = waku.filter.createSusbcription([TestDecoder])
subscription.addEventListener(contentTopic, (event) => {
const {data, error} = event.detail;
   if (data) {
      // handle data here
   } else if (error) {
      // handle error here
   }
});

@github-actions
Copy link

github-actions bot commented Oct 24, 2023

size-limit report 📦

Path Size Loading time (3g) Running time (snapdragon) Total time
Waku core 79.34 KB (+0.06% 🔺) 1.6 s (+0.06% 🔺) 369 ms (+20.44% 🔺) 2 s
Waku Simple Light Node 243.14 KB (+0.13% 🔺) 4.9 s (+0.13% 🔺) 604 ms (+23.69% 🔺) 5.5 s
ECIES encryption 73.66 KB (+0.02% 🔺) 1.5 s (+0.02% 🔺) 355 ms (-25.32% 🔽) 1.9 s
Symmetric encryption 73.65 KB (+0.02% 🔺) 1.5 s (+0.02% 🔺) 199 ms (-13.23% 🔽) 1.7 s
DNS discovery 121.92 KB (0%) 2.5 s (0%) 276 ms (-7.79% 🔽) 2.8 s
Privacy preserving protocols 127.1 KB (+0.01% 🔺) 2.6 s (+0.01% 🔺) 461 ms (+92.6% 🔺) 3.1 s
Light protocols 77.4 KB (+0.72% 🔺) 1.6 s (+0.72% 🔺) 251 ms (+24.63% 🔺) 1.8 s
History retrieval protocols 75.78 KB (+0.07% 🔺) 1.6 s (+0.07% 🔺) 179 ms (-51.89% 🔽) 1.7 s
Deterministic Message Hashing 5.92 KB (0%) 119 ms (0%) 43 ms (-15.71% 🔽) 161 ms

@danisharora099 danisharora099 force-pushed the feat/filter-event-refactor branch from ccaaa23 to bd0ee9f Compare October 24, 2023 15:31
@danisharora099 danisharora099 force-pushed the feat/filter-event-refactor branch 4 times, most recently from ab8f18d to 9cd3c57 Compare November 3, 2023 12:53
@danisharora099 danisharora099 force-pushed the feat/filter-event-refactor branch 2 times, most recently from 67a268f to dee75fb Compare November 8, 2023 14:02
@danisharora099 danisharora099 marked this pull request as ready for review November 8, 2023 14:03
@danisharora099 danisharora099 requested a review from a team as a code owner November 8, 2023 14:03
})
);
} catch (e) {
log.error("Error decoding message", e);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This function and Filter.onRequest should be updated to allow listeners being notified about errors as I mentioned here https://github.com/waku-org/js-waku/pull/1682/files#r1387338041

Copy link
Collaborator Author

@danisharora099 danisharora099 Nov 27, 2023

Choose a reason for hiding this comment

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

Refactored others.
For onRequest, this can be tricky since it's an async operation and outside the general flow of subscription. Perhaps introducing a callback/event-emission from the Filter class should be incorporated.
We also cannot use the same event-emitter as Subscription, since its segregated by the pubsub topic.
I'd prefer to revisit this once autosharding is implemented.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's split it into a follow-up task

packages/tests/tests/filter/eventListener.spec.ts Outdated Show resolved Hide resolved
await subscription.unsubscribe(["/test/2/waku-filter"]);
try {
await subscription.unsubscribe(["/test/2/waku-filter"]);
} catch (error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should move away from try/catch in favour of return error enum

Copy link
Collaborator Author

@danisharora099 danisharora099 Dec 1, 2023

Choose a reason for hiding this comment

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

tracked part of #1725 and #1694

packages/tests/tests/filter/eventListener.spec.ts Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants