Skip to content

Commit

Permalink
feat: ipcLink has options
Browse files Browse the repository at this point in the history
  • Loading branch information
azamuddin committed Jul 5, 2023
1 parent b9c4ea1 commit 33b46e2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-apricots-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'electron-trpc-plus': minor
---

ipcLink now have option
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ docs/.vitepress/cache/
coverage/
packages/electron-trpc/renderer.d.ts
packages/electron-trpc/main.d.ts
.env
2 changes: 1 addition & 1 deletion packages/electron-trpc/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "electron-trpc-plus",
"description": "Electron support for tRPC ",
"version": "0.5.3",
"version": "0.6.0",
"exports": {
"./main": {
"require": "./dist/main.cjs",
Expand Down
40 changes: 27 additions & 13 deletions packages/electron-trpc/src/renderer/ipcLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import debug from 'debug';

const log = debug('electron-trpc:renderer:ipcLink');

export interface IPCLinkOptions {
getContext?: () => Promise<any>;
}

type IPCCallbackResult<TRouter extends AnyRouter = AnyRouter> = TRPCResponseMessage<
unknown,
inferRouterContext<TRouter>
Expand Down Expand Up @@ -39,8 +43,10 @@ const getElectronTRPC = () => {
class IPCClient {
#pendingRequests = new Map<string | number, IPCRequest>();
#electronTRPC = getElectronTRPC();
#ipcLinkOptions: IPCLinkOptions | null = null;

constructor() {
constructor(options: IPCLinkOptions) {
this.#ipcLinkOptions = options;
this.#electronTRPC.onMessage((response: TRPCResponseMessage) => {
this.#handleResponse(response);
});
Expand All @@ -63,13 +69,24 @@ class IPCClient {
request(op: Operation, callbacks: IPCCallbacks) {
const { type, id } = op;

this.#pendingRequests.set(id, {
type,
callbacks,
op,
});

this.#electronTRPC.sendMessage({ method: 'request', operation: op });
if (this.#ipcLinkOptions?.getContext) {
this.#ipcLinkOptions.getContext().then((context) => {
const opWithContext = { ...op, context };
this.#pendingRequests.set(id, {
type,
callbacks,
op: opWithContext,
});
this.#electronTRPC.sendMessage({ method: 'request', operation: opWithContext });
});
} else {
this.#pendingRequests.set(id, {
type,
callbacks,
op,
});
this.#electronTRPC.sendMessage({ method: 'request', operation: op });
}

return () => {
const callbacks = this.#pendingRequests.get(id)?.callbacks;
Expand All @@ -88,16 +105,13 @@ class IPCClient {
}
}

export function ipcLink<TRouter extends AnyRouter>(): TRPCLink<TRouter> {
export function ipcLink<TRouter extends AnyRouter>(options: IPCLinkOptions): TRPCLink<TRouter> {
return (runtime) => {
const client = new IPCClient();
const client = new IPCClient(options);

return ({ op }) => {
return observable((observer) => {
op.input = runtime.transformer.serialize(op.input);
op.context = {
token: 'JUST A DUMMY TOKEN',
};

let isDone = false;
const unsubscribe = client.request(op, {
Expand Down

0 comments on commit 33b46e2

Please sign in to comment.