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

feat: add eventbus and dynamic import page init event, ref #38. #41

Merged
merged 1 commit into from
Jan 3, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 1 addition & 5 deletions examples/ts/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
export default {
alias: {
dva: 'dva-no-router',
},
};
export default {};
7 changes: 7 additions & 0 deletions examples/ts/pages/index/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import dva from 'dva';
import Count from './components/Count';
import styles from './page.css';

import { default as event, Events, EventTypes } from 'umi/event';
const { PAGE_INITIALIZED } = Events;

event.addEventListener<EventTypes.PAGE_INITIALIZED_TYPE>(PAGE_INITIALIZED, (evt) => {
console.log('PAGE_INITIALIZED', evt);
});

const app = dva();
app.model(require('./models/count').default);

Expand Down
11 changes: 3 additions & 8 deletions packages/umi-build-dev/src/getRouterContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ function getRouteComponents(routeConfig, config = {}, paths) {
const { loading } = config;
let loadingOpts = '';
if (loading) {
loadingOpts = `, { loading: require('${join(
paths.cwd,
loading,
)}').default }`;
loadingOpts = `loading: require('${join(paths.cwd, loading)}').default,`;
}

const routerComponents = Object.keys(routeConfig).map(key => {
Expand All @@ -49,11 +46,9 @@ function getRouteComponents(routeConfig, config = {}, paths) {
: '() => <div>Compiling...</div>';
return ` <Route exact path="${key}" component={${component}}></Route>`;
} else {
return ` <Route exact path="${
key
}" component={dynamic(() => import(/* webpackChunkName: '${normalizeEntry(
return ` <Route exact path="${key}" component={dynamic(() => import(/* webpackChunkName: '${normalizeEntry(
routeConfig[key],
)}' */'${pageJSFile}')${loadingOpts})}></Route>`;
)}' */'${pageJSFile}'), { callback: (err) => callback('${key}', err), ${loadingOpts} }) }></Route>`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

逗号应该在 loadingOpts 里。

}
});

Expand Down
8 changes: 8 additions & 0 deletions packages/umi-build-dev/template/router.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { Router, Route, Switch } from 'umi-router';
import dynamic from '<%= libraryName %>/dynamic';
import { default as event, Events } from '<%= libraryName %>/event';

export default function() {

function callback(key, err) {
if (!err) {
event.emit(Events.PAGE_INITIALIZED, { key });
}
}

return (
<%= routeComponents %>
);
Expand Down
7 changes: 6 additions & 1 deletion packages/umi/dynamic.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
declare const dynamic: (resolve: (value?: PromiseLike<any>) => void) => void;
declare const dynamic: (resolve: (value?: PromiseLike<any>) => void, opts?: {
/** LoadingComponent */
loading?: Function,
/** The callback of load script */
callback?: (err?: Error) => void,
}) => void;
export default dynamic;
28 changes: 28 additions & 0 deletions packages/umi/event.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export declare class EventBus {
private eventHandlers;
/**
* addEventListener
* @param eventType string
* @param callback event callback
*/
addEventListener<T>(eventType: string, callback: (event: T) => void): void;
/**
* removeEventListener
* @param eventType string
* @param callback event callback
*/
removeEventListener<T>(eventType: string, callback: (event: T) => void): void;
removeAllEventListeners(eventType: any): void;
emit<T>(eventType: any, data: T): void;
}

declare const defaultInstance: EventBus;
export default defaultInstance;

export const Events: {
PAGE_INITIALIZED: string,
};

export namespace EventTypes {
interface PAGE_INITIALIZED_TYPE { key: string }
}
3 changes: 2 additions & 1 deletion packages/umi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"babel.js",
"dynamic.d.ts",
"link.d.ts",
"router.d.ts"
"router.d.ts",
"event.d.ts"
],
"devDependencies": {
"glob": "^7.1.2"
Expand Down
1 change: 1 addition & 0 deletions packages/umi/src/babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export default function(context, opts = {}) {
[`${libraryName}/dynamic`]: require.resolve('./dynamic'),
[`${libraryName}/link`]: require.resolve('./link'),
[`${libraryName}/router`]: require.resolve('./router'),
[`${libraryName}/event`]: require.resolve('./event'),
},
},
],
Expand Down
27 changes: 18 additions & 9 deletions packages/umi/src/dynamic.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import React, { Component } from 'react';

export default function(resolve, opts = {}) {
const { loading: LoadingComponent = () => null } = opts;
const {
loading: LoadingComponent = () => null,
callback = () => null,
} = opts;

return class DynamicComponent extends Component {
constructor(...args) {
Expand All @@ -22,14 +25,20 @@ export default function(resolve, opts = {}) {
}

load() {
resolve().then(m => {
const AsyncComponent = m.default || m;
if (this.mounted) {
this.setState({ AsyncComponent });
} else {
this.state.AsyncComponent = AsyncComponent; // eslint-disable-line
}
});
resolve()
.then(m => {
const AsyncComponent = m.default || m;
if (this.mounted) {
this.setState({ AsyncComponent }, () => {
callback();
});
} else {
this.state.AsyncComponent = AsyncComponent; // eslint-disable-line
}
})
.catch(err => {
callback(err);
});
}

render() {
Expand Down
40 changes: 40 additions & 0 deletions packages/umi/src/event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
export class EventBus {
eventHandlers = {};

addEventListener(type, callback) {
if (!this.eventHandlers[type]) {
this.eventHandlers[type] = [];
}
this.eventHandlers[type].push(callback);
}

removeEventListener(type, callback) {
const element = this.eventHandlers[type];
if (element) {
const index = element.indexOf(callback);
if (index !== -1) {
element.splice(index, 1);
}
}
}

removeAllEventListeners(type) {
this.eventHandlers[type] = [];
}

emit(type, data) {
if (this.eventHandlers[type]) {
this.eventHandlers[type].forEach(handler => {
handler(data);
});
}
}
}

export default new EventBus();

const Events = {
PAGE_INITIALIZED: 'pageInitialized',
};

export { Events };