Skip to content

Commit

Permalink
Use channel message transformers for JSON serialization (#3450)
Browse files Browse the repository at this point in the history
This PR uses the new `transformChannelInboundMessages` and `transformChannelOutboundMessages` helpers to refactor the existing `getRpcSubscriptionsChannelWithJSONSerialization` function.
  • Loading branch information
lorisleiva authored Oct 27, 2024
1 parent 17c373d commit cecf20b
Showing 1 changed file with 11 additions and 21 deletions.
32 changes: 11 additions & 21 deletions packages/rpc-subscriptions/src/rpc-subscriptions-json.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
import { RpcSubscriptionsChannel } from '@solana/rpc-subscriptions-spec';
import { pipe } from '@solana/functional';
import {
RpcSubscriptionsChannel,
transformChannelInboundMessages,
transformChannelOutboundMessages,
} from '@solana/rpc-subscriptions-spec';

export function getRpcSubscriptionsChannelWithJSONSerialization(
channel: RpcSubscriptionsChannel<string, string>,
): RpcSubscriptionsChannel<unknown, unknown> {
return Object.freeze({
...channel,
on(type, listener, options) {
if (type !== 'message') {
return channel.on(type, listener, options);
}
return channel.on(
'message',
function deserializingListener(message: string) {
const deserializedMessage = JSON.parse(message);
listener(deserializedMessage);
},
options,
);
},
send(message) {
const serializedMessage = JSON.stringify(message);
return channel.send(serializedMessage);
},
} as RpcSubscriptionsChannel<unknown, unknown>);
return pipe(
channel,
c => transformChannelInboundMessages(c, JSON.parse),
c => transformChannelOutboundMessages(c, JSON.stringify),
);
}

0 comments on commit cecf20b

Please sign in to comment.