Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
qianlong66
uni-app
提交
38e534db
U
uni-app
项目概览
qianlong66
/
uni-app
与 Fork 源项目一致
Fork自
DCloud / uni-app
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
38e534db
编写于
5月 14, 2021
作者:
D
DCloud_LXH
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(h5): $emit、$on、$off、$once
上级
715d13e8
变更
6
展开全部
隐藏空白更改
内联
并排
Showing
6 changed file
with
688 addition
and
1849 deletion
+688
-1849
packages/uni-api/src/index.ts
packages/uni-api/src/index.ts
+1
-0
packages/uni-api/src/protocols/base/eventBus.ts
packages/uni-api/src/protocols/base/eventBus.ts
+23
-14
packages/uni-api/src/service/base/eventBus.ts
packages/uni-api/src/service/base/eventBus.ts
+106
-0
packages/uni-h5/dist/uni-h5.cjs.js
packages/uni-h5/dist/uni-h5.cjs.js
+190
-877
packages/uni-h5/dist/uni-h5.es.js
packages/uni-h5/dist/uni-h5.es.js
+364
-958
packages/uni-h5/src/service/api/index.ts
packages/uni-h5/src/service/api/index.ts
+4
-0
未找到文件。
packages/uni-api/src/index.ts
浏览文件 @
38e534db
export
*
from
'
./service/base/base64
'
export
*
from
'
./service/base/upx2px
'
export
*
from
'
./service/base/interceptor
'
export
*
from
'
./service/base/eventBus
'
export
*
from
'
./service/context/createVideoContext
'
export
*
from
'
./service/context/createMapContext
'
...
...
packages/uni-api/src/protocols/base/eventBus.ts
浏览文件 @
38e534db
export
const
$on
:
ProtocolOptions
<
String
|
Array
<
String
>
|
Function
>
[]
=
[
export
const
API_ON
=
'
$on
'
export
type
API_TYPE_ON
=
typeof
uni
.
$on
export
const
OnProtocol
:
ProtocolOptions
<
String
|
Function
>
[]
=
[
{
name
:
'
event
'
,
type
:
[
String
,
Array
]
,
type
:
String
,
required
:
true
,
},
{
...
...
@@ -11,20 +13,27 @@ export const $on: ProtocolOptions<String | Array<String> | Function>[] = [
},
]
export
const
$once
=
$on
export
const
API_ONCE
=
'
$once
'
export
type
API_TYPE_ONCE
=
typeof
uni
.
$once
export
const
OnceProtocol
=
OnProtocol
export
const
$off
:
ProtocolOptions
<
String
|
Array
<
String
>
|
Function
>
[]
=
[
{
name
:
'
event
'
,
type
:
[
String
,
Array
],
},
{
name
:
'
callback
'
,
type
:
Function
,
},
]
export
const
API_OFF
=
'
$off
'
export
type
API_TYPE_OFF
=
typeof
uni
.
$off
export
const
OffProtocol
:
ProtocolOptions
<
String
|
Function
|
Array
<
String
>>
[]
=
[
{
name
:
'
event
'
,
type
:
[
String
,
Array
],
},
{
name
:
'
callback
'
,
type
:
Function
,
},
]
export
const
$emit
:
ProtocolOptions
<
String
>
[]
=
[
export
const
API_EMIT
=
'
$emit
'
export
type
API_TYPE_EMIT
=
typeof
uni
.
$emit
export
const
EmitProtocol
:
ProtocolOptions
<
String
>
[]
=
[
{
name
:
'
event
'
,
type
:
String
,
...
...
packages/uni-api/src/service/base/eventBus.ts
0 → 100644
浏览文件 @
38e534db
import
{
defineSyncApi
}
from
'
../../helpers/api
'
import
{
API_ON
,
API_OFF
,
API_EMIT
,
API_ONCE
,
API_TYPE_ON
,
API_TYPE_OFF
,
API_TYPE_EMIT
,
API_TYPE_ONCE
,
OnProtocol
,
OffProtocol
,
EmitProtocol
,
OnceProtocol
,
}
from
'
../../protocols/base/eventBus
'
type
EventName
=
string
|
number
|
symbol
type
EventCallback
=
(...
args
:
any
)
=>
any
type
EventStopHandler
=
()
=>
void
class
Emitter
{
private
eventMap
:
Map
<
EventName
,
Array
<
EventCallback
>>
constructor
()
{
this
.
eventMap
=
new
Map
()
}
on
=
(
name
:
EventName
,
callback
:
EventCallback
):
EventStopHandler
=>
{
if
(
!
this
.
eventMap
.
has
(
name
))
{
this
.
eventMap
.
set
(
name
,
[])
}
this
.
eventMap
.
get
(
name
)
!
.
push
(
callback
)
return
()
=>
this
.
off
(
name
,
callback
)
}
once
=
(
name
:
EventName
,
callback
:
EventCallback
):
EventStopHandler
=>
{
const
listener
=
(...
args
:
any
[])
=>
{
this
.
off
(
name
,
listener
)
callback
(...
args
)
}
this
.
on
(
name
,
listener
)
return
()
=>
this
.
off
(
name
,
listener
)
}
emit
=
(
name
:
EventName
,
...
args
:
any
[]):
void
=>
{
const
cbs
=
this
.
eventMap
.
get
(
name
)
if
(
cbs
instanceof
Array
)
{
cbs
.
forEach
((
cb
)
=>
{
typeof
cb
===
'
function
'
&&
cb
(...
args
)
})
}
}
off
=
(
names
?:
EventName
|
EventName
[],
callback
?:
EventCallback
):
void
=>
{
if
(
!
names
)
{
this
.
eventMap
.
clear
()
return
}
if
(
!
(
names
instanceof
Array
))
{
names
=
[
names
]
}
if
(
typeof
callback
===
'
function
'
)
{
names
.
forEach
((
name
)
=>
{
if
(
this
.
eventMap
.
has
(
name
))
{
this
.
eventMap
.
set
(
name
,
this
.
eventMap
.
get
(
name
)
!
.
filter
((
cb
)
=>
cb
!==
callback
)
)
}
})
}
else
{
names
.
forEach
((
name
)
=>
{
this
.
eventMap
.
delete
(
name
)
})
}
}
}
const
emitter
=
new
Emitter
()
export
const
$on
=
defineSyncApi
<
API_TYPE_ON
>
(
API_ON
,
(
type
,
callback
):
EventStopHandler
=>
emitter
.
on
(
type
,
callback
),
OnProtocol
)
export
const
$once
=
defineSyncApi
<
API_TYPE_ONCE
>
(
API_ONCE
,
(
type
,
callback
):
EventStopHandler
=>
emitter
.
once
(
type
,
callback
),
OnceProtocol
)
export
const
$off
=
defineSyncApi
<
API_TYPE_OFF
>
(
API_OFF
,
(
type
,
callback
)
=>
{
emitter
.
off
(
type
,
callback
)
},
OffProtocol
)
export
const
$emit
=
defineSyncApi
<
API_TYPE_EMIT
>
(
API_EMIT
,
emitter
.
emit
,
EmitProtocol
)
packages/uni-h5/dist/uni-h5.cjs.js
浏览文件 @
38e534db
此差异已折叠。
点击以展开。
packages/uni-h5/dist/uni-h5.es.js
浏览文件 @
38e534db
此差异已折叠。
点击以展开。
packages/uni-h5/src/service/api/index.ts
浏览文件 @
38e534db
...
...
@@ -76,5 +76,9 @@ export {
canvasPutImageData
,
canvasToTempFilePath
,
getSelectedTextRange
,
$on
,
$off
,
$once
,
$emit
,
}
from
'
@dcloudio/uni-api
'
//#endif
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录