Factory Reboot

Allow your Users to Restart their App if any Errors Occur

The Factory Reboot SDK enables your application to provide users with a method to completely reset their app to its default state when errors occur. This feature clears local data, resets app settings, removes cached content, and returns the application to its default state, improving troubleshooting capabilities.

Quick Start

1. Implement Reset Functionality

Add the reset functionality to your application by using the Despia reset command.

// Reset app to default state
window.despia = "reset://"

2. Trigger on Error Detection

Implement error detection logic to trigger the reset when necessary.

// Example of triggering reset on error detection
try {
  // Your app functionality
} catch (error) {
  console.error("Critical error detected:", error);
  // Trigger app reset
  window.despia = "reset://";
}

3. Provide User Interface

Create a user interface element that allows users to manually reset the app if needed.

// Example of adding a reset button to your UI
document.getElementById('resetButton').addEventListener('click', function() {
  if (confirm('Are you sure you want to reset the app to its default state? All local data will be cleared.')) {
    window.despia = "reset://";
  }
});

How It Works

The Factory Reboot SDK provides a simple but powerful mechanism for resetting your application to its initial state. When the reset command is executed, the SDK:

  • Clears all locally stored data including localStorage, sessionStorage, and IndexedDB content

  • Resets all application settings to their default values

  • Removes cached content to ensure a clean slate

  • Returns the application to its default initialization state

This process effectively gives users a "factory reset" option similar to what they might find on hardware devices, allowing them to resolve persistent issues without uninstalling and reinstalling the application.

Implementation Examples

Below is a basic implementation example showing how to integrate the Factory Reboot functionality in different scenarios.

// Basic implementation
function resetApplication() {
  window.despia = "reset://";
}

// Implementation with confirmation dialog
function resetWithConfirmation() {
  if (confirm('This will reset the app to factory settings. All local data will be lost. Continue?')) {
    window.despia = "reset://";
  }
}

// Implementation with error tracking
function monitorAndReset() {
  window.onerror = function(message, source, lineno, colno, error) {
    console.error("Unhandled error:", error);
    
    // Count errors in session storage
    let errorCount = parseInt(sessionStorage.getItem('errorCount') || 0);
    errorCount++;
    sessionStorage.setItem('errorCount', errorCount);
    
    // If too many errors occur, suggest reset
    if (errorCount > 3) {
      if (confirm('Multiple errors detected. Would you like to reset the app?')) {
        window.despia = "reset://";
      }
    }
    
    return true; // Prevents default error handling
  };
}

Data Structure

The Factory Reboot SDK does not return any data structure as it simply performs the reset operation. The command is a simple string-based instruction:

window.despia = "reset://"

Best Practices

User Experience

  • Always confirm with the user before performing a reset

  • Clearly explain what will be lost during the reset process

  • Provide visual feedback during the reset process

  • Implement as a last-resort troubleshooting option

Implementation

  • Include reset functionality in your error handling strategy

  • Consider adding automatic reset recommendations after multiple errors

  • Provide a manual reset option in your app's settings menu

  • Document the reset feature in your application's help resources

Data Management

  • Consider backing up critical user data before reset if appropriate

  • Implement analytics to track reset frequency for quality improvement

  • Ensure server-side data remains intact if applicable

  • Test reset functionality thoroughly across different scenarios

Complete Implementation Example

Here's a complete implementation example that includes both automatic and manual reset options with proper error handling.

// Factory Reboot - Complete Implementation

// Initialize error tracking
let errorTracker = {
  count: 0,
  threshold: 3,
  resetCount: function() {
    this.count = 0;
    localStorage.setItem('errorCount', 0);
  },
  incrementCount: function() {
    this.count++;
    localStorage.setItem('errorCount', this.count);
    return this.count;
  },
  getCount: function() {
    this.count = parseInt(localStorage.getItem('errorCount') || 0);
    return this.count;
  }
};

// Setup error handling
window.onerror = function(message, source, lineno, colno, error) {
  console.error("Application error:", message);
  
  // Increment error count
  let currentErrorCount = errorTracker.incrementCount();
  
  // If errors exceed threshold, suggest reset
  if (currentErrorCount >= errorTracker.threshold) {
    showResetDialog("Multiple errors have been detected. Would you like to reset the application to resolve these issues?");
  }
  
  return true;
};

// Reset functionality
function resetApplication() {
  console.log("Performing application reset...");
  
  // Show loading indicator
  document.getElementById('resetSpinner').style.display = 'block';
  
  // Perform reset after brief delay to allow UI update
  setTimeout(function() {
    window.despia = "reset://";
  }, 500);
}

// Reset confirmation dialog
function showResetDialog(message) {
  let resetModal = document.getElementById('resetModal');
  document.getElementById('resetMessage').textContent = message;
  resetModal.style.display = 'block';
  
  // Setup confirm button
  document.getElementById('confirmReset').onclick = function() {
    resetModal.style.display = 'none';
    resetApplication();
  };
  
  // Setup cancel button
  document.getElementById('cancelReset').onclick = function() {
    resetModal.style.display = 'none';
    errorTracker.resetCount();
  };
}

// Initialize manual reset button in settings
document.addEventListener('DOMContentLoaded', function() {
  let resetButton = document.getElementById('manualResetButton');
  if (resetButton) {
    resetButton.addEventListener('click', function() {
      showResetDialog("This will reset the application to its default state. All local data will be cleared. Continue?");
    });
  }
});

Troubleshooting

Common Issues

Reset Doesn't Complete

  • Ensure there are no blocking operations preventing the reset

  • Check for service worker conflicts

  • Verify the Despia SDK is properly initialized

  • Try clearing browser cache manually if reset fails

Data Persists After Reset

  • Check if data is being stored in cookies instead of localStorage

  • Verify no background processes are restoring data

  • Ensure reset command is properly formed as "reset://"

  • Check for server-side data persistence if applicable

Reset Causes Application Crash

  • Implement the reset as the final operation in your code

  • Ensure no critical operations depend on data being reset

  • Add a page reload after reset for a clean application state

  • Test on different browsers and devices to ensure compatibility

Need Help?

For additional support or questions, please contact our support team at support@despia.com

Updated on