Skip to content

Commit 60bf837

Browse files
committed
feat: Add method listenAll and rename listen to listenPath instead
1 parent 780fa45 commit 60bf837

File tree

3 files changed

+49
-24
lines changed

3 files changed

+49
-24
lines changed

src/RouterModel.d.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import { Model } from '@redux-model/web';
33
import { Reducers } from '@redux-model/web/core/utils/types';
44

55
interface Data {
6-
location: Location;
7-
action: Action;
6+
location: RouterLocation;
7+
action: RouterAction;
88
}
99

10-
type UnsubscribeToken = string;
10+
export type RouterLocation = Location;
11+
export type RouterAction = Action;
1112

1213
declare class RouterModel extends Model<Data> {
1314
// They are properties exactly.
@@ -20,8 +21,9 @@ declare class RouterModel extends Model<Data> {
2021
goForward(): void;
2122

2223
getHistory(): History;
23-
subscribe<Params = any>(path: Path, fn: (params: Params, location: Location, action: Action) => void): UnsubscribeToken;
24-
unsubscribe(token: string): void;
24+
listenPath<Params = Record<string, string>>(path: Path, fn: (params: Params, location: RouterLocation, action: RouterAction) => void): string;
25+
listenAll(fn: (location: RouterLocation, action: RouterAction) => void): string;
26+
unlisten(token?: string): void;
2527
registerBrowser(history?: History): Reducers;
2628
registerHash(history?: History): Reducers;
2729

src/RouterModel.ts

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,24 @@ import { ForgetRegisterError } from '@redux-model/web/core/exceptions/ForgetRegi
1313
import { Model } from '@redux-model/web';
1414
import { getHistory, setHistory } from './history';
1515

16+
export type RouterLocation = Location;
17+
export type RouterAction = Action;
18+
1619
interface Data {
17-
location: Location,
18-
action: Action,
20+
location: RouterLocation,
21+
action: RouterAction,
1922
}
2023

21-
type UnsubscribeToken = string;
22-
23-
interface Subscriber {
24-
path: Path;
25-
reg: RegExp;
26-
keys: Key[];
27-
fn: (params: any, location: Location, action: Action) => void;
28-
token: UnsubscribeToken;
24+
type Subscriber = {
25+
path: Path | object;
26+
reg?: RegExp;
27+
keys?: Key[];
28+
fn: Function;
29+
token: string;
2930
}
3031

32+
const LISTEN_ALL = {};
33+
3134
class RouterModel extends Model<Data> {
3235
protected isInitialized = false;
3336

@@ -59,11 +62,27 @@ class RouterModel extends Model<Data> {
5962
this.getHistory().goForward();
6063
};
6164

62-
public subscribe<Params = any>(path: Path, fn: (params: Params, location: Location, action: Action) => void): UnsubscribeToken {
65+
public listenPath<Params = Record<string, string>>(
66+
path: Path,
67+
fn: (params: Params, location: RouterLocation, action: RouterAction) => void
68+
): string {
6369
const token = `un_${this.pathListeners.length}_${Math.random()}`;
6470
const keys: Key[] = [];
6571
const reg = pathToRegexp(path, keys);
66-
const subscriber = { path, fn, reg, keys, token };
72+
const subscriber: Subscriber = { path, fn, reg, keys, token };
73+
74+
this.pathListeners.push(subscriber);
75+
76+
if (this.isInitialized) {
77+
this.publishOne(subscriber, this.data.location, this.data.action);
78+
}
79+
80+
return token;
81+
}
82+
83+
public listenAll(fn: (location: RouterLocation, action: RouterAction) => void): string {
84+
const token = `un_${this.pathListeners.length}_${Math.random()}`;
85+
const subscriber: Subscriber = { path: LISTEN_ALL, fn, token };
6786

6887
this.pathListeners.push(subscriber);
6988

@@ -74,7 +93,7 @@ class RouterModel extends Model<Data> {
7493
return token;
7594
}
7695

77-
public unsubscribe(token: string): void {
96+
public unlisten(token?: string): void {
7897
this.pathListeners = this.pathListeners.filter((item) => {
7998
return item.token !== token;
8099
});
@@ -134,26 +153,30 @@ class RouterModel extends Model<Data> {
134153
return super.register();
135154
}
136155

137-
protected publishAll(location: Location, action: Action) {
156+
protected publishAll(location: RouterLocation, action: RouterAction) {
138157
this.pathListeners.forEach((subscriber) => {
139158
this.publishOne(subscriber, location, action);
140159
});
141160
}
142161

143-
protected publishOne({ fn, reg, keys }: Subscriber, location: Location, action: Action) {
144-
const result = reg.exec(location.pathname);
162+
protected publishOne(subscriber: Subscriber, location: RouterLocation, action: RouterAction) {
163+
if (subscriber.path === LISTEN_ALL) {
164+
return subscriber.fn(location, action);
165+
}
166+
167+
const result = subscriber.reg!.exec(location.pathname);
145168

146169
if (result === null) {
147170
return;
148171
}
149172

150173
const params: Record<string, string> = {};
151174

152-
keys.forEach(({ name }, index) => {
175+
subscriber.keys!.forEach(({ name }, index) => {
153176
params[name] = result[index + 1];
154177
});
155178

156-
fn(params, location, action);
179+
subscriber.fn(params, location, action);
157180
}
158181

159182
protected onReducerCreated(store): void {

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { routerModel } from './RouterModel';
1+
export { routerModel, RouterLocation, RouterAction } from './RouterModel';

0 commit comments

Comments
 (0)