2023-12-08 12:12:23 +02:00
|
|
|
export type OnNavigateCallback = ()=> Promise<boolean>;
|
|
|
|
|
2021-01-22 19:41:11 +02:00
|
|
|
export default class NavService {
|
|
|
|
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2021-01-22 19:41:11 +02:00
|
|
|
public static dispatch: Function = () => {};
|
2023-12-08 12:12:23 +02:00
|
|
|
private static handlers_: OnNavigateCallback[] = [];
|
2021-01-22 19:41:11 +02:00
|
|
|
|
2024-03-09 13:03:57 +02:00
|
|
|
public static async go(routeName: string, additionalProps: Record<string, any>|null = null) {
|
2018-02-22 00:08:34 +02:00
|
|
|
if (this.handlers_.length) {
|
2020-03-14 01:46:14 +02:00
|
|
|
const r = await this.handlers_[this.handlers_.length - 1]();
|
2018-02-22 00:08:34 +02:00
|
|
|
if (r) return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.dispatch({
|
|
|
|
type: 'NAV_GO',
|
|
|
|
routeName: routeName,
|
2024-03-09 13:03:57 +02:00
|
|
|
...additionalProps,
|
2019-07-29 15:43:53 +02:00
|
|
|
});
|
2023-12-08 12:12:23 +02:00
|
|
|
return false;
|
2018-02-22 00:08:34 +02:00
|
|
|
}
|
|
|
|
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2023-12-08 12:12:23 +02:00
|
|
|
public static addHandler(handler: OnNavigateCallback) {
|
2018-02-22 00:08:34 +02:00
|
|
|
for (let i = this.handlers_.length - 1; i >= 0; i--) {
|
|
|
|
const h = this.handlers_[i];
|
|
|
|
if (h === handler) return;
|
|
|
|
}
|
|
|
|
|
2021-01-22 19:41:11 +02:00
|
|
|
this.handlers_.push(handler);
|
2018-02-22 00:08:34 +02:00
|
|
|
}
|
|
|
|
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2024-02-26 18:53:48 +02:00
|
|
|
public static removeHandler(handler: OnNavigateCallback) {
|
2018-02-22 00:08:34 +02:00
|
|
|
for (let i = this.handlers_.length - 1; i >= 0; i--) {
|
|
|
|
const h = this.handlers_[i];
|
2024-02-26 18:53:48 +02:00
|
|
|
if (h === handler) this.handlers_.splice(i, 1);
|
2018-02-22 00:08:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|