nativescript-vue-router-extended
Version:
NativeScript Vue Router Extended for NativeScript Vue hybrid Apps.
68 lines • 2.13 kB
JavaScript
export class RouterGuardService {
constructor(to, from, isHook = false) {
this.guardCallbacks = [];
this.isCancelled = false;
this.lastContext = null;
this.isHook = false;
this.setRoutes(to, from);
this.isHook = isHook;
}
add(callback) {
this.guardCallbacks.push(callback);
}
async executeCallbacks(arr, predicate) {
for (const e of arr) {
if (await predicate(e)) {
return true;
}
}
return false;
}
run() {
this.lastContext = true;
const next = (vmContext) => {
if (typeof vmContext === "undefined") {
return;
}
this.lastContext = vmContext;
if (vmContext === false) {
this.isCancelled = true;
}
};
this.executeCallbacks(this.guardCallbacks.filter((callback) => typeof callback === "function"), async (callback) => {
if (this.isCancelled) {
return true;
}
let result = null;
if (this.isHook) {
const hookCallback = callback;
await hookCallback(this.routeTo, this.routeFrom);
}
else if (callback &&
typeof callback.then === "function") {
result = await callback(this.routeTo, this.routeFrom, next);
}
else {
result = callback(this.routeTo, this.routeFrom, next);
}
if (result === false) {
this.isCancelled = true;
this.lastContext = false;
return true;
}
if (result && result !== true) {
this.isCancelled = true;
this.lastContext = result;
return true;
}
return false;
});
return this.lastContext;
}
setRoutes(to, from) {
this.routeTo = to;
this.routeFrom = from;
this.isCancelled = false;
}
}
//# sourceMappingURL=router-guard-service.js.map