Browse Source

clean code

Yuki Takei 9 years ago
parent
commit
7678c333bb

+ 23 - 12
packages/growi-plugin-lsx/src/resource/js/util/Interceptor/LsxPostRenderInterceptor.js

@@ -34,26 +34,37 @@ export class LsxPostRenderInterceptor extends BasicInterceptor {
    */
   process(contextName, ...args) {
     let contexts = JSON.parse(sessionStorage.getItem('lsx-loading-contexts')) || {};
+
+    let keysToBeRemoved = [];
+
     // forEach keys of contexts
-    Object.keys(contexts).forEach((renderId) => {
-      const elem = document.getElementById(renderId);
+    Object.keys(contexts).forEach((key) => {
+      const elem = document.getElementById(key);
 
       if (elem) {
-        const context = new LsxLoadingContext(contexts[renderId]);
-
+        const contextObj = contexts[key]
+        // remove from context regardless of rendering success or failure
+        delete contexts[key];
         // render
-        ReactDOM.render(
-          <Lsx crowi={this.crowi}
-              currentPagePath={context.currentPagePath}
-              tagExpression={context.tagExpression}
-              fromPagePath={context.fromPagePath}
-              lsxArgs={context.lsxArgs} />,
-          elem
-        );
+        this.renderReactDOM(contextObj, elem);
       }
     });
 
+    // store contexts to sessionStorage
+    sessionStorage.setItem('lsx-loading-contexts', JSON.stringify(contexts));
+
     return Promise.resolve();
   }
 
+  renderReactDOM(contextObj, elem) {
+    const context = new LsxLoadingContext(contextObj);
+    ReactDOM.render(
+      <Lsx crowi={this.crowi}
+          currentPagePath={context.currentPagePath}
+          tagExpression={context.tagExpression}
+          fromPagePath={context.fromPagePath}
+          lsxArgs={context.lsxArgs} />,
+      elem
+    );
+  }
 }

+ 2 - 2
packages/growi-plugin-lsx/src/resource/js/util/PreProcessor/LsxPreProcessor.js

@@ -19,10 +19,10 @@ export class LsxPreProcessor {
       // see: https://regex101.com/r/NQq3s9/2
       .replace(/\$lsx\((.*)\)/g, (all, group1) => {
         const tagExpression = all;
-        const lsxArgs = group1;
+        const lsxArgs = group1.trim();
 
         // get cache container obj from sessionStorage
-        let lsxCache = sessionStorage.getItem('lsx-cache');
+        let lsxCache = JSON.parse(sessionStorage.getItem('lsx-cache'));
         if (lsxCache) {
           lsxCache = Object.create(LsxCache, lsxCache);
           const cache = lsxCache.getItem(tagExpression);