Skip to content

Commit 58337c5

Browse files
jameseasterdlongley
authored andcommitted
Update createEmitExtendable to optionally receive emit function.
1 parent dad56c1 commit 58337c5

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

lib/index.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@ export default function install(app) {
77
app.config.globalProperties.$emitExtendable = emitExtendable;
88
}
99

10-
export function createEmitExtendable() {
10+
export function createEmitExtendable({emit}) {
11+
if(!emit && !getCurrentInstance()) {
12+
throw new TypeError('Emit function is not present.');
13+
}
14+
if(emit) {
15+
return emitExtendable.bind({emit});
16+
}
1117
return emitExtendable.bind(getCurrentInstance());
1218
}
1319

14-
// Pass in the emit function from setup when using a Vue 3 composition api component
15-
export async function emitExtendable(name, event = {}, emit) {
20+
export async function emitExtendable(name, event = {}) {
1621
if(!(event && typeof event === 'object')) {
1722
throw new TypeError('"event" must be an object.');
1823
}
@@ -35,7 +40,7 @@ export async function emitExtendable(name, event = {}, emit) {
3540
}
3641
response = p;
3742
}
38-
}, emit);
43+
});
3944

4045
// await any lifetime extensions
4146
await Promise.all(promises);
@@ -50,13 +55,7 @@ function _emit(...args) {
5055
}
5156
if(this?.emit) {
5257
return this.emit(...args);
53-
}
54-
if(getCurrentInstance()) {
55-
const {emit} = getCurrentInstance();
56-
return emit(...args);
57-
}
58-
if(typeof args[2] === 'function') {
59-
const emit = args[2];
60-
return emit(...args.slice(0, 2));
6158
}
59+
const {emit} = getCurrentInstance();
60+
return emit(...args);
6261
}

0 commit comments

Comments
 (0)