Skip to content

Commit 44b2749

Browse files
Allow @AsyncComputed to be used with getters
1 parent 77feb81 commit 44b2749

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/index.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
import { IAsyncComputedValue, IAsyncComputedValueBase } from "vue-async-computed";
22
import { createDecorator, VueDecorator } from "vue-class-component";
3+
import { DefaultComputed } from "vue/types/options";
34

45
export type IAsyncComputedOptions<T> = IAsyncComputedValueBase<T>;
56

67
export default function AsyncComputed<TResult>(
7-
computedOptions?: IAsyncComputedOptions<TResult>): VueDecorator {
8+
computedOptions?: IAsyncComputedOptions<TResult>): VueDecorator {
89
return createDecorator((options, key) => {
910
options.asyncComputed = options.asyncComputed || {};
1011

11-
if (options.methods === undefined) {
12-
throw new Error("methods is undefined");
12+
let method: DefaultComputed | null = null
13+
if (options.methods?.[key]) {
14+
method = options.methods[key]
15+
delete options.methods[key];
16+
} else if (options.computed?.[key]) {
17+
method = options.computed[key]
18+
delete options.computed[key];
19+
} else {
20+
throw new Error(`AsyncComputed ${key} is not a method or computed property`);
1321
}
1422

15-
const method = options.methods[key];
16-
1723
options.asyncComputed[key] = {
1824
get: method,
1925
...computedOptions,
2026
} as IAsyncComputedValue<TResult>;
21-
22-
delete options.methods[key];
2327
});
2428
}

0 commit comments

Comments
 (0)