Skip to content

WIP: TypeScript typings #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "3.5.0",
"description": "Async computed properties for Vue",
"main": "dist/vue-async-computed.js",
"types": "src/index.d.ts",
"files": [
"bin/",
"dist/"
Expand Down
39 changes: 39 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Vue, { PluginFunction } from "vue";

export interface IAsyncComputedOptions {
errorHandler?: (error: string[]) => void;
useRawError?: boolean;
default?: any;
}

export default class AsyncComputed {
constructor(options?: IAsyncComputedOptions)
static install: PluginFunction<never>;
static version: string;
}

declare module "vue/types/options" {
interface ComputedOptions<T> {
asynchronous?: (() => T | Promise<T>) | {
get: (() => T | Promise<T>);
default?: T;
lazy?: boolean;
};
}
}

interface IASyncComputedState {
state: "updating" | "success" | "error";
updating: boolean;
success: boolean;
error: boolean;
exception: Error | null;
update: () => void;
}

declare module "vue/types/vue" {
// tslint:disable-next-line:interface-name
interface Vue {
$asyncComputed: {[K: string]: IASyncComputedState };
}
}
44 changes: 20 additions & 24 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,46 +20,42 @@ const AsyncComputed = {

Vue.mixin({
beforeCreate () {
const optionData = this.$options.data
const asyncComputed = this.$options.asyncComputed || {}
this.$asyncComputed = {}

for (const key in this.$options.computed) {
if (this.$options.computed[key].asynchronous) {
asyncComputed[key] = this.$options.computed[key].asynchronous
}
}

if (!Object.keys(asyncComputed).length) return

this.$options.asyncComputed = asyncComputed

if (!this.$options.computed) this.$options.computed = {}

for (const key in asyncComputed) {
const getter = getterFn(key, this.$options.asyncComputed[key])
this.$options.computed[prefix + key] = getter
const getset = makeLazyComputed(key)
this.$options.computed[key].get = getset.get
this.$options.computed[key].set = getset.set
}

this.$options.data = function vueAsyncComputedInjectedDataFn () {
const data = (
(typeof optionData === 'function')
? optionData.call(this)
: optionData
) || {}
for (const key in asyncComputed) {
const item = this.$options.asyncComputed[key]
if (isComputedLazy(item)) {
initLazy(data, key)
this.$options.computed[key] = makeLazyComputed(key)
} else {
data[key] = null
}
}
return data
},
data () {
let ret = {}
const asyncComputed = this.$options.asyncComputed || {}
for (const key in asyncComputed) {
initLazy(ret, key)
}
return ret
},
created () {
for (const key in this.$options.asyncComputed || {}) {
const item = this.$options.asyncComputed[key],
value = generateDefault.call(this, item, pluginOptions)
if (isComputedLazy(item)) {
silentSetLazy(this, key, value)
} else {
this[key] = value
}
silentSetLazy(this, key, value)
}

for (const key in this.$options.asyncComputed || {}) {
Expand All @@ -79,7 +75,7 @@ const AsyncComputed = {
newPromise.then(value => {
if (thisPromise !== promiseId) return
setAsyncState(this.$asyncComputed[key], 'success')
this[key] = value
silentSetLazy(this, key, value)
}).catch(err => {
if (thisPromise !== promiseId) return

Expand Down
Loading