diff --git a/package.json b/package.json index c5a4fdc..16a6d52 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@wxhccc/es-util", - "version": "1.4.0", + "version": "1.4.1", "description": "A library that contains some useful methods", "main": "dist/index.js", "module": "dist/index.esm.js", diff --git a/src/promise.ts b/src/promise.ts index 065fb73..39c8e43 100644 --- a/src/promise.ts +++ b/src/promise.ts @@ -68,11 +68,15 @@ const emptyPromise = () => { } const lockCtx: Record = {} -type WpReturn = PromiseWithLock + +type WpReturn = PromiseWithLock +type WpArrReturn = PromiseWithLock<[null, T] | [Error, undefined]> type WpPromise = Promise | (() => Promise) -function wrapPromise(this: any, promise: WpPromise, wrap?: boolean): WpReturn -function wrapPromise(this: any, promise: WpPromise, options?: WpOptions): WpReturn +function wrapPromise(this: any, promise: WpPromise, wrap?: false): WpReturn +function wrapPromise(this: any, promise: WpPromise, wrap: true): WpArrReturn +function wrapPromise(this: any, promise: WpPromise, options?: WpOptions & { wrap?: false }): WpReturn +function wrapPromise(this: any, promise: WpPromise, options?: WpOptions & { wrap: true }): WpArrReturn function wrapPromise(this: any, promise: WpPromise, wrapOrOptions?: boolean | WpOptions) { const contextType = checkContext(this) const isReactiveIns = contextType !== 'unknown' @@ -180,6 +184,7 @@ function wrapPromise(this: any, promise: WpPromise, wrapOrOptions?: boolea Object.defineProperties(corePromsie, { '__lockValue': { get: checkLock }, + _checkLockKey: { value: (key: string) => lockCtx[key] }, unlock: { value: unlock } }) @@ -191,16 +196,14 @@ function wrapPromise(this: any, promise: WpPromise, wrapOrOptions?: boolea return corePromsie as PromiseWithLock } -type WP = typeof wrapPromise & { _checkLockKey: (key: string) => boolean } - -export const wp: WP = Object.defineProperty(wrapPromise, '_checkLockKey', { value: (key: string) => lockCtx[key] }) +export const wp = wrapPromise export const wpVuePlugin = { install(appOrVue: any, key = '$wp') { if (appOrVue.config && 'globalProperties' in appOrVue.config) { - appOrVue.config.globalProperties[key] = wp + appOrVue.config.globalProperties[key] = wrapPromise } else { - appOrVue.prototype[key] = wp + appOrVue.prototype[key] = wrapPromise } } } \ No newline at end of file