diff --git a/changelog.md b/changelog.md index 3875def..834c244 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,11 @@ # Changelog +## 2024-10-06 - 1.3.0 - feat(smartrouter) +Add destroy method to SmartRouter class. + +- Introduced a destroy method to the SmartRouter class for cleaning up event listeners and route references. +- Refactored popstate event listener to be removable, improving resource management and preventing memory leaks. + ## 2024-10-06 - 1.2.1 - fix(core) Ensure stability and performance improvements diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 4976bb1..f2d0547 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@push.rocks/smartrouter', - version: '1.2.1', + version: '1.3.0', description: 'A JavaScript library providing routing capabilities for web applications.' } diff --git a/ts/smartrouter.classes.smartrouter.ts b/ts/smartrouter.classes.smartrouter.ts index cbac939..389593a 100644 --- a/ts/smartrouter.classes.smartrouter.ts +++ b/ts/smartrouter.classes.smartrouter.ts @@ -41,6 +41,11 @@ export class SmartRouter { */ private basePath: string; + /** + * Reference to the event listener function for cleanup + */ + private popstateListener: (event: PopStateEvent) => void; + /** * Creates an instance of Router. */ @@ -53,10 +58,11 @@ export class SmartRouter { this.basePath = basePath; // lets subscribe to route changes - window.addEventListener('popstate', (popStateEventArg) => { + this.popstateListener = (popStateEventArg) => { popStateEventArg.preventDefault(); this._handleRouteState(); - }); + }; + window.addEventListener('popstate', this.popstateListener); } /** @@ -119,4 +125,14 @@ export class SmartRouter { } as IRouteInfo); // not waiting here } } + + /** + * Destroy the router instance, removing all external references + */ + public destroy() { + // Remove the event listener for popstate + window.removeEventListener('popstate', this.popstateListener); + // Clear all routes + this.routes = []; + } } \ No newline at end of file