Browse Source

fix: parameter block not working (#894)

Ahmad Kholid 2 years ago
parent
commit
6259e66ff4

+ 1 - 1
src/components/newtab/workflow/edit/EditParameterPrompt.vue

@@ -9,7 +9,7 @@
     <ui-input
       :model-value="data.timeout"
       type="number"
-      label="Timeout (millisecond)"
+      label="Timeout (millisecond) (0 to disable)"
       class="w-full mt-2"
       @change="updateData({ timeout: +$event })"
     />

+ 8 - 9
src/params/App.vue

@@ -252,10 +252,9 @@ function runWorkflow(index, { data, params }) {
     });
 }
 function cancelParamBlock(index, { data }, message) {
-  const key = `params-prompt:${data.execId}__${data.blockId}`;
   browser.storage.local
     .set({
-      [key]: {
+      [data.promptId]: {
         message,
         $isError: true,
       },
@@ -265,15 +264,11 @@ function cancelParamBlock(index, { data }, message) {
     });
 }
 function continueWorkflow(index, { data, params }) {
-  if (Date.now() > data.timeout) {
-    deleteWorkflow(index);
-    return;
-  }
+  const timeout = Date.now() > data.timeout;
 
-  const key = `params-prompt:${data.execId}__${data.blockId}`;
   browser.storage.local
     .set({
-      [key]: getParamsValues(params),
+      [data.promptId]: timeout ? { $timeout: true } : getParamsValues(params),
     })
     .then(() => {
       deleteWorkflow(index);
@@ -308,7 +303,11 @@ browser.runtime.onMessage.addListener(({ name, data }) => {
     if (!checkTimeout) {
       checkTimeout = setInterval(() => {
         workflows.value.forEach((workflow, index) => {
-          if (workflow.type !== 'block' || Date.now() < workflow.data.timeout)
+          if (
+            workflow.type !== 'block' ||
+            Date.now() < workflow.data.timeout ||
+            workflow.data.timeoutMs <= 0
+          )
             return;
 
           cancelParamBlock(index, workflow, 'Timeout');

+ 1 - 0
src/workflowEngine/blocksHandler/handlerActiveTab.js

@@ -46,6 +46,7 @@ async function activeTab(block) {
 
     const [tab] = await browser.tabs.query({
       active: true,
+      url: '*://*/*',
       lastFocusedWindow: true,
     });
     if (!tab || !tab?.url.startsWith('http')) {

+ 17 - 17
src/workflowEngine/blocksHandler/handlerParameterPrompt.js

@@ -1,18 +1,19 @@
+import { nanoid } from 'nanoid/non-secure';
 import browser from 'webextension-polyfill';
 import { sleep } from '@/utils/helper';
 
-function getInputtedParams({ execId, blockId }, ms) {
+function getInputtedParams(promptId, ms = 10000) {
   return new Promise((resolve, reject) => {
-    let timeout = null;
-    const key = `params-prompt:${execId}__${blockId}`;
+    const timeout = null;
 
     const storageListener = (event) => {
-      if (!event[key]) return;
+      if (!event[promptId]) return;
 
       clearTimeout(timeout);
       browser.storage.onChanged.removeListener(storageListener);
+      browser.storage.local.remove(promptId);
 
-      const { newValue } = event[key];
+      const { newValue } = event[promptId];
       if (newValue.$isError) {
         reject(new Error(newValue.message));
         return;
@@ -21,10 +22,12 @@ function getInputtedParams({ execId, blockId }, ms) {
       resolve(newValue);
     };
 
-    timeout = setTimeout(() => {
-      browser.storage.onChanged.removeListener(storageListener);
-      resolve({});
-    }, ms || 10000);
+    if (ms > 0) {
+      setTimeout(() => {
+        browser.storage.onChanged.removeListener(storageListener);
+        resolve({});
+      }, ms);
+    }
 
     browser.storage.onChanged.addListener(storageListener);
   });
@@ -52,12 +55,15 @@ export default async function ({ data, id }) {
     await browser.windows.update(tab.windowId, { focused: true });
   }
 
-  const timeout = data.timeout || 20000;
+  const promptId = `params-prompt:${nanoid(4)}__${id}`;
+  const { timeout } = data;
 
   await browser.tabs.sendMessage(tab.id, {
     name: 'workflow:params-block',
     data: {
+      promptId,
       blockId: id,
+      timeoutMs: timeout,
       execId: this.engine.id,
       params: data.parameters,
       timeout: Date.now() + timeout,
@@ -67,13 +73,7 @@ export default async function ({ data, id }) {
     },
   });
 
-  const result = await getInputtedParams(
-    {
-      blockId: id,
-      execId: this.engine.id,
-    },
-    timeout
-  );
+  const result = await getInputtedParams(promptId, timeout);
 
   Object.entries(result).forEach(([varName, varValue]) => {
     this.setVariable(varName, varValue);