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