Skip to content

Commit

Permalink
Merge pull request #7 from wxhccc/dev
Browse files Browse the repository at this point in the history
fix: 添加更加具体的wrapPromise函数ts重载定义
  • Loading branch information
wxhccc authored Aug 7, 2021
2 parents 047d620 + cf1e704 commit 670e3e8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
19 changes: 11 additions & 8 deletions src/promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,15 @@ const emptyPromise = () => {
}
const lockCtx: Record<string, boolean> = {}

type WpReturn<T> = PromiseWithLock<T | undefined | [null, T] | [Error, undefined]>

type WpReturn<T> = PromiseWithLock<T | undefined>
type WpArrReturn<T> = PromiseWithLock<[null, T] | [Error, undefined]>
type WpPromise<T> = Promise<T> | (() => Promise<T>)

function wrapPromise<T>(this: any, promise: WpPromise<T>, wrap?: boolean): WpReturn<T>
function wrapPromise<T>(this: any, promise: WpPromise<T>, options?: WpOptions): WpReturn<T>
function wrapPromise<T>(this: any, promise: WpPromise<T>, wrap?: false): WpReturn<T>
function wrapPromise<T>(this: any, promise: WpPromise<T>, wrap: true): WpArrReturn<T>
function wrapPromise<T>(this: any, promise: WpPromise<T>, options?: WpOptions & { wrap?: false }): WpReturn<T>
function wrapPromise<T>(this: any, promise: WpPromise<T>, options?: WpOptions & { wrap: true }): WpArrReturn<T>
function wrapPromise<T>(this: any, promise: WpPromise<T>, wrapOrOptions?: boolean | WpOptions) {
const contextType = checkContext(this)
const isReactiveIns = contextType !== 'unknown'
Expand Down Expand Up @@ -180,6 +184,7 @@ function wrapPromise<T>(this: any, promise: WpPromise<T>, wrapOrOptions?: boolea

Object.defineProperties(corePromsie, {
'__lockValue': { get: checkLock },
_checkLockKey: { value: (key: string) => lockCtx[key] },
unlock: { value: unlock }
})

Expand All @@ -191,16 +196,14 @@ function wrapPromise<T>(this: any, promise: WpPromise<T>, wrapOrOptions?: boolea
return corePromsie as PromiseWithLock<T>
}

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
}
}
}

0 comments on commit 670e3e8

Please sign in to comment.