-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
71 additions
and
108 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 +1 @@ | ||
{"1.4.13":"1.4","2.0.11":"2.0","2.1.0":"2.1","2.2.2":"2.2","2.3.9":"2.3","2.4.11":"2.4","2.5.4":"2.5","2.6.3":"2.6","2.7.2":"2.7","2.8.2":"2.8","2.9.2":"2.9","2.10.1":"2.10","2.11.1":"2.11","2.12.0":"2.12","0.0.8":"2.13"} | ||
{"1.4.13":"1.4","2.0.11":"2.0","2.1.0":"2.1","2.2.2":"2.2","2.3.9":"2.3","2.4.11":"2.4","2.5.4":"2.5","2.6.3":"2.6","2.7.2":"2.7","2.8.2":"2.8","2.9.2":"2.9","2.10.1":"2.10","2.11.1":"2.11","2.12.0":"2.12","0.0.11":"2.13"} |
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,129 +1,92 @@ | ||
import { getCurrentInstance } from 'vue' | ||
import { capitalize } from 'element-ui/src/utils/util' | ||
const EVENT_NAME_KEY = Symbol('ELEMENT_EVENTS') | ||
import mitt from 'mitt' | ||
|
||
export function useEmitter(instance = getCurrentInstance()) { | ||
return { | ||
dispatch: dispatch(instance), | ||
broadcast: broadcast(instance), | ||
on: on(instance), | ||
once: once(instance), | ||
off: off(instance) | ||
} | ||
} | ||
const DISPATCH = 'dispatch' | ||
const BROADCAST = 'broadcast' | ||
|
||
function on(instance) { | ||
return (originalEventName, callback) => { | ||
const eventName = 'on' + capitalize(originalEventName) | ||
const wrapper = Symbol('wrapper') | ||
|
||
if (!instance.vnode.props) { | ||
instance.vnode.props = {} | ||
} | ||
const emitter = mitt() | ||
|
||
if (!instance.vnode.props[EVENT_NAME_KEY]) { | ||
instance.vnode.props[EVENT_NAME_KEY] = new Set() | ||
} | ||
instance.vnode.props[EVENT_NAME_KEY].add(eventName) | ||
export function useEmitter() { | ||
const currentComponentInstance = getCurrentInstance() | ||
|
||
if (!instance.vnode.props[eventName]) { | ||
instance.vnode.props[eventName] = (...params) => { | ||
const callbacks = instance.vnode.props[eventName].__events | ||
if (callbacks) { | ||
callbacks.forEach((cf) => { | ||
cf(...params) | ||
}) | ||
function on(type, handler) { | ||
const handleWrapper = (e) => { | ||
const { value, type, emitComponentInstance } = e | ||
if (type === BROADCAST) { | ||
if (isChildComponent(currentComponentInstance, emitComponentInstance)) { | ||
handler && handler(value) | ||
} | ||
} else if (type === DISPATCH) { | ||
if (isChildComponent(emitComponentInstance, currentComponentInstance)) { | ||
handler && handler(value) | ||
} | ||
} else { | ||
handler && handler(value) | ||
} | ||
instance.vnode.props[eventName].__events = [] | ||
} | ||
instance.vnode.props[eventName].__events.push(callback) | ||
} | ||
} | ||
function once(instance) { | ||
const $off = off(instance) | ||
const $on = on(instance) | ||
return (originalEventName, handle) => { | ||
const _on = (...params) => { | ||
$off(originalEventName, _on) | ||
handle(...params) | ||
} | ||
$on(originalEventName, _on) | ||
} | ||
} | ||
|
||
function off(instance) { | ||
return (originalEventName, callback) => { | ||
const eventNameList = | ||
instance.vnode.props && instance.vnode.props[EVENT_NAME_KEY] | ||
if (!eventNameList || !eventNameList.size) { | ||
return | ||
} | ||
// Save the real handler because the need to call off | ||
handler[wrapper] = handleWrapper | ||
emitter.on(type, handleWrapper) | ||
} | ||
|
||
if (!originalEventName) { | ||
eventNameList.forEach((eventName) => { | ||
delete instance.vnode.props[eventName] | ||
}) | ||
eventNameList.clear() | ||
return | ||
} | ||
function broadcast(type, evt) { | ||
emitter.emit(type, { | ||
type: BROADCAST, | ||
emitComponentInstance: currentComponentInstance, | ||
value: evt | ||
}) | ||
} | ||
|
||
const eventName = 'on' + capitalize(originalEventName) | ||
function dispatch(type, evt) { | ||
emitter.emit(type, { | ||
type: DISPATCH, | ||
emitComponentInstance: currentComponentInstance, | ||
value: evt | ||
}) | ||
} | ||
|
||
if (!callback) { | ||
delete instance.vnode.props[eventName] | ||
eventNameList.delete(eventName) | ||
return | ||
} | ||
function off(type, handler) { | ||
emitter.off(type, handler[wrapper]) | ||
} | ||
|
||
const handlers = | ||
instance.vnode.props[eventName] && | ||
instance.vnode.props[eventName].__events | ||
if (handlers && handlers.length) { | ||
const index = handlers.indexOf(callback) | ||
if (index > -1) { | ||
handlers.splice(index, 1) | ||
} | ||
} | ||
function emit(type, evt) { | ||
emitter.emit(type, { | ||
value: evt | ||
}) | ||
} | ||
} | ||
|
||
function dispatch(instance) { | ||
return (componentName, eventName, ...params) => { | ||
let parent = instance.parent | ||
while (parent && parent.type.name !== componentName) { | ||
parent = parent.parent | ||
function once(type, handler) { | ||
const handleOn = () => { | ||
handler && handler() | ||
off(type, handleOn) | ||
} | ||
on(type, handleOn) | ||
} | ||
|
||
if (parent) { | ||
parent.emit(eventName, ...params) | ||
} | ||
return { | ||
on, | ||
broadcast, | ||
dispatch, | ||
off, | ||
emit, | ||
once | ||
} | ||
} | ||
|
||
function broadcast(instance) { | ||
return (componentName, eventName, ...params) => { | ||
const _broadcast = (componentInstance) => { | ||
if (!componentInstance) return | ||
/** | ||
* check componentChild is componentParent child components | ||
* @param {*} componentChild | ||
* @param {*} componentParent | ||
*/ | ||
function isChildComponent(componentChild, componentParent) { | ||
const parentUId = componentParent.uid | ||
|
||
let children = componentInstance.subTree.children | ||
|
||
if (children.default) { | ||
children = children.default() | ||
} | ||
|
||
if (children) { | ||
children.forEach((vnode) => { | ||
const childComponent = vnode.component | ||
|
||
if (childComponent?.type?.name === componentName) { | ||
childComponent.emit(eventName, ...params) | ||
} else { | ||
_broadcast(childComponent) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
_broadcast(instance) | ||
while (componentChild && componentChild?.parent?.uid !== parentUId) { | ||
componentChild = componentChild.parent | ||
} | ||
|
||
return Boolean(componentChild) | ||
} |