Compare commits

...

10 Commits

5 changed files with 5032 additions and 3982 deletions

View File

@ -1,5 +1,34 @@
# Changelog # Changelog
## 2024-10-06 - 1.3.2 - fix(core)
Fix TypeScript type definition for route match function
- Updated the type definition for the matchFunction in the SmartRouter class to include a generic parameter.
## 2024-10-06 - 1.3.1 - fix(dependencies)
Updated dependencies to latest versions, resolving compatibility issues and improving performance.
- Updated devDependencies to their latest versions, including tsbuild, tsbundle, tstest, tapbundle, and @types/node
- Updated dependencies to latest versions of lik, smartrx, and path-to-regexp
## 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
- Improves platform compatibility for modern web applications.
- Enhances stability of the routing logic within SmartRouter class.
## 2024-10-06 - 1.2.0 - feat(core)
Add support for base paths and sub-routers.
- Added basePath feature to SmartRouter for handling base paths.
- Introduced createSubRouter method to create a sub-router with a specific prefix.
## 2024-10-06 - 1.1.1 - fix(core) ## 2024-10-06 - 1.1.1 - fix(core)
Remove SelectionDimension functionality Remove SelectionDimension functionality

View File

@ -1,6 +1,6 @@
{ {
"name": "@push.rocks/smartrouter", "name": "@push.rocks/smartrouter",
"version": "1.1.1", "version": "1.3.2",
"private": false, "private": false,
"description": "A JavaScript library providing routing capabilities for web applications.", "description": "A JavaScript library providing routing capabilities for web applications.",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",
@ -14,16 +14,16 @@
"buildDocs": "tsdoc" "buildDocs": "tsdoc"
}, },
"devDependencies": { "devDependencies": {
"@git.zone/tsbuild": "^2.1.70", "@git.zone/tsbuild": "^2.1.84",
"@git.zone/tsbundle": "^2.0.7", "@git.zone/tsbundle": "^2.0.15",
"@git.zone/tstest": "^1.0.81", "@git.zone/tstest": "^1.0.90",
"@push.rocks/tapbundle": "^5.0.15", "@push.rocks/tapbundle": "^5.3.0",
"@types/node": "^20.6.0" "@types/node": "^22.7.4"
}, },
"dependencies": { "dependencies": {
"@push.rocks/lik": "^6.0.5", "@push.rocks/lik": "^6.0.15",
"@push.rocks/smartrx": "^3.0.6", "@push.rocks/smartrx": "^3.0.7",
"path-to-regexp": "^6.2.0" "path-to-regexp": "^8.2.0"
}, },
"files": [ "files": [
"ts/**/*", "ts/**/*",

8915
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartrouter', name: '@push.rocks/smartrouter',
version: '1.1.1', version: '1.3.2',
description: 'A JavaScript library providing routing capabilities for web applications.' description: 'A JavaScript library providing routing capabilities for web applications.'
} }

View File

@ -32,35 +32,58 @@ export class SmartRouter {
* the routes we are handling * the routes we are handling
*/ */
public routes: Array<{ public routes: Array<{
matchFunction: plugins.pathToRegExp.MatchFunction; matchFunction: plugins.pathToRegExp.MatchFunction<any>;
handler: THandlerFunction; handler: THandlerFunction;
}> = []; }> = [];
/**
* base path for this router
*/
private basePath: string;
/**
* Reference to the event listener function for cleanup
*/
private popstateListener: (event: PopStateEvent) => void;
/** /**
* Creates an instance of Router. * Creates an instance of Router.
*/ */
constructor(optionsArg: IRouterOptions) { constructor(optionsArg: IRouterOptions, basePath: string = '') {
// lets set the router options // lets set the router options
this.options = { this.options = {
...this.options, ...this.options,
...optionsArg, ...optionsArg,
}; };
this.basePath = basePath;
// lets subscribe to route changes // lets subscribe to route changes
window.addEventListener('popstate', (popStateEventArg) => { this.popstateListener = (popStateEventArg) => {
popStateEventArg.preventDefault(); popStateEventArg.preventDefault();
this._handleRouteState(); this._handleRouteState();
}); };
window.addEventListener('popstate', this.popstateListener);
}
/**
* Create a sub-router with a specific prefix
* @param {string} subPath
* @param {IRouterOptions} [options]
*/
public createSubRouter(subPath: string, options?: IRouterOptions): SmartRouter {
const newBasePath = `${this.basePath}${subPath}`;
return new SmartRouter({ ...this.options, ...options }, newBasePath);
} }
/** /**
* Push route state to history stack * Push route state to history stack
*/ */
public async pushUrl(url: string = '/', state: any = {}) { public async pushUrl(url: string = '/', state: any = {}) {
if (url !== window.location.pathname) { const fullUrl = `${this.basePath}${url}`;
window.history.pushState(state, window.document.title, url); if (fullUrl !== window.location.pathname) {
window.history.pushState(state, window.document.title, fullUrl);
} else { } else {
window.history.replaceState(state, window.document.title, url); window.history.replaceState(state, window.document.title, fullUrl);
} }
await this._handleRouteState(); await this._handleRouteState();
} }
@ -71,8 +94,9 @@ export class SmartRouter {
* @param {function} handlerArg * @param {function} handlerArg
*/ */
public on(routeArg: string, handlerArg: THandlerFunction) { public on(routeArg: string, handlerArg: THandlerFunction) {
const fullRoute = `${this.basePath}${routeArg}`;
const routeObject = { const routeObject = {
matchFunction: plugins.pathToRegExp.match(routeArg), matchFunction: plugins.pathToRegExp.match(fullRoute),
handler: handlerArg, handler: handlerArg,
}; };
this.routes.push(routeObject); this.routes.push(routeObject);
@ -101,4 +125,14 @@ export class SmartRouter {
} as IRouteInfo); // not waiting here } 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 = [];
}
} }