@bjornlu/svelte-router
Advanced tools
Comparing version 0.3.1 to 0.3.2
# Changelog | ||
## 0.3.1 - 2020-10-22 | ||
## 0.3.2 - 2020-10-11 | ||
### Fixed | ||
- Keep `navigate` and `createLink` reference when initializing router | ||
## 0.3.1 - 2020-09-22 | ||
### Added | ||
@@ -6,0 +12,0 @@ |
import { Readable } from 'svelte/store'; | ||
import { Router, Route } from './router/base'; | ||
import { RouteRecord } from './types'; | ||
export declare let navigate: Router['navigate']; | ||
export declare let createLink: Router['createLink']; | ||
export declare const route: Readable<Route>; | ||
export declare const navigate: Router['navigate']; | ||
export declare const createLink: Router['createLink']; | ||
export declare function initHashRouter(routes: RouteRecord[]): void; | ||
export declare function initPathRouter(routes: RouteRecord[]): void; |
import { writable } from 'svelte/store'; | ||
import { HashRouter } from './router/hash-router'; | ||
import { PathRouter } from './router/path-router'; | ||
// Will be assigned when `initRouter` | ||
export let navigate = () => { | ||
throw new Error('Router must be initialized before calling navigate'); | ||
}; | ||
// Will be assigned when `initRouter` | ||
export let createLink = () => { | ||
throw new Error('Router must be initialized before calling createLink'); | ||
}; | ||
let globalRouter; | ||
const writableRoute = writable({ | ||
@@ -20,23 +13,34 @@ path: '', | ||
export const route = { subscribe: writableRoute.subscribe }; | ||
let inited = false; | ||
function initRouter(router) { | ||
navigate = router.navigate.bind(router); | ||
createLink = router.createLink.bind(router); | ||
router.currentRoute.subscribe((v) => writableRoute.set(v)); | ||
} | ||
function checkInit() { | ||
if (inited) { | ||
throw new Error('Router already initialized. Cannot re-initialize router.'); | ||
export const navigate = function () { | ||
if (globalRouter != null) { | ||
// @ts-expect-error | ||
return globalRouter.navigate(...arguments); | ||
} | ||
else { | ||
inited = true; | ||
throw new Error('Router must be initialized before calling navigate'); | ||
} | ||
} | ||
}; | ||
export const createLink = function () { | ||
if (globalRouter != null) { | ||
// @ts-expect-error | ||
return globalRouter.createLink(...arguments); | ||
} | ||
else { | ||
throw new Error('Router must be initialized before calling createLink'); | ||
} | ||
}; | ||
export function initHashRouter(routes) { | ||
checkInit(); | ||
initRouter(new HashRouter(routes)); | ||
} | ||
export function initPathRouter(routes) { | ||
checkInit(); | ||
initRouter(new PathRouter(routes)); | ||
} | ||
function initRouter(router) { | ||
if (globalRouter == null) { | ||
globalRouter = router; | ||
globalRouter.currentRoute.subscribe((v) => writableRoute.set(v)); | ||
} | ||
else { | ||
throw new Error('Router already initialized. Cannot re-initialize router.'); | ||
} | ||
} |
@@ -844,10 +844,3 @@ (function (global, factory) { | ||
// Will be assigned when `initRouter` | ||
exports.navigate = () => { | ||
throw new Error('Router must be initialized before calling navigate'); | ||
}; | ||
// Will be assigned when `initRouter` | ||
exports.createLink = () => { | ||
throw new Error('Router must be initialized before calling createLink'); | ||
}; | ||
let globalRouter; | ||
const writableRoute = writable({ | ||
@@ -861,24 +854,35 @@ path: '', | ||
const route = { subscribe: writableRoute.subscribe }; | ||
let inited = false; | ||
function initRouter(router) { | ||
exports.navigate = router.navigate.bind(router); | ||
exports.createLink = router.createLink.bind(router); | ||
router.currentRoute.subscribe((v) => writableRoute.set(v)); | ||
} | ||
function checkInit() { | ||
if (inited) { | ||
throw new Error('Router already initialized. Cannot re-initialize router.'); | ||
const navigate = function () { | ||
if (globalRouter != null) { | ||
// @ts-expect-error | ||
return globalRouter.navigate(...arguments); | ||
} | ||
else { | ||
inited = true; | ||
throw new Error('Router must be initialized before calling navigate'); | ||
} | ||
} | ||
}; | ||
const createLink = function () { | ||
if (globalRouter != null) { | ||
// @ts-expect-error | ||
return globalRouter.createLink(...arguments); | ||
} | ||
else { | ||
throw new Error('Router must be initialized before calling createLink'); | ||
} | ||
}; | ||
function initHashRouter(routes) { | ||
checkInit(); | ||
initRouter(new HashRouter(routes)); | ||
} | ||
function initPathRouter(routes) { | ||
checkInit(); | ||
initRouter(new PathRouter(routes)); | ||
} | ||
function initRouter(router) { | ||
if (globalRouter == null) { | ||
globalRouter = router; | ||
globalRouter.currentRoute.subscribe((v) => writableRoute.set(v)); | ||
} | ||
else { | ||
throw new Error('Router already initialized. Cannot re-initialize router.'); | ||
} | ||
} | ||
@@ -1352,3 +1356,3 @@ /* dist/components/RouterView.svelte generated by Svelte v3.24.1 */ | ||
if (result != null) { | ||
tick().then(() => exports.navigate(result, true)); | ||
tick().then(() => navigate(result, true)); | ||
} else { | ||
@@ -1491,3 +1495,3 @@ $$invalidate(0, canRender = true); | ||
e.preventDefault(); | ||
exports.navigate(to, replace); | ||
navigate(to, replace); | ||
} | ||
@@ -1509,3 +1513,3 @@ | ||
if ($$self.$$.dirty & /*to*/ 16) { | ||
$$subscribe_link($$invalidate(0, link = exports.createLink(to))); | ||
$$subscribe_link($$invalidate(0, link = createLink(to))); | ||
} | ||
@@ -1526,4 +1530,6 @@ }; | ||
exports.RouterView = RouterView; | ||
exports.createLink = createLink; | ||
exports.initHashRouter = initHashRouter; | ||
exports.initPathRouter = initPathRouter; | ||
exports.navigate = navigate; | ||
exports.route = route; | ||
@@ -1530,0 +1536,0 @@ |
{ | ||
"name": "@bjornlu/svelte-router", | ||
"description": "An easy-to-use SPA router for Svelte", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"main": "dist/svelte-router.umd.js", | ||
@@ -73,4 +73,4 @@ "module": "dist/index.js", | ||
"lint-staged": "^10.4.0", | ||
"prettier": "^2.1.1", | ||
"prettier-plugin-svelte": "^1.2.1", | ||
"prettier": "^2.1.2", | ||
"prettier-plugin-svelte": "^1.4.0", | ||
"rimraf": "^3.0.2", | ||
@@ -77,0 +77,0 @@ "rollup": "^2.26.10", |
@@ -7,3 +7,2 @@ # Svelte Router | ||
[![ci](https://github.com/bluwy/svelte-router/workflows/CI/badge.svg?event=push)](https://github.com/bluwy/svelte-router/actions) | ||
[![e2e](https://img.shields.io/endpoint?url=https://dashboard.cypress.io/badge/simple/vjxpm8/master&style=flat&logo=cypress)](https://dashboard.cypress.io/projects/vjxpm8/runs) | ||
@@ -30,3 +29,3 @@ An easy-to-use SPA router for Svelte. | ||
1. Install [`@bjornlu/svelte-router`](https://www.npmjs.com/package/@bjornlu/svelte-router): | ||
Install [`@bjornlu/svelte-router`](https://www.npmjs.com/package/@bjornlu/svelte-router): | ||
@@ -37,3 +36,3 @@ ```bash | ||
2. Define routes: | ||
Define routes: | ||
@@ -59,3 +58,3 @@ ```js | ||
3. Render routes and links: | ||
Render routes and links: | ||
@@ -77,3 +76,3 @@ ```svelte | ||
4. Done! | ||
Done! | ||
@@ -80,0 +79,0 @@ ## Documentation |
77031
1920
93