diff --git a/package/router/src/core.ts b/package/router/src/core.ts index 4bd934e7d..24504131b 100644 --- a/package/router/src/core.ts +++ b/package/router/src/core.ts @@ -1,4 +1,3 @@ -import {SignalInterface} from '@alwatr/signal'; import {createLogger, alwatrRegisteredList} from '@alwatr/logger'; import type {ParamList, RequestRouteParam, Route} from './type'; @@ -8,11 +7,8 @@ alwatrRegisteredList.push({ }); export const logger = createLogger('alwatr/router'); - -export const routerChangeSignal = new SignalInterface('router-change'); - /** - * Handle requests of 'router-change' signal. + * Handle requests of 'route-change' signal. */ export function routeSignalProvider(requestParam: RequestRouteParam): Route { logger.logMethodArgs('routeSignalProvider', {requestParam}); diff --git a/package/router/src/router.ts b/package/router/src/router.ts index 0d1a8f4eb..52898dd83 100644 --- a/package/router/src/router.ts +++ b/package/router/src/router.ts @@ -1,9 +1,10 @@ -import {joinParameterList, logger, routeSignalProvider, routerChangeSignal} from './core'; +import {joinParameterList, logger, routeSignalProvider} from './core'; +import {routeChangeSignal} from './signal'; import {clickTrigger} from './trigger-click'; import {popstateTrigger} from './trigger-popstate'; import type {InitOptions, Route} from './type'; -export {routerChangeSignal}; +export {routeChangeSignal}; /** * Initial and config the Router. @@ -14,12 +15,12 @@ export function initialRouter(options?: InitOptions): void { clickTrigger.enable = options?.clickTrigger ?? true; popstateTrigger.enable = options?.popstateTrigger ?? true; - routerChangeSignal.setProvider(routeSignalProvider, {debounce: true, receivePrevious: true}); + routeChangeSignal.setProvider(routeSignalProvider, {debounce: true, receivePrevious: true}); // first route request. - if (!routerChangeSignal.dispatched) { + if (!routeChangeSignal.dispatched) { const {pathname, search, hash} = window.location; - routerChangeSignal.request({pathname, search, hash, pushState: false}); + routeChangeSignal.request({pathname, search, hash, pushState: false}); } } diff --git a/package/router/src/signal.ts b/package/router/src/signal.ts new file mode 100644 index 000000000..5f5a76ebb --- /dev/null +++ b/package/router/src/signal.ts @@ -0,0 +1,14 @@ +import {SignalInterface} from '@alwatr/signal'; +import type {RequestRouteParam, Route} from './type'; + +declare global { + interface AlwatrSignals { + 'route-change': Route; + } + + interface AlwatrRequestSignals { + 'route-change': RequestRouteParam; + } +} + +export const routeChangeSignal = new SignalInterface('route-change'); diff --git a/package/router/src/trigger-click.ts b/package/router/src/trigger-click.ts index 57c79b915..011cf70bd 100644 --- a/package/router/src/trigger-click.ts +++ b/package/router/src/trigger-click.ts @@ -1,4 +1,5 @@ -import {logger, routerChangeSignal} from './core'; +import {logger} from './core'; +import {routeChangeSignal} from './signal'; let _enabled = false; @@ -67,7 +68,7 @@ export const clickTrigger = { // if none of the above, convert the click into a navigation signal. const {pathname, search, hash} = anchor; - routerChangeSignal.request({pathname, search, hash}); + routeChangeSignal.request({pathname, search, hash}); // for a click event, the scroll is reset to the top position. if (event.type === 'click') { window.scrollTo(0, 0); diff --git a/package/router/src/trigger-popstate.ts b/package/router/src/trigger-popstate.ts index 3da98429e..ffca89146 100644 --- a/package/router/src/trigger-popstate.ts +++ b/package/router/src/trigger-popstate.ts @@ -1,4 +1,5 @@ -import {logger, routerChangeSignal} from './core'; +import {logger} from './core'; +import {routeChangeSignal} from './signal'; let _enabled = false; @@ -15,7 +16,7 @@ export const popstateTrigger = { } // if none of the above, convert the click into a navigation signal. const {pathname, search, hash} = window.location; - routerChangeSignal.request({pathname, search, hash, pushState: false}); + routeChangeSignal.request({pathname, search, hash, pushState: false}); }, set enable(enable: boolean) { diff --git a/package/router/src/type.ts b/package/router/src/type.ts index 3eb6ca656..1aff51831 100644 --- a/package/router/src/type.ts +++ b/package/router/src/type.ts @@ -1,13 +1,3 @@ -declare global { - interface AlwatrSignals { - 'router-change': Route; - } - - interface AlwatrRequestSignals { - 'router-change': RequestRouteParam; - } -} - export type ParamList = Record; // @TODO: description