From 5bac478c6f8c2e0114bd3d4263d5ff76a22fceb3 Mon Sep 17 00:00:00 2001
From: Marin Karamihalev <marin.karamihalev@iopsys.eu>
Date: Tue, 22 Nov 2022 13:38:05 +0100
Subject: [PATCH] fixed isAsync bug on operate command

---
 package.json                    |  2 +-
 src/commands/recipes/operate.ts | 22 +++++++++++++---------
 2 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/package.json b/package.json
index c962764..371670a 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "usp-js",
-  "version": "0.3.4",
+  "version": "0.3.5",
   "description": "Helper library for easy usp communication using mqtt over tcp or ws.",
   "main": "node/index.js",
   "browser": "web/index.js",
diff --git a/src/commands/recipes/operate.ts b/src/commands/recipes/operate.ts
index f8c2f2c..de976d6 100644
--- a/src/commands/recipes/operate.ts
+++ b/src/commands/recipes/operate.ts
@@ -7,7 +7,8 @@ const make: MakeFn =
   (call, on): OperateRecipe =>
   async (path, opts) => {
     const Persistent = opts?.Persistent === undefined ? false : opts.Persistent;
-    const id = "NOTIFY@" + (opts?.ID || uniq(path));
+    const isAsync = !(opts && opts.isAsync === false);
+    const id = (isAsync ? "NOTIFY@" : "OPERATE@") + (opts?.ID || uniq(path));
     const operateInput = {
       Enable: true,
       ID: id,
@@ -18,11 +19,6 @@ const make: MakeFn =
 
     const commandKey = opts?.commandKey || "";
 
-    const newSubPath = await call("ADD", {
-      path: operateSubscriptionPath,
-      value: operateInput,
-    });
-
     const command: OperateFn = (input) => {
       return call(
         "OPERATE",
@@ -33,7 +29,7 @@ const make: MakeFn =
           resp: opts?.sendResponse ?? true,
           commandKey,
         },
-        opts && opts.isAsync === false ? {} : { responseMsgType: "NOTIFY" }
+        isAsync ? { responseMsgType: "NOTIFY" } : {}
       );
     };
 
@@ -54,9 +50,17 @@ const make: MakeFn =
       };
     };
 
-    const cleanup: OperateClearFn = () => call("DELETE", { paths: newSubPath });
+    if (isAsync) {
+      const newSubPath = await call("ADD", {
+        path: operateSubscriptionPath,
+        value: operateInput,
+      });
+
+      const cleanup: OperateClearFn = () =>
+        call("DELETE", { paths: newSubPath });
 
-    return [command, cleanup];
+      return [command, cleanup];
+    } else return [command, () => Promise.resolve()];
   };
 
 export default {
-- 
GitLab