diff --git a/packages/playground/uts/unpackage/nativeplugins/test-uniplugin/android/src/io/dcloud/uniplugin/TestModule.kt.map b/packages/playground/uts/unpackage/nativeplugins/test-uniplugin/android/src/io/dcloud/uniplugin/TestModule.kt.map index 6520990d61cd884dfa420b26086de264cef52838..47c9a85e00e63cb3a742dd30c8f92bdf6a8b4e1f 100644 --- a/packages/playground/uts/unpackage/nativeplugins/test-uniplugin/android/src/io/dcloud/uniplugin/TestModule.kt.map +++ b/packages/playground/uts/unpackage/nativeplugins/test-uniplugin/android/src/io/dcloud/uniplugin/TestModule.kt.map @@ -1 +1 @@ -{"version":3,"sources":["/Users/fxy/Projects/GitHub/uni-app/uni-app-next/packages/playground/uts/nativeplugins/test-uniplugin/android/src/io/dcloud/uniplugin/TestModule.uts"],"names":["TestModule","UniModule","TAG","UniJSMethod","uiThread","testAsyncFunc","options","callback","log","data","JSONObject","invoke","testSyncFunc","onActivityResult","requestCode","resultCode","REQUEST_CODE","hasExtra","getStringExtra"],"mappings":"AAAA;AAAA,OAAmB,sBAAwB,CAAC;AAC5C,OAAuB,+BAAiC,CAAC;AACzD,OAAsB,yCAA2C,CAAC;AAClE,OAAwB,+CAAiD,CAAC;AAC1E,OAA0B,6CAA+C,CAAC;AAC1E,OAAgB,2BAAO,CAAC;AACT,WAAMA,UAAU,GAASC,SAAS,EAAE;IACjDC,SAAAA,GAAG,GAAG,YAAY,CAAC;IAGnB,CAACC,WAAW,CAAC,CAAEC,QAAQ,GAAE,IAAI,CAAE,CAAC;IAAA,SAChCC,aAAa,CAACC,OAAmB,EAAV,UAAU,EAAEC,QAAwB,EAAb,aAAa,CAAA,EAAE;QAC3DC,GAAG,CAAC,IAAI,CAACN,GAAG,EAAE,yBAAyB,CAAC,CAAC;QACzC,IAAIK,QAAQ,IAAI,IAAI,EAAE;YACpB,IAAME,IAAI,GAAG,AAAIC,UAAU,EAAE,AAAC;YAC9BD,IAAI,CAAC,MAAM,CAAC,GAAG,cAAc,CAAC;YAC9BF,QAAQ,CAACI,MAAM,CAACF,IAAI,CAAC,CAAC;;;IAI1B,CAACN,WAAW,CAAC,CAAEC,QAAQ,GAAE,KAAK,CAAE,CAAC;IAAA,SACjCQ,YAAY,CAACN,OAAmB,EAAV,UAAU,GAAG,UAAU,CAAC;QAC5C,IAAMG,IAAI,GAAG,AAAIC,UAAU,EAAE,AAAC;QAC9BD,IAAI,CAAC,MAAM,CAAC,GAAG,YAAY,CAAC;QAC5B,OAAOA,IAAI,CAAC;;IAEd,aAASI,gBAAgB,CAACC,WAAgB,EAAH,GAAG,EAAEC,UAAe,EAAH,GAAG,EAAEN,IAAY,EAAN,MAAM,EAAE;QACzE,IAAIK,WAAW,IAAId,UAAU,CAACgB,YAAY,IAAIP,IAAI,CAACQ,QAAQ,CAAC,SAAS,CAAC,EAAE;YACtET,GAAG,CAAC,IAAI,CAACN,GAAG,EAAE,wBAAY,GAAGO,IAAI,CAACS,cAAc,CAAC,SAAS,CAAC,CAAC,CAAC;;QAC9D,MAAM;YACL,KAAK,CAACL,gBAAgB,CAACC,WAAW,EAAEC,UAAU,EAAEN,IAAI,CAAC,CAAC;;;;QAtB1D,IAAOO,YAAY,GAAG,IAAI,CAAC;;CAyB5B"} \ No newline at end of file +{"version":3,"sources":["/Users/fxy/Documents/projects/GitHub/uni-app/uni-app-next/packages/playground/uts/nativeplugins/test-uniplugin/android/src/io/dcloud/uniplugin/TestModule.uts"],"names":["TestModule","UniModule","TAG","UniJSMethod","uiThread","testAsyncFunc","options","callback","log","data","JSONObject","invoke","testSyncFunc","onActivityResult","requestCode","resultCode","REQUEST_CODE","hasExtra","getStringExtra"],"mappings":"AAAA;AAAA,OAAmB,sBAAwB,CAAC;AAC5C,OAAuB,+BAAiC,CAAC;AACzD,OAAsB,yCAA2C,CAAC;AAClE,OAAwB,+CAAiD,CAAC;AAC1E,OAA0B,6CAA+C,CAAC;AAC1E,OAAgB,2BAAO,CAAC;AACT,WAAMA,UAAU,GAASC,SAAS,EAAE;IACjDC,SAAAA,GAAG,GAAG,YAAY,CAAC;IAGnB,CAACC,WAAW,CAAC,CAAEC,QAAQ,GAAE,IAAI,CAAE,CAAC;IAAA,SAChCC,aAAa,CAACC,OAAmB,EAAV,UAAU,EAAEC,QAAwB,EAAb,aAAa,CAAA,EAAE;QAC3DC,GAAG,CAAC,IAAI,CAACN,GAAG,EAAE,yBAAyB,CAAC,CAAC;QACzC,IAAIK,QAAQ,IAAI,IAAI,EAAE;YACpB,IAAME,IAAI,GAAG,AAAIC,UAAU,EAAE,AAAC;YAC9BD,IAAI,CAAC,MAAM,CAAC,GAAG,cAAc,CAAC;YAC9BF,QAAQ,CAACI,MAAM,CAACF,IAAI,CAAC,CAAC;;;IAI1B,CAACN,WAAW,CAAC,CAAEC,QAAQ,GAAE,KAAK,CAAE,CAAC;IAAA,SACjCQ,YAAY,CAACN,OAAmB,EAAV,UAAU,GAAG,UAAU,CAAC;QAC5C,IAAMG,IAAI,GAAG,AAAIC,UAAU,EAAE,AAAC;QAC9BD,IAAI,CAAC,MAAM,CAAC,GAAG,YAAY,CAAC;QAC5B,OAAOA,IAAI,CAAC;;IAEd,aAASI,gBAAgB,CAACC,WAAgB,EAAH,GAAG,EAAEC,UAAe,EAAH,GAAG,EAAEN,IAAY,EAAN,MAAM,EAAE;QACzE,IAAIK,WAAW,IAAId,UAAU,CAACgB,YAAY,IAAIP,IAAI,CAACQ,QAAQ,CAAC,SAAS,CAAC,EAAE;YACtET,GAAG,CAAC,IAAI,CAACN,GAAG,EAAE,wBAAY,GAAGO,IAAI,CAACS,cAAc,CAAC,SAAS,CAAC,CAAC,CAAC;;QAC9D,MAAM;YACL,KAAK,CAACL,gBAAgB,CAACC,WAAW,EAAEC,UAAU,EAAEN,IAAI,CAAC,CAAC;;;;QAtB1D,IAAOO,YAAY,GAAG,IAAI,CAAC;;CAyB5B"} \ No newline at end of file diff --git a/packages/playground/uts/unpackage/nativeplugins/test-uniplugin/android/src/io/dcloud/uniplugin/log.kt.map b/packages/playground/uts/unpackage/nativeplugins/test-uniplugin/android/src/io/dcloud/uniplugin/log.kt.map index 55faeca61e1f300fb61af3cf87407c66ddb23242..60019af0eedd612823df47b7a7b5cc256970b8df 100644 --- a/packages/playground/uts/unpackage/nativeplugins/test-uniplugin/android/src/io/dcloud/uniplugin/log.kt.map +++ b/packages/playground/uts/unpackage/nativeplugins/test-uniplugin/android/src/io/dcloud/uniplugin/log.kt.map @@ -1 +1 @@ -{"version":3,"sources":["/Users/fxy/Projects/GitHub/uni-app/uni-app-next/packages/playground/uts/nativeplugins/test-uniplugin/android/src/io/dcloud/uniplugin/log.uts"],"names":["log","tag","msg","Log","e"],"mappings":"AAAA;AAAA,OAAgB,gBAAkB,CAAC;AACpB,IAASA,GAAG,CAACC,GAAW,EAAN,MAAM,EAAEC,GAAW,EAAN,MAAM,EAAE;IACpDC,GAAG,GAAEC,CAAC,CAACH,GAAG,EAAEC,GAAG,CAAC,CAAC"} \ No newline at end of file +{"version":3,"sources":["/Users/fxy/Documents/projects/GitHub/uni-app/uni-app-next/packages/playground/uts/nativeplugins/test-uniplugin/android/src/io/dcloud/uniplugin/log.uts"],"names":["log","tag","msg","Log","e"],"mappings":"AAAA;AAAA,OAAgB,gBAAkB,CAAC;AACpB,IAASA,GAAG,CAACC,GAAW,EAAN,MAAM,EAAEC,GAAW,EAAN,MAAM,EAAE;IACpDC,GAAG,GAAEC,CAAC,CAACH,GAAG,EAAEC,GAAG,CAAC,CAAC"} \ No newline at end of file diff --git a/packages/playground/uts/unpackage/nativeplugins/test-uniplugin/android/src/test.kt.map b/packages/playground/uts/unpackage/nativeplugins/test-uniplugin/android/src/test.kt.map index b38280d8b19940be49ae1e18569535346fb6cd08..15274d8a76d38f027171f54151d2bcc3688e34a0 100644 --- a/packages/playground/uts/unpackage/nativeplugins/test-uniplugin/android/src/test.kt.map +++ b/packages/playground/uts/unpackage/nativeplugins/test-uniplugin/android/src/test.kt.map @@ -1 +1 @@ -{"version":3,"sources":["/Users/fxy/Projects/GitHub/uni-app/uni-app-next/packages/playground/uts/nativeplugins/test-uniplugin/android/src/test.uts"],"names":["log","tag","msg","Log","e"],"mappings":"AAAA;AAAA,OAAgB,gBAAkB,CAAC;AACpB,IAASA,GAAG,CAACC,GAAW,EAAN,MAAM,EAAEC,GAAW,EAAN,MAAM,EAAE;IACpDC,GAAG,CAACC,CAAC,CAACH,GAAG,EAAEC,GAAG,CAAC,CAAC"} \ No newline at end of file +{"version":3,"sources":["/Users/fxy/Documents/projects/GitHub/uni-app/uni-app-next/packages/playground/uts/nativeplugins/test-uniplugin/android/src/test.uts"],"names":["log","tag","msg","Log","e"],"mappings":"AAAA;AAAA,OAAgB,gBAAkB,CAAC;AACpB,IAASA,GAAG,CAACC,GAAW,EAAN,MAAM,EAAEC,GAAW,EAAN,MAAM,EAAE;IACpDC,GAAG,CAACC,CAAC,CAACH,GAAG,EAAEC,GAAG,CAAC,CAAC"} \ No newline at end of file diff --git a/packages/uni-api/src/service/keyboard/getSelectedTextRange.ts b/packages/uni-api/src/service/keyboard/getSelectedTextRange.ts index 61d9569b3e8c598df265532d099717e589d2c17b..8a3ef8cbf134a622a0c5d09bb6173d586c9262bd 100644 --- a/packages/uni-api/src/service/keyboard/getSelectedTextRange.ts +++ b/packages/uni-api/src/service/keyboard/getSelectedTextRange.ts @@ -12,7 +12,7 @@ export const getSelectedTextRange = UniServiceJSBridge.invokeViewMethod< {}, UniApp.GetSelectedTextRangeSuccessCallbackResult - >('getSelectedTextRange', {}, getCurrentPageId(), (res) => { + >(API_GET_SELECTED_TEXT_RANGE, {}, getCurrentPageId(), (res) => { if ( typeof res.end === 'undefined' && typeof res.start === 'undefined' diff --git a/packages/uni-api/src/service/plugin/push.ts b/packages/uni-api/src/service/plugin/push.ts index ae198c20014262d189e08b1895ff5c2d46e40f96..fee6b4e5bba0c436dc76bb8332acd3a61354b632 100644 --- a/packages/uni-api/src/service/plugin/push.ts +++ b/packages/uni-api/src/service/plugin/push.ts @@ -1,5 +1,4 @@ -import { isFunction, isPlainObject } from '@vue/shared' -import { getApiCallbacks } from '../../helpers/api/callback' +import { defineAsyncApi } from '../../helpers/api' interface OnPushCidCallback { type: 'clientId' @@ -62,10 +61,6 @@ export function invokePushCallback( } } -interface GetPushCidOptions { - success?: OnPushMessageSuccess -} - const getPushCidCallbacks: ((cid?: string, errMsg?: string) => void)[] = [] function invokeGetPushCidCallbacks(cid?: string, errMsg?: string) { @@ -75,29 +70,22 @@ function invokeGetPushCidCallbacks(cid?: string, errMsg?: string) { getPushCidCallbacks.length = 0 } -export function getPushClientId(args: GetPushCidOptions) { - if (!isPlainObject(args)) { - args = {} - } - const { success, fail, complete } = getApiCallbacks(args) - const hasSuccess = isFunction(success) - const hasFail = isFunction(fail) - const hasComplete = isFunction(complete) - getPushCidCallbacks.push((cid?: string, errMsg?: string) => { - let res: Record - if (cid) { - res = { errMsg: 'getPushClientId:ok', cid } - hasSuccess && success(res) - } else { - res = { errMsg: 'getPushClientId:fail' + (errMsg ? ' ' + errMsg : '') } - hasFail && fail(res) +const API_GET_PUSH_CLIENT_ID = 'getPushClientId' +export const getPushClientId = defineAsyncApi( + API_GET_PUSH_CLIENT_ID, + (_, { resolve, reject }) => { + getPushCidCallbacks.push((cid?: string, errMsg?: string) => { + if (cid) { + resolve({ cid }) + } else { + reject(errMsg) + } + }) + if (typeof cid !== 'undefined') { + Promise.resolve().then(() => invokeGetPushCidCallbacks(cid, cidErrMsg)) } - hasComplete && complete(res) - }) - if (typeof cid !== 'undefined') { - Promise.resolve().then(() => invokeGetPushCidCallbacks(cid, cidErrMsg)) } -} +) interface OnPushMessageSuccess { type: 'click' | 'receive' diff --git a/packages/uni-app-plus/dist/uni.runtime.esm.js b/packages/uni-app-plus/dist/uni.runtime.esm.js index f25990c605cdf5926ca5dd437f5baf3c38cd7a77..0dcc63c47cecd1fb7e3dc2793203de083c7e6547 100644 --- a/packages/uni-app-plus/dist/uni.runtime.esm.js +++ b/packages/uni-app-plus/dist/uni.runtime.esm.js @@ -10994,7 +10994,7 @@ const setPageMeta = defineAsyncApi(API_SET_PAGE_META, (options, { resolve }) => const API_GET_SELECTED_TEXT_RANGE = 'getSelectedTextRange'; const getSelectedTextRange = defineAsyncApi(API_GET_SELECTED_TEXT_RANGE, (_, { resolve, reject }) => { - UniServiceJSBridge.invokeViewMethod('getSelectedTextRange', {}, getCurrentPageId(), (res) => { + UniServiceJSBridge.invokeViewMethod(API_GET_SELECTED_TEXT_RANGE, {}, getCurrentPageId(), (res) => { if (typeof res.end === 'undefined' && typeof res.start === 'undefined') { reject('no focused'); @@ -11115,30 +11115,20 @@ function invokeGetPushCidCallbacks(cid, errMsg) { }); getPushCidCallbacks.length = 0; } -function getPushClientId(args) { - if (!isPlainObject(args)) { - args = {}; - } - const { success, fail, complete } = getApiCallbacks(args); - const hasSuccess = isFunction(success); - const hasFail = isFunction(fail); - const hasComplete = isFunction(complete); +const API_GET_PUSH_CLIENT_ID = 'getPushClientId'; +const getPushClientId = defineAsyncApi(API_GET_PUSH_CLIENT_ID, (_, { resolve, reject }) => { getPushCidCallbacks.push((cid, errMsg) => { - let res; if (cid) { - res = { errMsg: 'getPushClientId:ok', cid }; - hasSuccess && success(res); + resolve({ cid }); } else { - res = { errMsg: 'getPushClientId:fail' + (errMsg ? ' ' + errMsg : '') }; - hasFail && fail(res); + reject(errMsg); } - hasComplete && complete(res); }); if (typeof cid !== 'undefined') { Promise.resolve().then(() => invokeGetPushCidCallbacks(cid, cidErrMsg)); } -} +}); const onPushMessageCallbacks = []; // 不使用 defineOnApi 实现,是因为 defineOnApi 依赖 UniServiceJSBridge ,该对象目前在小程序上未提供,故简单实现 const onPushMessage = (fn) => { diff --git a/packages/uni-h5/dist/uni-h5.es.js b/packages/uni-h5/dist/uni-h5.es.js index 6c8370f2970918f9a086050ab44d97fc21be54a6..9b80769f402b9adfcdc679c6b19e71c140e8d601 100644 --- a/packages/uni-h5/dist/uni-h5.es.js +++ b/packages/uni-h5/dist/uni-h5.es.js @@ -4508,7 +4508,7 @@ const setPageMeta = /* @__PURE__ */ defineAsyncApi(API_SET_PAGE_META, (options, }); const API_GET_SELECTED_TEXT_RANGE = "getSelectedTextRange"; const getSelectedTextRange$1 = /* @__PURE__ */ defineAsyncApi(API_GET_SELECTED_TEXT_RANGE, (_, { resolve, reject }) => { - UniServiceJSBridge.invokeViewMethod("getSelectedTextRange", {}, getCurrentPageId(), (res) => { + UniServiceJSBridge.invokeViewMethod(API_GET_SELECTED_TEXT_RANGE, {}, getCurrentPageId(), (res) => { if (typeof res.end === "undefined" && typeof res.start === "undefined") { reject("no focused"); } else { @@ -4619,29 +4619,19 @@ function invokeGetPushCidCallbacks(cid2, errMsg) { }); getPushCidCallbacks.length = 0; } -function getPushClientId(args) { - if (!isPlainObject(args)) { - args = {}; - } - const { success, fail, complete } = getApiCallbacks(args); - const hasSuccess = isFunction(success); - const hasFail = isFunction(fail); - const hasComplete = isFunction(complete); +const API_GET_PUSH_CLIENT_ID = "getPushClientId"; +const getPushClientId = /* @__PURE__ */ defineAsyncApi(API_GET_PUSH_CLIENT_ID, (_, { resolve, reject }) => { getPushCidCallbacks.push((cid2, errMsg) => { - let res; if (cid2) { - res = { errMsg: "getPushClientId:ok", cid: cid2 }; - hasSuccess && success(res); + resolve({ cid: cid2 }); } else { - res = { errMsg: "getPushClientId:fail" + (errMsg ? " " + errMsg : "") }; - hasFail && fail(res); + reject(errMsg); } - hasComplete && complete(res); }); if (typeof cid !== "undefined") { Promise.resolve().then(() => invokeGetPushCidCallbacks(cid, cidErrMsg)); } -} +}); const onPushMessageCallbacks = []; const onPushMessage = (fn) => { if (onPushMessageCallbacks.indexOf(fn) === -1) { diff --git a/packages/uni-mp-alipay/dist/uni.api.esm.js b/packages/uni-mp-alipay/dist/uni.api.esm.js index 8b197df1e097b89550cd496bd7489d2ebff71698..3434da038d7b8163de81035730dc3e66658024d4 100644 --- a/packages/uni-mp-alipay/dist/uni.api.esm.js +++ b/packages/uni-mp-alipay/dist/uni.api.esm.js @@ -1,6 +1,6 @@ -import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawType, makeMap, isFunction, isPromise, remove, extend } from '@vue/shared'; +import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawType, makeMap, isFunction, isPromise, extend, remove } from '@vue/shared'; import { normalizeLocale, LOCALE_EN } from '@dcloudio/uni-i18n'; -import { Emitter, onCreateVueApp, invokeCreateVueAppHook } from '@dcloudio/uni-shared'; +import { LINEFEED, Emitter, onCreateVueApp, invokeCreateVueAppHook } from '@dcloudio/uni-shared'; const eventChannels = {}; const eventChannelStack = []; @@ -190,6 +190,32 @@ function tryCatch(fn) { }; } +let invokeCallbackId = 1; +const invokeCallbacks = {}; +function addInvokeCallback(id, name, callback, keepAlive = false) { + invokeCallbacks[id] = { + name, + keepAlive, + callback, + }; + return id; +} +// onNativeEventReceive((event,data)=>{}) 需要两个参数,目前写死最多两个参数 +function invokeCallback(id, res, extras) { + if (typeof id === 'number') { + const opts = invokeCallbacks[id]; + if (opts) { + if (!opts.keepAlive) { + delete invokeCallbacks[id]; + } + return opts.callback(res, extras); + } + } + return res; +} +const API_SUCCESS = 'success'; +const API_FAIL = 'fail'; +const API_COMPLETE = 'complete'; function getApiCallbacks(args) { const apiCallbacks = {}; for (const name in args) { @@ -200,6 +226,36 @@ function getApiCallbacks(args) { } } return apiCallbacks; +} +function normalizeErrMsg$1(errMsg, name) { + if (!errMsg || errMsg.indexOf(':fail') === -1) { + return name + ':ok'; + } + return name + errMsg.substring(errMsg.indexOf(':fail')); +} +function createAsyncApiCallback(name, args = {}, { beforeAll, beforeSuccess } = {}) { + if (!isPlainObject(args)) { + args = {}; + } + const { success, fail, complete } = getApiCallbacks(args); + const hasSuccess = isFunction(success); + const hasFail = isFunction(fail); + const hasComplete = isFunction(complete); + const callbackId = invokeCallbackId++; + addInvokeCallback(callbackId, name, (res) => { + res = res || {}; + res.errMsg = normalizeErrMsg$1(res.errMsg, name); + isFunction(beforeAll) && beforeAll(res); + if (res.errMsg === name + ':ok') { + isFunction(beforeSuccess) && beforeSuccess(res, args); + hasSuccess && success(res); + } + else { + hasFail && fail(res); + } + hasComplete && complete(res); + }); + return callbackId; } const HOOK_SUCCESS = 'success'; @@ -301,6 +357,13 @@ function invokeApi(method, api, options, params) { return api(options, ...params); } +function hasCallback(args) { + if (isPlainObject(args) && + [API_SUCCESS, API_FAIL, API_COMPLETE].find((cb) => isFunction(args[cb]))) { + return true; + } + return false; +} function handlePromise(promise) { // if (__UNI_FEATURE_PROMISE__) { // return promise @@ -310,6 +373,16 @@ function handlePromise(promise) { // .catch((err) => [err]) // } return promise; +} +function promisify$1(name, fn) { + return (args = {}, ...rest) => { + if (hasCallback(args)) { + return wrapperReturnValue(name, invokeApi(name, fn, args, rest)); + } + return wrapperReturnValue(name, handlePromise(new Promise((resolve, reject) => { + invokeApi(name, fn, extend(args, { success: resolve, fail: reject }), rest); + }))); + }; } function formatApiArgs(args, options) { @@ -337,6 +410,12 @@ function formatApiArgs(args, options) { } } } +function invokeSuccess(id, name, res) { + return invokeCallback(id, extend(res || {}, { errMsg: name + ':ok' })); +} +function invokeFail(id, name, errMsg, errRes) { + return invokeCallback(id, extend({ errMsg: name + ':fail' + (errMsg ? ' ' + errMsg : '') }, errRes)); +} function beforeInvokeApi(name, args, protocol, options) { if ((process.env.NODE_ENV !== 'production')) { validateProtocols(name, args, protocol); @@ -352,6 +431,29 @@ function beforeInvokeApi(name, args, protocol, options) { return errMsg; } } +function normalizeErrMsg(errMsg) { + if (!errMsg || isString(errMsg)) { + return errMsg; + } + if (errMsg.stack) { + console.error(errMsg.message + LINEFEED + errMsg.stack); + return errMsg.message; + } + return errMsg; +} +function wrapperTaskApi(name, fn, protocol, options) { + return (args) => { + const id = createAsyncApiCallback(name, args, options); + const errMsg = beforeInvokeApi(name, [args], protocol, options); + if (errMsg) { + return invokeFail(id, name, errMsg); + } + return fn(args, { + resolve: (res) => invokeSuccess(id, name, res), + reject: (errMsg, errRes) => invokeFail(id, name, normalizeErrMsg(errMsg), errRes), + }); + }; +} function wrapperSyncApi(name, fn, protocol, options) { return (...args) => { const errMsg = beforeInvokeApi(name, args, protocol, options); @@ -361,8 +463,14 @@ function wrapperSyncApi(name, fn, protocol, options) { return fn.apply(null, args); }; } +function wrapperAsyncApi(name, fn, protocol, options) { + return wrapperTaskApi(name, fn, protocol, options); +} function defineSyncApi(name, fn, protocol, options) { return wrapperSyncApi(name, fn, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options); +} +function defineAsyncApi(name, fn, protocol, options) { + return promisify$1(name, wrapperAsyncApi(name, fn, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options)); } const API_UPX2PX = 'upx2px'; @@ -582,30 +690,20 @@ function invokeGetPushCidCallbacks(cid, errMsg) { }); getPushCidCallbacks.length = 0; } -function getPushClientId(args) { - if (!isPlainObject(args)) { - args = {}; - } - const { success, fail, complete } = getApiCallbacks(args); - const hasSuccess = isFunction(success); - const hasFail = isFunction(fail); - const hasComplete = isFunction(complete); +const API_GET_PUSH_CLIENT_ID = 'getPushClientId'; +const getPushClientId = defineAsyncApi(API_GET_PUSH_CLIENT_ID, (_, { resolve, reject }) => { getPushCidCallbacks.push((cid, errMsg) => { - let res; if (cid) { - res = { errMsg: 'getPushClientId:ok', cid }; - hasSuccess && success(res); + resolve({ cid }); } else { - res = { errMsg: 'getPushClientId:fail' + (errMsg ? ' ' + errMsg : '') }; - hasFail && fail(res); + reject(errMsg); } - hasComplete && complete(res); }); if (typeof cid !== 'undefined') { Promise.resolve().then(() => invokeGetPushCidCallbacks(cid, cidErrMsg)); } -} +}); const onPushMessageCallbacks = []; // 不使用 defineOnApi 实现,是因为 defineOnApi 依赖 UniServiceJSBridge ,该对象目前在小程序上未提供,故简单实现 const onPushMessage = (fn) => { diff --git a/packages/uni-mp-baidu/dist/uni.api.esm.js b/packages/uni-mp-baidu/dist/uni.api.esm.js index 6dfa5ffed56d54bd6a55c1597363def5b1118774..9d2d73ade8cd17b968c2f11d1c4abd2f96dce410 100644 --- a/packages/uni-mp-baidu/dist/uni.api.esm.js +++ b/packages/uni-mp-baidu/dist/uni.api.esm.js @@ -1,6 +1,6 @@ -import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawType, makeMap, isFunction, isPromise, remove, extend } from '@vue/shared'; +import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawType, makeMap, isFunction, isPromise, extend, remove } from '@vue/shared'; import { normalizeLocale, LOCALE_EN } from '@dcloudio/uni-i18n'; -import { Emitter, onCreateVueApp, invokeCreateVueAppHook } from '@dcloudio/uni-shared'; +import { LINEFEED, Emitter, onCreateVueApp, invokeCreateVueAppHook } from '@dcloudio/uni-shared'; const eventChannels = {}; const eventChannelStack = []; @@ -190,6 +190,32 @@ function tryCatch(fn) { }; } +let invokeCallbackId = 1; +const invokeCallbacks = {}; +function addInvokeCallback(id, name, callback, keepAlive = false) { + invokeCallbacks[id] = { + name, + keepAlive, + callback, + }; + return id; +} +// onNativeEventReceive((event,data)=>{}) 需要两个参数,目前写死最多两个参数 +function invokeCallback(id, res, extras) { + if (typeof id === 'number') { + const opts = invokeCallbacks[id]; + if (opts) { + if (!opts.keepAlive) { + delete invokeCallbacks[id]; + } + return opts.callback(res, extras); + } + } + return res; +} +const API_SUCCESS = 'success'; +const API_FAIL = 'fail'; +const API_COMPLETE = 'complete'; function getApiCallbacks(args) { const apiCallbacks = {}; for (const name in args) { @@ -200,6 +226,36 @@ function getApiCallbacks(args) { } } return apiCallbacks; +} +function normalizeErrMsg$1(errMsg, name) { + if (!errMsg || errMsg.indexOf(':fail') === -1) { + return name + ':ok'; + } + return name + errMsg.substring(errMsg.indexOf(':fail')); +} +function createAsyncApiCallback(name, args = {}, { beforeAll, beforeSuccess } = {}) { + if (!isPlainObject(args)) { + args = {}; + } + const { success, fail, complete } = getApiCallbacks(args); + const hasSuccess = isFunction(success); + const hasFail = isFunction(fail); + const hasComplete = isFunction(complete); + const callbackId = invokeCallbackId++; + addInvokeCallback(callbackId, name, (res) => { + res = res || {}; + res.errMsg = normalizeErrMsg$1(res.errMsg, name); + isFunction(beforeAll) && beforeAll(res); + if (res.errMsg === name + ':ok') { + isFunction(beforeSuccess) && beforeSuccess(res, args); + hasSuccess && success(res); + } + else { + hasFail && fail(res); + } + hasComplete && complete(res); + }); + return callbackId; } const HOOK_SUCCESS = 'success'; @@ -301,6 +357,13 @@ function invokeApi(method, api, options, params) { return api(options, ...params); } +function hasCallback(args) { + if (isPlainObject(args) && + [API_SUCCESS, API_FAIL, API_COMPLETE].find((cb) => isFunction(args[cb]))) { + return true; + } + return false; +} function handlePromise(promise) { // if (__UNI_FEATURE_PROMISE__) { // return promise @@ -310,6 +373,16 @@ function handlePromise(promise) { // .catch((err) => [err]) // } return promise; +} +function promisify$1(name, fn) { + return (args = {}, ...rest) => { + if (hasCallback(args)) { + return wrapperReturnValue(name, invokeApi(name, fn, args, rest)); + } + return wrapperReturnValue(name, handlePromise(new Promise((resolve, reject) => { + invokeApi(name, fn, extend(args, { success: resolve, fail: reject }), rest); + }))); + }; } function formatApiArgs(args, options) { @@ -337,6 +410,12 @@ function formatApiArgs(args, options) { } } } +function invokeSuccess(id, name, res) { + return invokeCallback(id, extend(res || {}, { errMsg: name + ':ok' })); +} +function invokeFail(id, name, errMsg, errRes) { + return invokeCallback(id, extend({ errMsg: name + ':fail' + (errMsg ? ' ' + errMsg : '') }, errRes)); +} function beforeInvokeApi(name, args, protocol, options) { if ((process.env.NODE_ENV !== 'production')) { validateProtocols(name, args, protocol); @@ -352,6 +431,29 @@ function beforeInvokeApi(name, args, protocol, options) { return errMsg; } } +function normalizeErrMsg(errMsg) { + if (!errMsg || isString(errMsg)) { + return errMsg; + } + if (errMsg.stack) { + console.error(errMsg.message + LINEFEED + errMsg.stack); + return errMsg.message; + } + return errMsg; +} +function wrapperTaskApi(name, fn, protocol, options) { + return (args) => { + const id = createAsyncApiCallback(name, args, options); + const errMsg = beforeInvokeApi(name, [args], protocol, options); + if (errMsg) { + return invokeFail(id, name, errMsg); + } + return fn(args, { + resolve: (res) => invokeSuccess(id, name, res), + reject: (errMsg, errRes) => invokeFail(id, name, normalizeErrMsg(errMsg), errRes), + }); + }; +} function wrapperSyncApi(name, fn, protocol, options) { return (...args) => { const errMsg = beforeInvokeApi(name, args, protocol, options); @@ -361,8 +463,14 @@ function wrapperSyncApi(name, fn, protocol, options) { return fn.apply(null, args); }; } +function wrapperAsyncApi(name, fn, protocol, options) { + return wrapperTaskApi(name, fn, protocol, options); +} function defineSyncApi(name, fn, protocol, options) { return wrapperSyncApi(name, fn, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options); +} +function defineAsyncApi(name, fn, protocol, options) { + return promisify$1(name, wrapperAsyncApi(name, fn, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options)); } const API_UPX2PX = 'upx2px'; @@ -582,30 +690,20 @@ function invokeGetPushCidCallbacks(cid, errMsg) { }); getPushCidCallbacks.length = 0; } -function getPushClientId(args) { - if (!isPlainObject(args)) { - args = {}; - } - const { success, fail, complete } = getApiCallbacks(args); - const hasSuccess = isFunction(success); - const hasFail = isFunction(fail); - const hasComplete = isFunction(complete); +const API_GET_PUSH_CLIENT_ID = 'getPushClientId'; +const getPushClientId = defineAsyncApi(API_GET_PUSH_CLIENT_ID, (_, { resolve, reject }) => { getPushCidCallbacks.push((cid, errMsg) => { - let res; if (cid) { - res = { errMsg: 'getPushClientId:ok', cid }; - hasSuccess && success(res); + resolve({ cid }); } else { - res = { errMsg: 'getPushClientId:fail' + (errMsg ? ' ' + errMsg : '') }; - hasFail && fail(res); + reject(errMsg); } - hasComplete && complete(res); }); if (typeof cid !== 'undefined') { Promise.resolve().then(() => invokeGetPushCidCallbacks(cid, cidErrMsg)); } -} +}); const onPushMessageCallbacks = []; // 不使用 defineOnApi 实现,是因为 defineOnApi 依赖 UniServiceJSBridge ,该对象目前在小程序上未提供,故简单实现 const onPushMessage = (fn) => { diff --git a/packages/uni-mp-kuaishou/dist/uni.api.esm.js b/packages/uni-mp-kuaishou/dist/uni.api.esm.js index 6d7cca8cd42efc0873b528780cad94acaf27522a..7fa4bae752bf19f0370c2ea173a476defd8ff8d2 100644 --- a/packages/uni-mp-kuaishou/dist/uni.api.esm.js +++ b/packages/uni-mp-kuaishou/dist/uni.api.esm.js @@ -1,6 +1,6 @@ -import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawType, makeMap, isFunction, isPromise, remove, extend } from '@vue/shared'; +import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawType, makeMap, isFunction, isPromise, extend, remove } from '@vue/shared'; import { normalizeLocale, LOCALE_EN } from '@dcloudio/uni-i18n'; -import { Emitter, onCreateVueApp, invokeCreateVueAppHook } from '@dcloudio/uni-shared'; +import { LINEFEED, Emitter, onCreateVueApp, invokeCreateVueAppHook } from '@dcloudio/uni-shared'; const eventChannels = {}; const eventChannelStack = []; @@ -190,6 +190,32 @@ function tryCatch(fn) { }; } +let invokeCallbackId = 1; +const invokeCallbacks = {}; +function addInvokeCallback(id, name, callback, keepAlive = false) { + invokeCallbacks[id] = { + name, + keepAlive, + callback, + }; + return id; +} +// onNativeEventReceive((event,data)=>{}) 需要两个参数,目前写死最多两个参数 +function invokeCallback(id, res, extras) { + if (typeof id === 'number') { + const opts = invokeCallbacks[id]; + if (opts) { + if (!opts.keepAlive) { + delete invokeCallbacks[id]; + } + return opts.callback(res, extras); + } + } + return res; +} +const API_SUCCESS = 'success'; +const API_FAIL = 'fail'; +const API_COMPLETE = 'complete'; function getApiCallbacks(args) { const apiCallbacks = {}; for (const name in args) { @@ -200,6 +226,36 @@ function getApiCallbacks(args) { } } return apiCallbacks; +} +function normalizeErrMsg$1(errMsg, name) { + if (!errMsg || errMsg.indexOf(':fail') === -1) { + return name + ':ok'; + } + return name + errMsg.substring(errMsg.indexOf(':fail')); +} +function createAsyncApiCallback(name, args = {}, { beforeAll, beforeSuccess } = {}) { + if (!isPlainObject(args)) { + args = {}; + } + const { success, fail, complete } = getApiCallbacks(args); + const hasSuccess = isFunction(success); + const hasFail = isFunction(fail); + const hasComplete = isFunction(complete); + const callbackId = invokeCallbackId++; + addInvokeCallback(callbackId, name, (res) => { + res = res || {}; + res.errMsg = normalizeErrMsg$1(res.errMsg, name); + isFunction(beforeAll) && beforeAll(res); + if (res.errMsg === name + ':ok') { + isFunction(beforeSuccess) && beforeSuccess(res, args); + hasSuccess && success(res); + } + else { + hasFail && fail(res); + } + hasComplete && complete(res); + }); + return callbackId; } const HOOK_SUCCESS = 'success'; @@ -301,6 +357,13 @@ function invokeApi(method, api, options, params) { return api(options, ...params); } +function hasCallback(args) { + if (isPlainObject(args) && + [API_SUCCESS, API_FAIL, API_COMPLETE].find((cb) => isFunction(args[cb]))) { + return true; + } + return false; +} function handlePromise(promise) { // if (__UNI_FEATURE_PROMISE__) { // return promise @@ -310,6 +373,16 @@ function handlePromise(promise) { // .catch((err) => [err]) // } return promise; +} +function promisify$1(name, fn) { + return (args = {}, ...rest) => { + if (hasCallback(args)) { + return wrapperReturnValue(name, invokeApi(name, fn, args, rest)); + } + return wrapperReturnValue(name, handlePromise(new Promise((resolve, reject) => { + invokeApi(name, fn, extend(args, { success: resolve, fail: reject }), rest); + }))); + }; } function formatApiArgs(args, options) { @@ -337,6 +410,12 @@ function formatApiArgs(args, options) { } } } +function invokeSuccess(id, name, res) { + return invokeCallback(id, extend(res || {}, { errMsg: name + ':ok' })); +} +function invokeFail(id, name, errMsg, errRes) { + return invokeCallback(id, extend({ errMsg: name + ':fail' + (errMsg ? ' ' + errMsg : '') }, errRes)); +} function beforeInvokeApi(name, args, protocol, options) { if ((process.env.NODE_ENV !== 'production')) { validateProtocols(name, args, protocol); @@ -352,6 +431,29 @@ function beforeInvokeApi(name, args, protocol, options) { return errMsg; } } +function normalizeErrMsg(errMsg) { + if (!errMsg || isString(errMsg)) { + return errMsg; + } + if (errMsg.stack) { + console.error(errMsg.message + LINEFEED + errMsg.stack); + return errMsg.message; + } + return errMsg; +} +function wrapperTaskApi(name, fn, protocol, options) { + return (args) => { + const id = createAsyncApiCallback(name, args, options); + const errMsg = beforeInvokeApi(name, [args], protocol, options); + if (errMsg) { + return invokeFail(id, name, errMsg); + } + return fn(args, { + resolve: (res) => invokeSuccess(id, name, res), + reject: (errMsg, errRes) => invokeFail(id, name, normalizeErrMsg(errMsg), errRes), + }); + }; +} function wrapperSyncApi(name, fn, protocol, options) { return (...args) => { const errMsg = beforeInvokeApi(name, args, protocol, options); @@ -361,8 +463,14 @@ function wrapperSyncApi(name, fn, protocol, options) { return fn.apply(null, args); }; } +function wrapperAsyncApi(name, fn, protocol, options) { + return wrapperTaskApi(name, fn, protocol, options); +} function defineSyncApi(name, fn, protocol, options) { return wrapperSyncApi(name, fn, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options); +} +function defineAsyncApi(name, fn, protocol, options) { + return promisify$1(name, wrapperAsyncApi(name, fn, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options)); } const API_UPX2PX = 'upx2px'; @@ -582,30 +690,20 @@ function invokeGetPushCidCallbacks(cid, errMsg) { }); getPushCidCallbacks.length = 0; } -function getPushClientId(args) { - if (!isPlainObject(args)) { - args = {}; - } - const { success, fail, complete } = getApiCallbacks(args); - const hasSuccess = isFunction(success); - const hasFail = isFunction(fail); - const hasComplete = isFunction(complete); +const API_GET_PUSH_CLIENT_ID = 'getPushClientId'; +const getPushClientId = defineAsyncApi(API_GET_PUSH_CLIENT_ID, (_, { resolve, reject }) => { getPushCidCallbacks.push((cid, errMsg) => { - let res; if (cid) { - res = { errMsg: 'getPushClientId:ok', cid }; - hasSuccess && success(res); + resolve({ cid }); } else { - res = { errMsg: 'getPushClientId:fail' + (errMsg ? ' ' + errMsg : '') }; - hasFail && fail(res); + reject(errMsg); } - hasComplete && complete(res); }); if (typeof cid !== 'undefined') { Promise.resolve().then(() => invokeGetPushCidCallbacks(cid, cidErrMsg)); } -} +}); const onPushMessageCallbacks = []; // 不使用 defineOnApi 实现,是因为 defineOnApi 依赖 UniServiceJSBridge ,该对象目前在小程序上未提供,故简单实现 const onPushMessage = (fn) => { diff --git a/packages/uni-mp-lark/dist/uni.api.esm.js b/packages/uni-mp-lark/dist/uni.api.esm.js index f3666fcd96d229d5b10c3aa1d64331311d8689ae..6e6730e93fccb3f6eba9f95b8216e51202edf570 100644 --- a/packages/uni-mp-lark/dist/uni.api.esm.js +++ b/packages/uni-mp-lark/dist/uni.api.esm.js @@ -1,6 +1,6 @@ -import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawType, makeMap, isFunction, isPromise, remove, extend } from '@vue/shared'; +import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawType, makeMap, isFunction, isPromise, extend, remove } from '@vue/shared'; import { normalizeLocale, LOCALE_EN } from '@dcloudio/uni-i18n'; -import { Emitter, onCreateVueApp, invokeCreateVueAppHook } from '@dcloudio/uni-shared'; +import { LINEFEED, Emitter, onCreateVueApp, invokeCreateVueAppHook } from '@dcloudio/uni-shared'; const eventChannels = {}; const eventChannelStack = []; @@ -190,6 +190,32 @@ function tryCatch(fn) { }; } +let invokeCallbackId = 1; +const invokeCallbacks = {}; +function addInvokeCallback(id, name, callback, keepAlive = false) { + invokeCallbacks[id] = { + name, + keepAlive, + callback, + }; + return id; +} +// onNativeEventReceive((event,data)=>{}) 需要两个参数,目前写死最多两个参数 +function invokeCallback(id, res, extras) { + if (typeof id === 'number') { + const opts = invokeCallbacks[id]; + if (opts) { + if (!opts.keepAlive) { + delete invokeCallbacks[id]; + } + return opts.callback(res, extras); + } + } + return res; +} +const API_SUCCESS = 'success'; +const API_FAIL = 'fail'; +const API_COMPLETE = 'complete'; function getApiCallbacks(args) { const apiCallbacks = {}; for (const name in args) { @@ -200,6 +226,36 @@ function getApiCallbacks(args) { } } return apiCallbacks; +} +function normalizeErrMsg$1(errMsg, name) { + if (!errMsg || errMsg.indexOf(':fail') === -1) { + return name + ':ok'; + } + return name + errMsg.substring(errMsg.indexOf(':fail')); +} +function createAsyncApiCallback(name, args = {}, { beforeAll, beforeSuccess } = {}) { + if (!isPlainObject(args)) { + args = {}; + } + const { success, fail, complete } = getApiCallbacks(args); + const hasSuccess = isFunction(success); + const hasFail = isFunction(fail); + const hasComplete = isFunction(complete); + const callbackId = invokeCallbackId++; + addInvokeCallback(callbackId, name, (res) => { + res = res || {}; + res.errMsg = normalizeErrMsg$1(res.errMsg, name); + isFunction(beforeAll) && beforeAll(res); + if (res.errMsg === name + ':ok') { + isFunction(beforeSuccess) && beforeSuccess(res, args); + hasSuccess && success(res); + } + else { + hasFail && fail(res); + } + hasComplete && complete(res); + }); + return callbackId; } const HOOK_SUCCESS = 'success'; @@ -301,6 +357,13 @@ function invokeApi(method, api, options, params) { return api(options, ...params); } +function hasCallback(args) { + if (isPlainObject(args) && + [API_SUCCESS, API_FAIL, API_COMPLETE].find((cb) => isFunction(args[cb]))) { + return true; + } + return false; +} function handlePromise(promise) { // if (__UNI_FEATURE_PROMISE__) { // return promise @@ -310,6 +373,16 @@ function handlePromise(promise) { // .catch((err) => [err]) // } return promise; +} +function promisify$1(name, fn) { + return (args = {}, ...rest) => { + if (hasCallback(args)) { + return wrapperReturnValue(name, invokeApi(name, fn, args, rest)); + } + return wrapperReturnValue(name, handlePromise(new Promise((resolve, reject) => { + invokeApi(name, fn, extend(args, { success: resolve, fail: reject }), rest); + }))); + }; } function formatApiArgs(args, options) { @@ -337,6 +410,12 @@ function formatApiArgs(args, options) { } } } +function invokeSuccess(id, name, res) { + return invokeCallback(id, extend(res || {}, { errMsg: name + ':ok' })); +} +function invokeFail(id, name, errMsg, errRes) { + return invokeCallback(id, extend({ errMsg: name + ':fail' + (errMsg ? ' ' + errMsg : '') }, errRes)); +} function beforeInvokeApi(name, args, protocol, options) { if ((process.env.NODE_ENV !== 'production')) { validateProtocols(name, args, protocol); @@ -352,6 +431,29 @@ function beforeInvokeApi(name, args, protocol, options) { return errMsg; } } +function normalizeErrMsg(errMsg) { + if (!errMsg || isString(errMsg)) { + return errMsg; + } + if (errMsg.stack) { + console.error(errMsg.message + LINEFEED + errMsg.stack); + return errMsg.message; + } + return errMsg; +} +function wrapperTaskApi(name, fn, protocol, options) { + return (args) => { + const id = createAsyncApiCallback(name, args, options); + const errMsg = beforeInvokeApi(name, [args], protocol, options); + if (errMsg) { + return invokeFail(id, name, errMsg); + } + return fn(args, { + resolve: (res) => invokeSuccess(id, name, res), + reject: (errMsg, errRes) => invokeFail(id, name, normalizeErrMsg(errMsg), errRes), + }); + }; +} function wrapperSyncApi(name, fn, protocol, options) { return (...args) => { const errMsg = beforeInvokeApi(name, args, protocol, options); @@ -361,8 +463,14 @@ function wrapperSyncApi(name, fn, protocol, options) { return fn.apply(null, args); }; } +function wrapperAsyncApi(name, fn, protocol, options) { + return wrapperTaskApi(name, fn, protocol, options); +} function defineSyncApi(name, fn, protocol, options) { return wrapperSyncApi(name, fn, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options); +} +function defineAsyncApi(name, fn, protocol, options) { + return promisify$1(name, wrapperAsyncApi(name, fn, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options)); } const API_UPX2PX = 'upx2px'; @@ -582,30 +690,20 @@ function invokeGetPushCidCallbacks(cid, errMsg) { }); getPushCidCallbacks.length = 0; } -function getPushClientId(args) { - if (!isPlainObject(args)) { - args = {}; - } - const { success, fail, complete } = getApiCallbacks(args); - const hasSuccess = isFunction(success); - const hasFail = isFunction(fail); - const hasComplete = isFunction(complete); +const API_GET_PUSH_CLIENT_ID = 'getPushClientId'; +const getPushClientId = defineAsyncApi(API_GET_PUSH_CLIENT_ID, (_, { resolve, reject }) => { getPushCidCallbacks.push((cid, errMsg) => { - let res; if (cid) { - res = { errMsg: 'getPushClientId:ok', cid }; - hasSuccess && success(res); + resolve({ cid }); } else { - res = { errMsg: 'getPushClientId:fail' + (errMsg ? ' ' + errMsg : '') }; - hasFail && fail(res); + reject(errMsg); } - hasComplete && complete(res); }); if (typeof cid !== 'undefined') { Promise.resolve().then(() => invokeGetPushCidCallbacks(cid, cidErrMsg)); } -} +}); const onPushMessageCallbacks = []; // 不使用 defineOnApi 实现,是因为 defineOnApi 依赖 UniServiceJSBridge ,该对象目前在小程序上未提供,故简单实现 const onPushMessage = (fn) => { diff --git a/packages/uni-mp-qq/dist/uni.api.esm.js b/packages/uni-mp-qq/dist/uni.api.esm.js index ada4bc95d1e54eb3e58db90713d66574effb9c38..4d6f2fc2efe730a4bc65174e460f236a41f31e1a 100644 --- a/packages/uni-mp-qq/dist/uni.api.esm.js +++ b/packages/uni-mp-qq/dist/uni.api.esm.js @@ -1,6 +1,6 @@ -import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawType, makeMap, isFunction, isPromise, remove, extend } from '@vue/shared'; +import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawType, makeMap, isFunction, isPromise, extend, remove } from '@vue/shared'; import { normalizeLocale, LOCALE_EN } from '@dcloudio/uni-i18n'; -import { Emitter, onCreateVueApp, invokeCreateVueAppHook } from '@dcloudio/uni-shared'; +import { LINEFEED, Emitter, onCreateVueApp, invokeCreateVueAppHook } from '@dcloudio/uni-shared'; const eventChannels = {}; const eventChannelStack = []; @@ -190,6 +190,32 @@ function tryCatch(fn) { }; } +let invokeCallbackId = 1; +const invokeCallbacks = {}; +function addInvokeCallback(id, name, callback, keepAlive = false) { + invokeCallbacks[id] = { + name, + keepAlive, + callback, + }; + return id; +} +// onNativeEventReceive((event,data)=>{}) 需要两个参数,目前写死最多两个参数 +function invokeCallback(id, res, extras) { + if (typeof id === 'number') { + const opts = invokeCallbacks[id]; + if (opts) { + if (!opts.keepAlive) { + delete invokeCallbacks[id]; + } + return opts.callback(res, extras); + } + } + return res; +} +const API_SUCCESS = 'success'; +const API_FAIL = 'fail'; +const API_COMPLETE = 'complete'; function getApiCallbacks(args) { const apiCallbacks = {}; for (const name in args) { @@ -200,6 +226,36 @@ function getApiCallbacks(args) { } } return apiCallbacks; +} +function normalizeErrMsg$1(errMsg, name) { + if (!errMsg || errMsg.indexOf(':fail') === -1) { + return name + ':ok'; + } + return name + errMsg.substring(errMsg.indexOf(':fail')); +} +function createAsyncApiCallback(name, args = {}, { beforeAll, beforeSuccess } = {}) { + if (!isPlainObject(args)) { + args = {}; + } + const { success, fail, complete } = getApiCallbacks(args); + const hasSuccess = isFunction(success); + const hasFail = isFunction(fail); + const hasComplete = isFunction(complete); + const callbackId = invokeCallbackId++; + addInvokeCallback(callbackId, name, (res) => { + res = res || {}; + res.errMsg = normalizeErrMsg$1(res.errMsg, name); + isFunction(beforeAll) && beforeAll(res); + if (res.errMsg === name + ':ok') { + isFunction(beforeSuccess) && beforeSuccess(res, args); + hasSuccess && success(res); + } + else { + hasFail && fail(res); + } + hasComplete && complete(res); + }); + return callbackId; } const HOOK_SUCCESS = 'success'; @@ -301,6 +357,13 @@ function invokeApi(method, api, options, params) { return api(options, ...params); } +function hasCallback(args) { + if (isPlainObject(args) && + [API_SUCCESS, API_FAIL, API_COMPLETE].find((cb) => isFunction(args[cb]))) { + return true; + } + return false; +} function handlePromise(promise) { // if (__UNI_FEATURE_PROMISE__) { // return promise @@ -310,6 +373,16 @@ function handlePromise(promise) { // .catch((err) => [err]) // } return promise; +} +function promisify$1(name, fn) { + return (args = {}, ...rest) => { + if (hasCallback(args)) { + return wrapperReturnValue(name, invokeApi(name, fn, args, rest)); + } + return wrapperReturnValue(name, handlePromise(new Promise((resolve, reject) => { + invokeApi(name, fn, extend(args, { success: resolve, fail: reject }), rest); + }))); + }; } function formatApiArgs(args, options) { @@ -337,6 +410,12 @@ function formatApiArgs(args, options) { } } } +function invokeSuccess(id, name, res) { + return invokeCallback(id, extend(res || {}, { errMsg: name + ':ok' })); +} +function invokeFail(id, name, errMsg, errRes) { + return invokeCallback(id, extend({ errMsg: name + ':fail' + (errMsg ? ' ' + errMsg : '') }, errRes)); +} function beforeInvokeApi(name, args, protocol, options) { if ((process.env.NODE_ENV !== 'production')) { validateProtocols(name, args, protocol); @@ -352,6 +431,29 @@ function beforeInvokeApi(name, args, protocol, options) { return errMsg; } } +function normalizeErrMsg(errMsg) { + if (!errMsg || isString(errMsg)) { + return errMsg; + } + if (errMsg.stack) { + console.error(errMsg.message + LINEFEED + errMsg.stack); + return errMsg.message; + } + return errMsg; +} +function wrapperTaskApi(name, fn, protocol, options) { + return (args) => { + const id = createAsyncApiCallback(name, args, options); + const errMsg = beforeInvokeApi(name, [args], protocol, options); + if (errMsg) { + return invokeFail(id, name, errMsg); + } + return fn(args, { + resolve: (res) => invokeSuccess(id, name, res), + reject: (errMsg, errRes) => invokeFail(id, name, normalizeErrMsg(errMsg), errRes), + }); + }; +} function wrapperSyncApi(name, fn, protocol, options) { return (...args) => { const errMsg = beforeInvokeApi(name, args, protocol, options); @@ -361,8 +463,14 @@ function wrapperSyncApi(name, fn, protocol, options) { return fn.apply(null, args); }; } +function wrapperAsyncApi(name, fn, protocol, options) { + return wrapperTaskApi(name, fn, protocol, options); +} function defineSyncApi(name, fn, protocol, options) { return wrapperSyncApi(name, fn, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options); +} +function defineAsyncApi(name, fn, protocol, options) { + return promisify$1(name, wrapperAsyncApi(name, fn, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options)); } const API_UPX2PX = 'upx2px'; @@ -582,30 +690,20 @@ function invokeGetPushCidCallbacks(cid, errMsg) { }); getPushCidCallbacks.length = 0; } -function getPushClientId(args) { - if (!isPlainObject(args)) { - args = {}; - } - const { success, fail, complete } = getApiCallbacks(args); - const hasSuccess = isFunction(success); - const hasFail = isFunction(fail); - const hasComplete = isFunction(complete); +const API_GET_PUSH_CLIENT_ID = 'getPushClientId'; +const getPushClientId = defineAsyncApi(API_GET_PUSH_CLIENT_ID, (_, { resolve, reject }) => { getPushCidCallbacks.push((cid, errMsg) => { - let res; if (cid) { - res = { errMsg: 'getPushClientId:ok', cid }; - hasSuccess && success(res); + resolve({ cid }); } else { - res = { errMsg: 'getPushClientId:fail' + (errMsg ? ' ' + errMsg : '') }; - hasFail && fail(res); + reject(errMsg); } - hasComplete && complete(res); }); if (typeof cid !== 'undefined') { Promise.resolve().then(() => invokeGetPushCidCallbacks(cid, cidErrMsg)); } -} +}); const onPushMessageCallbacks = []; // 不使用 defineOnApi 实现,是因为 defineOnApi 依赖 UniServiceJSBridge ,该对象目前在小程序上未提供,故简单实现 const onPushMessage = (fn) => { diff --git a/packages/uni-mp-toutiao/dist/uni.api.esm.js b/packages/uni-mp-toutiao/dist/uni.api.esm.js index e46ae76826ba75a65a1d4142affe1b50a5d38d70..3e9697929a2bffbe3c719697f24e03c1072b6af4 100644 --- a/packages/uni-mp-toutiao/dist/uni.api.esm.js +++ b/packages/uni-mp-toutiao/dist/uni.api.esm.js @@ -1,6 +1,6 @@ -import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawType, makeMap, isFunction, isPromise, remove, extend } from '@vue/shared'; +import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawType, makeMap, isFunction, isPromise, extend, remove } from '@vue/shared'; import { normalizeLocale, LOCALE_EN } from '@dcloudio/uni-i18n'; -import { Emitter, onCreateVueApp, invokeCreateVueAppHook } from '@dcloudio/uni-shared'; +import { LINEFEED, Emitter, onCreateVueApp, invokeCreateVueAppHook } from '@dcloudio/uni-shared'; const eventChannels = {}; const eventChannelStack = []; @@ -190,6 +190,32 @@ function tryCatch(fn) { }; } +let invokeCallbackId = 1; +const invokeCallbacks = {}; +function addInvokeCallback(id, name, callback, keepAlive = false) { + invokeCallbacks[id] = { + name, + keepAlive, + callback, + }; + return id; +} +// onNativeEventReceive((event,data)=>{}) 需要两个参数,目前写死最多两个参数 +function invokeCallback(id, res, extras) { + if (typeof id === 'number') { + const opts = invokeCallbacks[id]; + if (opts) { + if (!opts.keepAlive) { + delete invokeCallbacks[id]; + } + return opts.callback(res, extras); + } + } + return res; +} +const API_SUCCESS = 'success'; +const API_FAIL = 'fail'; +const API_COMPLETE = 'complete'; function getApiCallbacks(args) { const apiCallbacks = {}; for (const name in args) { @@ -200,6 +226,36 @@ function getApiCallbacks(args) { } } return apiCallbacks; +} +function normalizeErrMsg$1(errMsg, name) { + if (!errMsg || errMsg.indexOf(':fail') === -1) { + return name + ':ok'; + } + return name + errMsg.substring(errMsg.indexOf(':fail')); +} +function createAsyncApiCallback(name, args = {}, { beforeAll, beforeSuccess } = {}) { + if (!isPlainObject(args)) { + args = {}; + } + const { success, fail, complete } = getApiCallbacks(args); + const hasSuccess = isFunction(success); + const hasFail = isFunction(fail); + const hasComplete = isFunction(complete); + const callbackId = invokeCallbackId++; + addInvokeCallback(callbackId, name, (res) => { + res = res || {}; + res.errMsg = normalizeErrMsg$1(res.errMsg, name); + isFunction(beforeAll) && beforeAll(res); + if (res.errMsg === name + ':ok') { + isFunction(beforeSuccess) && beforeSuccess(res, args); + hasSuccess && success(res); + } + else { + hasFail && fail(res); + } + hasComplete && complete(res); + }); + return callbackId; } const HOOK_SUCCESS = 'success'; @@ -301,6 +357,13 @@ function invokeApi(method, api, options, params) { return api(options, ...params); } +function hasCallback(args) { + if (isPlainObject(args) && + [API_SUCCESS, API_FAIL, API_COMPLETE].find((cb) => isFunction(args[cb]))) { + return true; + } + return false; +} function handlePromise(promise) { // if (__UNI_FEATURE_PROMISE__) { // return promise @@ -310,6 +373,16 @@ function handlePromise(promise) { // .catch((err) => [err]) // } return promise; +} +function promisify$1(name, fn) { + return (args = {}, ...rest) => { + if (hasCallback(args)) { + return wrapperReturnValue(name, invokeApi(name, fn, args, rest)); + } + return wrapperReturnValue(name, handlePromise(new Promise((resolve, reject) => { + invokeApi(name, fn, extend(args, { success: resolve, fail: reject }), rest); + }))); + }; } function formatApiArgs(args, options) { @@ -337,6 +410,12 @@ function formatApiArgs(args, options) { } } } +function invokeSuccess(id, name, res) { + return invokeCallback(id, extend(res || {}, { errMsg: name + ':ok' })); +} +function invokeFail(id, name, errMsg, errRes) { + return invokeCallback(id, extend({ errMsg: name + ':fail' + (errMsg ? ' ' + errMsg : '') }, errRes)); +} function beforeInvokeApi(name, args, protocol, options) { if ((process.env.NODE_ENV !== 'production')) { validateProtocols(name, args, protocol); @@ -352,6 +431,29 @@ function beforeInvokeApi(name, args, protocol, options) { return errMsg; } } +function normalizeErrMsg(errMsg) { + if (!errMsg || isString(errMsg)) { + return errMsg; + } + if (errMsg.stack) { + console.error(errMsg.message + LINEFEED + errMsg.stack); + return errMsg.message; + } + return errMsg; +} +function wrapperTaskApi(name, fn, protocol, options) { + return (args) => { + const id = createAsyncApiCallback(name, args, options); + const errMsg = beforeInvokeApi(name, [args], protocol, options); + if (errMsg) { + return invokeFail(id, name, errMsg); + } + return fn(args, { + resolve: (res) => invokeSuccess(id, name, res), + reject: (errMsg, errRes) => invokeFail(id, name, normalizeErrMsg(errMsg), errRes), + }); + }; +} function wrapperSyncApi(name, fn, protocol, options) { return (...args) => { const errMsg = beforeInvokeApi(name, args, protocol, options); @@ -361,8 +463,14 @@ function wrapperSyncApi(name, fn, protocol, options) { return fn.apply(null, args); }; } +function wrapperAsyncApi(name, fn, protocol, options) { + return wrapperTaskApi(name, fn, protocol, options); +} function defineSyncApi(name, fn, protocol, options) { return wrapperSyncApi(name, fn, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options); +} +function defineAsyncApi(name, fn, protocol, options) { + return promisify$1(name, wrapperAsyncApi(name, fn, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options)); } const API_UPX2PX = 'upx2px'; @@ -582,30 +690,20 @@ function invokeGetPushCidCallbacks(cid, errMsg) { }); getPushCidCallbacks.length = 0; } -function getPushClientId(args) { - if (!isPlainObject(args)) { - args = {}; - } - const { success, fail, complete } = getApiCallbacks(args); - const hasSuccess = isFunction(success); - const hasFail = isFunction(fail); - const hasComplete = isFunction(complete); +const API_GET_PUSH_CLIENT_ID = 'getPushClientId'; +const getPushClientId = defineAsyncApi(API_GET_PUSH_CLIENT_ID, (_, { resolve, reject }) => { getPushCidCallbacks.push((cid, errMsg) => { - let res; if (cid) { - res = { errMsg: 'getPushClientId:ok', cid }; - hasSuccess && success(res); + resolve({ cid }); } else { - res = { errMsg: 'getPushClientId:fail' + (errMsg ? ' ' + errMsg : '') }; - hasFail && fail(res); + reject(errMsg); } - hasComplete && complete(res); }); if (typeof cid !== 'undefined') { Promise.resolve().then(() => invokeGetPushCidCallbacks(cid, cidErrMsg)); } -} +}); const onPushMessageCallbacks = []; // 不使用 defineOnApi 实现,是因为 defineOnApi 依赖 UniServiceJSBridge ,该对象目前在小程序上未提供,故简单实现 const onPushMessage = (fn) => { diff --git a/packages/uni-mp-weixin/dist/uni.api.esm.js b/packages/uni-mp-weixin/dist/uni.api.esm.js index e9647d976f6f8617eaaff0238d4c26ed43f8f213..058f750f7be84240e2b0563f07dc6fb1b93e2d1c 100644 --- a/packages/uni-mp-weixin/dist/uni.api.esm.js +++ b/packages/uni-mp-weixin/dist/uni.api.esm.js @@ -1,6 +1,6 @@ -import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawType, makeMap, isFunction, isPromise, remove, extend } from '@vue/shared'; +import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawType, makeMap, isFunction, isPromise, extend, remove } from '@vue/shared'; import { normalizeLocale, LOCALE_EN } from '@dcloudio/uni-i18n'; -import { Emitter, onCreateVueApp, invokeCreateVueAppHook, sortObject } from '@dcloudio/uni-shared'; +import { LINEFEED, Emitter, onCreateVueApp, invokeCreateVueAppHook, sortObject } from '@dcloudio/uni-shared'; function getBaseSystemInfo() { return wx.getSystemInfoSync() @@ -154,6 +154,32 @@ function tryCatch(fn) { }; } +let invokeCallbackId = 1; +const invokeCallbacks = {}; +function addInvokeCallback(id, name, callback, keepAlive = false) { + invokeCallbacks[id] = { + name, + keepAlive, + callback, + }; + return id; +} +// onNativeEventReceive((event,data)=>{}) 需要两个参数,目前写死最多两个参数 +function invokeCallback(id, res, extras) { + if (typeof id === 'number') { + const opts = invokeCallbacks[id]; + if (opts) { + if (!opts.keepAlive) { + delete invokeCallbacks[id]; + } + return opts.callback(res, extras); + } + } + return res; +} +const API_SUCCESS = 'success'; +const API_FAIL = 'fail'; +const API_COMPLETE = 'complete'; function getApiCallbacks(args) { const apiCallbacks = {}; for (const name in args) { @@ -164,6 +190,36 @@ function getApiCallbacks(args) { } } return apiCallbacks; +} +function normalizeErrMsg$1(errMsg, name) { + if (!errMsg || errMsg.indexOf(':fail') === -1) { + return name + ':ok'; + } + return name + errMsg.substring(errMsg.indexOf(':fail')); +} +function createAsyncApiCallback(name, args = {}, { beforeAll, beforeSuccess } = {}) { + if (!isPlainObject(args)) { + args = {}; + } + const { success, fail, complete } = getApiCallbacks(args); + const hasSuccess = isFunction(success); + const hasFail = isFunction(fail); + const hasComplete = isFunction(complete); + const callbackId = invokeCallbackId++; + addInvokeCallback(callbackId, name, (res) => { + res = res || {}; + res.errMsg = normalizeErrMsg$1(res.errMsg, name); + isFunction(beforeAll) && beforeAll(res); + if (res.errMsg === name + ':ok') { + isFunction(beforeSuccess) && beforeSuccess(res, args); + hasSuccess && success(res); + } + else { + hasFail && fail(res); + } + hasComplete && complete(res); + }); + return callbackId; } const HOOK_SUCCESS = 'success'; @@ -265,6 +321,13 @@ function invokeApi(method, api, options, params) { return api(options, ...params); } +function hasCallback(args) { + if (isPlainObject(args) && + [API_SUCCESS, API_FAIL, API_COMPLETE].find((cb) => isFunction(args[cb]))) { + return true; + } + return false; +} function handlePromise(promise) { // if (__UNI_FEATURE_PROMISE__) { // return promise @@ -274,6 +337,16 @@ function handlePromise(promise) { // .catch((err) => [err]) // } return promise; +} +function promisify$1(name, fn) { + return (args = {}, ...rest) => { + if (hasCallback(args)) { + return wrapperReturnValue(name, invokeApi(name, fn, args, rest)); + } + return wrapperReturnValue(name, handlePromise(new Promise((resolve, reject) => { + invokeApi(name, fn, extend(args, { success: resolve, fail: reject }), rest); + }))); + }; } function formatApiArgs(args, options) { @@ -301,6 +374,12 @@ function formatApiArgs(args, options) { } } } +function invokeSuccess(id, name, res) { + return invokeCallback(id, extend(res || {}, { errMsg: name + ':ok' })); +} +function invokeFail(id, name, errMsg, errRes) { + return invokeCallback(id, extend({ errMsg: name + ':fail' + (errMsg ? ' ' + errMsg : '') }, errRes)); +} function beforeInvokeApi(name, args, protocol, options) { if ((process.env.NODE_ENV !== 'production')) { validateProtocols(name, args, protocol); @@ -316,6 +395,29 @@ function beforeInvokeApi(name, args, protocol, options) { return errMsg; } } +function normalizeErrMsg(errMsg) { + if (!errMsg || isString(errMsg)) { + return errMsg; + } + if (errMsg.stack) { + console.error(errMsg.message + LINEFEED + errMsg.stack); + return errMsg.message; + } + return errMsg; +} +function wrapperTaskApi(name, fn, protocol, options) { + return (args) => { + const id = createAsyncApiCallback(name, args, options); + const errMsg = beforeInvokeApi(name, [args], protocol, options); + if (errMsg) { + return invokeFail(id, name, errMsg); + } + return fn(args, { + resolve: (res) => invokeSuccess(id, name, res), + reject: (errMsg, errRes) => invokeFail(id, name, normalizeErrMsg(errMsg), errRes), + }); + }; +} function wrapperSyncApi(name, fn, protocol, options) { return (...args) => { const errMsg = beforeInvokeApi(name, args, protocol, options); @@ -325,8 +427,14 @@ function wrapperSyncApi(name, fn, protocol, options) { return fn.apply(null, args); }; } +function wrapperAsyncApi(name, fn, protocol, options) { + return wrapperTaskApi(name, fn, protocol, options); +} function defineSyncApi(name, fn, protocol, options) { return wrapperSyncApi(name, fn, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options); +} +function defineAsyncApi(name, fn, protocol, options) { + return promisify$1(name, wrapperAsyncApi(name, fn, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options)); } const API_UPX2PX = 'upx2px'; @@ -546,30 +654,20 @@ function invokeGetPushCidCallbacks(cid, errMsg) { }); getPushCidCallbacks.length = 0; } -function getPushClientId(args) { - if (!isPlainObject(args)) { - args = {}; - } - const { success, fail, complete } = getApiCallbacks(args); - const hasSuccess = isFunction(success); - const hasFail = isFunction(fail); - const hasComplete = isFunction(complete); +const API_GET_PUSH_CLIENT_ID = 'getPushClientId'; +const getPushClientId = defineAsyncApi(API_GET_PUSH_CLIENT_ID, (_, { resolve, reject }) => { getPushCidCallbacks.push((cid, errMsg) => { - let res; if (cid) { - res = { errMsg: 'getPushClientId:ok', cid }; - hasSuccess && success(res); + resolve({ cid }); } else { - res = { errMsg: 'getPushClientId:fail' + (errMsg ? ' ' + errMsg : '') }; - hasFail && fail(res); + reject(errMsg); } - hasComplete && complete(res); }); if (typeof cid !== 'undefined') { Promise.resolve().then(() => invokeGetPushCidCallbacks(cid, cidErrMsg)); } -} +}); const onPushMessageCallbacks = []; // 不使用 defineOnApi 实现,是因为 defineOnApi 依赖 UniServiceJSBridge ,该对象目前在小程序上未提供,故简单实现 const onPushMessage = (fn) => { diff --git a/packages/uni-quickapp-webview/dist/uni.api.esm.js b/packages/uni-quickapp-webview/dist/uni.api.esm.js index 310aa185343bbac3b6622ff0bb1f78537dac6896..519fa94ebba8075f17a027f65f4d178513a7190a 100644 --- a/packages/uni-quickapp-webview/dist/uni.api.esm.js +++ b/packages/uni-quickapp-webview/dist/uni.api.esm.js @@ -1,6 +1,6 @@ -import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawType, makeMap, isFunction, isPromise, remove, extend } from '@vue/shared'; +import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawType, makeMap, isFunction, isPromise, extend, remove } from '@vue/shared'; import { normalizeLocale, LOCALE_EN } from '@dcloudio/uni-i18n'; -import { Emitter, onCreateVueApp, invokeCreateVueAppHook } from '@dcloudio/uni-shared'; +import { LINEFEED, Emitter, onCreateVueApp, invokeCreateVueAppHook } from '@dcloudio/uni-shared'; const eventChannels = {}; const eventChannelStack = []; @@ -190,6 +190,32 @@ function tryCatch(fn) { }; } +let invokeCallbackId = 1; +const invokeCallbacks = {}; +function addInvokeCallback(id, name, callback, keepAlive = false) { + invokeCallbacks[id] = { + name, + keepAlive, + callback, + }; + return id; +} +// onNativeEventReceive((event,data)=>{}) 需要两个参数,目前写死最多两个参数 +function invokeCallback(id, res, extras) { + if (typeof id === 'number') { + const opts = invokeCallbacks[id]; + if (opts) { + if (!opts.keepAlive) { + delete invokeCallbacks[id]; + } + return opts.callback(res, extras); + } + } + return res; +} +const API_SUCCESS = 'success'; +const API_FAIL = 'fail'; +const API_COMPLETE = 'complete'; function getApiCallbacks(args) { const apiCallbacks = {}; for (const name in args) { @@ -200,6 +226,36 @@ function getApiCallbacks(args) { } } return apiCallbacks; +} +function normalizeErrMsg$1(errMsg, name) { + if (!errMsg || errMsg.indexOf(':fail') === -1) { + return name + ':ok'; + } + return name + errMsg.substring(errMsg.indexOf(':fail')); +} +function createAsyncApiCallback(name, args = {}, { beforeAll, beforeSuccess } = {}) { + if (!isPlainObject(args)) { + args = {}; + } + const { success, fail, complete } = getApiCallbacks(args); + const hasSuccess = isFunction(success); + const hasFail = isFunction(fail); + const hasComplete = isFunction(complete); + const callbackId = invokeCallbackId++; + addInvokeCallback(callbackId, name, (res) => { + res = res || {}; + res.errMsg = normalizeErrMsg$1(res.errMsg, name); + isFunction(beforeAll) && beforeAll(res); + if (res.errMsg === name + ':ok') { + isFunction(beforeSuccess) && beforeSuccess(res, args); + hasSuccess && success(res); + } + else { + hasFail && fail(res); + } + hasComplete && complete(res); + }); + return callbackId; } const HOOK_SUCCESS = 'success'; @@ -301,6 +357,13 @@ function invokeApi(method, api, options, params) { return api(options, ...params); } +function hasCallback(args) { + if (isPlainObject(args) && + [API_SUCCESS, API_FAIL, API_COMPLETE].find((cb) => isFunction(args[cb]))) { + return true; + } + return false; +} function handlePromise(promise) { // if (__UNI_FEATURE_PROMISE__) { // return promise @@ -310,6 +373,16 @@ function handlePromise(promise) { // .catch((err) => [err]) // } return promise; +} +function promisify$1(name, fn) { + return (args = {}, ...rest) => { + if (hasCallback(args)) { + return wrapperReturnValue(name, invokeApi(name, fn, args, rest)); + } + return wrapperReturnValue(name, handlePromise(new Promise((resolve, reject) => { + invokeApi(name, fn, extend(args, { success: resolve, fail: reject }), rest); + }))); + }; } function formatApiArgs(args, options) { @@ -337,6 +410,12 @@ function formatApiArgs(args, options) { } } } +function invokeSuccess(id, name, res) { + return invokeCallback(id, extend(res || {}, { errMsg: name + ':ok' })); +} +function invokeFail(id, name, errMsg, errRes) { + return invokeCallback(id, extend({ errMsg: name + ':fail' + (errMsg ? ' ' + errMsg : '') }, errRes)); +} function beforeInvokeApi(name, args, protocol, options) { if ((process.env.NODE_ENV !== 'production')) { validateProtocols(name, args, protocol); @@ -352,6 +431,29 @@ function beforeInvokeApi(name, args, protocol, options) { return errMsg; } } +function normalizeErrMsg(errMsg) { + if (!errMsg || isString(errMsg)) { + return errMsg; + } + if (errMsg.stack) { + console.error(errMsg.message + LINEFEED + errMsg.stack); + return errMsg.message; + } + return errMsg; +} +function wrapperTaskApi(name, fn, protocol, options) { + return (args) => { + const id = createAsyncApiCallback(name, args, options); + const errMsg = beforeInvokeApi(name, [args], protocol, options); + if (errMsg) { + return invokeFail(id, name, errMsg); + } + return fn(args, { + resolve: (res) => invokeSuccess(id, name, res), + reject: (errMsg, errRes) => invokeFail(id, name, normalizeErrMsg(errMsg), errRes), + }); + }; +} function wrapperSyncApi(name, fn, protocol, options) { return (...args) => { const errMsg = beforeInvokeApi(name, args, protocol, options); @@ -361,8 +463,14 @@ function wrapperSyncApi(name, fn, protocol, options) { return fn.apply(null, args); }; } +function wrapperAsyncApi(name, fn, protocol, options) { + return wrapperTaskApi(name, fn, protocol, options); +} function defineSyncApi(name, fn, protocol, options) { return wrapperSyncApi(name, fn, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options); +} +function defineAsyncApi(name, fn, protocol, options) { + return promisify$1(name, wrapperAsyncApi(name, fn, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options)); } const API_UPX2PX = 'upx2px'; @@ -582,30 +690,20 @@ function invokeGetPushCidCallbacks(cid, errMsg) { }); getPushCidCallbacks.length = 0; } -function getPushClientId(args) { - if (!isPlainObject(args)) { - args = {}; - } - const { success, fail, complete } = getApiCallbacks(args); - const hasSuccess = isFunction(success); - const hasFail = isFunction(fail); - const hasComplete = isFunction(complete); +const API_GET_PUSH_CLIENT_ID = 'getPushClientId'; +const getPushClientId = defineAsyncApi(API_GET_PUSH_CLIENT_ID, (_, { resolve, reject }) => { getPushCidCallbacks.push((cid, errMsg) => { - let res; if (cid) { - res = { errMsg: 'getPushClientId:ok', cid }; - hasSuccess && success(res); + resolve({ cid }); } else { - res = { errMsg: 'getPushClientId:fail' + (errMsg ? ' ' + errMsg : '') }; - hasFail && fail(res); + reject(errMsg); } - hasComplete && complete(res); }); if (typeof cid !== 'undefined') { Promise.resolve().then(() => invokeGetPushCidCallbacks(cid, cidErrMsg)); } -} +}); const onPushMessageCallbacks = []; // 不使用 defineOnApi 实现,是因为 defineOnApi 依赖 UniServiceJSBridge ,该对象目前在小程序上未提供,故简单实现 const onPushMessage = (fn) => { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index dd68ec77cbcd45d84e4db9cc7892c7ad283d20d2..585d63738d4beab283a9bff7e21a99b5702b11f7 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5178,7 +5178,7 @@ packages: resolution: {integrity: sha512-9ByhssR2fPVsNZj478qUUbKfmL0+t5BDVyjShtyZZLiK7ZDAArFFfopyOTj0M05wE2tJPisA4iTnnXl2YoPvOA==} /graceful-fs/4.2.9: - resolution: {integrity: sha512-NtNxqUcXgpW2iMrfqSfR73Glt39K+BLwWsPs94yR63v45T0Wbej7eRmL5cWfwEgqXnmjQp3zaJTshdRW/qC2ZQ==, registry: https://registry.yarnpkg.com/} + resolution: {integrity: sha512-NtNxqUcXgpW2iMrfqSfR73Glt39K+BLwWsPs94yR63v45T0Wbej7eRmL5cWfwEgqXnmjQp3zaJTshdRW/qC2ZQ==, registry: https://registry.yarnpkg.com/, tarball: https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.9.tgz} dev: true /has-flag/3.0.0: