Skip to content

Commit

Permalink
IPC query invalidation
Browse files Browse the repository at this point in the history
  • Loading branch information
ericvicenti committed Jul 27, 2023
1 parent fd7b358 commit e8a84ce
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 20 deletions.
34 changes: 19 additions & 15 deletions frontend/apps/electron/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import z from 'zod'
import {initTRPC} from '@trpc/server'
// import {observable} from '@trpc/server/observable'
import {observable} from '@trpc/server/observable'
// import {EventEmitter} from 'events'
import superjson from 'superjson'
import {BrowserWindow} from 'electron'
import {BrowserWindow, ipcMain} from 'electron'
import {createIPCHandler} from 'electron-trpc/main'
import path from 'path'

Expand All @@ -13,6 +13,12 @@ let windowIdCount = 1

const allWindows = new Map<string, BrowserWindow>()

const invalidationHandlers = new Set<(queryKey: any) => void>()

ipcMain.on('invalidate_queries', (_event, info) => {
invalidationHandlers.forEach((handler) => handler(info))
})

export const router = t.router({
createAppWindow: t.procedure
.input(
Expand Down Expand Up @@ -73,19 +79,17 @@ export const router = t.router({
// if (!import.meta.env.PROD) browserWindow.webContents.openDevTools()
}),

// subscription: t.procedure.subscription(() => {
// return observable((emit) => {
// function onGreet(text: string) {
// emit.next({text})
// }

// ee.on('greeting', onGreet)

// return () => {
// ee.off('greeting', onGreet)
// }
// })
// }),
queryInvalidation: t.procedure.subscription(() => {
return observable((emit) => {
function handler(value: any[]) {
emit.next(value)
}
invalidationHandlers.add(handler)
return () => {
invalidationHandlers.delete(handler)
}
})
}),
})

const trpcHandlers = createIPCHandler({router, windows: []})
Expand Down
2 changes: 1 addition & 1 deletion frontend/apps/electron/src/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {AppIPC} from '@mintter/app/src/app-ipc'
import {client} from './trpc'
import {decodeRouteFromPath} from '@mintter/app/src/utils/route-encoding'
// import {ipcRenderer} from 'electron/renderer'
console.log('load ipc.ts')

export function createIPC(): AppIPC {
return {
invoke: async (cmd: string, args?: Record<string, unknown>) => {
Expand Down
9 changes: 7 additions & 2 deletions frontend/apps/electron/src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@ ipcRenderer.addListener('initWindow', (info, event) => {

contextBridge.exposeInMainWorld('ipc', {
send: (cmd, args) => {
console.log('IPC SENDING', cmd, args)
ipcRenderer.send(cmd, args)
},
listen: async (cmd: string, handler: (event: any) => void) => {
const innerHandler = (value: any) => {
console.log('IPC event received', cmd, value)
handler(value)
}
console.log('listening!', cmd)
ipcRenderer.addListener(cmd, handler)
ipcRenderer.addListener(cmd, innerHandler)
return () => {
ipcRenderer.removeListener(cmd, handler)
ipcRenderer.removeListener(cmd, innerHandler)
}
},
})
13 changes: 12 additions & 1 deletion frontend/apps/electron/src/root.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useMemo, useState} from 'react'
import React, {useEffect, useMemo, useState} from 'react'
import ReactDOM from 'react-dom/client'
import Main from '@mintter/app/src/pages/main'
import {createGrpcWebTransport} from '@bufbuild/connect-web'
Expand All @@ -19,6 +19,7 @@ import {createTRPCReact} from '@trpc/react-query'
import superjson from 'superjson'
import {AppIPC} from '@mintter/app/src/app-ipc'
import {decodeRouteFromPath} from '@mintter/app/src/utils/route-encoding'
import {client} from './trpc'

const trpcReact = createTRPCReact<AppRouter>()

Expand Down Expand Up @@ -113,6 +114,16 @@ function MainApp({
function ElectronApp() {
const ipc = useMemo(() => createIPC(), [])
const queryClient = useMemo(() => getQueryClient(ipc), [ipc])
useEffect(() => {
const sub = client.queryInvalidation.subscribe(undefined, {
onData: (queryKey) => {
queryClient.client.invalidateQueries(queryKey)
},
})
return () => {
sub.unsubscribe()
}
}, [queryClient])
const trpcClient = useMemo(
() =>
trpcReact.createClient({
Expand Down
3 changes: 2 additions & 1 deletion frontend/packages/app/src/query-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ export function getQueryClient(ipc: AppIPC): AppQueryClient {

function appInvalidateQueries(queryKey: QueryKey) {
ipc.send?.('invalidate_queries', queryKey)
client.invalidateQueries(queryKey)
// this invalidation is required in Tauri but not in electron because the ipc invalidation will be triggered in root.tsx
// client.invalidateQueries(queryKey)
}

return {
Expand Down

0 comments on commit e8a84ce

Please sign in to comment.