Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(router): use seperate signal file #106

Merged
merged 1 commit into from
Mar 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions package/router/src/core.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {SignalInterface} from '@alwatr/signal';
import {createLogger, alwatrRegisteredList} from '@alwatr/logger';
import type {ParamList, RequestRouteParam, Route} from './type';

Expand All @@ -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});
Expand Down
11 changes: 6 additions & 5 deletions package/router/src/router.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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});
}
}

Expand Down
14 changes: 14 additions & 0 deletions package/router/src/signal.ts
Original file line number Diff line number Diff line change
@@ -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');
5 changes: 3 additions & 2 deletions package/router/src/trigger-click.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {logger, routerChangeSignal} from './core';
import {logger} from './core';
import {routeChangeSignal} from './signal';

let _enabled = false;

Expand Down Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions package/router/src/trigger-popstate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {logger, routerChangeSignal} from './core';
import {logger} from './core';
import {routeChangeSignal} from './signal';

let _enabled = false;

Expand All @@ -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) {
Expand Down
10 changes: 0 additions & 10 deletions package/router/src/type.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
declare global {
interface AlwatrSignals {
'router-change': Route;
}

interface AlwatrRequestSignals {
'router-change': RequestRouteParam;
}
}

export type ParamList = Record<string, string | number | boolean>;

// @TODO: description
Expand Down