提交 38e534db 编写于 作者: D DCloud_LXH

feat(h5): $emit、$on、$off、$once

上级 715d13e8
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'
......
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,
......
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
)
此差异已折叠。
此差异已折叠。
......@@ -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.
先完成此消息的编辑!
想要评论请 注册