Skip to content

Commit ed88abd

Browse files
committed
[change] Adapt asynchronous computation
1 parent cc81d76 commit ed88abd

File tree

7 files changed

+132
-39
lines changed

7 files changed

+132
-39
lines changed

Diff for: package-lock.json

+35-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"core-js": "^2.6.5",
1212
"register-service-worker": "^1.6.2",
1313
"vue": "^2.6.6",
14+
"vue-async-computed": "^3.6.1",
1415
"vue-class-component": "^6.0.0",
1516
"vue-property-decorator": "^8.0.0"
1617
},

Diff for: src/AsyncComputed.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// (from: https://github.com/foxbenjaminfox/vue-async-computed/issues/25#issuecomment-459369072)
2+
3+
import { createDecorator, VueDecorator } from 'vue-class-component';
4+
import { IAsyncComputedProperty } from 'vue-async-computed';
5+
6+
export function AsyncComputed<T>(
7+
computedOptions?: IAsyncComputedProperty<T>,
8+
): VueDecorator {
9+
return createDecorator((_options, key) => {
10+
// TODO: Not use any casting
11+
const options: any = _options as any;
12+
options.asyncComputed = options.asyncComputed || {};
13+
const method = options.methods![key];
14+
options.asyncComputed![key] = <IAsyncComputedProperty<T>>{
15+
get: method,
16+
...computedOptions,
17+
};
18+
delete options.methods![key];
19+
});
20+
}

Diff for: src/components/HelloWorld.vue

+12-28
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,27 @@
11
<template>
22
<div class="hello">
33
<h1>{{ msg }}</h1>
4-
<p>
5-
For a guide and recipes on how to configure / customize this project,<br>
6-
check out the
7-
<a href="https://cli.vuejs.org" target="_blank" rel="noopener">vue-cli documentation</a>.
8-
</p>
9-
<h3>Installed CLI Plugins</h3>
10-
<ul>
11-
<li><a href="https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-babel" target="_blank" rel="noopener">babel</a></li>
12-
<li><a href="https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-typescript" target="_blank" rel="noopener">typescript</a></li>
13-
<li><a href="https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-pwa" target="_blank" rel="noopener">pwa</a></li>
14-
<li><a href="https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-unit-mocha" target="_blank" rel="noopener">unit-mocha</a></li>
15-
</ul>
16-
<h3>Essential Links</h3>
17-
<ul>
18-
<li><a href="https://vuejs.org" target="_blank" rel="noopener">Core Docs</a></li>
19-
<li><a href="https://forum.vuejs.org" target="_blank" rel="noopener">Forum</a></li>
20-
<li><a href="https://chat.vuejs.org" target="_blank" rel="noopener">Community Chat</a></li>
21-
<li><a href="https://twitter.com/vuejs" target="_blank" rel="noopener">Twitter</a></li>
22-
<li><a href="https://news.vuejs.org" target="_blank" rel="noopener">News</a></li>
23-
</ul>
24-
<h3>Ecosystem</h3>
25-
<ul>
26-
<li><a href="https://router.vuejs.org" target="_blank" rel="noopener">vue-router</a></li>
27-
<li><a href="https://vuex.vuejs.org" target="_blank" rel="noopener">vuex</a></li>
28-
<li><a href="https://github.com/vuejs/vue-devtools#vue-devtools" target="_blank" rel="noopener">vue-devtools</a></li>
29-
<li><a href="https://vue-loader.vuejs.org" target="_blank" rel="noopener">vue-loader</a></li>
30-
<li><a href="https://github.com/vuejs/awesome-vue" target="_blank" rel="noopener">awesome-vue</a></li>
31-
</ul>
4+
{{ myAsyncMessage }}
325
</div>
336
</template>
347

358
<script lang="ts">
369
import { Component, Prop, Vue } from 'vue-property-decorator';
10+
import { AsyncComputed } from '../AsyncComputed';
3711
3812
@Component
3913
export default class HelloWorld extends Vue {
4014
@Prop() private msg!: string;
15+
16+
// IMPORTANT! This uses an asynchronous computation
17+
@AsyncComputed()
18+
async myAsyncMessage(): Promise<string> {
19+
return new Promise(resolve => {
20+
setTimeout(()=>{
21+
resolve("This is an asynchronous message!");
22+
}, 1000);
23+
});
24+
}
4125
}
4226
</script>
4327

Diff for: src/main.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import Vue from 'vue'
22
import App from './App.vue'
33
import './registerServiceWorker'
4+
import AsyncComputed from 'vue-async-computed'
45

56
Vue.config.productionTip = false
67

8+
Vue.use(AsyncComputed)
9+
710
new Vue({
811
render: h => h(App),
912
}).$mount('#app')

Diff for: tsconfig.json

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
"paths": {
2020
"@/*": [
2121
"src/*"
22+
],
23+
"*": [
24+
"types/*"
2225
]
2326
},
2427
"lib": [

Diff for: types/vue-async-computed.d.ts

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// (base: https://raw.githubusercontent.com/saraedum/vue-async-computed/5debb7dcd81f52183be55e05b866fc43278a9905/types/index.d.ts)
2+
3+
declare module 'vue-async-computed' {
4+
import Vue, { PluginFunction } from "vue";
5+
6+
interface IAsyncComputedOptions {
7+
errorHandler?: (error: string[]) => void;
8+
useRawError?: boolean;
9+
default?: any;
10+
}
11+
12+
export default class AsyncComputed {
13+
constructor(options?: IAsyncComputedOptions)
14+
static install: PluginFunction<never>;
15+
static version: string;
16+
}
17+
18+
type AsyncComputedGetter<T> = () => (Promise<T> | T);
19+
export interface IAsyncComputedProperty<T> {
20+
default?: T | (() => T);
21+
get?: AsyncComputedGetter<T>;
22+
watch?: () => void;
23+
shouldUpdate?: () => boolean;
24+
lazy?: boolean;
25+
}
26+
27+
interface IAsyncComputedProperties {
28+
[K: string]: AsyncComputedGetter<any> | IAsyncComputedProperty<any>;
29+
}
30+
}
31+
32+
declare module "vue/types/options" {
33+
import Vue from "vue";
34+
import { IAsyncComputedProperties } from "vue-async-computed";
35+
36+
export class InjectKey{}
37+
38+
// tslint:disable-next-line:interface-name
39+
interface ComponentOptions<V extends Vue> {
40+
asyncComputed?: IAsyncComputedProperties;
41+
}
42+
}
43+
44+
interface IASyncComputedState {
45+
state: "updating" | "success" | "error";
46+
updating: boolean;
47+
success: boolean;
48+
error: boolean;
49+
exception: Error | null;
50+
update: () => void;
51+
}
52+
53+
declare module "vue/types/vue" {
54+
// tslint:disable-next-line:interface-name
55+
interface Vue {
56+
$asyncComputed: {[K: string]: IASyncComputedState };
57+
}
58+
}

0 commit comments

Comments
 (0)