|
| 1 | +--- |
| 2 | +title: Custom plugins |
| 3 | +id: custom-plugins |
| 4 | +--- |
| 5 | + |
| 6 | +Tanstack devtools allows you to create your own custom plugins by emitting and listening to our event bus. |
| 7 | + |
| 8 | +## Prerequisite |
| 9 | + |
| 10 | +This guide will walk you through a simple example where our library is a counter with a count history. A working example can be found in our [custom-plugin example](https://tanstack.com/devtools/latest/docs/framework/react/examples/custom-plugin). |
| 11 | + |
| 12 | +This is our library code: |
| 13 | + |
| 14 | +counter.ts |
| 15 | +```tsx |
| 16 | +export function createCounter() { |
| 17 | + let count = 0; |
| 18 | + const history = []; |
| 19 | + |
| 20 | + return { |
| 21 | + getCount: () => count, |
| 22 | + increment: () => { |
| 23 | + history.push(count); |
| 24 | + count++; |
| 25 | + }, |
| 26 | + decrement: () => { |
| 27 | + history.push(count); |
| 28 | + count--; |
| 29 | + }, |
| 30 | + }; |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +## Event Client Setup |
| 35 | + |
| 36 | +Install the [TanStack Devtools Event Client](https://tanstack.com/devtools/) utils. |
| 37 | + |
| 38 | +```bash |
| 39 | +npm i @tanstack/devtools-event-client |
| 40 | +``` |
| 41 | + |
| 42 | +First you will need to setup the `EventClient`. |
| 43 | + |
| 44 | +eventClient.ts |
| 45 | +```tsx |
| 46 | +import { EventClient } from '@tanstack/devtools-event-client' |
| 47 | + |
| 48 | + |
| 49 | +type EventMap = { |
| 50 | + // The key of the event map is a combination of {pluginId}:{eventSuffix} |
| 51 | + // The value is the expected type of the event payload |
| 52 | + 'custom-devtools:counter-state': { count: number, history: number[], } |
| 53 | +} |
| 54 | + |
| 55 | +class CustomEventClient extends EventClient<EventMap> { |
| 56 | +constructor() { |
| 57 | + super({ |
| 58 | + // The pluginId must match that of the event map key |
| 59 | + pluginId: 'custom-devtools', |
| 60 | + }) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// This is where the magic happens, it'll be used throughout your application. |
| 65 | +export const DevtoolsEventClient = new FormEventClient() |
| 66 | +``` |
| 67 | + |
| 68 | +## Event Client Integration |
| 69 | + |
| 70 | +Now we need to hook our `EventClient` into out application code. This can be done in many way's, a UseEffect that emits the current state, or a subscription to an observer, all that matters is that when you want to emit the current state you do the following. |
| 71 | + |
| 72 | +Our new library code will looks as follows: |
| 73 | + |
| 74 | +counter.ts |
| 75 | +```tsx |
| 76 | +import { DevtoolsEventClient } from './eventClient.ts' |
| 77 | + |
| 78 | +export function createCounter() { |
| 79 | + let count = 0 |
| 80 | + const history: Array<number> = [] |
| 81 | + |
| 82 | + return { |
| 83 | + getCount: () => count, |
| 84 | + increment: () => { |
| 85 | + history.push(count) |
| 86 | + |
| 87 | + // The emit eventSuffix must match that of the EventMap defined in eventClient.ts |
| 88 | + DevtoolsEventClient.emit('counter-state', { |
| 89 | + count: count++, |
| 90 | + history: history, |
| 91 | + }) |
| 92 | + }, |
| 93 | + decrement: () => { |
| 94 | + history.push(count) |
| 95 | + |
| 96 | + DevtoolsEventClient.emit('counter-state', { |
| 97 | + count: count--, |
| 98 | + history: history, |
| 99 | + }) |
| 100 | + }, |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +> **Important** `EventClient` is framework agnostic so this process will be the same regardless of framework or even in vanilla JavaScript. |
| 106 | +
|
| 107 | +## Consuming The Event Client |
| 108 | + |
| 109 | +Now we need to create our devtools panel, for a simple approach write the devtools in the framework that the adapter is, be aware that this will make the plugin framework specific. |
| 110 | + |
| 111 | +> Because TanStack is framework agnostic we have taken a more complicated approach that will be explained in coming docs (if framework agnosticism is not a concern to you you can ignore this). |
| 112 | +
|
| 113 | +DevtoolsPanel.ts |
| 114 | +```tsx |
| 115 | +import { DevtoolsEventClient } from './eventClient.ts' |
| 116 | + |
| 117 | +export function DevtoolPanel() { |
| 118 | + const [state,setState] = useState(); |
| 119 | + |
| 120 | + useEffect(() => { |
| 121 | + // subscribe to the emitted event |
| 122 | + const cleanup = client.on("counter-state", e => setState(e.payload) |
| 123 | + return cleanup |
| 124 | + }, []); |
| 125 | + |
| 126 | + return ( |
| 127 | + <div> |
| 128 | + <div>{state.count}</div> |
| 129 | + <div>{JSON.stringify(state.history)}</div> |
| 130 | + <div/> |
| 131 | + ) |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +## Application Integration |
| 136 | + |
| 137 | +This step follows what's shown in [../basic-setup] for a more documented guide go check it out. As well as the complete [custom-plugin example](https://tanstack.com/devtools/latest/docs/framework/react/examples/custom-plugin) in our examples section. |
| 138 | + |
| 139 | +Main.tsx |
| 140 | +```tsx |
| 141 | +import { DevtoolPanel } from './DevtoolPanel' |
| 142 | + |
| 143 | +createRoot(document.getElementById('root')!).render( |
| 144 | + <StrictMode> |
| 145 | + <App /> |
| 146 | + |
| 147 | + <TanstackDevtools |
| 148 | + plugins={[ |
| 149 | + { |
| 150 | + // Name it what you like, this is how it will appear in the Menu |
| 151 | + name: 'Custom devtools', |
| 152 | + render: <DevtoolPanel />, |
| 153 | + }, |
| 154 | + ]} |
| 155 | + /> |
| 156 | + </StrictMode>, |
| 157 | +) |
| 158 | + |
| 159 | +``` |
| 160 | + |
| 161 | +## Debugging |
| 162 | + |
| 163 | +Both the TansTack `TanstackDevtools` component and the TanStack `EventClient` come with built in debug mode which will log to the console the emitted event as well as the EventClient status. |
| 164 | + |
| 165 | +TanstackDevtool's debugging mode can be activated like so: |
| 166 | +```tsx |
| 167 | +<TanstackDevtools |
| 168 | + eventBusConfig={{ debug: true }} |
| 169 | + plugins={[ |
| 170 | + { |
| 171 | + // call it what you like, this is how it will appear in the Menu |
| 172 | + name: 'Custom devtools', |
| 173 | + render: <DevtoolPanel />, |
| 174 | + }, |
| 175 | + ]} |
| 176 | +/> |
| 177 | +``` |
| 178 | + |
| 179 | +Where as the EventClient's debug mode can be activated by: |
| 180 | +```tsx |
| 181 | +class CustomEventClient extends EventClient<EventMap> { |
| 182 | + constructor() { |
| 183 | + super({ |
| 184 | + pluginId: 'custom-devtools', |
| 185 | + debug: true, |
| 186 | + }) |
| 187 | + } |
| 188 | +} |
| 189 | +``` |
| 190 | + |
| 191 | +Activating the debug mode will log to the console the current events that emitter has emitted or listened to. The EventClient will have appended `[tanstack-devtools:${pluginId}]` and the client will have appended `[tanstack-devtools:client-bus]`. |
| 192 | + |
| 193 | +Heres an example of both: |
| 194 | +``` |
| 195 | +🌴 [tanstack-devtools:client-bus] Initializing client event bus |
| 196 | + |
| 197 | +🌴 [tanstack-devtools:custom-devtools-plugin] Registered event to bus custom-devtools:counter-state |
| 198 | +``` |
0 commit comments