File tree 7 files changed +65
-25
lines changed
7 files changed +65
-25
lines changed Original file line number Diff line number Diff line change 13
13
"plasmo" : " ^0.84.0" ,
14
14
"react" : " ^18.2.0" ,
15
15
"react-dom" : " ^18.2.0" ,
16
+ "superjson" : " ^2.2.1" ,
16
17
"zod" : " ^3.19.1"
17
18
},
18
19
"devDependencies" : {
Original file line number Diff line number Diff line change 1
1
import { initTRPC } from '@trpc/server' ;
2
+ import { observable } from '@trpc/server/observable' ;
3
+ import SuperJSON from 'superjson' ;
2
4
import { z } from 'zod' ;
3
5
4
6
import { createChromeHandler } from '../../../adapter' ;
5
7
6
8
const t = initTRPC . create ( {
7
9
isServer : false ,
8
10
allowOutsideOfServer : true ,
11
+ transformer : SuperJSON ,
9
12
} ) ;
10
13
11
14
const appRouter = t . router ( {
12
15
openNewTab : t . procedure . input ( z . object ( { url : z . string ( ) . url ( ) } ) ) . mutation ( async ( { input } ) => {
13
16
await chrome . tabs . create ( { url : input . url , active : true } ) ;
14
17
} ) ,
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
+ } ) ,
15
26
} ) ;
16
27
17
28
export type AppRouter = typeof appRouter ;
Original file line number Diff line number Diff line change 1
1
import { relay } from 'trpc-browser/relay' ;
2
2
import { autoConnect } from 'trpc-browser/shared/chrome' ;
3
3
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 ( ( ) => {
7
10
return console . log ( 'Content script loaded - bridge initialized' ) ;
8
11
} ) ;
Original file line number Diff line number Diff line change 1
1
import { createTRPCProxyClient } from '@trpc/client' ;
2
2
import type { PlasmoCSConfig } from 'plasmo' ;
3
3
import type { FC } from 'react' ;
4
+ import SuperJSON from 'superjson' ;
4
5
import { windowLink } from 'trpc-browser/link' ;
5
6
import type { AppRouter } from '~background' ;
6
7
@@ -12,6 +13,7 @@ export const config: PlasmoCSConfig = {
12
13
} ;
13
14
14
15
const windowClient = createTRPCProxyClient < AppRouter > ( {
16
+ transformer : SuperJSON ,
15
17
links : [ /* 👉 */ windowLink ( { window } ) ] ,
16
18
} ) ;
17
19
@@ -30,8 +32,10 @@ const ExtensionInpageUi: FC = () => {
30
32
cursor : 'pointer' ,
31
33
} }
32
34
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
+ } ,
35
39
} ) ;
36
40
} }
37
41
>
Original file line number Diff line number Diff line change @@ -67,14 +67,16 @@ export const createChromeHandler = <TRouter extends AnyRouter>(
67
67
68
68
sendResponse ( {
69
69
// 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
+ ) ,
78
80
} ) ;
79
81
} ;
80
82
@@ -105,7 +107,10 @@ export const createChromeHandler = <TRouter extends AnyRouter>(
105
107
}
106
108
107
109
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
+ } ,
109
114
error : handleError ,
110
115
complete : ( ) => sendResponse ( { result : { type : 'stopped' } } ) ,
111
116
} ) ;
Original file line number Diff line number Diff line change @@ -73,14 +73,16 @@ export const createWindowHandler = <TRouter extends AnyRouter>(
73
73
74
74
sendResponse ( {
75
75
// 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
+ ) ,
84
86
} ) ;
85
87
} ;
86
88
@@ -111,7 +113,11 @@ export const createWindowHandler = <TRouter extends AnyRouter>(
111
113
}
112
114
113
115
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
+ } ,
115
121
error : handleError ,
116
122
complete : ( ) => sendResponse ( { result : { type : 'stopped' } } ) ,
117
123
} ) ;
You can’t perform that action at this time.
0 commit comments