@vue-composable/core
Advanced tools
Comparing version 1.0.0-dev.15 to 1.0.0-dev.16
@@ -173,3 +173,3 @@ 'use strict'; | ||
function usePromise(fn, throwException = false) { | ||
function usePromise(fn, lazyOptions) { | ||
if (!fn) { | ||
@@ -181,2 +181,7 @@ throw new Error(`[usePromise] argument can't be '${fn}'`); | ||
} | ||
const [lazy, throwException] = isBoolean(lazyOptions) | ||
? [lazyOptions, false] | ||
: isObject(lazyOptions) | ||
? [lazyOptions.lazy, lazyOptions.throwException] | ||
: [false, false]; | ||
const loading = compositionApi.ref(false); | ||
@@ -189,4 +194,3 @@ const error = compositionApi.ref(null); | ||
error.value = null; | ||
result.value = null; | ||
let throwExp = args && | ||
const throwExp = args && | ||
fn.length !== args.length && | ||
@@ -218,2 +222,13 @@ args.length > 0 && | ||
}; | ||
if (!lazy) { | ||
/* istanbul ignore else */ | ||
{ | ||
if (fn.length > 0 && | ||
!isBoolean(isObject(lazyOptions) ? lazyOptions.lazy : lazyOptions)) { | ||
console.warn("[usePromise] parameters detected on `fn` factory. Executing promise without arguments."); | ||
} | ||
} | ||
// @ts-ignore | ||
exec(); | ||
} | ||
return { | ||
@@ -228,6 +243,15 @@ exec, | ||
function useCancellablePromise(fn, throwException = false) { | ||
function useCancellablePromise(fn, lazyOptions) { | ||
const cancelled = compositionApi.ref(false); | ||
let _cancel = undefined; | ||
const cancel = (result) => _cancel(result); // TODO add warning if cancel is undefined | ||
const cancel = (result) => { | ||
if (!_cancel) { | ||
/* istanbul ignore else */ | ||
{ | ||
console.warn("[useCancellablePromise] There's no promise to cancel. Please make sure to call `exec`"); | ||
} | ||
return; | ||
} | ||
_cancel(result); | ||
}; | ||
const promise = (p) => new Promise((res, rej) => { | ||
@@ -240,3 +264,3 @@ _cancel = result => { | ||
}); | ||
const use = usePromise((...args) => promise(fn(...args)), throwException); | ||
const use = usePromise((...args) => promise(fn(...args)), lazyOptions); | ||
return { | ||
@@ -389,2 +413,10 @@ ...use, | ||
function usePromiseLazy(fn, throwException = false) { | ||
// @ts-ignore | ||
return usePromise(fn, { | ||
lazy: true, | ||
throwException | ||
}); | ||
} | ||
/** | ||
@@ -849,4 +881,5 @@ * Create `debounced` function, options object can be changed after creation to update behaviour | ||
exports.usePromise = usePromise; | ||
exports.usePromiseLazy = usePromiseLazy; | ||
exports.useRetry = useRetry; | ||
exports.useValidation = useValidation; | ||
exports.wrap = wrap; |
@@ -161,3 +161,3 @@ 'use strict'; | ||
function usePromise(fn, throwException = false) { | ||
function usePromise(fn, lazyOptions) { | ||
if (!fn) { | ||
@@ -169,2 +169,7 @@ throw new Error(`[usePromise] argument can't be '${fn}'`); | ||
} | ||
const [lazy, throwException] = isBoolean(lazyOptions) | ||
? [lazyOptions, false] | ||
: isObject(lazyOptions) | ||
? [lazyOptions.lazy, lazyOptions.throwException] | ||
: [false, false]; | ||
const loading = compositionApi.ref(false); | ||
@@ -177,4 +182,3 @@ const error = compositionApi.ref(null); | ||
error.value = null; | ||
result.value = null; | ||
let throwExp = args && | ||
const throwExp = args && | ||
fn.length !== args.length && | ||
@@ -206,2 +210,6 @@ args.length > 0 && | ||
}; | ||
if (!lazy) { | ||
// @ts-ignore | ||
exec(); | ||
} | ||
return { | ||
@@ -216,6 +224,11 @@ exec, | ||
function useCancellablePromise(fn, throwException = false) { | ||
function useCancellablePromise(fn, lazyOptions) { | ||
const cancelled = compositionApi.ref(false); | ||
let _cancel = undefined; | ||
const cancel = (result) => _cancel(result); // TODO add warning if cancel is undefined | ||
const cancel = (result) => { | ||
if (!_cancel) { | ||
return; | ||
} | ||
_cancel(result); | ||
}; | ||
const promise = (p) => new Promise((res, rej) => { | ||
@@ -228,3 +241,3 @@ _cancel = result => { | ||
}); | ||
const use = usePromise((...args) => promise(fn(...args)), throwException); | ||
const use = usePromise((...args) => promise(fn(...args)), lazyOptions); | ||
return { | ||
@@ -377,2 +390,10 @@ ...use, | ||
function usePromiseLazy(fn, throwException = false) { | ||
// @ts-ignore | ||
return usePromise(fn, { | ||
lazy: true, | ||
throwException | ||
}); | ||
} | ||
/** | ||
@@ -819,4 +840,5 @@ * Create `debounced` function, options object can be changed after creation to update behaviour | ||
exports.usePromise = usePromise; | ||
exports.usePromiseLazy = usePromiseLazy; | ||
exports.useRetry = useRetry; | ||
exports.useValidation = useValidation; | ||
exports.wrap = wrap; |
@@ -154,2 +154,13 @@ import { Ref } from '@vue/composition-api'; | ||
export declare interface PromiseOptions { | ||
/** | ||
* if `true` allows to catch exception when `exec()` | ||
*/ | ||
throwException?: boolean; | ||
/** | ||
* Only executes on `exec` | ||
*/ | ||
lazy?: boolean; | ||
} | ||
declare interface PromiseResult<T extends Promise<any>, TR = PromiseType<T>, TError = any> { | ||
@@ -255,16 +266,24 @@ promise: Ref<T | undefined>; | ||
export declare function useCancellablePromise<T extends any, TArgs extends Array<any>>(fn: (...args: TArgs) => Promise<T>, throwException: boolean): PromiseResultFactory<Promise<T>, TArgs> & CancellablePromiseResult; | ||
export declare function useCancellablePromise<T extends any, TArgs extends Array<any>>(fn: (...args: TArgs) => Promise<T>, lazy: boolean): PromiseResultFactory<Promise<T>, TArgs> & CancellablePromiseResult; | ||
export declare function useCancellablePromise<T extends any, TArgs extends Array<any>>(fn: (...args: TArgs) => Promise<T>, options: PromiseOptions): PromiseResultFactory<Promise<T>, TArgs> & CancellablePromiseResult; | ||
export declare function useCancellablePromise<T extends any>(fn: () => T): PromiseResultFactory<Promise<T>> & CancellablePromiseResult; | ||
export declare function useCancellablePromise<T extends any>(fn: () => T, throwException: boolean): PromiseResultFactory<Promise<T>> & CancellablePromiseResult; | ||
export declare function useCancellablePromise<T extends any>(fn: () => T, lazy: boolean): PromiseResultFactory<Promise<T>> & CancellablePromiseResult; | ||
export declare function useCancellablePromise<T extends any>(fn: () => T, options: PromiseOptions): PromiseResultFactory<Promise<T>> & CancellablePromiseResult; | ||
export declare function useCancellablePromise<T extends Promise<TR>, TR, TArgs extends Array<any>>(fn: (...args: TArgs) => T): PromiseResultFactory<T, TArgs> & CancellablePromiseResult; | ||
export declare function useCancellablePromise<T extends Promise<TR>, TR, TArgs extends Array<any>>(fn: (...args: TArgs) => T, throwException: boolean): PromiseResultFactory<T, TArgs> & CancellablePromiseResult; | ||
export declare function useCancellablePromise<T extends Promise<TR>, TR, TArgs extends Array<any>>(fn: (...args: TArgs) => T, lazy: boolean): PromiseResultFactory<T, TArgs> & CancellablePromiseResult; | ||
export declare function useCancellablePromise<T extends Promise<TR>, TR, TArgs extends Array<any>>(fn: (...args: TArgs) => T, options: PromiseOptions): PromiseResultFactory<T, TArgs> & CancellablePromiseResult; | ||
export declare function useCancellablePromise<T = any>(fn: () => T): PromiseResultFactory<Promise<T>> & CancellablePromiseResult; | ||
export declare function useCancellablePromise<T = any>(fn: () => T, throwException: boolean): PromiseResultFactory<Promise<T>> & CancellablePromiseResult; | ||
export declare function useCancellablePromise<T = any>(fn: () => T, lazy: boolean): PromiseResultFactory<Promise<T>> & CancellablePromiseResult; | ||
export declare function useCancellablePromise<T = any>(fn: () => T, options: PromiseOptions): PromiseResultFactory<Promise<T>> & CancellablePromiseResult; | ||
export declare function useCancellablePromise<T extends Promise<TR>, TR>(fn: () => T): PromiseResultFactory<T> & CancellablePromiseResult; | ||
@@ -339,20 +358,49 @@ | ||
* @param fn - factory function | ||
* @param throwException - if `true` allows to catch exception when `exec()` | ||
* @param lazy - Delays execution until `exec` is called | ||
*/ | ||
export declare function usePromise<T = any, TArgs extends Array<any> = Array<any>>(fn: (...args: TArgs) => Promise<T>, throwException?: boolean): PromiseResultFactory<Promise<T>, TArgs>; | ||
export declare function usePromise<T = any, TArgs extends Array<any> = Array<any>>(fn: (...args: TArgs) => Promise<T>, lazy?: boolean): PromiseResultFactory<Promise<T>, TArgs>; | ||
export declare function usePromise<T = any, TArgs extends Array<any> = Array<any>>(fn: (...args: TArgs) => Promise<T>, options: PromiseOptions): PromiseResultFactory<Promise<T>, TArgs>; | ||
export declare function usePromise<T = any, TArgs extends Array<any> = Array<any>>(fn: (...args: TArgs) => Promise<T>): PromiseResultFactory<Promise<T>, TArgs>; | ||
export declare function usePromise<T = any, TArgs extends Array<any> = Array<any>>(fn: (...args: TArgs) => T, throwException: boolean): PromiseResultFactory<Promise<T>, TArgs>; | ||
export declare function usePromise<T = any, TArgs extends Array<any> = Array<any>>(fn: (...args: TArgs) => T, lazy: boolean): PromiseResultFactory<Promise<T>, TArgs>; | ||
export declare function usePromise<T = any, TArgs extends Array<any> = Array<any>>(fn: (...args: TArgs) => T, options: PromiseOptions): PromiseResultFactory<Promise<T>, TArgs>; | ||
export declare function usePromise<T = any, TArgs extends Array<any> = Array<any>>(fn: (...args: TArgs) => T): PromiseResultFactory<Promise<T>, TArgs>; | ||
export declare function usePromise<T = any>(fn: () => Promise<T>, throwException: boolean): PromiseResultFactory<Promise<T>>; | ||
export declare function usePromise<T = any>(fn: () => Promise<T>, lazy: boolean): PromiseResultFactory<Promise<T>>; | ||
export declare function usePromise<T = any>(fn: () => Promise<T>, options: PromiseOptions): PromiseResultFactory<Promise<T>>; | ||
export declare function usePromise<T = any>(fn: () => Promise<T>): PromiseResultFactory<Promise<T>>; | ||
export declare function usePromise<T = any>(fn: () => T, throwException: boolean): PromiseResultFactory<Promise<T>>; | ||
export declare function usePromise<T = any>(fn: () => T, lazy: boolean): PromiseResultFactory<Promise<T>>; | ||
export declare function usePromise<T = any>(fn: () => T, options: PromiseOptions): PromiseResultFactory<Promise<T>>; | ||
export declare function usePromise<T = any>(fn: () => T): PromiseResultFactory<Promise<T>>; | ||
/** | ||
* Promise only created on `exec()` | ||
* @param fn - factory function | ||
* @param throwException - if `true` allows to catch exception when `exec()` | ||
*/ | ||
export declare function usePromiseLazy<T = any, TArgs extends Array<any> = Array<any>>(fn: (...args: TArgs) => Promise<T>, throwException?: boolean): PromiseResultFactory<Promise<T>, TArgs>; | ||
export declare function usePromiseLazy<T = any, TArgs extends Array<any> = Array<any>>(fn: (...args: TArgs) => Promise<T>): PromiseResultFactory<Promise<T>, TArgs>; | ||
export declare function usePromiseLazy<T = any, TArgs extends Array<any> = Array<any>>(fn: (...args: TArgs) => T, throwException: boolean): PromiseResultFactory<Promise<T>, TArgs>; | ||
export declare function usePromiseLazy<T = any, TArgs extends Array<any> = Array<any>>(fn: (...args: TArgs) => T): PromiseResultFactory<Promise<T>, TArgs>; | ||
export declare function usePromiseLazy<T = any>(fn: () => Promise<T>, throwException: boolean): PromiseResultFactory<Promise<T>>; | ||
export declare function usePromiseLazy<T = any>(fn: () => Promise<T>): PromiseResultFactory<Promise<T>>; | ||
export declare function usePromiseLazy<T = any>(fn: () => T, throwException: boolean): PromiseResultFactory<Promise<T>>; | ||
export declare function usePromiseLazy<T = any>(fn: () => T): PromiseResultFactory<Promise<T>>; | ||
export declare function useRetry(options?: RetryOptions): RetryReturnNoFactory; | ||
@@ -359,0 +407,0 @@ |
@@ -169,3 +169,3 @@ import { isRef, ref, computed, watch, onUnmounted, reactive, inject, provide } from '@vue/composition-api'; | ||
function usePromise(fn, throwException = false) { | ||
function usePromise(fn, lazyOptions) { | ||
if (!fn) { | ||
@@ -177,2 +177,7 @@ throw new Error(`[usePromise] argument can't be '${fn}'`); | ||
} | ||
const [lazy, throwException] = isBoolean(lazyOptions) | ||
? [lazyOptions, false] | ||
: isObject(lazyOptions) | ||
? [lazyOptions.lazy, lazyOptions.throwException] | ||
: [false, false]; | ||
const loading = ref(false); | ||
@@ -185,4 +190,3 @@ const error = ref(null); | ||
error.value = null; | ||
result.value = null; | ||
let throwExp = args && | ||
const throwExp = args && | ||
fn.length !== args.length && | ||
@@ -214,2 +218,13 @@ args.length > 0 && | ||
}; | ||
if (!lazy) { | ||
/* istanbul ignore else */ | ||
if ((process.env.NODE_ENV !== 'production')) { | ||
if (fn.length > 0 && | ||
!isBoolean(isObject(lazyOptions) ? lazyOptions.lazy : lazyOptions)) { | ||
console.warn("[usePromise] parameters detected on `fn` factory. Executing promise without arguments."); | ||
} | ||
} | ||
// @ts-ignore | ||
exec(); | ||
} | ||
return { | ||
@@ -224,6 +239,15 @@ exec, | ||
function useCancellablePromise(fn, throwException = false) { | ||
function useCancellablePromise(fn, lazyOptions) { | ||
const cancelled = ref(false); | ||
let _cancel = undefined; | ||
const cancel = (result) => _cancel(result); // TODO add warning if cancel is undefined | ||
const cancel = (result) => { | ||
if (!_cancel) { | ||
/* istanbul ignore else */ | ||
if ((process.env.NODE_ENV !== 'production')) { | ||
console.warn("[useCancellablePromise] There's no promise to cancel. Please make sure to call `exec`"); | ||
} | ||
return; | ||
} | ||
_cancel(result); | ||
}; | ||
const promise = (p) => new Promise((res, rej) => { | ||
@@ -236,3 +260,3 @@ _cancel = result => { | ||
}); | ||
const use = usePromise((...args) => promise(fn(...args)), throwException); | ||
const use = usePromise((...args) => promise(fn(...args)), lazyOptions); | ||
return { | ||
@@ -385,2 +409,10 @@ ...use, | ||
function usePromiseLazy(fn, throwException = false) { | ||
// @ts-ignore | ||
return usePromise(fn, { | ||
lazy: true, | ||
throwException | ||
}); | ||
} | ||
/** | ||
@@ -811,2 +843,2 @@ * Create `debounced` function, options object can be changed after creation to update behaviour | ||
export { FALSE_OP, NO_OP, PASSIVE_EV, buildI18n, debounce, deepClone, exponentialDelay, isArray, isBoolean, isClient, isDate, isElement, isFunction, isNumber, isObject, isPromise, isString, isSymbol, minMax, noDelay, promisedTimeout, setI18n, unwrap, useArrayPagination, useCancellablePromise, useDateNow, useDebounce, useFormat, useI18n, useNow, usePagination, usePath, usePerformanceNow, usePromise, useRetry, useValidation, wrap }; | ||
export { FALSE_OP, NO_OP, PASSIVE_EV, buildI18n, debounce, deepClone, exponentialDelay, isArray, isBoolean, isClient, isDate, isElement, isFunction, isNumber, isObject, isPromise, isString, isSymbol, minMax, noDelay, promisedTimeout, setI18n, unwrap, useArrayPagination, useCancellablePromise, useDateNow, useDebounce, useFormat, useI18n, useNow, usePagination, usePath, usePerformanceNow, usePromise, usePromiseLazy, useRetry, useValidation, wrap }; |
{ | ||
"name": "@vue-composable/core", | ||
"version": "1.0.0-dev.15", | ||
"version": "1.0.0-dev.16", | ||
"description": "@vue-composable/core", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -57,2 +57,3 @@ # @vue-composable/core | ||
- [usePromise][promise] : Handles promise states | ||
- [promiseLazy](https://pikax.me/vue-composable/composable/promise/promiseLazy) - Sugar for [usePromise](https://pikax.me/vue-composable/composable/promise/promise) `lazy:true` | ||
- [useCancellablePromise][cancellable-promise] : Uses [usePromise][promise] and prevent setting `result` if canceled | ||
@@ -59,0 +60,0 @@ - [useRetry][retry] : Allows to retry if a promise throws an exception |
106737
2856
91
13