Browse Source

Merge pull request #423 from JeremLopes/dev

feat: add clear cache on workflow success
Ahmad Kholid 3 years ago
parent
commit
e9bc91c16c

+ 3 - 0
src/background/workflow-engine/engine.js

@@ -2,6 +2,7 @@ import browser from 'webextension-polyfill';
 import { nanoid } from 'nanoid';
 import { tasks } from '@/utils/shared';
 import {
+  clearCache,
   toCamelCase,
   sleep,
   parseJSON,
@@ -361,6 +362,8 @@ class WorkflowEngine {
         });
       }
 
+      if (status === 'success') clearCache(this.workflow);
+
       this.isDestroyed = true;
       this.eventListeners = {};
     } catch (error) {

+ 5 - 22
src/components/newtab/workflow/WorkflowSettings.vue

@@ -66,7 +66,7 @@
           {{ t('workflow.settings.clearCache.description') }}
         </p>
       </div>
-      <ui-button @click="clearCache">
+      <ui-button @click="onClearCacheClick">
         {{ t('workflow.settings.clearCache.btn') }}
       </ui-button>
     </div>
@@ -76,8 +76,7 @@
 import { onMounted, reactive, watch } from 'vue';
 import { useI18n } from 'vue-i18n';
 import { useToast } from 'vue-toastification';
-import browser from 'webextension-polyfill';
-import { debounce, parseJSON } from '@/utils/helper';
+import { clearCache, debounce } from '@/utils/helper';
 
 const props = defineProps({
   workflow: {
@@ -131,25 +130,9 @@ const settings = reactive({
   restartTimes: 3,
 });
 
-async function clearCache() {
-  try {
-    await browser.storage.local.remove(`last-state:${props.workflow.id}`);
-
-    const flows = parseJSON(props.workflow.drawflow, null);
-    const blocks = flows && flows.drawflow.Home.data;
-
-    if (blocks) {
-      Object.values(blocks).forEach(({ name, id }) => {
-        if (name !== 'loop-data') return;
-
-        localStorage.removeItem(`index:${id}`);
-      });
-    }
-
-    toast(t('workflow.settings.clearCache.info'));
-  } catch (error) {
-    console.error(error);
-  }
+async function onClearCacheClick() {
+  const cacheCleared = await clearCache(props.workflow);
+  if (cacheCleared) toast(t('workflow.settings.clearCache.info'));
 }
 
 watch(

+ 24 - 0
src/utils/helper.js

@@ -1,3 +1,5 @@
+import browser from 'webextension-polyfill';
+
 export function scrollIfNeeded(element) {
   const { top, left, bottom, right } = element.getBoundingClientRect();
   const isInViewport =
@@ -198,3 +200,25 @@ export function debounce(callback, time = 200) {
     });
   };
 }
+
+export async function clearCache(workflow) {
+  try {
+    await browser.storage.local.remove(`last-state:${workflow.id}`);
+
+    const flows = parseJSON(workflow.drawflow, null);
+    const blocks = flows && flows.drawflow.Home.data;
+
+    if (blocks) {
+      Object.values(blocks).forEach(({ name, id }) => {
+        if (name !== 'loop-data') return;
+
+        localStorage.removeItem(`index:${id}`);
+      });
+    }
+
+    return true;
+  } catch (error) {
+    console.error(error);
+    return false;
+  }
+}