Просмотр исходного кода

impl feature of clearing cache

Yuki Takei 9 лет назад
Родитель
Сommit
31756aa15c

+ 19 - 0
packages/growi-plugin-lsx/src/resource/js/util/Interceptor/LsxPreRenderInterceptor.js

@@ -5,6 +5,7 @@ import BasicInterceptor from '../../../../lib/util/BasicInterceptor';
 
 import { Lsx } from '../../components/Lsx';
 import { LsxContext } from '../LsxContext';
+import { LsxCacheHelper } from '../LsxCacheHelper';
 
 /**
  * The interceptor for lsx
@@ -38,6 +39,8 @@ export class LsxPreRenderInterceptor extends BasicInterceptor {
     const parsedHTML = context.parsedHTML;
     const currentPagePath = context.currentPagePath;
 
+    this.initializeCache(contextName);
+
     context.lsxContextMap = {};
 
     // TODO retrieve from args for interceptor
@@ -71,8 +74,24 @@ export class LsxPreRenderInterceptor extends BasicInterceptor {
     return `<div id="${renderId}" />`;
   }
 
+  /**
+   * initialize cache
+   *  when contextName is 'preRender'         -> clear cache
+   *  when contextName is 'preRenderPreview'  -> doesn't clear cache
+   *
+   * @param {string} contextName
+   *
+   * @memberOf LsxPreRenderInterceptor
+   */
+  initializeCache(contextName) {
+    if (contextName === 'preRender') {
+      LsxCacheHelper.clearAllStateCaches();
+    }
+  }
+
   /**
    * @see http://qiita.com/ryounagaoka/items/4736c225bdd86a74d59c
+   *
    * @param {number} length
    * @return random strings
    */

+ 16 - 5
packages/growi-plugin-lsx/src/resource/js/util/LsxCacheHelper.js

@@ -11,7 +11,7 @@ export class LsxCacheHelper {
    * stringify and save obj
    *
    * @static
-   * @param {any} cacheObj
+   * @param {object} cacheObj
    *
    * @memberOf LsxCacheHelper
    */
@@ -23,7 +23,7 @@ export class LsxCacheHelper {
    * generate cache key for storing to storage
    *
    * @static
-   * @param {any} lsxContext
+   * @param {LsxContext} lsxContext
    * @returns
    *
    * @memberOf LsxCacheHelper
@@ -36,7 +36,7 @@ export class LsxCacheHelper {
    *
    *
    * @static
-   * @param {any} key
+   * @param {string} key
    * @returns
    *
    * @memberOf LsxCacheHelper
@@ -50,8 +50,8 @@ export class LsxCacheHelper {
    * store state object of React Component with specified key
    *
    * @static
-   * @param {any} key
-   * @param {any} lsxState state object of React Component
+   * @param {string} key
+   * @param {object} lsxState state object of React Component
    *
    * @memberOf LsxCacheHelper
    */
@@ -61,4 +61,15 @@ export class LsxCacheHelper {
 
     LsxCacheHelper.saveToSessionStorage(cacheObj);
   }
+
+  /**
+   * clear all state caches
+   *
+   * @static
+   *
+   * @memberOf LsxCacheHelper
+   */
+  static clearAllStateCaches() {
+    LsxCacheHelper.saveToSessionStorage({});
+  }
 }