Skip to content

Commit

Permalink
feat: Trigger event once subscriber is created
Browse files Browse the repository at this point in the history
  • Loading branch information
fwh1990 committed Aug 23, 2019
1 parent 9ea9f0d commit 89d9071
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 27 deletions.
74 changes: 48 additions & 26 deletions src/RouterModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,20 @@ interface Data {

type UnsubscribeToken = string;

interface Subscriber {
path: Path;
reg: RegExp;
keys: Key[];
fn: (params: any, location: Location, action: Action) => void;
token: UnsubscribeToken;
}

class RouterModel extends Model<Data> {
protected isRegistered = false;

protected unregister: UnregisterCallback | undefined;

protected pathListeners: Array<{
path: Path;
reg: RegExp;
keys: Key[];
fn: (params: any, location: Location, action: Action) => void;
token: UnsubscribeToken;
}> = [];
protected pathListeners: Array<Subscriber> = [];

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

this.pathListeners.push({ path, fn, reg, keys, token });
this.pathListeners.push(subscriber);

if (this.isRegistered) {
this.publishOne(subscriber, this.data.location, this.data.action);
}

return token;
}
Expand Down Expand Up @@ -103,33 +112,38 @@ class RouterModel extends Model<Data> {
}

if (!this.unregister) {
this.unregister = history.listen(this.onHistoryChange.bind(this));
this.unregister = history.listen((location, action) => {
this.changeHistory({
location,
action,
});
this.publishAll(location, action);
});
}

return super.register();
}

protected onHistoryChange(location: Location, action: Action) {
this.changeHistory({
location,
action,
protected publishAll(location: Location, action: Action) {
this.pathListeners.forEach((subscriber) => {
this.publishOne(subscriber, location, action);
});
}

this.pathListeners.forEach(({ fn, reg, keys }) => {
const result = reg.exec(location.pathname);

if (result === null) {
return;
}
protected publishOne({ fn, reg, keys }: Subscriber, location: Location, action: Action) {
const result = reg.exec(location.pathname);

const params: Record<string, string> = {};
if (result === null) {
return;
}

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

fn(params, location, action);
keys.forEach(({ name }, index) => {
params[name] = result[index + 1];
});

fn(params, location, action);
}

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

protected initReducer(): Data | (() => Data) {
return () => {
const history = this.getHistory();

setTimeout(() => {
this.publishAll(history.location, history.action);
}, 0);

this.isRegistered = true;

return {
location: this.getHistory().location,
action: this.getHistory().action,
location: history.location,
action: history.action,
};
};
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"module": "commonjs",
"allowJs": false,
"declaration": true,
"lib": ["es2015"],
"lib": ["es2015", "dom"],
"removeComments": false,
"outDir": "./build",
"rootDir": "./",
Expand Down

0 comments on commit 89d9071

Please sign in to comment.