/** * Memorio Session Advanced Example * * This example shows advanced session (sessionStorage) operations. * Session data is cleared when the browser tab closes. * * Run: npx ts-node examples/session-advanced.ts */ import 'memorio' // ============================================ // CHECK PERSISTENCE // ============================================ console.debug('=== Session Persistence Check ===') console.debug('Is persistent (survives tab close):', session.isPersistent) // In browser: true (sessionStorage) // In Node.js/Deno: false (memory fallback) if (!session.isPersistent) { console.debug('⚠️ Warning: Using in-memory storage. Data will be lost on process restart!') } // ============================================ // AUTHENTICATION // ============================================ // Store auth token (temporary - cleared when tab closes) session.set('authToken', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...') session.set('userId', 12345) // Check if user is logged in const token = session.get('authToken') if (token) { console.debug('User is logged in, token:', token.substring(0, 20) + '...') } // ============================================ // FORM PROGRESS // ============================================ // Save form draft session.set('formDraft', { name: 'Mario', email: 'mario@example.com', message: 'Hello world!' }) // Restore on page refresh const draft = session.get('formDraft') if (draft) { console.debug('Restored draft:', draft) } // ============================================ // SHOPPING CART // ============================================ // Store cart items (temporary) session.set('cart', [ { id: 1, name: 'Super Mushroom', price: 99, qty: 2 }, { id: 2, name: 'Fire Flower', price: 199, qty: 1 } ]) // Calculate total const cart = session.get('cart') || [] const total = cart.reduce((sum, item) => sum + (item.price * item.qty), 0) console.debug('Cart total:', total) // ============================================ // SESSION SIZE // ============================================ // Get session storage size console.debug('Session size:', session.size(), 'bytes') // ============================================ // CLEANUP // ============================================ // Remove specific item session.remove('formDraft') // Clear all session data (logout) // session.removeAll() // Or use alias // session.clearAll() console.debug('Session advanced example complete!')