/** * Memorio Observer Example * * This example demonstrates the observer pattern for reactive state changes. * Perfect for logging, analytics, auto-save, and UI updates. * * Run: npx ts-node examples/observer.ts */ import 'memorio' // ============================================ // SIMPLE OBSERVER // ============================================ // Watch a single value observer('state.counter', (newValue, oldValue) => { console.debug(`Counter: ${oldValue} → ${newValue}`) }) state.counter = 0 state.counter = 1 state.counter = 2 // ============================================ // OBJECT OBSERVER // ============================================ // Watch entire objects observer('state.user', (newUser, oldUser) => { console.debug(`User changed: ${oldUser?.name} → ${newUser?.name}`) }) state.user = { name: 'Mario', level: 1 } state.user = { name: 'Luigi', level: 2 } // ============================================ // MULTIPLE OBSERVERS // ============================================ // Multiple observers on same path observer('state.notifications', (count) => { console.debug(`New notification count: ${count}`) }) // List all observers console.debug('Active observers:', observer.list) // ============================================ // CLEANUP // ============================================ // Remove specific observer observer.remove('state.counter') // Remove all observers - call remove for each path // observer.remove('state.counter') // observer.remove('state.notifications') console.debug('Observer example complete!')