Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/subscriptions #11

Merged
merged 4 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/with-plasmo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"plasmo": "^0.84.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"superjson": "^2.2.1",
"zod": "^3.19.1"
},
"devDependencies": {
Expand Down
11 changes: 11 additions & 0 deletions examples/with-plasmo/src/background.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
import { initTRPC } from '@trpc/server';
import { observable } from '@trpc/server/observable';
import SuperJSON from 'superjson';
import { z } from 'zod';

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

const t = initTRPC.create({
isServer: false,
allowOutsideOfServer: true,
transformer: SuperJSON,
});

const appRouter = t.router({
openNewTab: t.procedure.input(z.object({ url: z.string().url() })).mutation(async ({ input }) => {
await chrome.tabs.create({ url: input.url, active: true });
}),
subscribeToAnything: t.procedure.subscription(() => {
return observable<string | undefined>((emit) => {
const interval = setInterval(() => {
emit.next(new Date().toISOString());
}, 1000);
return () => clearInterval(interval);
});
}),
});

export type AppRouter = typeof appRouter;
Expand Down
9 changes: 6 additions & 3 deletions examples/with-plasmo/src/contents/content.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { relay } from 'trpc-browser/relay';
import { autoConnect } from 'trpc-browser/shared/chrome';

void autoConnect(chrome, (port) => {
return relay(window, port); // return so it has the cleanup function for disconnect
}).then(() => {
void autoConnect(
() => chrome.runtime.connect(),
(port) => {
return relay(window, port); // return so it has the cleanup function for disconnect
},
).then(() => {
return console.log('Content script loaded - bridge initialized');
});
8 changes: 6 additions & 2 deletions examples/with-plasmo/src/contents/inpage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createTRPCProxyClient } from '@trpc/client';
import type { PlasmoCSConfig } from 'plasmo';
import type { FC } from 'react';
import SuperJSON from 'superjson';
import { windowLink } from 'trpc-browser/link';
import type { AppRouter } from '~background';

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

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

Expand All @@ -30,8 +32,10 @@ const ExtensionInpageUi: FC = () => {
cursor: 'pointer',
}}
onClick={async () => {
await windowClient.openNewTab.mutate({
url: 'https://google.com',
await windowClient.subscribeToAnything.subscribe(undefined, {
onData(data) {
console.log('data', data);
},
});
}}
>
Expand Down
14 changes: 12 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 14 additions & 9 deletions src/adapter/chrome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,16 @@ export const createChromeHandler = <TRouter extends AnyRouter>(

sendResponse({
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
error: getErrorShape({
config: router._def._config,
error,
type: method,
path: params.path,
input: params.input,
ctx,
}),
error: transformer.output.serialize(
getErrorShape({
config: router._def._config,
error,
type: method,
path: params.path,
input: params.input,
ctx,
}),
),
});
};

Expand Down Expand Up @@ -105,7 +107,10 @@ export const createChromeHandler = <TRouter extends AnyRouter>(
}

const subscription = result.subscribe({
next: (data) => sendResponse({ result: { type: 'data', data } }),
next: (data) => {
const serializedData = transformer.output.serialize(data);
sendResponse({ result: { type: 'data', data: serializedData } });
},
error: handleError,
complete: () => sendResponse({ result: { type: 'stopped' } }),
});
Expand Down
24 changes: 15 additions & 9 deletions src/adapter/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,16 @@ export const createWindowHandler = <TRouter extends AnyRouter>(

sendResponse({
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
error: getErrorShape({
config: router._def._config,
error,
type: method,
path: params.path,
input: params.input,
ctx,
}),
error: transformer.output.serialize(
getErrorShape({
config: router._def._config,
error,
type: method,
path: params.path,
input: params.input,
ctx,
}),
),
});
};

Expand Down Expand Up @@ -111,7 +113,11 @@ export const createWindowHandler = <TRouter extends AnyRouter>(
}

const subscription = result.subscribe({
next: (data) => sendResponse({ result: { type: 'data', data } }),
next: (data) => {
const serializedData = transformer.output.serialize(data);

sendResponse({ result: { type: 'data', data: serializedData } });
},
error: handleError,
complete: () => sendResponse({ result: { type: 'stopped' } }),
});
Expand Down
Loading