Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

Commit

Permalink
feat: make ProviderSendError.egressMessage non optional
Browse files Browse the repository at this point in the history
  • Loading branch information
kratico committed Nov 10, 2022
1 parent 7a39157 commit 2176929
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
7 changes: 4 additions & 3 deletions rpc/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class Client<
}

#listener: ProviderListener<SendErrorData, HandlerErrorData> = (e) => {
if (e instanceof ProviderSendError && e.egressMessage) {
if (e instanceof ProviderSendError) {
const egressMessageId = e.egressMessage.id;
const pendingCall = this.pendingCalls[egressMessageId];
if (!pendingCall) {
Expand Down Expand Up @@ -103,7 +103,7 @@ export class Client<
delete this.activeSubscriptionByMessageId[message.id];
waiter.resolve(activeSubscriptionId);
};
this.pendingSubscriptions[message.id] = listener.bind({
const listenerBound = listener.bind({
message,
stop,
state: <T>(ctor: new() => T) => {
Expand All @@ -114,10 +114,11 @@ export class Client<
);
},
}) as ClientSubscribeListener<SendErrorData, HandlerErrorData>;
this.pendingSubscriptions[message.id] = listenerBound;
this.call(message)
.then((maybeError) => {
if (maybeError instanceof Error) {
this.pendingSubscriptions[message.id]!(maybeError);
listenerBound(maybeError);
stop();
}
});
Expand Down
2 changes: 1 addition & 1 deletion rpc/provider/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export class ProviderSendError<Data> extends Error {
override readonly name = "ProviderSendError";
constructor(
override readonly cause: Data,
readonly egressMessage?: msg.EgressMessage,
readonly egressMessage: msg.EgressMessage,
) {
super();
}
Expand Down
8 changes: 6 additions & 2 deletions rpc/provider/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ export const proxyProvider: Provider<string, Event, Event, Event> = (url, listen
(async () => {
const openError = await ensureWsOpen(conn.inner);
if (openError) {
conn.forEachListener(new ProviderSendError(openError));
} else {
conn.forEachListener(new ProviderSendError(openError, message));
return;
}
try {
conn.inner.send(JSON.stringify(message));
} catch (error) {
listener(new ProviderSendError(error as Event, message));
}
})();
},
Expand Down
8 changes: 2 additions & 6 deletions rpc/provider/smoldot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,9 @@ export const smoldotProvider: Provider<
return;
}
try {
return conn.inner.sendJsonRpc(JSON.stringify(message));
conn.inner.sendJsonRpc(JSON.stringify(message));
} catch (error) {
if (error instanceof MalformedJsonRpcError || error instanceof QueueFullError) {
listener(new ProviderSendError(error, message));
return;
}
conn.forEachListener(new ProviderSendError(error as SmoldotSendErrorData));
listener(new ProviderSendError(error as SmoldotSendErrorData, message));
}
})();
},
Expand Down

0 comments on commit 2176929

Please sign in to comment.