[bugfix] functional component multi listeners (#2717)

This commit is contained in:
neverland
2019-02-12 10:44:40 +08:00
committed by GitHub
parent 0bf4a9e799
commit e6584a28d3
11 changed files with 45 additions and 29 deletions

View File

@@ -20,7 +20,8 @@ const inheritKey = [
const mapInheritKey: ObjectIndex = { nativeOn: 'on' };
export function inheritContext(context: Context): InheritContext {
// inherit partial context, map nativeOn to on
export function inherit(context: Context): InheritContext {
return inheritKey.reduce(
(obj, key) => {
if (context.data[key]) {
@@ -31,3 +32,17 @@ export function inheritContext(context: Context): InheritContext {
{} as InheritContext
);
}
// emit event
export function emit(context: Context, eventName: string, ...args: any[]) {
const listeners = context.listeners[eventName];
if (listeners) {
if (Array.isArray(listeners)) {
listeners.forEach(listener => {
listener(...args);
})
} else {
listeners(...args);
}
}
}