1. class FormCache {
  2. constructor() {
  3. this.cache = {}; // Store form data and results
  4. this.logLevel = 'verbose'; // Set log level (e.g., 'debug', 'info', 'warn', 'error', 'verbose')
  5. }
  6. _log(message) {
  7. console.log(`[${this.logLevel}] ${message}`);
  8. }
  9. /**
  10. * Caches the result of a web form submission.
  11. * @param {string} formId - Unique identifier for the form.
  12. * @param {object} formData - The form data.
  13. * @param {function} submitForm - Function to submit the form (returns a promise).
  14. * @returns {Promise<any>} - A promise that resolves with the cached result, or rejects if not cached.
  15. */
  16. cacheFormResult(formId, formData, submitForm) {
  17. this._log(`Caching form ${formId} with data: ${JSON.stringify(formData)}`);
  18. const cacheKey = `${formId}-${JSON.stringify(formData)}`;
  19. if (this.cache[cacheKey]) {
  20. this._log(`Retrieving from cache for form ${formId}`);
  21. return this.cache[cacheKey];
  22. }
  23. return submitForm()
  24. .then(result => {
  25. this.cache[cacheKey] = result; // Store the result in the cache
  26. this._log(`Form ${formId} cached successfully.`);
  27. return result;
  28. })
  29. .catch(error => {
  30. this._log(`Error submitting form ${formId}:`, error);
  31. throw error; // Re-throw the error
  32. });
  33. }
  34. /**
  35. * Retrieves a form result from the cache.
  36. * @param {string} formId - Unique identifier for the form.
  37. * @param {object} formData - The form data.
  38. * @returns {any} - The cached result, or undefined if not found.
  39. */
  40. getFormResult(formId, formData) {
  41. const cacheKey = `${formId}-${JSON.stringify(formData)}`;
  42. if (this.cache[cacheKey]) {
  43. this._log(`Retrieving from cache for form ${formId}`);
  44. return this.cache[cacheKey];
  45. } else {
  46. this._log(`Form ${formId} not found in cache.`);
  47. return undefined;
  48. }
  49. }
  50. /**
  51. * Clears the cache.
  52. */
  53. clearCache() {
  54. this._log("Clearing the cache.");
  55. this.cache = {};
  56. }
  57. }
  58. // Example Usage (replace with your actual form submission logic)
  59. // const formCache = new FormCache();
  60. // async function submitMyForm(formData) {
  61. // // Simulate a form submission with a promise
  62. // return new Promise(resolve => {
  63. // setTimeout(() => {
  64. // resolve(`Result for form with data: ${JSON.stringify(formData)}`);
  65. // }, 500);
  66. // });
  67. // }
  68. // async function main() {
  69. // const formId = "my-form";
  70. // const formData = { name: "John Doe", age: 30 };
  71. // // First submission - submit the form
  72. // const result1 = await formCache.cacheFormResult(formId, formData, submitMyForm);
  73. // console.log("First result:", result1);
  74. // // Second submission with the same data - retrieve from cache
  75. // const result2 = formCache.getFormResult(formId, formData);
  76. // console.log("Second result (from cache):", result2);
  77. // //Try getting a non-cached form
  78. // const result3 = formCache.getFormResult('different-form', {a:1});
  79. // console.log("Third result (not cached):", result3);
  80. // //Clear the cache
  81. // formCache.clearCache();
  82. // }
  83. // main();

Add your comment