Skip to content

Commit 4d8d132

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 4d8d132

File tree

5 files changed

+1087
-139
lines changed

5 files changed

+1087
-139
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

+27-15
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,6 +12,32 @@ export default class AsyncComputed {
1112
static version: string;
1213
}
1314

15+
declare module "vue/types/options" {
16+
interface ComputedOptions<T> {
17+
asynchronous?: boolean;
18+
}
19+
}
20+
21+
interface IASyncComputedState {
22+
state: "updating" | "success" | "error";
23+
updating: boolean;
24+
success: boolean;
25+
error: boolean;
26+
exception: Error | null;
27+
update: () => void;
28+
}
29+
30+
declare module "vue/types/vue" {
31+
// tslint:disable-next-line:interface-name
32+
interface Vue {
33+
$asyncComputed: {[K: string]: IASyncComputedState };
34+
}
35+
}
36+
37+
// import { ThisTypedComponentOptionsWithArrayProps, ThisTypedComponentOptionsWithRecordProps } from "vue/types/options";
38+
39+
/*
40+
1441
type AsyncComputedGetter<T> = () => Promise<T>;
1542
interface IAsyncComputedProperty<T> {
1643
default?: T | (() => T);
@@ -31,18 +58,3 @@ declare module "vue/types/options" {
3158
}
3259
}
3360

34-
interface IASyncComputedState {
35-
state: "updating" | "success" | "error";
36-
updating: boolean;
37-
success: boolean;
38-
error: boolean;
39-
exception: Error | null;
40-
update: () => void;
41-
}
42-
43-
declare module "vue/types/vue" {
44-
// tslint:disable-next-line:interface-name
45-
interface Vue {
46-
$asyncComputed: {[K: string]: IASyncComputedState };
47-
}
48-
}

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)