|
| 1 | +import { |
| 2 | + DevtoolsServer, |
| 3 | + DevtoolsPlugin, |
| 4 | + type EventMap, |
| 5 | +} from '@tanstack/devtools/server' |
| 6 | +import { z } from 'zod' |
| 7 | +const devtoolsServer = new DevtoolsServer() |
| 8 | + |
| 9 | +devtoolsServer.start() |
| 10 | + |
| 11 | +devtoolsServer.on('click-event', (payload) => { |
| 12 | + console.log('Click event received:', payload) |
| 13 | +}) |
| 14 | + |
| 15 | +devtoolsServer.onAll((e) => { |
| 16 | + console.log('All events:', e) |
| 17 | +}) |
| 18 | + |
| 19 | +setInterval(() => { |
| 20 | + console.log('Emitting server event...') |
| 21 | + devtoolsServer.emit({ |
| 22 | + type: 'server-event', |
| 23 | + payload: { |
| 24 | + title: 'Server Event', |
| 25 | + description: 'This is a custom event emitted by the server.', |
| 26 | + }, |
| 27 | + }) |
| 28 | +}, 5000) |
| 29 | + |
| 30 | +const eventMap = { |
| 31 | + 'query-devtools:test': z.object({ |
| 32 | + title: z.string(), |
| 33 | + description: z.string(), |
| 34 | + }), |
| 35 | + 'query-devtools:init': z.object({ |
| 36 | + title: z.string(), |
| 37 | + description: z.string(), |
| 38 | + }), |
| 39 | + 'query-devtools:b': z.object({ |
| 40 | + title: z.string(), |
| 41 | + description: z.string(), |
| 42 | + }), |
| 43 | +} satisfies EventMap<'query-devtools'> |
| 44 | + |
| 45 | +class QueryDevtoolsPlugin extends DevtoolsPlugin<typeof eventMap> { |
| 46 | + constructor() { |
| 47 | + super({ |
| 48 | + pluginId: 'query-devtools', |
| 49 | + }) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +const plugin = new QueryDevtoolsPlugin() |
| 54 | +/* plugin.emit({ |
| 55 | + type: `query-devtools:init`, |
| 56 | + payload: { |
| 57 | + // Add your payload data here |
| 58 | + }, |
| 59 | +}) */ |
| 60 | + |
| 61 | +plugin.onAll((e) => { |
| 62 | + if (e.type === 'query-devtools:test') { |
| 63 | + console.log('Received query-devtools:test event:', e.payload) |
| 64 | + } |
| 65 | +}) |
| 66 | +plugin.on('query-devtools:test', (e) => { |
| 67 | + e.payload |
| 68 | +}) |
| 69 | + |
| 70 | +export { devtoolsServer } |
0 commit comments