-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from umijs/feat-event
feat: add eventbus and dynamic import page init event, ref #38.
- Loading branch information
Showing
10 changed files
with
114 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1 @@ | ||
export default { | ||
alias: { | ||
dva: 'dva-no-router', | ||
}, | ||
}; | ||
export default {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |