You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

224 lines
6.6 KiB

/**
* Phase 1.3 Rolling Window Safety Usage Example
*
* Demonstrates rolling window safety functionality
* Shows how notifications are maintained for today and tomorrow
*
* @author Matthew Raymer
* @version 1.0.0
*/
import { DailyNotification } from '@timesafari/daily-notification-plugin';
/**
* Example: Configure rolling window safety
*/
async function configureRollingWindowSafety() {
try {
console.log('Configuring rolling window safety...');
// Configure with rolling window settings
await DailyNotification.configure({
storage: 'shared',
ttlSeconds: 1800, // 30 minutes TTL
prefetchLeadMinutes: 15,
maxNotificationsPerDay: 20 // iOS limit
});
console.log('✅ Rolling window safety configured');
// The plugin will now automatically:
// - Keep today's remaining notifications armed
// - Arm tomorrow's notifications if within iOS caps
// - Maintain window state every 15 minutes
} catch (error) {
console.error('❌ Rolling window configuration failed:', error);
}
}
/**
* Example: Manual rolling window maintenance
*/
async function manualRollingWindowMaintenance() {
try {
console.log('Triggering manual rolling window maintenance...');
// Force window maintenance (useful for testing)
await DailyNotification.maintainRollingWindow();
console.log('✅ Rolling window maintenance completed');
// This will:
// - Arm today's remaining notifications
// - Arm tomorrow's notifications if within capacity
// - Update window state and statistics
} catch (error) {
console.error('❌ Manual maintenance failed:', error);
}
}
/**
* Example: Check rolling window statistics
*/
async function checkRollingWindowStats() {
try {
console.log('Checking rolling window statistics...');
// Get rolling window statistics
const stats = await DailyNotification.getRollingWindowStats();
console.log('📊 Rolling Window Statistics:');
console.log(` Stats: ${stats.stats}`);
console.log(` Maintenance Needed: ${stats.maintenanceNeeded}`);
console.log(` Time Until Next Maintenance: ${stats.timeUntilNextMaintenance}ms`);
// Example output:
// Stats: Rolling window stats: pending=5/100, daily=3/50, platform=Android
// Maintenance Needed: false
// Time Until Next Maintenance: 450000ms (7.5 minutes)
} catch (error) {
console.error('❌ Rolling window stats check failed:', error);
}
}
/**
* Example: Schedule multiple notifications with rolling window
*/
async function scheduleMultipleNotifications() {
try {
console.log('Scheduling multiple notifications with rolling window...');
// Configure rolling window safety
await configureRollingWindowSafety();
// Schedule notifications for different times
const notifications = [
{ time: '08:00', title: 'Morning Update', body: 'Good morning!' },
{ time: '12:00', title: 'Lunch Reminder', body: 'Time for lunch!' },
{ time: '18:00', title: 'Evening Summary', body: 'End of day summary' },
{ time: '22:00', title: 'Good Night', body: 'Time to rest' }
];
for (const notification of notifications) {
await DailyNotification.scheduleDailyNotification({
url: 'https://api.example.com/daily-content',
time: notification.time,
title: notification.title,
body: notification.body
});
}
console.log('✅ Multiple notifications scheduled');
// The rolling window will ensure:
// - All future notifications today are armed
// - Tomorrow's notifications are armed if within iOS caps
// - Window state is maintained automatically
} catch (error) {
console.error('❌ Multiple notification scheduling failed:', error);
}
}
/**
* Example: Demonstrate iOS capacity limits
*/
async function demonstrateIOSCapacityLimits() {
try {
console.log('Demonstrating iOS capacity limits...');
// Configure with iOS-like limits
await DailyNotification.configure({
storage: 'shared',
ttlSeconds: 3600, // 1 hour TTL
prefetchLeadMinutes: 30,
maxNotificationsPerDay: 20 // iOS limit
});
// Schedule many notifications to test capacity
const notifications = [];
for (let i = 0; i < 25; i++) {
notifications.push({
time: `${8 + i}:00`,
title: `Notification ${i + 1}`,
body: `This is notification number ${i + 1}`
});
}
for (const notification of notifications) {
await DailyNotification.scheduleDailyNotification({
url: 'https://api.example.com/daily-content',
time: notification.time,
title: notification.title,
body: notification.body
});
}
console.log('✅ Many notifications scheduled');
// Check statistics to see capacity management
const stats = await DailyNotification.getRollingWindowStats();
console.log('📊 Capacity Management:', stats.stats);
// The rolling window will:
// - Arm notifications up to the daily limit
// - Skip additional notifications if at capacity
// - Log capacity violations for debugging
} catch (error) {
console.error('❌ iOS capacity demonstration failed:', error);
}
}
/**
* Example: Monitor rolling window over time
*/
async function monitorRollingWindowOverTime() {
try {
console.log('Monitoring rolling window over time...');
// Configure rolling window
await configureRollingWindowSafety();
// Schedule some notifications
await scheduleMultipleNotifications();
// Monitor window state over time
const monitorInterval = setInterval(async () => {
try {
const stats = await DailyNotification.getRollingWindowStats();
console.log('📊 Window State:', stats.stats);
if (stats.maintenanceNeeded) {
console.log('⚠️ Maintenance needed, triggering...');
await DailyNotification.maintainRollingWindow();
}
} catch (error) {
console.error('❌ Monitoring error:', error);
}
}, 60000); // Check every minute
// Stop monitoring after 5 minutes
setTimeout(() => {
clearInterval(monitorInterval);
console.log('✅ Monitoring completed');
}, 300000);
} catch (error) {
console.error('❌ Rolling window monitoring failed:', error);
}
}
// Export examples for use
export {
configureRollingWindowSafety,
manualRollingWindowMaintenance,
checkRollingWindowStats,
scheduleMultipleNotifications,
demonstrateIOSCapacityLimits,
monitorRollingWindowOverTime
};