Commit d3d620f4fc75dd69270e4d090a71d426701272ef
1 parent
ea6834ae
perf(utils): mitt default export is changed from Class to Function
Showing
8 changed files
with
107 additions
and
86 deletions
CHANGELOG.zh_CN.md
package.json
... | ... | @@ -111,7 +111,7 @@ |
111 | 111 | "lint-staged": "^11.0.0", |
112 | 112 | "postcss": "^8.3.5", |
113 | 113 | "prettier": "^2.3.1", |
114 | - "pretty-quick": "^3.1.0", | |
114 | + "pretty-quick": "^3.1.1", | |
115 | 115 | "rimraf": "^3.0.2", |
116 | 116 | "rollup-plugin-visualizer": "5.5.0", |
117 | 117 | "stylelint": "^13.13.1", |
... | ... | @@ -120,7 +120,7 @@ |
120 | 120 | "stylelint-order": "^4.1.0", |
121 | 121 | "ts-jest": "^27.0.3", |
122 | 122 | "ts-node": "^10.0.0", |
123 | - "typescript": "4.3.3", | |
123 | + "typescript": "4.3.4", | |
124 | 124 | "vite": "2.3.7", |
125 | 125 | "vite-plugin-compression": "^0.2.5", |
126 | 126 | "vite-plugin-html": "^2.0.7", | ... | ... |
src/components/SimpleMenu/src/components/Menu.vue
... | ... | @@ -22,7 +22,7 @@ |
22 | 22 | import { useDesign } from '/@/hooks/web/useDesign'; |
23 | 23 | import { propTypes } from '/@/utils/propTypes'; |
24 | 24 | import { createSimpleRootMenuContext } from './useSimpleMenuContext'; |
25 | - import Mitt from '/@/utils/mitt'; | |
25 | + import mitt from '/@/utils/mitt'; | |
26 | 26 | export default defineComponent({ |
27 | 27 | name: 'Menu', |
28 | 28 | props: { |
... | ... | @@ -44,7 +44,7 @@ |
44 | 44 | }, |
45 | 45 | emits: ['select', 'open-change'], |
46 | 46 | setup(props, { emit }) { |
47 | - const rootMenuEmitter = new Mitt(); | |
47 | + const rootMenuEmitter = mitt(); | |
48 | 48 | const instance = getCurrentInstance(); |
49 | 49 | |
50 | 50 | const currentActiveName = ref<string | number>(''); | ... | ... |
src/components/SimpleMenu/src/components/SubMenuItem.vue
... | ... | @@ -77,7 +77,7 @@ |
77 | 77 | import Icon from '/@/components/Icon'; |
78 | 78 | import { Popover } from 'ant-design-vue'; |
79 | 79 | import { isBoolean, isObject } from '/@/utils/is'; |
80 | - import Mitt from '/@/utils/mitt'; | |
80 | + import mitt from '/@/utils/mitt'; | |
81 | 81 | |
82 | 82 | const DELAY = 200; |
83 | 83 | export default defineComponent({ |
... | ... | @@ -114,7 +114,7 @@ |
114 | 114 | |
115 | 115 | const { prefixCls } = useDesign('menu'); |
116 | 116 | |
117 | - const subMenuEmitter = new Mitt(); | |
117 | + const subMenuEmitter = mitt(); | |
118 | 118 | |
119 | 119 | const { rootMenuEmitter } = useSimpleRootMenuContext(); |
120 | 120 | ... | ... |
src/components/SimpleMenu/src/components/useSimpleMenuContext.ts
1 | 1 | import type { InjectionKey, Ref } from 'vue'; |
2 | 2 | import { createContext, useContext } from '/@/hooks/core/useContext'; |
3 | -import Mitt from '/@/utils/mitt'; | |
3 | +import mitt from '/@/utils/mitt'; | |
4 | 4 | |
5 | 5 | export interface SimpleRootMenuContextProps { |
6 | - rootMenuEmitter: Mitt; | |
6 | + rootMenuEmitter: typeof mitt; | |
7 | 7 | activeName: Ref<string | number>; |
8 | 8 | } |
9 | 9 | ... | ... |
src/logics/mitt/routeChange.ts
... | ... | @@ -2,11 +2,11 @@ |
2 | 2 | * Used to monitor routing changes to change the status of menus and tabs. There is no need to monitor the route, because the route status change is affected by the page rendering time, which will be slow |
3 | 3 | */ |
4 | 4 | |
5 | -import Mitt from '/@/utils/mitt'; | |
5 | +import mitt from '/@/utils/mitt'; | |
6 | 6 | import type { RouteLocationNormalized } from 'vue-router'; |
7 | 7 | import { getRawRoute } from '/@/utils'; |
8 | 8 | |
9 | -const mitt = new Mitt(); | |
9 | +const emitter = mitt(); | |
10 | 10 | |
11 | 11 | const key = Symbol(); |
12 | 12 | |
... | ... | @@ -14,7 +14,7 @@ let lastChangeTab: RouteLocationNormalized; |
14 | 14 | |
15 | 15 | export function setRouteChange(lastChangeRoute: RouteLocationNormalized) { |
16 | 16 | const r = getRawRoute(lastChangeRoute); |
17 | - mitt.emit(key, r); | |
17 | + emitter.emit(key, r); | |
18 | 18 | lastChangeTab = r; |
19 | 19 | } |
20 | 20 | |
... | ... | @@ -22,10 +22,10 @@ export function listenerRouteChange( |
22 | 22 | callback: (route: RouteLocationNormalized) => void, |
23 | 23 | immediate = true |
24 | 24 | ) { |
25 | - mitt.on(key, callback); | |
25 | + emitter.on(key, callback); | |
26 | 26 | immediate && lastChangeTab && callback(lastChangeTab); |
27 | 27 | } |
28 | 28 | |
29 | 29 | export function removeTabChangeListener() { |
30 | - mitt.clear(); | |
30 | + emitter.clear(); | |
31 | 31 | } | ... | ... |
src/utils/mitt.ts
1 | 1 | /** |
2 | - * Mitt: Tiny functional event emitter / pubsub | |
3 | - * | |
4 | - * @name mitt | |
5 | - * @param {Array} [all] Optional array of event names to registered handler functions | |
6 | - * @returns {Function} The function's instance | |
2 | + * https://github.com/developit/mitt | |
7 | 3 | */ |
8 | -export default class Mitt { | |
9 | - private cache: Map<string | Symbol, Array<(...data: any) => void>>; | |
10 | - constructor(all = []) { | |
11 | - // A Map of event names to registered handler functions. | |
12 | - this.cache = new Map(all); | |
13 | - } | |
14 | 4 | |
15 | - once(type: string | Symbol, handler: Fn) { | |
16 | - const decor = (...args: any[]) => { | |
17 | - handler && handler.apply(this, args); | |
18 | - this.off(type, decor); | |
19 | - }; | |
20 | - this.on(type, decor); | |
21 | - return this; | |
22 | - } | |
5 | +export type EventType = string | symbol; | |
6 | + | |
7 | +// An event handler can take an optional event argument | |
8 | +// and should not return a value | |
9 | +export type Handler<T = any> = (event?: T) => void; | |
10 | +export type WildcardHandler = (type: EventType, event?: any) => void; | |
11 | + | |
12 | +// An array of all currently registered event handlers for a type | |
13 | +export type EventHandlerList = Array<Handler>; | |
14 | +export type WildCardEventHandlerList = Array<WildcardHandler>; | |
15 | + | |
16 | +// A map of event types and their corresponding event handlers. | |
17 | +export type EventHandlerMap = Map<EventType, EventHandlerList | WildCardEventHandlerList>; | |
18 | + | |
19 | +export interface Emitter { | |
20 | + all: EventHandlerMap; | |
21 | + | |
22 | + on<T = any>(type: EventType, handler: Handler<T>): void; | |
23 | + on(type: '*', handler: WildcardHandler): void; | |
24 | + | |
25 | + off<T = any>(type: EventType, handler: Handler<T>): void; | |
26 | + off(type: '*', handler: WildcardHandler): void; | |
27 | + | |
28 | + emit<T = any>(type: EventType, event?: T): void; | |
29 | + emit(type: '*', event?: any): void; | |
30 | +} | |
31 | + | |
32 | +/** | |
33 | + * Mitt: Tiny (~200b) functional event emitter / pubsub. | |
34 | + * @name mitt | |
35 | + * @returns {Mitt} | |
36 | + */ | |
37 | +export default function mitt(all?: EventHandlerMap): Emitter { | |
38 | + all = all || new Map(); | |
23 | 39 | |
24 | - /** | |
25 | - * Register an event handler for the given type. | |
26 | - * | |
27 | - * @param {string|symbol} type Type of event to listen for, or `"*"` for all events | |
28 | - * @param {Function} handler Function to call in response to given event | |
29 | - */ | |
30 | - on(type: string | Symbol, handler: Fn) { | |
31 | - const handlers = this.cache?.get(type); | |
32 | - const added = handlers && handlers.push(handler); | |
33 | - if (!added) { | |
34 | - this.cache.set(type, [handler]); | |
35 | - } | |
36 | - } | |
40 | + return { | |
41 | + /** | |
42 | + * A Map of event names to registered handler functions. | |
43 | + */ | |
44 | + all, | |
37 | 45 | |
38 | - /** | |
39 | - * Remove an event handler for the given type. | |
40 | - * | |
41 | - * @param {string|symbol} type Type of event to unregister `handler` from, or `"*"` | |
42 | - * @param {Function} handler Handler function to remove | |
43 | - */ | |
44 | - off(type: string | Symbol, handler: Fn) { | |
45 | - const handlers = this.cache.get(type); | |
46 | - if (handlers) { | |
47 | - handlers.splice(handlers.indexOf(handler) >>> 0, 1); | |
48 | - } | |
49 | - } | |
46 | + /** | |
47 | + * Register an event handler for the given type. | |
48 | + * @param {string|symbol} type Type of event to listen for, or `"*"` for all events | |
49 | + * @param {Function} handler Function to call in response to given event | |
50 | + * @memberOf mitt | |
51 | + */ | |
52 | + on<T = any>(type: EventType, handler: Handler<T>) { | |
53 | + const handlers = all?.get(type); | |
54 | + const added = handlers && handlers.push(handler); | |
55 | + if (!added) { | |
56 | + all?.set(type, [handler]); | |
57 | + } | |
58 | + }, | |
50 | 59 | |
51 | - /** | |
52 | - * Invoke all handlers for the given type. | |
53 | - * If present, `"*"` handlers are invoked after type-matched handlers. | |
54 | - * | |
55 | - * Note: Manually firing "*" handlers is not supported. | |
56 | - * | |
57 | - * @param {string|symbol} type The event type to invoke | |
58 | - * @param {*} [evt] Any value (object is recommended and powerful), passed to each handler | |
59 | - */ | |
60 | - emit(type: string | Symbol, evt?: any) { | |
61 | - for (const handler of (this.cache.get(type) || []).slice()) handler(evt); | |
62 | - for (const handler of (this.cache.get('*') || []).slice()) handler(type, evt); | |
63 | - } | |
60 | + /** | |
61 | + * Remove an event handler for the given type. | |
62 | + * @param {string|symbol} type Type of event to unregister `handler` from, or `"*"` | |
63 | + * @param {Function} handler Handler function to remove | |
64 | + * @memberOf mitt | |
65 | + */ | |
66 | + off<T = any>(type: EventType, handler: Handler<T>) { | |
67 | + const handlers = all?.get(type); | |
68 | + if (handlers) { | |
69 | + handlers.splice(handlers.indexOf(handler) >>> 0, 1); | |
70 | + } | |
71 | + }, | |
64 | 72 | |
65 | - /** | |
66 | - * Remove all event handlers. | |
67 | - * | |
68 | - * Note: This will also remove event handlers passed via `mitt(all: EventHandlerMap)`. | |
69 | - */ | |
70 | - clear() { | |
71 | - this.cache.clear(); | |
72 | - } | |
73 | + /** | |
74 | + * Invoke all handlers for the given type. | |
75 | + * If present, `"*"` handlers are invoked after type-matched handlers. | |
76 | + * | |
77 | + * Note: Manually firing "*" handlers is not supported. | |
78 | + * | |
79 | + * @param {string|symbol} type The event type to invoke | |
80 | + * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler | |
81 | + * @memberOf mitt | |
82 | + */ | |
83 | + emit<T = any>(type: EventType, evt: T) { | |
84 | + ((all?.get(type) || []) as EventHandlerList).slice().map((handler) => { | |
85 | + handler(evt); | |
86 | + }); | |
87 | + ((all?.get('*') || []) as WildCardEventHandlerList).slice().map((handler) => { | |
88 | + handler(type, evt); | |
89 | + }); | |
90 | + }, | |
91 | + }; | |
73 | 92 | } | ... | ... |
yarn.lock
... | ... | @@ -9760,10 +9760,10 @@ pretty-format@^27.0.2: |
9760 | 9760 | ansi-styles "^5.0.0" |
9761 | 9761 | react-is "^17.0.1" |
9762 | 9762 | |
9763 | -pretty-quick@^3.1.0: | |
9764 | - version "3.1.0" | |
9765 | - resolved "https://registry.npmjs.org/pretty-quick/-/pretty-quick-3.1.0.tgz#cb172e9086deb57455dea7c7e8f136cd0a4aef6c" | |
9766 | - integrity sha512-DtxIxksaUWCgPFN7E1ZZk4+Aav3CCuRdhrDSFZENb404sYMtuo9Zka823F+Mgeyt8Zt3bUiCjFzzWYE9LYqkmQ== | |
9763 | +pretty-quick@^3.1.1: | |
9764 | + version "3.1.1" | |
9765 | + resolved "https://registry.npmjs.org/pretty-quick/-/pretty-quick-3.1.1.tgz#93ca4e2dd38cc4e970e3f54a0ead317a25454688" | |
9766 | + integrity sha512-ZYLGiMoV2jcaas3vTJrLvKAYsxDoXQBUn8OSTxkl67Fyov9lyXivJTl0+2WVh+y6EovGcw7Lm5ThYpH+Sh3XxQ== | |
9767 | 9767 | dependencies: |
9768 | 9768 | chalk "^3.0.0" |
9769 | 9769 | execa "^4.0.0" |
... | ... | @@ -11765,10 +11765,10 @@ typescript-vscode-sh-plugin@^0.6.14: |
11765 | 11765 | resolved "https://registry.npmjs.org/typescript-vscode-sh-plugin/-/typescript-vscode-sh-plugin-0.6.14.tgz#a81031b502f6346a26ea49ce082438c3e353bb38" |
11766 | 11766 | integrity sha512-AkNlRBbI6K7gk29O92qthNSvc6jjmNQ6isVXoYxkFwPa8D04tIv2SOPd+sd+mNpso4tNdL2gy7nVtrd5yFqvlA== |
11767 | 11767 | |
11768 | -typescript@4.3.3: | |
11769 | - version "4.3.3" | |
11770 | - resolved "https://registry.npmjs.org/typescript/-/typescript-4.3.3.tgz#5401db69bd3203daf1851a1a74d199cb3112c11a" | |
11771 | - integrity sha512-rUvLW0WtF7PF2b9yenwWUi9Da9euvDRhmH7BLyBG4DCFfOJ850LGNknmRpp8Z8kXNUPObdZQEfKOiHtXuQHHKA== | |
11768 | +typescript@4.3.4: | |
11769 | + version "4.3.4" | |
11770 | + resolved "https://registry.npmjs.org/typescript/-/typescript-4.3.4.tgz#3f85b986945bcf31071decdd96cf8bfa65f9dcbc" | |
11771 | + integrity sha512-uauPG7XZn9F/mo+7MrsRjyvbxFpzemRjKEZXS4AK83oP2KKOJPvb+9cO/gmnv8arWZvhnjVOXz7B49m1l0e9Ew== | |
11772 | 11772 | |
11773 | 11773 | uglify-js@^3.1.4: |
11774 | 11774 | version "3.13.3" | ... | ... |