Skip to content

Commit 0a92b1c

Browse files
committed
Merge asyncComputed with computed
this is a proof of concept that asyncComputed can be merged into computed and the distinction between a normal computed and an async-computed can be made by an extra keyword. The upside of this is that it fixes typings as we can rely on the standard typings that come with Vue.js' computed properties.
1 parent 8abb270 commit 0a92b1c

File tree

4 files changed

+220
-141
lines changed

4 files changed

+220
-141
lines changed

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "3.5.0",
44
"description": "Async computed properties for Vue",
55
"main": "dist/vue-async-computed.js",
6-
"types": "types/index.d.ts",
6+
"types": "src/index.d.ts",
77
"files": [
88
"bin/",
99
"dist/"

Diff for: types/index.d.ts renamed to src/index.d.ts

+4-17
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Vue, { PluginFunction } from "vue";
33
export interface IAsyncComputedOptions {
44
errorHandler?: (error: string | Error) => void;
55
useRawError?: boolean;
6+
default?: any;
67
}
78

89
export default class AsyncComputed {
@@ -11,23 +12,9 @@ export default class AsyncComputed {
1112
static version: string;
1213
}
1314

14-
type AsyncComputedGetter<T> = () => Promise<T>;
15-
interface IAsyncComputedProperty<T> {
16-
default?: T | (() => T);
17-
get: AsyncComputedGetter<T>;
18-
watch?: () => void;
19-
shouldUpdate?: () => boolean;
20-
lazy?: boolean;
21-
}
22-
23-
interface IAsyncComputedProperties {
24-
[K: string]: AsyncComputedGetter<any> | IAsyncComputedProperty<any>;
25-
}
26-
2715
declare module "vue/types/options" {
28-
// tslint:disable-next-line:interface-name
29-
interface ComponentOptions<V extends Vue> {
30-
asyncComputed?: IAsyncComputedProperties;
16+
interface ComputedOptions<T> {
17+
asynchronous?: boolean;
3118
}
3219
}
3320

@@ -45,4 +32,4 @@ declare module "vue/types/vue" {
4532
interface Vue {
4633
$asyncComputed: {[K: string]: IASyncComputedState };
4734
}
48-
}
35+
}

Diff for: src/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,17 @@ const AsyncComputed = {
2424
const asyncComputed = this.$options.asyncComputed || {}
2525
this.$asyncComputed = {}
2626

27+
for (const key in this.$options.computed) {
28+
if (this.$options.computed[key].asynchronous) {
29+
asyncComputed[key] = this.$options.computed[key]
30+
delete this.$options.computed[key]
31+
}
32+
}
33+
2734
if (!Object.keys(asyncComputed).length) return
2835

36+
this.$options.asyncComputed = asyncComputed
37+
2938
if (!this.$options.computed) this.$options.computed = {}
3039

3140
for (const key in asyncComputed) {

0 commit comments

Comments
 (0)