Skip to content

Commit 89d9071

Browse files
committed
feat: Trigger event once subscriber is created
1 parent 9ea9f0d commit 89d9071

File tree

2 files changed

+49
-27
lines changed

2 files changed

+49
-27
lines changed

src/RouterModel.ts

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,20 @@ interface Data {
2020

2121
type UnsubscribeToken = string;
2222

23+
interface Subscriber {
24+
path: Path;
25+
reg: RegExp;
26+
keys: Key[];
27+
fn: (params: any, location: Location, action: Action) => void;
28+
token: UnsubscribeToken;
29+
}
30+
2331
class RouterModel extends Model<Data> {
32+
protected isRegistered = false;
33+
2434
protected unregister: UnregisterCallback | undefined;
2535

26-
protected pathListeners: Array<{
27-
path: Path;
28-
reg: RegExp;
29-
keys: Key[];
30-
fn: (params: any, location: Location, action: Action) => void;
31-
token: UnsubscribeToken;
32-
}> = [];
36+
protected pathListeners: Array<Subscriber> = [];
3337

3438
protected readonly changeHistory = this.actionNormal((_, payload: Data) => {
3539
return payload;
@@ -59,8 +63,13 @@ class RouterModel extends Model<Data> {
5963
const token = `un_${this.pathListeners.length}_${Math.random()}`;
6064
const keys: Key[] = [];
6165
const reg = pathToRegexp(path, keys);
66+
const subscriber = { path, fn, reg, keys, token };
6267

63-
this.pathListeners.push({ path, fn, reg, keys, token });
68+
this.pathListeners.push(subscriber);
69+
70+
if (this.isRegistered) {
71+
this.publishOne(subscriber, this.data.location, this.data.action);
72+
}
6473

6574
return token;
6675
}
@@ -103,33 +112,38 @@ class RouterModel extends Model<Data> {
103112
}
104113

105114
if (!this.unregister) {
106-
this.unregister = history.listen(this.onHistoryChange.bind(this));
115+
this.unregister = history.listen((location, action) => {
116+
this.changeHistory({
117+
location,
118+
action,
119+
});
120+
this.publishAll(location, action);
121+
});
107122
}
108123

109124
return super.register();
110125
}
111126

112-
protected onHistoryChange(location: Location, action: Action) {
113-
this.changeHistory({
114-
location,
115-
action,
127+
protected publishAll(location: Location, action: Action) {
128+
this.pathListeners.forEach((subscriber) => {
129+
this.publishOne(subscriber, location, action);
116130
});
131+
}
117132

118-
this.pathListeners.forEach(({ fn, reg, keys }) => {
119-
const result = reg.exec(location.pathname);
120-
121-
if (result === null) {
122-
return;
123-
}
133+
protected publishOne({ fn, reg, keys }: Subscriber, location: Location, action: Action) {
134+
const result = reg.exec(location.pathname);
124135

125-
const params: Record<string, string> = {};
136+
if (result === null) {
137+
return;
138+
}
126139

127-
keys.forEach(({ name }, index) => {
128-
params[name] = result[index + 1];
129-
});
140+
const params: Record<string, string> = {};
130141

131-
fn(params, location, action);
142+
keys.forEach(({ name }, index) => {
143+
params[name] = result[index + 1];
132144
});
145+
146+
fn(params, location, action);
133147
}
134148

135149
protected getHistory(): History {
@@ -144,9 +158,17 @@ class RouterModel extends Model<Data> {
144158

145159
protected initReducer(): Data | (() => Data) {
146160
return () => {
161+
const history = this.getHistory();
162+
163+
setTimeout(() => {
164+
this.publishAll(history.location, history.action);
165+
}, 0);
166+
167+
this.isRegistered = true;
168+
147169
return {
148-
location: this.getHistory().location,
149-
action: this.getHistory().action,
170+
location: history.location,
171+
action: history.action,
150172
};
151173
};
152174
}

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"module": "commonjs",
55
"allowJs": false,
66
"declaration": true,
7-
"lib": ["es2015"],
7+
"lib": ["es2015", "dom"],
88
"removeComments": false,
99
"outDir": "./build",
1010
"rootDir": "./",

0 commit comments

Comments
 (0)