Skip to content

Commit 9d6d33a

Browse files
authored
Merge pull request jlalmes#11 from diegodelrieu/fix/subscriptions
Fix/subscriptions
2 parents 8386348 + da87a30 commit 9d6d33a

File tree

7 files changed

+65
-25
lines changed

7 files changed

+65
-25
lines changed

examples/with-plasmo/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"plasmo": "^0.84.0",
1414
"react": "^18.2.0",
1515
"react-dom": "^18.2.0",
16+
"superjson": "^2.2.1",
1617
"zod": "^3.19.1"
1718
},
1819
"devDependencies": {

examples/with-plasmo/src/background.ts

+11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
11
import { initTRPC } from '@trpc/server';
2+
import { observable } from '@trpc/server/observable';
3+
import SuperJSON from 'superjson';
24
import { z } from 'zod';
35

46
import { createChromeHandler } from '../../../adapter';
57

68
const t = initTRPC.create({
79
isServer: false,
810
allowOutsideOfServer: true,
11+
transformer: SuperJSON,
912
});
1013

1114
const appRouter = t.router({
1215
openNewTab: t.procedure.input(z.object({ url: z.string().url() })).mutation(async ({ input }) => {
1316
await chrome.tabs.create({ url: input.url, active: true });
1417
}),
18+
subscribeToAnything: t.procedure.subscription(() => {
19+
return observable<string | undefined>((emit) => {
20+
const interval = setInterval(() => {
21+
emit.next(new Date().toISOString());
22+
}, 1000);
23+
return () => clearInterval(interval);
24+
});
25+
}),
1526
});
1627

1728
export type AppRouter = typeof appRouter;
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { relay } from 'trpc-browser/relay';
22
import { autoConnect } from 'trpc-browser/shared/chrome';
33

4-
void autoConnect(chrome, (port) => {
5-
return relay(window, port); // return so it has the cleanup function for disconnect
6-
}).then(() => {
4+
void autoConnect(
5+
() => chrome.runtime.connect(),
6+
(port) => {
7+
return relay(window, port); // return so it has the cleanup function for disconnect
8+
},
9+
).then(() => {
710
return console.log('Content script loaded - bridge initialized');
811
});

examples/with-plasmo/src/contents/inpage.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createTRPCProxyClient } from '@trpc/client';
22
import type { PlasmoCSConfig } from 'plasmo';
33
import type { FC } from 'react';
4+
import SuperJSON from 'superjson';
45
import { windowLink } from 'trpc-browser/link';
56
import type { AppRouter } from '~background';
67

@@ -12,6 +13,7 @@ export const config: PlasmoCSConfig = {
1213
};
1314

1415
const windowClient = createTRPCProxyClient<AppRouter>({
16+
transformer: SuperJSON,
1517
links: [/* 👉 */ windowLink({ window })],
1618
});
1719

@@ -30,8 +32,10 @@ const ExtensionInpageUi: FC = () => {
3032
cursor: 'pointer',
3133
}}
3234
onClick={async () => {
33-
await windowClient.openNewTab.mutate({
34-
url: 'https://google.com',
35+
await windowClient.subscribeToAnything.subscribe(undefined, {
36+
onData(data) {
37+
console.log('data', data);
38+
},
3539
});
3640
}}
3741
>

package-lock.json

+12-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/adapter/chrome.ts

+14-9
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,16 @@ export const createChromeHandler = <TRouter extends AnyRouter>(
6767

6868
sendResponse({
6969
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
70-
error: getErrorShape({
71-
config: router._def._config,
72-
error,
73-
type: method,
74-
path: params.path,
75-
input: params.input,
76-
ctx,
77-
}),
70+
error: transformer.output.serialize(
71+
getErrorShape({
72+
config: router._def._config,
73+
error,
74+
type: method,
75+
path: params.path,
76+
input: params.input,
77+
ctx,
78+
}),
79+
),
7880
});
7981
};
8082

@@ -105,7 +107,10 @@ export const createChromeHandler = <TRouter extends AnyRouter>(
105107
}
106108

107109
const subscription = result.subscribe({
108-
next: (data) => sendResponse({ result: { type: 'data', data } }),
110+
next: (data) => {
111+
const serializedData = transformer.output.serialize(data);
112+
sendResponse({ result: { type: 'data', data: serializedData } });
113+
},
109114
error: handleError,
110115
complete: () => sendResponse({ result: { type: 'stopped' } }),
111116
});

src/adapter/window.ts

+15-9
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,16 @@ export const createWindowHandler = <TRouter extends AnyRouter>(
7373

7474
sendResponse({
7575
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
76-
error: getErrorShape({
77-
config: router._def._config,
78-
error,
79-
type: method,
80-
path: params.path,
81-
input: params.input,
82-
ctx,
83-
}),
76+
error: transformer.output.serialize(
77+
getErrorShape({
78+
config: router._def._config,
79+
error,
80+
type: method,
81+
path: params.path,
82+
input: params.input,
83+
ctx,
84+
}),
85+
),
8486
});
8587
};
8688

@@ -111,7 +113,11 @@ export const createWindowHandler = <TRouter extends AnyRouter>(
111113
}
112114

113115
const subscription = result.subscribe({
114-
next: (data) => sendResponse({ result: { type: 'data', data } }),
116+
next: (data) => {
117+
const serializedData = transformer.output.serialize(data);
118+
119+
sendResponse({ result: { type: 'data', data: serializedData } });
120+
},
115121
error: handleError,
116122
complete: () => sendResponse({ result: { type: 'stopped' } }),
117123
});

0 commit comments

Comments
 (0)