boring-router
Advanced tools
Comparing version 0.3.0-alpha.30 to 0.3.0-alpha.31
@@ -12,1 +12,2 @@ import { Dict } from 'tslang'; | ||
export declare function parsePath(path: string): Location; | ||
export declare function getNextId(): number; |
@@ -90,2 +90,7 @@ "use strict"; | ||
exports.parsePath = parsePath; | ||
let lastId = 0; | ||
function getNextId() { | ||
return ++lastId; | ||
} | ||
exports.getNextId = getNextId; | ||
//# sourceMappingURL=@utils.js.map |
@@ -11,2 +11,3 @@ export declare type UnregisterCallback = () => void; | ||
hash: string; | ||
state?: unknown; | ||
} | ||
@@ -16,5 +17,5 @@ export declare type LocationListener = (location: Location) => void; | ||
location: Location; | ||
push(path: string): void; | ||
replace(location: string | LocationDescriptorObject): void; | ||
push(path: string, state?: unknown): void; | ||
replace(location: string | LocationDescriptorObject, state?: unknown): void; | ||
listen(listener: LocationListener): UnregisterCallback; | ||
} |
import { IHistory } from '../history'; | ||
import { Router } from '../router'; | ||
import { RouteMatch, RouteSource } from './route-match'; | ||
@@ -6,3 +7,3 @@ import { GeneralParamDict, RouteMatchShared, RouteMatchSharedOptions } from './route-match-shared'; | ||
readonly $parent: NextRouteMatch | undefined; | ||
constructor(name: string, prefix: string, source: RouteSource, parent: NextRouteMatch<TParamDict> | undefined, origin: RouteMatch<TParamDict>, extension: object, history: IHistory, options: RouteMatchSharedOptions); | ||
constructor(name: string, prefix: string, router: Router, source: RouteSource, parent: NextRouteMatch<TParamDict> | undefined, origin: RouteMatch<TParamDict>, extension: object, history: IHistory, options: RouteMatchSharedOptions); | ||
/** | ||
@@ -9,0 +10,0 @@ * A reactive value indicates whether this route is exactly matched. |
@@ -5,4 +5,4 @@ "use strict"; | ||
class NextRouteMatch extends route_match_shared_1.RouteMatchShared { | ||
constructor(name, prefix, source, parent, origin, extension, history, options) { | ||
super(name, prefix, source, parent, history, options); | ||
constructor(name, prefix, router, source, parent, origin, extension, history, options) { | ||
super(name, prefix, router, source, parent, history, options); | ||
this._origin = origin; | ||
@@ -9,0 +9,0 @@ for (let key of Object.keys(extension)) { |
import { Dict, EmptyObjectPatch } from 'tslang'; | ||
import { IHistory } from '../history'; | ||
import { Router, RouterOnRouteComplete } from '../router'; | ||
import { RouteSource } from './route-match'; | ||
@@ -26,2 +27,6 @@ export declare type GeneralSegmentDict = Dict<string | undefined>; | ||
preserveQuery?: boolean; | ||
/** | ||
* The callback that will be called after a route completed (after all the hooks). | ||
*/ | ||
onComplete?: RouterOnRouteComplete; | ||
} | ||
@@ -47,3 +52,3 @@ export interface RouteMatchSharedOptions { | ||
readonly $parent: RouteMatchShared | undefined; | ||
constructor(name: string, prefix: string, source: RouteSource, parent: RouteMatchShared | undefined, history: IHistory, { match, query, group }: RouteMatchSharedOptions); | ||
constructor(name: string, prefix: string, router: Router, source: RouteSource, parent: RouteMatchShared | undefined, history: IHistory, { match, query, group }: RouteMatchSharedOptions); | ||
/** | ||
@@ -67,2 +72,3 @@ * A dictionary of the combination of query string and segments. | ||
$replace(params?: Partial<TParamDict> & EmptyObjectPatch, options?: RouterMatchRefOptions<TGroupName>): void; | ||
private _generateState; | ||
} |
@@ -7,3 +7,3 @@ "use strict"; | ||
class RouteMatchShared { | ||
constructor(name, prefix, source, parent, history, { match, query, group }) { | ||
constructor(name, prefix, router, source, parent, history, { match, query, group }) { | ||
this.$name = name; | ||
@@ -13,2 +13,3 @@ this.$group = group; | ||
this._prefix = prefix; | ||
this._router = router; | ||
this._source = source; | ||
@@ -136,3 +137,4 @@ this._history = history; | ||
let ref = this.$ref(params, options); | ||
this._history.push(ref); | ||
let state = this._generateState(options); | ||
this._history.push(ref, state); | ||
} | ||
@@ -144,4 +146,15 @@ /** | ||
let ref = this.$ref(params, options); | ||
this._history.replace(ref); | ||
let state = this._generateState(options); | ||
this._history.replace(ref, state); | ||
} | ||
_generateState({ onComplete: onCompleteListener, } = {}) { | ||
if (!onCompleteListener) { | ||
return undefined; | ||
} | ||
let onCompleteListenerId = _utils_1.getNextId(); | ||
this._router._onRouteCompleteListenerMap.set(onCompleteListenerId, onCompleteListener); | ||
return { | ||
onCompleteListenerId, | ||
}; | ||
} | ||
} | ||
@@ -148,0 +161,0 @@ tslib_1.__decorate([ |
import { OmitValueOfKey, OmitValueWithType } from 'tslang'; | ||
import { IHistory } from '../history'; | ||
import { Router } from '../router'; | ||
import { NextRouteMatch } from './next-route-match'; | ||
@@ -59,3 +60,3 @@ import { GeneralParamDict, GeneralQueryDict, RouteMatchShared, RouteMatchSharedOptions } from './route-match-shared'; | ||
readonly $next: TNextRouteMatch; | ||
constructor(name: string, prefix: string, source: RouteSource, parent: RouteMatch | undefined, extension: object, history: IHistory, { exact, ...sharedOptions }: RouteMatchOptions); | ||
constructor(name: string, prefix: string, router: Router, source: RouteSource, parent: RouteMatch | undefined, extension: object, history: IHistory, { exact, ...sharedOptions }: RouteMatchOptions); | ||
/** | ||
@@ -62,0 +63,0 @@ * A reactive value indicates whether this route is matched. |
@@ -8,5 +8,5 @@ "use strict"; | ||
class RouteMatch extends route_match_shared_1.RouteMatchShared { | ||
constructor(name, prefix, source, parent, extension, history, _a) { | ||
constructor(name, prefix, router, source, parent, extension, history, _a) { | ||
var { exact } = _a, sharedOptions = tslib_1.__rest(_a, ["exact"]); | ||
super(name, prefix, source, parent, history, sharedOptions); | ||
super(name, prefix, router, source, parent, history, sharedOptions); | ||
/** @internal */ | ||
@@ -13,0 +13,0 @@ this._beforeEnterCallbacks = []; |
@@ -38,2 +38,6 @@ import { Dict, EmptyObjectPatch } from 'tslang'; | ||
export declare type RouterOnChange = (from: Location | undefined, to: Location) => void; | ||
export declare type RouterOnRouteComplete = () => void; | ||
export interface RouterOnRouteCompleteLocationState { | ||
onCompleteListenerId: number; | ||
} | ||
export interface RouterOptions { | ||
@@ -40,0 +44,0 @@ /** |
@@ -32,2 +32,4 @@ "use strict"; | ||
/** @internal */ | ||
this._onRouteCompleteListenerMap = new Map(); | ||
/** @internal */ | ||
this._onLocationChange = (location) => { | ||
@@ -41,2 +43,9 @@ this._nextLocation = location; | ||
this._asyncOnLocationChange = (nextLocation) => tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
let onCompleteListener; | ||
let state = nextLocation.state; | ||
if (isRouterOnRouteCompleteLocationState(state)) { | ||
let onCompleteListenerId = state.onCompleteListenerId; | ||
onCompleteListener = this._onRouteCompleteListenerMap.get(onCompleteListenerId); | ||
this._onRouteCompleteListenerMap.delete(onCompleteListenerId); | ||
} | ||
if (this._isNextLocationOutDated(nextLocation)) { | ||
@@ -124,2 +133,5 @@ return; | ||
} | ||
if (onCompleteListener) { | ||
onCompleteListener(); | ||
} | ||
}); | ||
@@ -334,4 +346,4 @@ this._history = history; | ||
}; | ||
let routeMatch = new route_match_1.RouteMatch(routeName, prefix, source, parent instanceof route_match_1.RouteMatch ? parent : undefined, extension, history, options); | ||
let nextRouteMatch = new route_match_1.NextRouteMatch(routeName, prefix, matchingSource, matchingParent instanceof route_match_1.NextRouteMatch ? matchingParent : undefined, routeMatch, extension, history, options); | ||
let routeMatch = new route_match_1.RouteMatch(routeName, prefix, this, source, parent instanceof route_match_1.RouteMatch ? parent : undefined, extension, history, options); | ||
let nextRouteMatch = new route_match_1.NextRouteMatch(routeName, prefix, this, matchingSource, matchingParent instanceof route_match_1.NextRouteMatch ? matchingParent : undefined, routeMatch, extension, history, options); | ||
routeMatch.$next = nextRouteMatch; | ||
@@ -368,2 +380,5 @@ routeMatches.push(routeMatch); | ||
} | ||
function isRouterOnRouteCompleteLocationState(object) { | ||
return object && typeof object.onCompleteListenerId === 'number'; | ||
} | ||
//# sourceMappingURL=router.js.map |
{ | ||
"name": "boring-router", | ||
"version": "0.3.0-alpha.30", | ||
"version": "0.3.0-alpha.31", | ||
"description": "A light-weight, type-safe, yet reactive router service using MobX.", | ||
@@ -39,3 +39,3 @@ "repository": { | ||
}, | ||
"gitHead": "885ff96fd32f217d2ad26d0c01a507313a92d453" | ||
"gitHead": "a56609e60175a6874c275831cf1ddf29079196d8" | ||
} |
@@ -129,1 +129,7 @@ import {Dict} from 'tslang'; | ||
} | ||
let lastId = 0; | ||
export function getNextId(): number { | ||
return ++lastId; | ||
} |
@@ -13,2 +13,3 @@ export type UnregisterCallback = () => void; | ||
hash: string; | ||
state?: unknown; | ||
} | ||
@@ -20,5 +21,5 @@ | ||
location: Location; | ||
push(path: string): void; | ||
replace(location: string | LocationDescriptorObject): void; | ||
push(path: string, state?: unknown): void; | ||
replace(location: string | LocationDescriptorObject, state?: unknown): void; | ||
listen(listener: LocationListener): UnregisterCallback; | ||
} |
import {IHistory} from '../history'; | ||
import {Router} from '../router'; | ||
@@ -22,2 +23,3 @@ import {RouteMatch, RouteMatchEntry, RouteSource} from './route-match'; | ||
prefix: string, | ||
router: Router, | ||
source: RouteSource, | ||
@@ -30,3 +32,3 @@ parent: NextRouteMatch<TParamDict> | undefined, | ||
) { | ||
super(name, prefix, source, parent, history, options); | ||
super(name, prefix, router, source, parent, history, options); | ||
@@ -33,0 +35,0 @@ this._origin = origin; |
import {computed} from 'mobx'; | ||
import {Dict, EmptyObjectPatch} from 'tslang'; | ||
import {buildRef} from '../@utils'; | ||
import {buildRef, getNextId} from '../@utils'; | ||
import {IHistory} from '../history'; | ||
import { | ||
Router, | ||
RouterOnRouteComplete, | ||
RouterOnRouteCompleteLocationState, | ||
} from '../router'; | ||
@@ -37,2 +42,6 @@ import {RouteMatchEntry, RouteSource} from './route-match'; | ||
preserveQuery?: boolean; | ||
/** | ||
* The callback that will be called after a route completed (after all the hooks). | ||
*/ | ||
onComplete?: RouterOnRouteComplete; | ||
} | ||
@@ -81,5 +90,9 @@ | ||
/** @internal */ | ||
protected _router: Router; | ||
constructor( | ||
name: string, | ||
prefix: string, | ||
router: Router, | ||
source: RouteSource, | ||
@@ -94,2 +107,3 @@ parent: RouteMatchShared | undefined, | ||
this._prefix = prefix; | ||
this._router = router; | ||
this._source = source; | ||
@@ -296,3 +310,5 @@ this._history = history; | ||
let ref = this.$ref(params, options); | ||
this._history.push(ref); | ||
let state = this._generateState(options); | ||
this._history.push(ref, state); | ||
} | ||
@@ -308,3 +324,5 @@ | ||
let ref = this.$ref(params, options); | ||
this._history.replace(ref); | ||
let state = this._generateState(options); | ||
this._history.replace(ref, state); | ||
} | ||
@@ -316,2 +334,23 @@ | ||
): RouteMatchEntry | undefined; | ||
private _generateState({ | ||
onComplete: onCompleteListener, | ||
}: RouterMatchRefOptions<TGroupName> = {}): | ||
| RouterOnRouteCompleteLocationState | ||
| undefined { | ||
if (!onCompleteListener) { | ||
return undefined; | ||
} | ||
let onCompleteListenerId = getNextId(); | ||
this._router._onRouteCompleteListenerMap.set( | ||
onCompleteListenerId, | ||
onCompleteListener, | ||
); | ||
return { | ||
onCompleteListenerId, | ||
}; | ||
} | ||
} |
@@ -6,2 +6,3 @@ import {observable} from 'mobx'; | ||
import {IHistory} from '../history'; | ||
import {Router} from '../router'; | ||
@@ -161,2 +162,3 @@ import {NextRouteMatch} from './next-route-match'; | ||
prefix: string, | ||
router: Router, | ||
source: RouteSource, | ||
@@ -168,3 +170,3 @@ parent: RouteMatch | undefined, | ||
) { | ||
super(name, prefix, source, parent, history, sharedOptions); | ||
super(name, prefix, router, source, parent, history, sharedOptions); | ||
@@ -171,0 +173,0 @@ for (let [key, defaultValue] of Object.entries(extension)) { |
@@ -43,3 +43,5 @@ import hyphenate from 'hyphenate'; | ||
} | ||
? TMatch extends string ? never : T | ||
? TMatch extends string | ||
? never | ||
: T | ||
: never; | ||
@@ -189,2 +191,8 @@ | ||
export type RouterOnRouteComplete = () => void; | ||
export interface RouterOnRouteCompleteLocationState { | ||
onCompleteListenerId: number; | ||
} | ||
export interface RouterOptions { | ||
@@ -274,2 +282,5 @@ /** | ||
/** @internal */ | ||
_onRouteCompleteListenerMap = new Map<number, RouterOnRouteComplete>(); | ||
private constructor( | ||
@@ -398,2 +409,15 @@ primarySchema: RouteSchemaDict, | ||
): Promise<void> => { | ||
let onCompleteListener: RouterOnRouteComplete | undefined; | ||
let state = nextLocation.state; | ||
if (isRouterOnRouteCompleteLocationState(state)) { | ||
let onCompleteListenerId = state.onCompleteListenerId; | ||
onCompleteListener = this._onRouteCompleteListenerMap.get( | ||
onCompleteListenerId, | ||
); | ||
this._onRouteCompleteListenerMap.delete(onCompleteListenerId); | ||
} | ||
if (this._isNextLocationOutDated(nextLocation)) { | ||
@@ -549,2 +573,6 @@ return; | ||
} | ||
if (onCompleteListener) { | ||
onCompleteListener(); | ||
} | ||
}; | ||
@@ -774,2 +802,3 @@ | ||
prefix, | ||
this, | ||
source, | ||
@@ -785,2 +814,3 @@ parent instanceof RouteMatch ? parent : undefined, | ||
prefix, | ||
this, | ||
matchingSource, | ||
@@ -850,1 +880,7 @@ matchingParent instanceof NextRouteMatch ? matchingParent : undefined, | ||
} | ||
function isRouterOnRouteCompleteLocationState( | ||
object: any, | ||
): object is RouterOnRouteCompleteLocationState { | ||
return object && typeof object.onCompleteListenerId === 'number'; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
150816
3243
0