From 0a9f3f7469cec1b0f0c22789c28ba584bca8bb2d Mon Sep 17 00:00:00 2001 From: Vasilica Olariu Date: Wed, 17 Sep 2025 15:49:02 +0300 Subject: [PATCH 1/2] Add new method: destroy() --- src/main.ts | 23 ++++++++++++++++++++++- types/src/main.d.ts | 2 +- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/main.ts b/src/main.ts index 4141e8ac..db62181d 100644 --- a/src/main.ts +++ b/src/main.ts @@ -38,7 +38,7 @@ export type NavigationAppProps = { integrations?: {[key: string]: 'disable'} } -export type TcUniNavMethods = 'init' | 'update' | 'triggerFlow' +export type TcUniNavMethods = 'init' | 'update' | 'destroy' | 'triggerFlow' export type TcUniNavFn = ( method: TcUniNavMethods, @@ -54,6 +54,23 @@ const NavigationLoadersMap = { const instancesContextStore: { [key: string]: Map> } = {} +async function destroy( + targetId: string, +) { + if (typeof targetId !== 'string') { + throw new Error(`'targetId' should be a string`); + } + + const targetEl: Element | null = document.getElementById(targetId); + + if (targetEl?.nodeType !== Node.ELEMENT_NODE) { + throw new Error(`[TcUnivNav] 'target' must be a valid dom element with an id of #${targetId}!`); + } + + targetEl.innerHTML = ''; + delete instancesContextStore[targetId]; +} + /** * Initialize the navigation component * @param targetId Target element ID @@ -161,6 +178,10 @@ function execQueueCall(method: TcUniNavMethods, ...args: any[]) { triggerFlow.call(null, ...args) } + else if (method === 'destroy') { + destroy.call(null, ...args); + } + else if (method === 'trigger') { appPubSub.publish.call(appPubSub, ...args); } diff --git a/types/src/main.d.ts b/types/src/main.d.ts index dda39c0e..cf0a307f 100644 --- a/types/src/main.d.ts +++ b/types/src/main.d.ts @@ -21,5 +21,5 @@ export declare type NavigationAppProps = { [key: string]: 'disable'; }; }; -export declare type TcUniNavMethods = 'init' | 'update' | 'triggerFlow'; +export declare type TcUniNavMethods = 'init' | 'update' | 'destroy' | 'triggerFlow'; export declare type TcUniNavFn = (method: TcUniNavMethods, targetId: string, config: NavigationAppProps) => void; From 774cc3d3c233f8a4eb83f8ef31e871e65f3d90e6 Mon Sep 17 00:00:00 2001 From: Vasilica Olariu Date: Wed, 17 Sep 2025 15:52:43 +0300 Subject: [PATCH 2/2] safe check & delete instance --- src/main.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index db62181d..332f4964 100644 --- a/src/main.ts +++ b/src/main.ts @@ -57,6 +57,10 @@ const instancesContextStore: { [key: string]: Map> } = {} async function destroy( targetId: string, ) { + if (Object.prototype.hasOwnProperty.call(instancesContextStore, targetId)) { + delete instancesContextStore[targetId]; + } + if (typeof targetId !== 'string') { throw new Error(`'targetId' should be a string`); } @@ -68,7 +72,6 @@ async function destroy( } targetEl.innerHTML = ''; - delete instancesContextStore[targetId]; } /**