From 5d27f7d200312b3d7299cf323513c4b558c4a53d Mon Sep 17 00:00:00 2001
From: Lukas <mail@lukasseiler.de>
Date: Thu, 4 Aug 2022 10:54:20 +0200
Subject: [PATCH] Disconnect when LiveKitRoom unmounts

---
 .changeset/little-donuts-attack.md      | 5 +++++
 .changeset/sharp-actors-kick.md         | 6 ++++++
 example/package.json                    | 2 +-
 packages/components/package.json        | 4 ++--
 packages/components/src/LiveKitRoom.tsx | 6 ++++++
 packages/core/package.json              | 4 ++--
 yarn.lock                               | 8 ++++----
 7 files changed, 26 insertions(+), 9 deletions(-)
 create mode 100644 .changeset/little-donuts-attack.md
 create mode 100644 .changeset/sharp-actors-kick.md

diff --git a/.changeset/little-donuts-attack.md b/.changeset/little-donuts-attack.md
new file mode 100644
index 0000000..60403a0
--- /dev/null
+++ b/.changeset/little-donuts-attack.md
@@ -0,0 +1,5 @@
+---
+'@livekit/react-components': patch
+---
+
+Disconnect from room when LiveKitRoom unmounts
diff --git a/.changeset/sharp-actors-kick.md b/.changeset/sharp-actors-kick.md
new file mode 100644
index 0000000..83fbe6d
--- /dev/null
+++ b/.changeset/sharp-actors-kick.md
@@ -0,0 +1,6 @@
+---
+'@livekit/react-components': patch
+'@livekit/react-core': patch
+---
+
+Update livekit client
diff --git a/example/package.json b/example/package.json
index 7935cb9..8659d2f 100644
--- a/example/package.json
+++ b/example/package.json
@@ -13,7 +13,7 @@
   "dependencies": {
     "@fortawesome/free-solid-svg-icons": "^6.1.1",
     "@livekit/react-components": "^1.0.1",
-    "livekit-client": "1.2.7",
+    "livekit-client": "1.2.9",
     "react": "^18.1.0",
     "react-aspect-ratio": "^1.0.50",
     "react-dom": "^18.1.0",
diff --git a/packages/components/package.json b/packages/components/package.json
index 10913f1..ca05d0a 100644
--- a/packages/components/package.json
+++ b/packages/components/package.json
@@ -34,7 +34,7 @@
     "react-tiny-popover": "^6.0.5"
   },
   "peerDependencies": {
-    "livekit-client": "1.2.7",
+    "livekit-client": "1.2.9",
     "react": ">=15",
     "react-dom": ">=15"
   },
@@ -59,7 +59,7 @@
     "eslint-plugin-prettier": "^3.1.1",
     "eslint-plugin-promise": "^4.2.1",
     "eslint-plugin-standard": "^4.0.1",
-    "livekit-client": "1.2.7",
+    "livekit-client": "1.2.9",
     "microbundle-crl": "^0.13.10",
     "npm-run-all": "^4.1.5",
     "prettier": "^2.3.1",
diff --git a/packages/components/src/LiveKitRoom.tsx b/packages/components/src/LiveKitRoom.tsx
index 9bd714b..c134df2 100644
--- a/packages/components/src/LiveKitRoom.tsx
+++ b/packages/components/src/LiveKitRoom.tsx
@@ -42,6 +42,12 @@ export const LiveKitRoom = ({
         onConnected(room);
       }
     });
+
+    return () => {
+      if (roomState.room?.state !== ConnectionState.Disconnected) {
+        roomState.room?.disconnect();
+      }
+    };
   }, []);
 
   const selectedStageRenderer = stageRenderer ?? StageView;
diff --git a/packages/core/package.json b/packages/core/package.json
index d6e69bd..e923641 100644
--- a/packages/core/package.json
+++ b/packages/core/package.json
@@ -24,7 +24,7 @@
     "test:watch": "react-scripts test --env=jsdom"
   },
   "peerDependencies": {
-    "livekit-client": "1.2.7",
+    "livekit-client": "1.2.9",
     "react": ">=15",
     "react-dom": ">=15"
   },
@@ -48,7 +48,7 @@
     "eslint-plugin-prettier": "^3.1.1",
     "eslint-plugin-promise": "^4.2.1",
     "eslint-plugin-standard": "^4.0.1",
-    "livekit-client": "1.2.7",
+    "livekit-client": "1.2.9",
     "microbundle-crl": "^0.13.10",
     "npm-run-all": "^4.1.5",
     "prettier": "^2.3.1",
diff --git a/yarn.lock b/yarn.lock
index f141247..c8eac38 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -7132,10 +7132,10 @@ lines-and-columns@^1.1.6:
   resolved "https://registry.yarnpkg.com/lines-and-columns/-/lines-and-columns-1.2.4.tgz#eca284f75d2965079309dc0ad9255abb2ebc1632"
   integrity sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg==
 
-livekit-client@1.2.7:
-  version "1.2.7"
-  resolved "https://registry.yarnpkg.com/livekit-client/-/livekit-client-1.2.7.tgz#cb59c4b37ccefc16d297b38b3125b71cf9023052"
-  integrity sha512-SbdOQ5U/g2xz8AcSWMSYPUxcTYe5cxyM+GmYOikXG131mYoCqRJnOlb6YfxnPh5ZajE7rV5V7pqxd0IVY3QOHg==
+livekit-client@1.2.9:
+  version "1.2.9"
+  resolved "https://registry.yarnpkg.com/livekit-client/-/livekit-client-1.2.9.tgz#22a87e6680ee049283b8711b2847d8e56d69dbe8"
+  integrity sha512-6o7az6/gX7+IcbXWvezAvQdEa+bbM64J0aiNTg+7OWTPchbY3Bl49P/Q2A/BpjLsBuCN3b40pF6BGKqQCsH+ew==
   dependencies:
     async-await-queue "^1.2.1"
     events "^3.3.0"
