Skip to content

Commit

Permalink
Use nanoevents instead of eventemitter3 (#9)
Browse files Browse the repository at this point in the history
Reduces bundle size.
  • Loading branch information
robknight authored Sep 23, 2024
1 parent 068fc2a commit a4d22f0
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 36 deletions.
3 changes: 1 addition & 2 deletions packages/app-connector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
"@parcnet-js/podspec": "workspace:*",
"@pcd/gpc": "^0.0.8",
"@pcd/pod": "^0.1.7",
"eventemitter3": "^5.0.1",
"json-bigint": "^1.0.0",
"nanoevents": "^9.0.0",
"valibot": "^0.42.0"
},
"devDependencies": {
Expand Down
22 changes: 11 additions & 11 deletions packages/app-connector/src/api_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import type * as p from "@parcnet-js/podspec";
import type { GPCBoundConfig, GPCProof, GPCRevealedClaims } from "@pcd/gpc";
import type { PODEntries } from "@pcd/pod";
import { POD } from "@pcd/pod";
import { EventEmitter } from "eventemitter3";
import { type Emitter, createNanoEvents } from "nanoevents";
import type { ParcnetRPCConnector } from "./rpc_client.js";

type SubscriptionEvents = {
update: (result: POD[]) => void;
};

/**
* A Subscription object is returned to the caller when a subscription is
* created. It allows the caller to attach event listeners to the subscription
Expand All @@ -19,13 +23,13 @@ import type { ParcnetRPCConnector } from "./rpc_client.js";
* first creating the subscription, before any updates are available.
*/
export class Subscription<E extends p.EntriesSchema> {
#emitter: EventEmitter;
#emitter: Emitter<SubscriptionEvents>;
#query: p.PodSpec<E>;
#api: ParcnetPODWrapper;

constructor(
query: p.PodSpec<E>,
emitter: EventEmitter,
emitter: Emitter<SubscriptionEvents>,
api: ParcnetPODWrapper
) {
this.#emitter = emitter;
Expand All @@ -37,18 +41,14 @@ export class Subscription<E extends p.EntriesSchema> {
return this.#api.query(this.#query);
}

on(event: "update", callback: (result: POD[]) => void): void {
this.#emitter.on(event, callback);
}

off(event: "update", callback: (result: POD[]) => void): void {
this.#emitter.off(event, callback);
on(event: "update", callback: (result: POD[]) => void): () => void {
return this.#emitter.on(event, callback);
}
}

export class ParcnetPODWrapper {
#api: ParcnetRPCConnector;
#subscriptionEmitters: Map<string, EventEmitter>;
#subscriptionEmitters: Map<string, Emitter<SubscriptionEvents>>;

constructor(api: ParcnetRPCConnector) {
this.#api = api;
Expand All @@ -73,7 +73,7 @@ export class ParcnetPODWrapper {
query: p.PodSpec<E>
): Promise<Subscription<E>> {
const subscriptionId = await this.#api.pod.subscribe(query.schema);
const emitter = new EventEmitter();
const emitter = createNanoEvents<SubscriptionEvents>();
const subscription = new Subscription(query, emitter, this);
this.#subscriptionEmitters.set(subscriptionId, emitter);
return subscription;
Expand Down
25 changes: 9 additions & 16 deletions packages/app-connector/src/rpc_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ import {
import type { PodspecProofRequest } from "@parcnet-js/podspec";
import type { GPCBoundConfig, GPCProof, GPCRevealedClaims } from "@pcd/gpc";
import type { PODEntries } from "@pcd/pod";
import { EventEmitter } from "eventemitter3";
import { type Emitter, createNanoEvents } from "nanoevents";
import * as v from "valibot";
import type { DialogController } from "./adapters/iframe.js";

interface ParcnetEventSignatures {
"subscription-update": (result: SubscriptionUpdateResult) => void;
}

/**
* The RPC connector handles low-level communication with the client.
* It is responsible for sending and receiving messages via MessagePorts,
Expand Down Expand Up @@ -51,7 +55,7 @@ export class ParcnetRPCConnector implements ParcnetRPC, ParcnetEvents {
number,
{ resolve: (value: unknown) => void; reject: (reason?: unknown) => void }
>();
#emitter: EventEmitter;
#emitter: Emitter<ParcnetEventSignatures>;
#connected = false;

/**
Expand Down Expand Up @@ -108,7 +112,7 @@ export class ParcnetRPCConnector implements ParcnetRPC, ParcnetEvents {
constructor(port: MessagePort, dialogController: DialogController) {
this.#port = port;
this.#dialogController = dialogController;
this.#emitter = new EventEmitter();
this.#emitter = createNanoEvents<ParcnetEventSignatures>();
this.pod = {
query: async (query: PODQuery): Promise<string[]> => {
return this.#typedInvoke(
Expand Down Expand Up @@ -282,19 +286,8 @@ export class ParcnetRPCConnector implements ParcnetRPC, ParcnetEvents {
on(
event: "subscription-update",
callback: (result: SubscriptionUpdateResult) => void
): void {
this.#emitter.on("subscription-update", callback);
}

off(
event: "subscription-update",
callback: (result: SubscriptionUpdateResult) => void
): void {
this.#emitter.off("subscription-update", callback);
}

removeAllListeners(): void {
this.#emitter.removeAllListeners("subscription-update");
): () => void {
return this.#emitter.on("subscription-update", callback);
}

#emitSubscriptionUpdate(update: string[], subscriptionId: string): void {
Expand Down
2 changes: 1 addition & 1 deletion packages/client-rpc/src/rpc_interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,5 @@ export interface ParcnetEvents {
on: (
event: "subscription-update",
callback: (result: SubscriptionUpdateResult) => void
) => void;
) => () => void;
}
15 changes: 9 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a4d22f0

Please sign in to comment.