/** * Memorio State Advanced Example * * This example shows advanced state features: locking, path tracking, and nesting. * * Run: npx ts-node examples/state-advanced.ts */ import 'memorio' // ============================================ // NESTED OBJECTS // ============================================ // Create nested state state.user = { name: 'Mario', profile: { email: 'mario@example.com', settings: { theme: 'dark', notifications: true } } } // Access nested values console.debug('User name:', state.user.name) console.debug('Email:', state.user.profile.email) console.debug('Theme:', state.user.profile.settings.theme) // ============================================ // ARRAYS // ============================================ // State arrays work like regular arrays state.items = [1, 2, 3] state.items.push(4) console.debug('Items:', state.items) // [1, 2, 3, 4] state.users = [ { name: 'Mario', id: 1 }, { name: 'Luigi', id: 2 } ] state.users.push({ name: 'Peach', id: 3 }) console.debug('Users:', state.users) // ============================================ // LOCKING STATE // ============================================ // Lock a value to prevent modifications state.config = { maxUsers: 100, timeout: 30 } state.config.lock() // This will fail: // state.config.maxUsers = 200 // Error: state 'config' is locked console.debug('Config locked:', state.config) // ============================================ // PATH TRACKING // ============================================ // Get path information console.debug('Path:', state.user.__path) // "state.user" // Use path tracker for debugging const path = state.user.profile console.debug('Profile path:', path.email.toString()) // "state.user.profile.email" // ============================================ // LIST ALL STATES // ============================================ // Get all current state keys console.debug('All states:', state.list) // ============================================ // REMOVE STATE // ============================================ // Remove specific state state.remove('items') // Remove all states // state.removeAll() console.debug('State advanced example complete!')