forked from jlalmes/trpc-chrome
-
Notifications
You must be signed in to change notification settings - Fork 11
/
chrome.ts
30 lines (27 loc) · 805 Bytes
/
chrome.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import type { TRPCLink } from '@trpc/client';
import type { AnyRouter } from '@trpc/server';
import { createBaseLink } from './internal/base';
export type ChromeLinkOptions = {
port: chrome.runtime.Port;
};
export const chromeLink = <TRouter extends AnyRouter>(
opts: ChromeLinkOptions,
): TRPCLink<TRouter> => {
return createBaseLink({
postMessage(message) {
opts.port.postMessage(message);
},
addMessageListener(listener) {
opts.port.onMessage.addListener(listener);
},
removeMessageListener(listener) {
opts.port.onMessage.removeListener(listener);
},
addCloseListener(listener) {
opts.port.onDisconnect.addListener(listener);
},
removeCloseListener(listener) {
opts.port.onDisconnect.removeListener(listener);
},
});
};