From 89d9071113e0c998ca071fd64829ac7d7361a893 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8C=83=E6=96=87=E5=8D=8E?= <531362022@qq.com> Date: Fri, 23 Aug 2019 14:37:30 +0800 Subject: [PATCH] feat: Trigger event once subscriber is created --- src/RouterModel.ts | 74 ++++++++++++++++++++++++++++++---------------- tsconfig.json | 2 +- 2 files changed, 49 insertions(+), 27 deletions(-) diff --git a/src/RouterModel.ts b/src/RouterModel.ts index bcaaffc..353df50 100644 --- a/src/RouterModel.ts +++ b/src/RouterModel.ts @@ -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 { + 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 = []; protected readonly changeHistory = this.actionNormal((_, payload: Data) => { return payload; @@ -59,8 +63,13 @@ class RouterModel extends Model { 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; } @@ -103,33 +112,38 @@ class RouterModel extends Model { } 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 = {}; + if (result === null) { + return; + } - keys.forEach(({ name }, index) => { - params[name] = result[index + 1]; - }); + const params: Record = {}; - fn(params, location, action); + keys.forEach(({ name }, index) => { + params[name] = result[index + 1]; }); + + fn(params, location, action); } protected getHistory(): History { @@ -144,9 +158,17 @@ class RouterModel extends Model { 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, }; }; } diff --git a/tsconfig.json b/tsconfig.json index bde45c0..dc5ea30 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,7 +4,7 @@ "module": "commonjs", "allowJs": false, "declaration": true, - "lib": ["es2015"], + "lib": ["es2015", "dom"], "removeComments": false, "outDir": "./build", "rootDir": "./",