From 6a090f63ab1076eb307fd0ba3d1cb86619906f9f Mon Sep 17 00:00:00 2001 From: Twilight <46562212+twlite@users.noreply.github.com> Date: Sun, 10 Nov 2024 11:01:13 +0545 Subject: [PATCH 1/6] feat(websocket): add ability to remove listener Change listeners array to use a set instead, which prevents addition of duplicated listeners which could possibly lead to memory leaks. --- plugins/websocket/guest-js/index.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/plugins/websocket/guest-js/index.ts b/plugins/websocket/guest-js/index.ts index 8d787742aa..b71b5847eb 100644 --- a/plugins/websocket/guest-js/index.ts +++ b/plugins/websocket/guest-js/index.ts @@ -32,9 +32,9 @@ export type Message = export default class WebSocket { id: number - private readonly listeners: Array<(arg: Message) => void> + private readonly listeners: Set<(arg: Message) => void> - constructor(id: number, listeners: Array<(arg: Message) => void>) { + constructor(id: number, listeners: Set<(arg: Message) => void>) { this.id = id this.listeners = listeners } @@ -43,7 +43,7 @@ export default class WebSocket { url: string, config?: ConnectionConfig ): Promise { - const listeners: Array<(arg: Message) => void> = [] + const listeners: Set<(arg: Message) => void> = new Set() const onMessage = new Channel() onMessage.onmessage = (message: Message): void => { @@ -63,8 +63,12 @@ export default class WebSocket { }).then((id) => new WebSocket(id, listeners)) } - addListener(cb: (arg: Message) => void): void { - this.listeners.push(cb) + addListener(cb: (arg: Message) => void): () => void { + this.listeners.add(cb) + + return () => { + this.listeners.delete(cb) + } } async send(message: Message | string | number[]): Promise { From 07bf5725f3e12cf60656e2efa2e4f12c8fd09878 Mon Sep 17 00:00:00 2001 From: Twilight <46562212+twlite@users.noreply.github.com> Date: Sun, 10 Nov 2024 11:06:32 +0545 Subject: [PATCH 2/6] chore: add changelog --- .changes/cleanup-fn-websocket.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changes/cleanup-fn-websocket.md diff --git a/.changes/cleanup-fn-websocket.md b/.changes/cleanup-fn-websocket.md new file mode 100644 index 0000000000..1af811979f --- /dev/null +++ b/.changes/cleanup-fn-websocket.md @@ -0,0 +1,5 @@ +--- +websocket-js: minor +--- + +`addListener` now returns a cleanup function instead of `void` to remove the listener. \ No newline at end of file From ca913a6e57318c67b81fadcf856b72ba73a5a3cb Mon Sep 17 00:00:00 2001 From: Twilight <46562212+twlite@users.noreply.github.com> Date: Mon, 11 Nov 2024 15:35:36 +0000 Subject: [PATCH 3/6] fix: build iife --- plugins/websocket/api-iife.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/websocket/api-iife.js b/plugins/websocket/api-iife.js index ec81c68f3a..7cae996db8 100644 --- a/plugins/websocket/api-iife.js +++ b/plugins/websocket/api-iife.js @@ -1 +1 @@ -if("__TAURI__"in window){var __TAURI_PLUGIN_WEBSOCKET__=function(){"use strict";function e(e,t,s,n){if("a"===s&&!n)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!n:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===s?n:"a"===s?n.call(e):n?n.value:t.get(e)}function t(e,t,s,n,r){if("function"==typeof t?e!==t||!r:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return t.set(e,s),s}var s,n,r;"function"==typeof SuppressedError&&SuppressedError;class i{constructor(){this.__TAURI_CHANNEL_MARKER__=!0,s.set(this,(()=>{})),n.set(this,0),r.set(this,{}),this.id=function(e,t=!1){return window.__TAURI_INTERNALS__.transformCallback(e,t)}((({message:i,id:a})=>{if(a===e(this,n,"f")){t(this,n,a+1),e(this,s,"f").call(this,i);const o=Object.keys(e(this,r,"f"));if(o.length>0){let i=a+1;for(const t of o.sort()){if(parseInt(t)!==i)break;{const n=e(this,r,"f")[t];delete e(this,r,"f")[t],e(this,s,"f").call(this,n),i+=1}}t(this,n,i)}}else e(this,r,"f")[a.toString()]=i}))}set onmessage(e){t(this,s,e)}get onmessage(){return e(this,s,"f")}toJSON(){return`__CHANNEL__:${this.id}`}}async function a(e,t={},s){return window.__TAURI_INTERNALS__.invoke(e,t,s)}s=new WeakMap,n=new WeakMap,r=new WeakMap;class o{constructor(e,t){this.id=e,this.listeners=t}static async connect(e,t){const s=[],n=new i;return n.onmessage=e=>{s.forEach((t=>{t(e)}))},t?.headers&&(t.headers=Array.from(new Headers(t.headers).entries())),await a("plugin:websocket|connect",{url:e,onMessage:n,config:t}).then((e=>new o(e,s)))}addListener(e){this.listeners.push(e)}async send(e){let t;if("string"==typeof e)t={type:"Text",data:e};else if("object"==typeof e&&"type"in e)t=e;else{if(!Array.isArray(e))throw new Error("invalid `message` type, expected a `{ type: string, data: any }` object, a string or a numeric array");t={type:"Binary",data:e}}await a("plugin:websocket|send",{id:this.id,message:t})}async disconnect(){await this.send({type:"Close",data:{code:1e3,reason:"Disconnected by client"}})}}return o}();Object.defineProperty(window.__TAURI__,"websocket",{value:__TAURI_PLUGIN_WEBSOCKET__})} +if("__TAURI__"in window){var __TAURI_PLUGIN_WEBSOCKET__=function(){"use strict";function e(e,t,s,n){if("a"===s&&!n)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!n:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===s?n:"a"===s?n.call(e):n?n.value:t.get(e)}function t(e,t,s,n,r){if("function"==typeof t?e!==t||!r:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return t.set(e,s),s}var s,n,r;"function"==typeof SuppressedError&&SuppressedError;class i{constructor(){this.__TAURI_CHANNEL_MARKER__=!0,s.set(this,(()=>{})),n.set(this,0),r.set(this,{}),this.id=function(e,t=!1){return window.__TAURI_INTERNALS__.transformCallback(e,t)}((({message:i,id:a})=>{if(a===e(this,n,"f")){t(this,n,a+1),e(this,s,"f").call(this,i);const o=Object.keys(e(this,r,"f"));if(o.length>0){let i=a+1;for(const t of o.sort()){if(parseInt(t)!==i)break;{const n=e(this,r,"f")[t];delete e(this,r,"f")[t],e(this,s,"f").call(this,n),i+=1}}t(this,n,i)}}else e(this,r,"f")[a.toString()]=i}))}set onmessage(e){t(this,s,e)}get onmessage(){return e(this,s,"f")}toJSON(){return`__CHANNEL__:${this.id}`}}async function a(e,t={},s){return window.__TAURI_INTERNALS__.invoke(e,t,s)}s=new WeakMap,n=new WeakMap,r=new WeakMap;class o{constructor(e,t){this.id=e,this.listeners=t}static async connect(e,t){const s=new Set,n=new i;return n.onmessage=e=>{s.forEach((t=>{t(e)}))},t?.headers&&(t.headers=Array.from(new Headers(t.headers).entries())),await a("plugin:websocket|connect",{url:e,onMessage:n,config:t}).then((e=>new o(e,s)))}addListener(e){return this.listeners.add(e),()=>{this.listeners.delete(e)}}async send(e){let t;if("string"==typeof e)t={type:"Text",data:e};else if("object"==typeof e&&"type"in e)t=e;else{if(!Array.isArray(e))throw new Error("invalid `message` type, expected a `{ type: string, data: any }` object, a string or a numeric array");t={type:"Binary",data:e}}await a("plugin:websocket|send",{id:this.id,message:t})}async disconnect(){await this.send({type:"Close",data:{code:1e3,reason:"Disconnected by client"}})}}return o}();Object.defineProperty(window.__TAURI__,"websocket",{value:__TAURI_PLUGIN_WEBSOCKET__})} From 4a86210375e00e18dfc85c9cf3d52d694d91f57d Mon Sep 17 00:00:00 2001 From: Twilight <46562212+twlite@users.noreply.github.com> Date: Tue, 12 Nov 2024 16:42:31 +0000 Subject: [PATCH 4/6] fix: rebuild websocket api-iife --- plugins/websocket/api-iife.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/websocket/api-iife.js b/plugins/websocket/api-iife.js index 7cae996db8..7b728fe595 100644 --- a/plugins/websocket/api-iife.js +++ b/plugins/websocket/api-iife.js @@ -1 +1 @@ -if("__TAURI__"in window){var __TAURI_PLUGIN_WEBSOCKET__=function(){"use strict";function e(e,t,s,n){if("a"===s&&!n)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!n:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===s?n:"a"===s?n.call(e):n?n.value:t.get(e)}function t(e,t,s,n,r){if("function"==typeof t?e!==t||!r:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return t.set(e,s),s}var s,n,r;"function"==typeof SuppressedError&&SuppressedError;class i{constructor(){this.__TAURI_CHANNEL_MARKER__=!0,s.set(this,(()=>{})),n.set(this,0),r.set(this,{}),this.id=function(e,t=!1){return window.__TAURI_INTERNALS__.transformCallback(e,t)}((({message:i,id:a})=>{if(a===e(this,n,"f")){t(this,n,a+1),e(this,s,"f").call(this,i);const o=Object.keys(e(this,r,"f"));if(o.length>0){let i=a+1;for(const t of o.sort()){if(parseInt(t)!==i)break;{const n=e(this,r,"f")[t];delete e(this,r,"f")[t],e(this,s,"f").call(this,n),i+=1}}t(this,n,i)}}else e(this,r,"f")[a.toString()]=i}))}set onmessage(e){t(this,s,e)}get onmessage(){return e(this,s,"f")}toJSON(){return`__CHANNEL__:${this.id}`}}async function a(e,t={},s){return window.__TAURI_INTERNALS__.invoke(e,t,s)}s=new WeakMap,n=new WeakMap,r=new WeakMap;class o{constructor(e,t){this.id=e,this.listeners=t}static async connect(e,t){const s=new Set,n=new i;return n.onmessage=e=>{s.forEach((t=>{t(e)}))},t?.headers&&(t.headers=Array.from(new Headers(t.headers).entries())),await a("plugin:websocket|connect",{url:e,onMessage:n,config:t}).then((e=>new o(e,s)))}addListener(e){return this.listeners.add(e),()=>{this.listeners.delete(e)}}async send(e){let t;if("string"==typeof e)t={type:"Text",data:e};else if("object"==typeof e&&"type"in e)t=e;else{if(!Array.isArray(e))throw new Error("invalid `message` type, expected a `{ type: string, data: any }` object, a string or a numeric array");t={type:"Binary",data:e}}await a("plugin:websocket|send",{id:this.id,message:t})}async disconnect(){await this.send({type:"Close",data:{code:1e3,reason:"Disconnected by client"}})}}return o}();Object.defineProperty(window.__TAURI__,"websocket",{value:__TAURI_PLUGIN_WEBSOCKET__})} +if("__TAURI__"in window){var __TAURI_PLUGIN_WEBSOCKET__=function(){"use strict";function e(e,t,s,n){if("a"===s&&!n)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!n:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===s?n:"a"===s?n.call(e):n?n.value:t.get(e)}function t(e,t,s,n,r){if("function"==typeof t?e!==t||!r:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return t.set(e,s),s}var s,n,r;"function"==typeof SuppressedError&&SuppressedError;const i="__TAURI_TO_IPC_KEY__";class a{constructor(){this.__TAURI_CHANNEL_MARKER__=!0,s.set(this,(()=>{})),n.set(this,0),r.set(this,{}),this.id=function(e,t=!1){return window.__TAURI_INTERNALS__.transformCallback(e,t)}((({message:i,id:a})=>{if(a===e(this,n,"f")){t(this,n,a+1),e(this,s,"f").call(this,i);const o=Object.keys(e(this,r,"f"));if(o.length>0){let i=a+1;for(const t of o.sort()){if(parseInt(t)!==i)break;{const n=e(this,r,"f")[t];delete e(this,r,"f")[t],e(this,s,"f").call(this,n),i+=1}}t(this,n,i)}}else e(this,r,"f")[a.toString()]=i}))}set onmessage(e){t(this,s,e)}get onmessage(){return e(this,s,"f")}[(s=new WeakMap,n=new WeakMap,r=new WeakMap,i)](){return`__CHANNEL__:${this.id}`}toJSON(){return this[i]()}}async function o(e,t={},s){return window.__TAURI_INTERNALS__.invoke(e,t,s)}class c{constructor(e,t){this.id=e,this.listeners=t}static async connect(e,t){const s=new Set,n=new a;return n.onmessage=e=>{s.forEach((t=>{t(e)}))},t?.headers&&(t.headers=Array.from(new Headers(t.headers).entries())),await o("plugin:websocket|connect",{url:e,onMessage:n,config:t}).then((e=>new c(e,s)))}addListener(e){return this.listeners.add(e),()=>{this.listeners.delete(e)}}async send(e){let t;if("string"==typeof e)t={type:"Text",data:e};else if("object"==typeof e&&"type"in e)t=e;else{if(!Array.isArray(e))throw new Error("invalid `message` type, expected a `{ type: string, data: any }` object, a string or a numeric array");t={type:"Binary",data:e}}await o("plugin:websocket|send",{id:this.id,message:t})}async disconnect(){await this.send({type:"Close",data:{code:1e3,reason:"Disconnected by client"}})}}return c}();Object.defineProperty(window.__TAURI__,"websocket",{value:__TAURI_PLUGIN_WEBSOCKET__})} From f59a074959182ac80c6d8a65f6ef6278b16f55e9 Mon Sep 17 00:00:00 2001 From: Twilight <46562212+twlite@users.noreply.github.com> Date: Mon, 13 Jan 2025 18:37:32 +0000 Subject: [PATCH 5/6] chore: rebuild websocket iife --- plugins/websocket/api-iife.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/websocket/api-iife.js b/plugins/websocket/api-iife.js index 7b728fe595..219d828b6e 100644 --- a/plugins/websocket/api-iife.js +++ b/plugins/websocket/api-iife.js @@ -1 +1 @@ -if("__TAURI__"in window){var __TAURI_PLUGIN_WEBSOCKET__=function(){"use strict";function e(e,t,s,n){if("a"===s&&!n)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!n:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===s?n:"a"===s?n.call(e):n?n.value:t.get(e)}function t(e,t,s,n,r){if("function"==typeof t?e!==t||!r:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return t.set(e,s),s}var s,n,r;"function"==typeof SuppressedError&&SuppressedError;const i="__TAURI_TO_IPC_KEY__";class a{constructor(){this.__TAURI_CHANNEL_MARKER__=!0,s.set(this,(()=>{})),n.set(this,0),r.set(this,{}),this.id=function(e,t=!1){return window.__TAURI_INTERNALS__.transformCallback(e,t)}((({message:i,id:a})=>{if(a===e(this,n,"f")){t(this,n,a+1),e(this,s,"f").call(this,i);const o=Object.keys(e(this,r,"f"));if(o.length>0){let i=a+1;for(const t of o.sort()){if(parseInt(t)!==i)break;{const n=e(this,r,"f")[t];delete e(this,r,"f")[t],e(this,s,"f").call(this,n),i+=1}}t(this,n,i)}}else e(this,r,"f")[a.toString()]=i}))}set onmessage(e){t(this,s,e)}get onmessage(){return e(this,s,"f")}[(s=new WeakMap,n=new WeakMap,r=new WeakMap,i)](){return`__CHANNEL__:${this.id}`}toJSON(){return this[i]()}}async function o(e,t={},s){return window.__TAURI_INTERNALS__.invoke(e,t,s)}class c{constructor(e,t){this.id=e,this.listeners=t}static async connect(e,t){const s=new Set,n=new a;return n.onmessage=e=>{s.forEach((t=>{t(e)}))},t?.headers&&(t.headers=Array.from(new Headers(t.headers).entries())),await o("plugin:websocket|connect",{url:e,onMessage:n,config:t}).then((e=>new c(e,s)))}addListener(e){return this.listeners.add(e),()=>{this.listeners.delete(e)}}async send(e){let t;if("string"==typeof e)t={type:"Text",data:e};else if("object"==typeof e&&"type"in e)t=e;else{if(!Array.isArray(e))throw new Error("invalid `message` type, expected a `{ type: string, data: any }` object, a string or a numeric array");t={type:"Binary",data:e}}await o("plugin:websocket|send",{id:this.id,message:t})}async disconnect(){await this.send({type:"Close",data:{code:1e3,reason:"Disconnected by client"}})}}return c}();Object.defineProperty(window.__TAURI__,"websocket",{value:__TAURI_PLUGIN_WEBSOCKET__})} +if("__TAURI__"in window){var __TAURI_PLUGIN_WEBSOCKET__=function(){"use strict";function e(e,t,s,n){if("a"===s&&!n)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!n:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===s?n:"a"===s?n.call(e):n?n.value:t.get(e)}function t(e,t,s,n,i){if("function"==typeof t||!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return t.set(e,s),s}var s,n,i;"function"==typeof SuppressedError&&SuppressedError;const r="__TAURI_TO_IPC_KEY__";class a{constructor(){this.__TAURI_CHANNEL_MARKER__=!0,s.set(this,(()=>{})),n.set(this,0),i.set(this,[]),this.id=function(e,t=!1){return window.__TAURI_INTERNALS__.transformCallback(e,t)}((({message:r,id:a})=>{if(a==e(this,n,"f"))for(e(this,s,"f").call(this,r),t(this,n,e(this,n,"f")+1);e(this,n,"f")in e(this,i,"f");){const r=e(this,i,"f")[e(this,n,"f")];e(this,s,"f").call(this,r),delete e(this,i,"f")[e(this,n,"f")],t(this,n,e(this,n,"f")+1)}else e(this,i,"f")[a]=r}))}set onmessage(e){t(this,s,e)}get onmessage(){return e(this,s,"f")}[(s=new WeakMap,n=new WeakMap,i=new WeakMap,r)](){return`__CHANNEL__:${this.id}`}toJSON(){return this[r]()}}async function o(e,t={},s){return window.__TAURI_INTERNALS__.invoke(e,t,s)}class c{constructor(e,t){this.id=e,this.listeners=t}static async connect(e,t){const s=new Set,n=new a;return n.onmessage=e=>{s.forEach((t=>{t(e)}))},t?.headers&&(t.headers=Array.from(new Headers(t.headers).entries())),await o("plugin:websocket|connect",{url:e,onMessage:n,config:t}).then((e=>new c(e,s)))}addListener(e){return this.listeners.add(e),()=>{this.listeners.delete(e)}}async send(e){let t;if("string"==typeof e)t={type:"Text",data:e};else if("object"==typeof e&&"type"in e)t=e;else{if(!Array.isArray(e))throw new Error("invalid `message` type, expected a `{ type: string, data: any }` object, a string or a numeric array");t={type:"Binary",data:e}}await o("plugin:websocket|send",{id:this.id,message:t})}async disconnect(){await this.send({type:"Close",data:{code:1e3,reason:"Disconnected by client"}})}}return c}();Object.defineProperty(window.__TAURI__,"websocket",{value:__TAURI_PLUGIN_WEBSOCKET__})} From 5d6a22cb3976b5d18ffe6203a42a43421414f678 Mon Sep 17 00:00:00 2001 From: Twilight <46562212+twlite@users.noreply.github.com> Date: Tue, 14 Jan 2025 00:49:22 +0545 Subject: [PATCH 6/6] chore: bump rust package --- .changes/cleanup-fn-websocket.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.changes/cleanup-fn-websocket.md b/.changes/cleanup-fn-websocket.md index 1af811979f..59348e6eb7 100644 --- a/.changes/cleanup-fn-websocket.md +++ b/.changes/cleanup-fn-websocket.md @@ -1,5 +1,6 @@ --- websocket-js: minor +websocket: minor --- -`addListener` now returns a cleanup function instead of `void` to remove the listener. \ No newline at end of file +`addListener` now returns a cleanup function instead of `void` to remove the listener.