Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Bug 终结者
vue-vben-admin
提交
ae5f5cb1
V
vue-vben-admin
项目概览
Bug 终结者
/
vue-vben-admin
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
V
vue-vben-admin
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
ae5f5cb1
编写于
4月 06, 2023
作者:
K
Kirk Lin
提交者:
GitHub
4月 06, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: sync mitt (#2677)
上级
ccaa84c3
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
56 addition
and
35 deletion
+56
-35
src/utils/mitt.ts
src/utils/mitt.ts
+56
-35
未找到文件。
src/utils/mitt.ts
浏览文件 @
ae5f5cb1
...
...
@@ -2,32 +2,37 @@
* copy to https://github.com/developit/mitt
* Expand clear method
*/
export
type
EventType
=
string
|
symbol
;
// An event handler can take an optional event argument
// and should not return a value
export
type
Handler
<
T
=
any
>
=
(
event
?:
T
)
=>
void
;
export
type
WildcardHandler
=
(
type
:
EventType
,
event
?:
any
)
=>
void
;
export
type
Handler
<
T
=
unknown
>
=
(
event
:
T
)
=>
void
;
export
type
WildcardHandler
<
T
=
Record
<
string
,
unknown
>>
=
(
type
:
keyof
T
,
event
:
T
[
keyof
T
],
)
=>
void
;
// An array of all currently registered event handlers for a type
export
type
EventHandlerList
=
Array
<
Handler
>
;
export
type
WildCardEventHandlerList
=
Array
<
WildcardHandler
>
;
export
type
EventHandlerList
<
T
=
unknown
>
=
Array
<
Handler
<
T
>
>
;
export
type
WildCardEventHandlerList
<
T
=
Record
<
string
,
unknown
>>
=
Array
<
WildcardHandler
<
T
>
>
;
// A map of event types and their corresponding event handlers.
export
type
EventHandlerMap
=
Map
<
EventType
,
EventHandlerList
|
WildCardEventHandlerList
>
;
export
type
EventHandlerMap
<
Events
extends
Record
<
EventType
,
unknown
>>
=
Map
<
keyof
Events
|
'
*
'
,
EventHandlerList
<
Events
[
keyof
Events
]
>
|
WildCardEventHandlerList
<
Events
>
>
;
export
interface
Emitter
{
all
:
EventHandlerMap
;
export
interface
Emitter
<
Events
extends
Record
<
EventType
,
unknown
>>
{
all
:
EventHandlerMap
<
Events
>
;
on
<
T
=
any
>
(
type
:
EventType
,
handler
:
Handler
<
T
>
):
void
;
on
(
type
:
'
*
'
,
handler
:
WildcardHandler
):
void
;
on
<
Key
extends
keyof
Events
>
(
type
:
Key
,
handler
:
Handler
<
Events
[
Key
]
>
):
void
;
on
(
type
:
'
*
'
,
handler
:
WildcardHandler
<
Events
>
):
void
;
off
<
T
=
any
>
(
type
:
EventType
,
handler
:
Handler
<
T
>
):
void
;
off
(
type
:
'
*
'
,
handler
:
WildcardHandler
):
void
;
off
<
Key
extends
keyof
Events
>
(
type
:
Key
,
handler
?:
Handler
<
Events
[
Key
]
>
):
void
;
off
(
type
:
'
*
'
,
handler
:
WildcardHandler
<
Events
>
):
void
;
emit
<
T
=
any
>
(
type
:
EventType
,
event
?:
T
):
void
;
emit
(
type
:
'
*
'
,
event
?:
any
):
void
;
emit
<
Key
extends
keyof
Events
>
(
type
:
Key
,
event
:
Events
[
Key
]
):
void
;
emit
<
Key
extends
keyof
Events
>
(
type
:
undefined
extends
Events
[
Key
]
?
Key
:
never
):
void
;
clear
():
void
;
}
...
...
@@ -36,7 +41,10 @@ export interface Emitter {
* @name mitt
* @returns {Mitt}
*/
export
default
function
mitt
(
all
?:
EventHandlerMap
):
Emitter
{
export
function
mitt
<
Events
extends
Record
<
EventType
,
unknown
>>
(
all
?:
EventHandlerMap
<
Events
>
,
):
Emitter
<
Events
>
{
type
GenericEventHandler
=
Handler
<
Events
[
keyof
Events
]
>
|
WildcardHandler
<
Events
>
;
all
=
all
||
new
Map
();
return
{
...
...
@@ -47,48 +55,61 @@ export default function mitt(all?: EventHandlerMap): Emitter {
/**
* Register an event handler for the given type.
* @param {string|symbol} type Type of event to listen for, or `
"*"
` for all events
* @param {string|symbol} type Type of event to listen for, or `
'*'
` for all events
* @param {Function} handler Function to call in response to given event
* @memberOf mitt
*/
on
<
T
=
any
>
(
type
:
EventType
,
handler
:
Handler
<
T
>
)
{
const
handlers
=
all
?.
get
(
type
);
const
added
=
handlers
&&
handlers
.
push
(
handler
);
if
(
!
added
)
{
all
?.
set
(
type
,
[
handler
]);
on
<
Key
extends
keyof
Events
>
(
type
:
Key
,
handler
:
GenericEventHandler
)
{
const
handlers
:
Array
<
GenericEventHandler
>
|
undefined
=
all
!
.
get
(
type
);
if
(
handlers
)
{
handlers
.
push
(
handler
);
}
else
{
all
!
.
set
(
type
,
[
handler
]
as
EventHandlerList
<
Events
[
keyof
Events
]
>
);
}
},
/**
* Remove an event handler for the given type.
* @param {string|symbol} type Type of event to unregister `handler` from, or `"*"`
* @param {Function} handler Handler function to remove
* If `handler` is omitted, all handlers of the given type are removed.
* @param {string|symbol} type Type of event to unregister `handler` from (`'*'` to remove a wildcard handler)
* @param {Function} [handler] Handler function to remove
* @memberOf mitt
*/
off
<
T
=
any
>
(
type
:
EventType
,
handler
:
Handler
<
T
>
)
{
const
handlers
=
all
?
.
get
(
type
);
off
<
Key
extends
keyof
Events
>
(
type
:
Key
,
handler
?:
GenericEventHandler
)
{
const
handlers
:
Array
<
GenericEventHandler
>
|
undefined
=
all
!
.
get
(
type
);
if
(
handlers
)
{
handlers
.
splice
(
handlers
.
indexOf
(
handler
)
>>>
0
,
1
);
if
(
handler
)
{
handlers
.
splice
(
handlers
.
indexOf
(
handler
)
>>>
0
,
1
);
}
else
{
all
!
.
set
(
type
,
[]);
}
}
},
/**
* Invoke all handlers for the given type.
* If present, `
"*"
` handlers are invoked after type-matched handlers.
* If present, `
'*'
` handlers are invoked after type-matched handlers.
*
* Note: Manually firing
"*"
handlers is not supported.
* Note: Manually firing
'*'
handlers is not supported.
*
* @param {string|symbol} type The event type to invoke
* @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler
* @memberOf mitt
*/
emit
<
T
=
any
>
(
type
:
EventType
,
evt
:
T
)
{
((
all
?.
get
(
type
)
||
[])
as
EventHandlerList
).
slice
().
map
((
handler
)
=>
{
handler
(
evt
);
});
((
all
?.
get
(
'
*
'
)
||
[])
as
WildCardEventHandlerList
).
slice
().
map
((
handler
)
=>
{
handler
(
type
,
evt
);
});
emit
<
Key
extends
keyof
Events
>
(
type
:
Key
,
evt
?:
Events
[
Key
])
{
let
handlers
=
all
!
.
get
(
type
);
if
(
handlers
)
{
(
handlers
as
EventHandlerList
<
Events
[
keyof
Events
]
>
).
slice
().
forEach
((
handler
)
=>
{
handler
(
evt
as
Events
[
Key
]);
});
}
handlers
=
all
!
.
get
(
'
*
'
);
if
(
handlers
)
{
(
handlers
as
WildCardEventHandlerList
<
Events
>
).
slice
().
forEach
((
handler
)
=>
{
handler
(
type
,
evt
as
Events
[
Key
]);
});
}
},
/**
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录