|
| 1 | +/** |
| 2 | + * Example: Using runWhenBrowserIsIdle for cleanup operations |
| 3 | + * |
| 4 | + * This example demonstrates how to use the cleanupFactory with the |
| 5 | + * runWhenBrowserIsIdle option to perform cleanup operations when |
| 6 | + * the browser is idle, improving performance by not blocking the |
| 7 | + * main thread during busy periods. |
| 8 | + */ |
| 9 | + |
| 10 | +import { cleanupFactory } from "../src/index.mjs"; |
| 11 | + |
| 12 | +// Example 1: Basic idle cleanup |
| 13 | +console.log("🔄 Example 1: Basic idle cleanup"); |
| 14 | + |
| 15 | +// Create cleanup that runs when browser is idle |
| 16 | +const idleCleanup = cleanupFactory(localStorage, { |
| 17 | + expiresInSeconds: 3600, // 1 hour expiration |
| 18 | + runWhenBrowserIsIdle: true, |
| 19 | +}); |
| 20 | + |
| 21 | +// Add some test data |
| 22 | +localStorage.setItem("user-preference", "dark-mode"); |
| 23 | +localStorage.setItem( |
| 24 | + "session-data", |
| 25 | + JSON.stringify({ userId: 123, token: "abc" }) |
| 26 | +); |
| 27 | +localStorage.setItem("temp-cache", "some-cached-data"); |
| 28 | + |
| 29 | +console.log("Added test data to localStorage"); |
| 30 | +console.log("Running idle cleanup..."); |
| 31 | + |
| 32 | +// This will use requestIdleCallback if available, setTimeout as fallback |
| 33 | +idleCleanup.runCleanup(); |
| 34 | + |
| 35 | +console.log("Cleanup scheduled for when browser is idle"); |
| 36 | +console.log("Check developer tools to see the wrapped values in localStorage"); |
| 37 | + |
| 38 | +// Example 2: Comparison with immediate cleanup |
| 39 | +console.log("\n🚀 Example 2: Immediate vs Idle cleanup comparison"); |
| 40 | + |
| 41 | +const immediateCleanup = cleanupFactory(localStorage, { |
| 42 | + expiresInSeconds: 7200, // 2 hours |
| 43 | + runWhenBrowserIsIdle: false, // Run immediately |
| 44 | +}); |
| 45 | + |
| 46 | +// Simulate adding data during heavy computation |
| 47 | +console.log("Simulating heavy computation..."); |
| 48 | +const start = performance.now(); |
| 49 | + |
| 50 | +// Add data |
| 51 | +localStorage.setItem("immediate-test", "test-data"); |
| 52 | + |
| 53 | +// Run immediate cleanup |
| 54 | +immediateCleanup.runCleanup(); |
| 55 | + |
| 56 | +const immediateTime = performance.now() - start; |
| 57 | +console.log(`Immediate cleanup took: ${immediateTime}ms`); |
| 58 | + |
| 59 | +// Now with idle cleanup |
| 60 | +localStorage.setItem("idle-test", "test-data"); |
| 61 | + |
| 62 | +const idleStart = performance.now(); |
| 63 | +const idleCleanup2 = cleanupFactory(localStorage, { |
| 64 | + expiresInSeconds: 7200, |
| 65 | + runWhenBrowserIsIdle: true, |
| 66 | +}); |
| 67 | + |
| 68 | +idleCleanup2.runCleanup(); |
| 69 | +const idleScheduleTime = performance.now() - idleStart; |
| 70 | + |
| 71 | +console.log(`Idle cleanup scheduling took: ${idleScheduleTime}ms`); |
| 72 | +console.log("Actual cleanup will run when browser is idle"); |
| 73 | + |
| 74 | +// Example 3: Periodic idle cleanup |
| 75 | +console.log("\n⏰ Example 3: Periodic idle cleanup"); |
| 76 | + |
| 77 | +function setupPeriodicIdleCleanup() { |
| 78 | + const periodicCleanup = cleanupFactory(localStorage, { |
| 79 | + expiresInSeconds: 1800, // 30 minutes |
| 80 | + runWhenBrowserIsIdle: true, |
| 81 | + }); |
| 82 | + |
| 83 | + // Schedule cleanup every 5 minutes, but only when browser is idle |
| 84 | + setInterval(() => { |
| 85 | + console.log("Running periodic idle cleanup..."); |
| 86 | + periodicCleanup.runCleanup(); |
| 87 | + }, 5 * 60 * 1000); // 5 minutes |
| 88 | +} |
| 89 | + |
| 90 | +// setupPeriodicIdleCleanup(); |
| 91 | +console.log("Periodic cleanup example ready (uncomment to activate)"); |
| 92 | + |
| 93 | +// Example 4: Feature detection |
| 94 | +console.log("\n🔍 Example 4: Feature detection"); |
| 95 | + |
| 96 | +if (typeof window !== "undefined" && window.requestIdleCallback) { |
| 97 | + console.log("✅ Native requestIdleCallback is available"); |
| 98 | +} else { |
| 99 | + console.log("📦 Using polyfill fallback for requestIdleCallback"); |
| 100 | +} |
| 101 | + |
| 102 | +console.log( |
| 103 | + "\n✨ All examples complete! Check localStorage in developer tools." |
| 104 | +); |
0 commit comments