-
Notifications
You must be signed in to change notification settings - Fork 911
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use channel message transformers for JSON serialization (#3450)
This PR uses the new `transformChannelInboundMessages` and `transformChannelOutboundMessages` helpers to refactor the existing `getRpcSubscriptionsChannelWithJSONSerialization` function.
- Loading branch information
1 parent
17c373d
commit cecf20b
Showing
1 changed file
with
11 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
); | ||
} |