|
| 1 | +# Valtio Plugin Examples |
| 2 | + |
| 3 | +This directory contains examples demonstrating how to use the Valtio Plugin System. |
| 4 | + |
| 5 | +## Examples |
| 6 | + |
| 7 | +### 1. Logging Plugin (`logging-plugin.ts`) |
| 8 | + |
| 9 | +A comprehensive example showing: |
| 10 | + |
| 11 | +- ✅ **Global Plugin Registration**: Register plugins that affect all proxy instances |
| 12 | +- ✅ **TypeScript Support**: Full autocomplete and type safety when importing from 'valtio' |
| 13 | +- ✅ **Plugin API Access**: Access plugin methods via `proxy.pluginId` |
| 14 | +- ✅ **Lifecycle Hooks**: Implement `onInit`, `beforeChange`, `afterChange` hooks |
| 15 | +- ✅ **Method Chaining**: Use `proxy.use().method()` pattern |
| 16 | +- ✅ **Instance Creation**: Create independent proxy instances with `proxy.createInstance()` |
| 17 | + |
| 18 | +## Running the Examples |
| 19 | + |
| 20 | +### Prerequisites |
| 21 | + |
| 22 | +Make sure you have the plugin built: |
| 23 | + |
| 24 | +```bash |
| 25 | +npm run build |
| 26 | +``` |
| 27 | + |
| 28 | +### Quick Start |
| 29 | + |
| 30 | +```bash |
| 31 | +# Run the JavaScript example (easiest) |
| 32 | +node examples/logging-example.js |
| 33 | + |
| 34 | +# Verify TypeScript compiles correctly |
| 35 | +npx tsc --noEmit examples/typescript-example.ts |
| 36 | + |
| 37 | +# Run TypeScript example with tsx (if you have it installed) |
| 38 | +npx tsx examples/typescript-example.ts |
| 39 | +``` |
| 40 | + |
| 41 | +### Available Examples |
| 42 | + |
| 43 | +1. **`logging-example.js`** - JavaScript version, runs immediately |
| 44 | +2. **`logging-plugin.ts`** - Full TypeScript version with detailed documentation |
| 45 | +3. **`typescript-example.ts`** - Focused on TypeScript autocomplete features |
| 46 | + |
| 47 | +## Key Features Demonstrated |
| 48 | + |
| 49 | +### TypeScript Module Augmentation |
| 50 | + |
| 51 | +When you import any export from this package, the `proxy` from 'valtio' automatically gets enhanced: |
| 52 | + |
| 53 | +```typescript |
| 54 | +import { proxy } from 'valtio' |
| 55 | +import { ValtioPlugin } from 'valtio-plugin' // This enables augmentation |
| 56 | +
|
| 57 | +// Now you have full TypeScript support: |
| 58 | +proxy.use(myPlugin) // ✅ Autocomplete |
| 59 | +proxy.clearPlugins() // ✅ Autocomplete |
| 60 | +proxy.createInstance() // ✅ Autocomplete |
| 61 | +proxy.getPlugins() // ✅ Autocomplete |
| 62 | +``` |
| 63 | + |
| 64 | +### Plugin API Access |
| 65 | + |
| 66 | +Access your plugins as properties with full typing: |
| 67 | + |
| 68 | +```typescript |
| 69 | +const loggingPlugin = createLoggingPlugin() |
| 70 | +proxy.use(loggingPlugin) |
| 71 | + |
| 72 | +// Access with TypeScript support |
| 73 | +const logger = proxy.logger as LoggingPlugin |
| 74 | +logger.info('Hello world!') // ✅ Autocomplete |
| 75 | +logger.getLogCount() // ✅ Autocomplete |
| 76 | +``` |
| 77 | + |
| 78 | +### Global vs Instance Plugins |
| 79 | + |
| 80 | +```typescript |
| 81 | +// Global plugins affect ALL proxy instances |
| 82 | +proxy.use(globalLoggingPlugin) |
| 83 | + |
| 84 | +const store1 = proxy({ count: 0 }) // Logging enabled |
| 85 | +const store2 = proxy({ name: 'John' }) // Logging enabled |
| 86 | + |
| 87 | +// Instance plugins only affect that instance |
| 88 | +const instance = proxy.createInstance() |
| 89 | +instance.use(instanceOnlyPlugin) |
| 90 | + |
| 91 | +const store3 = instance({ value: 'test' }) // Both global AND instance plugins |
| 92 | +const store4 = proxy({ other: 'data' }) // Only global plugins |
| 93 | +``` |
| 94 | + |
| 95 | +## Creating Your Own Plugins |
| 96 | + |
| 97 | +### Basic Plugin Structure |
| 98 | + |
| 99 | +```typescript |
| 100 | +import { ValtioPlugin } from 'valtio-plugin' |
| 101 | + |
| 102 | +const myPlugin: ValtioPlugin = { |
| 103 | + id: 'my-plugin', // Required: unique identifier |
| 104 | + name: 'My Plugin', // Optional: display name |
| 105 | + |
| 106 | + // Lifecycle hooks (all optional) |
| 107 | + onInit: () => { |
| 108 | + console.log('Plugin initialized') |
| 109 | + }, |
| 110 | + |
| 111 | + onAttach: (proxyFactory) => { |
| 112 | + console.log('Plugin attached to factory') |
| 113 | + }, |
| 114 | + |
| 115 | + beforeChange: (path, newValue, oldValue, state) => { |
| 116 | + console.log(`Changing ${path.join('.')}`) |
| 117 | + return true // Return false to prevent the change |
| 118 | + }, |
| 119 | + |
| 120 | + afterChange: (path, newValue, state) => { |
| 121 | + console.log(`Changed ${path.join('.')}`) |
| 122 | + }, |
| 123 | + |
| 124 | + onSubscribe: (proxyObject, callback) => { |
| 125 | + console.log('New subscription created') |
| 126 | + }, |
| 127 | + |
| 128 | + alterSnapshot: (snapshot) => { |
| 129 | + // Modify snapshot before it's returned |
| 130 | + return { ...snapshot, _modified: true } |
| 131 | + }, |
| 132 | + |
| 133 | + // Custom plugin methods |
| 134 | + myMethod: () => 'Hello from plugin!', |
| 135 | + customAction: (data: any) => { /* do something */ } |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +### Plugin with TypeScript Interface |
| 140 | + |
| 141 | +```typescript |
| 142 | +interface MyPlugin extends ValtioPlugin { |
| 143 | + myMethod: () => string |
| 144 | + customAction: (data: any) => void |
| 145 | +} |
| 146 | + |
| 147 | +const createMyPlugin = (): MyPlugin => ({ |
| 148 | + id: 'my-plugin', |
| 149 | + myMethod: () => 'Hello!', |
| 150 | + customAction: (data) => console.log(data) |
| 151 | +}) |
| 152 | + |
| 153 | +// Usage with typing |
| 154 | +proxy.use(createMyPlugin()) |
| 155 | +const plugin = proxy['my-plugin'] as MyPlugin |
| 156 | +plugin.myMethod() // ✅ Full TypeScript support |
| 157 | +``` |
| 158 | + |
| 159 | +## Best Practices |
| 160 | + |
| 161 | +1. **Use unique plugin IDs** to avoid conflicts |
| 162 | +2. **Implement TypeScript interfaces** for better developer experience |
| 163 | +3. **Handle errors gracefully** in plugin hooks |
| 164 | +4. **Use global plugins sparingly** - they affect all proxy instances |
| 165 | +5. **Prefer instance plugins** for application-specific functionality |
| 166 | +6. **Document your plugin APIs** for other developers |
0 commit comments