mirror of
https://github.com/misskey-dev/misskey.git
synced 2026-05-23 00:34:14 +02:00
refactor(frontend): router refactoring
This commit is contained in:
@@ -5,8 +5,8 @@
|
||||
|
||||
import { defineAsyncComponent } from 'vue';
|
||||
import type { AsyncComponentLoader } from 'vue';
|
||||
import type { IRouter, RouteDef } from '@/nirax.js';
|
||||
import { Router } from '@/nirax.js';
|
||||
import type { RouteDef } from '@/router.js';
|
||||
import { Router } from '@/router.js';
|
||||
import { $i, iAmModerator } from '@/i.js';
|
||||
import MkLoading from '@/pages/_loading_.vue';
|
||||
import MkError from '@/pages/_error_.vue';
|
||||
@@ -578,6 +578,6 @@ const routes: RouteDef[] = [{
|
||||
component: page(() => import('@/pages/not-found.vue')),
|
||||
}];
|
||||
|
||||
export function createMainRouter(path: string): IRouter {
|
||||
export function createMainRouter(path: string): Router {
|
||||
return new Router(routes, path, !!$i, page(() => import('@/pages/not-found.vue')));
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
*/
|
||||
|
||||
import { EventEmitter } from 'eventemitter3';
|
||||
import type { IRouter, Resolved, RouteDef, RouterEvent, RouterFlag } from '@/nirax.js';
|
||||
import type { Router, Resolved, RouteDef, RouterEvent, RouterFlag } from '@/router.js';
|
||||
|
||||
import type { App, ShallowRef } from 'vue';
|
||||
import { analytics } from '@/analytics.js';
|
||||
@@ -13,7 +13,7 @@ import { analytics } from '@/analytics.js';
|
||||
* {@link Router}による画面遷移を可能とするために{@link mainRouter}をセットアップする。
|
||||
* また、{@link Router}のインスタンスを作成するためのファクトリも{@link provide}経由で公開する(`routerFactory`というキーで取得可能)
|
||||
*/
|
||||
export function setupRouter(app: App, routerFactory: ((path: string) => IRouter)): void {
|
||||
export function setupRouter(app: App, routerFactory: ((path: string) => Router)): void {
|
||||
app.provide('routerFactory', routerFactory);
|
||||
|
||||
const mainRouter = routerFactory(location.pathname + location.search + location.hash);
|
||||
@@ -43,7 +43,7 @@ export function setupRouter(app: App, routerFactory: ((path: string) => IRouter)
|
||||
setMainRouter(mainRouter);
|
||||
}
|
||||
|
||||
function getMainRouter(): IRouter {
|
||||
function getMainRouter(): Router {
|
||||
const router = mainRouterHolder;
|
||||
if (!router) {
|
||||
throw new Error('mainRouter is not found.');
|
||||
@@ -56,7 +56,7 @@ function getMainRouter(): IRouter {
|
||||
* メインルータを設定する。一度設定すると、それ以降は変更できない。
|
||||
* {@link setupRouter}から呼び出されることのみを想定している。
|
||||
*/
|
||||
export function setMainRouter(router: IRouter) {
|
||||
export function setMainRouter(router: Router) {
|
||||
if (mainRouterHolder) {
|
||||
throw new Error('mainRouter is already exists.');
|
||||
}
|
||||
@@ -69,10 +69,10 @@ export function setMainRouter(router: IRouter) {
|
||||
* {@link mainRouter}は起動シーケンスの一部にて初期化されるため、僅かにundefinedになる期間がある。
|
||||
* その僅かな期間のためだけに型をundefined込みにしたくないのでこのクラスを緩衝材として使用する。
|
||||
*/
|
||||
class MainRouterProxy implements IRouter {
|
||||
private supplier: () => IRouter;
|
||||
class MainRouterProxy implements Router {
|
||||
private supplier: () => Router;
|
||||
|
||||
constructor(supplier: () => IRouter) {
|
||||
constructor(supplier: () => Router) {
|
||||
this.supplier = supplier;
|
||||
}
|
||||
|
||||
@@ -194,6 +194,6 @@ class MainRouterProxy implements IRouter {
|
||||
}
|
||||
}
|
||||
|
||||
let mainRouterHolder: IRouter | null = null;
|
||||
let mainRouterHolder: Router | null = null;
|
||||
|
||||
export const mainRouter: IRouter = new MainRouterProxy(getMainRouter);
|
||||
export const mainRouter: Router = new MainRouterProxy(getMainRouter);
|
||||
|
||||
@@ -4,15 +4,15 @@
|
||||
*/
|
||||
|
||||
import { inject } from 'vue';
|
||||
import type { IRouter } from '@/nirax.js';
|
||||
import type { Router } from '@/router.js';
|
||||
import { mainRouter } from '@/router/main.js';
|
||||
import { DI } from '@/di.js';
|
||||
|
||||
/**
|
||||
* メインの{@link Router}を取得する。
|
||||
* あらかじめ{@link setupRouter}を実行しておく必要がある({@link provide}により{@link IRouter}のインスタンスを注入可能であるならばこの限りではない)
|
||||
* あらかじめ{@link setupRouter}を実行しておく必要がある({@link provide}により{@link Router}のインスタンスを注入可能であるならばこの限りではない)
|
||||
*/
|
||||
export function useRouter(): IRouter {
|
||||
export function useRouter(): Router {
|
||||
return inject(DI.router, null) ?? mainRouter;
|
||||
}
|
||||
|
||||
@@ -20,8 +20,8 @@ export function useRouter(): IRouter {
|
||||
* 任意の{@link Router}を取得するためのファクトリを取得する。
|
||||
* あらかじめ{@link setupRouter}を実行しておく必要がある。
|
||||
*/
|
||||
export function useRouterFactory(): (path: string) => IRouter {
|
||||
const factory = inject<(path: string) => IRouter>('routerFactory');
|
||||
export function useRouterFactory(): (path: string) => Router {
|
||||
const factory = inject<(path: string) => Router>('routerFactory');
|
||||
if (!factory) {
|
||||
console.error('routerFactory is not defined.');
|
||||
throw new Error('routerFactory is not defined.');
|
||||
|
||||
Reference in New Issue
Block a user