diff --git a/packages/uni-api/src/helpers/api/index.ts b/packages/uni-api/src/helpers/api/index.ts index be472e01064368c53cdf785a1cb58519304f932e..2074eb1aa6132f70e1b9a8a8bee6a6992cfb3711 100644 --- a/packages/uni-api/src/helpers/api/index.ts +++ b/packages/uni-api/src/helpers/api/index.ts @@ -11,20 +11,6 @@ import { removeKeepAliveApiCallback, } from './callback' import { promisify } from './promise' - -export const API_TYPE_ON = 0 -export const API_TYPE_OFF = 1 -export const API_TYPE_TASK = 2 -export const API_TYPE_SYNC = 3 -export const API_TYPE_ASYNC = 4 - -type API_TYPES = - | typeof API_TYPE_ON - | typeof API_TYPE_OFF - | typeof API_TYPE_TASK - | typeof API_TYPE_SYNC - | typeof API_TYPE_ASYNC - interface AsyncMethodOptionLike { success?: (...args: any[]) => void } @@ -131,17 +117,8 @@ function wrapperSyncApi(fn: Function) { return (...args: any[]) => fn.apply(null, args) } -function wrapperAsyncApi( - name: string, - fn: (args: unknown) => Promise, - options?: ApiOptions -) { - return (args: Record) => { - const id = createAsyncApiCallback(name, args, options) - fn(args) - .then((res) => invokeSuccess(id, name, res)) - .catch((err) => invokeFail(id, name, err)) - } +function wrapperAsyncApi(name: string, fn: Function, options?: ApiOptions) { + return wrapperTaskApi(name, fn, options) } function wrapperApi( @@ -166,10 +143,9 @@ export function defineOnApi( fn: () => void, options?: ApiOptions ) { - return (defineApi( - API_TYPE_ON, + return (wrapperApi( + wrapperOnApi(name, fn), name, - fn, __DEV__ ? API_TYPE_ON_PROTOCOLS : undefined, options ) as unknown) as T @@ -180,10 +156,9 @@ export function defineOffApi( fn: () => void, options?: ApiOptions ) { - return (defineApi( - API_TYPE_OFF, + return (wrapperApi( + wrapperOffApi(name, fn), name, - fn, __DEV__ ? API_TYPE_ON_PROTOCOLS : undefined, options ) as unknown) as T @@ -194,7 +169,7 @@ export function defineTaskApi>( fn: ( args: Omit, res: { - resolve: (res: AsyncApiRes

) => void + resolve: (res?: AsyncApiRes

) => void reject: (err?: string) => void } ) => ReturnType, @@ -202,7 +177,12 @@ export function defineTaskApi>( options?: ApiOptions ) { return (promisify( - defineApi(API_TYPE_TASK, name, fn, __DEV__ ? protocol : undefined, options) + wrapperApi( + wrapperTaskApi(name, fn), + name, + __DEV__ ? protocol : undefined, + options + ) ) as unknown) as T } @@ -212,10 +192,9 @@ export function defineSyncApi( protocol?: ApiProtocols, options?: ApiOptions ) { - return (defineApi( - API_TYPE_SYNC, + return (wrapperApi( + wrapperSyncApi(fn), name, - fn, __DEV__ ? protocol : undefined, options ) as unknown) as T @@ -224,38 +203,21 @@ export function defineSyncApi( export function defineAsyncApi>( name: string, fn: ( - args: Omit - ) => Promise | void>, + args: Omit, + res: { + resolve: (res?: AsyncApiRes

) => void + reject: (err?: string) => void + } + ) => void, protocol?: ApiProtocols, options?: ApiOptions ) { return promisify( - defineApi(API_TYPE_ASYNC, name, fn, __DEV__ ? protocol : undefined, options) + wrapperApi( + wrapperAsyncApi(name, fn as any, options), + name, + __DEV__ ? protocol : undefined, + options + ) ) as AsyncApi

} - -function defineApi( - type: API_TYPES, - name: string, - fn: Function, - protocol?: ApiProtocols, - options?: ApiOptions -) { - switch (type) { - case API_TYPE_ON: - return wrapperApi(wrapperOnApi(name, fn), name, protocol, options) - case API_TYPE_OFF: - return wrapperApi(wrapperOffApi(name, fn), name, protocol, options) - case API_TYPE_TASK: - return wrapperApi(wrapperTaskApi(name, fn), name, protocol, options) - case API_TYPE_SYNC: - return wrapperApi(wrapperSyncApi(fn), name, protocol, options) - case API_TYPE_ASYNC: - return wrapperApi( - wrapperAsyncApi(name, fn as any, options), - name, - protocol, - options - ) - } -} diff --git a/packages/uni-h5/dist/uni-h5.esm.js b/packages/uni-h5/dist/uni-h5.esm.js index 613084e61ffa141731fe1d2ef2cfcedc97f9b0b6..a0a0e023d4b48173f6309c54116a5125df9f599a 100644 --- a/packages/uni-h5/dist/uni-h5.esm.js +++ b/packages/uni-h5/dist/uni-h5.esm.js @@ -7779,11 +7779,6 @@ function promisify(fn) { })); }; } -const API_TYPE_ON = 0; -const API_TYPE_OFF = 1; -const API_TYPE_TASK = 2; -const API_TYPE_SYNC = 3; -const API_TYPE_ASYNC = 4; function formatApiArgs(args, options) { const params = args[0]; if (!options || !isPlainObject(options.formatArgs) && isPlainObject(params)) { @@ -7835,10 +7830,7 @@ function wrapperSyncApi(fn) { return (...args) => fn.apply(null, args); } function wrapperAsyncApi(name, fn, options) { - return (args) => { - const id2 = createAsyncApiCallback(name, args, options); - fn(args).then((res) => invokeSuccess(id2, name, res)).catch((err) => invokeFail(id2, name, err)); - }; + return wrapperTaskApi(name, fn, options); } function wrapperApi(fn, name, protocol, options) { return function(...args) { @@ -7852,33 +7844,19 @@ function wrapperApi(fn, name, protocol, options) { }; } function defineOnApi(name, fn, options) { - return defineApi(API_TYPE_ON, name, fn, process.env.NODE_ENV !== "production" ? API_TYPE_ON_PROTOCOLS : void 0, options); + return wrapperApi(wrapperOnApi(name, fn), name, process.env.NODE_ENV !== "production" ? API_TYPE_ON_PROTOCOLS : void 0, options); } function defineOffApi(name, fn, options) { - return defineApi(API_TYPE_OFF, name, fn, process.env.NODE_ENV !== "production" ? API_TYPE_ON_PROTOCOLS : void 0, options); + return wrapperApi(wrapperOffApi(name, fn), name, process.env.NODE_ENV !== "production" ? API_TYPE_ON_PROTOCOLS : void 0, options); } function defineTaskApi(name, fn, protocol, options) { - return promisify(defineApi(API_TYPE_TASK, name, fn, process.env.NODE_ENV !== "production" ? protocol : void 0, options)); + return promisify(wrapperApi(wrapperTaskApi(name, fn), name, process.env.NODE_ENV !== "production" ? protocol : void 0, options)); } function defineSyncApi(name, fn, protocol, options) { - return defineApi(API_TYPE_SYNC, name, fn, process.env.NODE_ENV !== "production" ? protocol : void 0, options); + return wrapperApi(wrapperSyncApi(fn), name, process.env.NODE_ENV !== "production" ? protocol : void 0, options); } function defineAsyncApi(name, fn, protocol, options) { - return promisify(defineApi(API_TYPE_ASYNC, name, fn, process.env.NODE_ENV !== "production" ? protocol : void 0, options)); -} -function defineApi(type, name, fn, protocol, options) { - switch (type) { - case API_TYPE_ON: - return wrapperApi(wrapperOnApi(name, fn), name, protocol, options); - case API_TYPE_OFF: - return wrapperApi(wrapperOffApi(name, fn), name, protocol, options); - case API_TYPE_TASK: - return wrapperApi(wrapperTaskApi(name, fn), name, protocol, options); - case API_TYPE_SYNC: - return wrapperApi(wrapperSyncApi(fn), name, protocol, options); - case API_TYPE_ASYNC: - return wrapperApi(wrapperAsyncApi(name, fn, options), name, protocol, options); - } + return promisify(wrapperApi(wrapperAsyncApi(name, fn, options), name, process.env.NODE_ENV !== "production" ? protocol : void 0, options)); } const API_BASE64_TO_ARRAY_BUFFER = "base64ToArrayBuffer"; const API_ARRAY_BUFFER_TO_BASE64 = "arrayBufferToBase64"; @@ -8419,9 +8397,9 @@ const canIUse = defineSyncApi(API_CAN_I_USE, (schema) => { } return true; }, CanIUseProtocol); -const makePhoneCall = defineAsyncApi(API_MAKE_PHONE_CALL, ({phoneNumber}) => { +const makePhoneCall = defineAsyncApi(API_MAKE_PHONE_CALL, ({phoneNumber}, {resolve}) => { window.location.href = `tel:${phoneNumber}`; - return Promise.resolve(); + return resolve(); }, MakePhoneCallProtocol); const getSystemInfoSync = defineSyncApi("getSystemInfoSync", () => { const pixelRatio2 = window.devicePixelRatio; @@ -8523,8 +8501,8 @@ const getSystemInfoSync = defineSyncApi("getSystemInfoSync", () => { } }; }); -const getSystemInfo = defineAsyncApi("getSystemInfo", () => { - return Promise.resolve(getSystemInfoSync()); +const getSystemInfo = defineAsyncApi("getSystemInfo", (_args, {resolve}) => { + return resolve(getSystemInfoSync()); }); const API_ON_NETWORK_STATUS_CHANGE = "onNetworkStatusChange"; function networkListener() { @@ -8556,7 +8534,7 @@ const offNetworkStatusChange = defineOffApi("offNetworkStatusChange", () => { window.removeEventListener("online", networkListener); } }); -const getNetworkType = defineAsyncApi("getNetworkType", () => { +const getNetworkType = defineAsyncApi("getNetworkType", (_args, {resolve}) => { const connection = getConnection(); let networkType = "unknown"; if (connection) { @@ -8569,30 +8547,28 @@ const getNetworkType = defineAsyncApi("getNetworkType", () => { } else if (navigator.onLine === false) { networkType = "none"; } - return Promise.resolve({networkType}); + return resolve({networkType}); }); -const openDocument = defineAsyncApi(API_OPEN_DOCUMENT, ({filePath}) => { +const openDocument = defineAsyncApi(API_OPEN_DOCUMENT, ({filePath}, {resolve}) => { window.open(filePath); - return Promise.resolve(); + return resolve(); }, OpenDocumentProtocol); function _getServiceAddress() { return window.location.protocol + "//" + window.location.host; } -const getImageInfo = defineAsyncApi(API_GET_IMAGE_INFO, ({src}) => { +const getImageInfo = defineAsyncApi(API_GET_IMAGE_INFO, ({src}, {resolve, reject}) => { const img = new Image(); - return new Promise((resolve, reject) => { - img.onload = function() { - resolve({ - width: img.naturalWidth, - height: img.naturalHeight, - path: src.indexOf("/") === 0 ? _getServiceAddress() + src : src - }); - }; - img.onerror = function() { - reject(); - }; - img.src = src; - }); + img.onload = function() { + resolve({ + width: img.naturalWidth, + height: img.naturalHeight, + path: src.indexOf("/") === 0 ? _getServiceAddress() + src : src + }); + }; + img.onerror = function() { + reject(); + }; + img.src = src; }, GetImageInfoProtocol, GetImageInfoOptions); const request = defineTaskApi(API_REQUEST, ({ url, @@ -8713,7 +8689,7 @@ function parseHeaders(headers) { }); return headersObject; } -const navigateBack = defineAsyncApi(API_NAVIGATE_BACK, ({delta}) => new Promise((resolve, reject) => { +const navigateBack = defineAsyncApi(API_NAVIGATE_BACK, ({delta}, {resolve, reject}) => { let canBack = true; const vm = getCurrentPageVm(); if (vm && vm.$callHook("onBackPress") === true) { @@ -8723,8 +8699,8 @@ const navigateBack = defineAsyncApi(API_NAVIGATE_BACK, ({delta}) => new Promise( return reject("onBackPress"); } getApp().$router.go(-delta); - resolve(); -}), NavigateBackProtocol, NavigateBackOptions); + return resolve(); +}, NavigateBackProtocol, NavigateBackOptions); function navigate(type, url) { const router = getApp().$router; return new Promise((resolve, reject) => { @@ -8740,10 +8716,10 @@ function navigate(type, url) { }); }); } -const navigateTo = defineAsyncApi(API_NAVIGATE_TO, ({url}) => navigate(API_NAVIGATE_TO, url), NavigateToProtocol, NavigateToOptions); -const redirectTo = defineAsyncApi(API_REDIRECT_TO, ({url}) => navigate(API_REDIRECT_TO, url), RedirectToProtocol, RedirectToOptions); -const reLaunch = defineAsyncApi(API_RE_LAUNCH, ({url}) => navigate(API_RE_LAUNCH, url), ReLaunchProtocol, ReLaunchOptions); -const switchTab = defineAsyncApi(API_SWITCH_TAB, ({url}) => navigate(API_SWITCH_TAB, url), SwitchTabProtocol, SwitchTabOptions); +const navigateTo = defineAsyncApi(API_NAVIGATE_TO, ({url}, {resolve, reject}) => navigate(API_NAVIGATE_TO, url).then(resolve).catch(reject), NavigateToProtocol, NavigateToOptions); +const redirectTo = defineAsyncApi(API_REDIRECT_TO, ({url}, {resolve, reject}) => navigate(API_REDIRECT_TO, url).then(resolve).catch(reject), RedirectToProtocol, RedirectToOptions); +const reLaunch = defineAsyncApi(API_RE_LAUNCH, ({url}, {resolve, reject}) => navigate(API_RE_LAUNCH, url).then(resolve).catch(reject), ReLaunchProtocol, ReLaunchOptions); +const switchTab = defineAsyncApi(API_SWITCH_TAB, ({url}, {resolve, reject}) => navigate(API_SWITCH_TAB, url).then(resolve).catch(reject), SwitchTabProtocol, SwitchTabOptions); var api = /* @__PURE__ */ Object.freeze({ __proto__: null, [Symbol.toStringTag]: "Module", diff --git a/packages/uni-h5/src/service/api/device/getSystemInfo.ts b/packages/uni-h5/src/service/api/device/getSystemInfo.ts index 54534df20ff10978f4faabe5ba9bdeb84f501731..74ab7d53d8e6060dacc338145d4d49eb7940cf7d 100644 --- a/packages/uni-h5/src/service/api/device/getSystemInfo.ts +++ b/packages/uni-h5/src/service/api/device/getSystemInfo.ts @@ -4,7 +4,7 @@ import { getSystemInfoSync } from './getSystemInfoSync' export const getSystemInfo = defineAsyncApi( 'getSystemInfo', - () => { - return Promise.resolve(getSystemInfoSync()) + (_args, { resolve }) => { + return resolve(getSystemInfoSync()) } ) diff --git a/packages/uni-h5/src/service/api/device/makePhoneCall.ts b/packages/uni-h5/src/service/api/device/makePhoneCall.ts index f63866cf6cbb817a351017c541d0b5de3cd77b49..b21a1774e9903ea1883e320a39b16f192199e686 100644 --- a/packages/uni-h5/src/service/api/device/makePhoneCall.ts +++ b/packages/uni-h5/src/service/api/device/makePhoneCall.ts @@ -6,9 +6,9 @@ import { export const makePhoneCall = defineAsyncApi( API_MAKE_PHONE_CALL, - ({ phoneNumber }) => { + ({ phoneNumber }, { resolve }) => { window.location.href = `tel:${phoneNumber}` - return Promise.resolve() + return resolve() }, MakePhoneCallProtocol ) diff --git a/packages/uni-h5/src/service/api/device/network.ts b/packages/uni-h5/src/service/api/device/network.ts index 6dd0c4459d257769d8305994287afbd339b714f3..acf0e014b370889fa61bfe3177ccb3829ee73cbf 100644 --- a/packages/uni-h5/src/service/api/device/network.ts +++ b/packages/uni-h5/src/service/api/device/network.ts @@ -51,7 +51,7 @@ export const offNetworkStatusChange = defineOffApi< export const getNetworkType = defineAsyncApi( 'getNetworkType', - () => { + (_args, { resolve }) => { const connection = getConnection() let networkType = 'unknown' if (connection) { @@ -64,6 +64,6 @@ export const getNetworkType = defineAsyncApi( } else if (navigator.onLine === false) { networkType = 'none' } - return Promise.resolve({ networkType }) + return resolve({ networkType }) } ) diff --git a/packages/uni-h5/src/service/api/file/openDocument.ts b/packages/uni-h5/src/service/api/file/openDocument.ts index 31fde7232a4796bc7d8daf7660677e3b55d0f2c1..198f6039f5bd6ac66b0ef17e7d9e5730efa115e4 100644 --- a/packages/uni-h5/src/service/api/file/openDocument.ts +++ b/packages/uni-h5/src/service/api/file/openDocument.ts @@ -6,9 +6,9 @@ import { export const openDocument = defineAsyncApi( API_OPEN_DOCUMENT, - ({ filePath }) => { + ({ filePath }, { resolve }) => { window.open(filePath) - return Promise.resolve() + return resolve() }, OpenDocumentProtocol ) diff --git a/packages/uni-h5/src/service/api/media/getImageInfo.ts b/packages/uni-h5/src/service/api/media/getImageInfo.ts index c9c865a474fcc669543e62ce963de5071661c662..5d6d69a044b686a531497e19b12549b1345f59fc 100644 --- a/packages/uni-h5/src/service/api/media/getImageInfo.ts +++ b/packages/uni-h5/src/service/api/media/getImageInfo.ts @@ -11,21 +11,19 @@ function _getServiceAddress() { export const getImageInfo = defineAsyncApi( API_GET_IMAGE_INFO, - ({ src }) => { + ({ src }, { resolve, reject }) => { const img = new Image() - return new Promise((resolve, reject) => { - img.onload = function () { - resolve({ - width: img.naturalWidth, - height: img.naturalHeight, - path: src.indexOf('/') === 0 ? _getServiceAddress() + src : src, - } as UniApp.GetImageInfoSuccessData) // orientation和type是可选的,但GetImageInfoSuccessData定义的不对,暂时强制转换 - } - img.onerror = function () { - reject() - } - img.src = src - }) + img.onload = function () { + resolve({ + width: img.naturalWidth, + height: img.naturalHeight, + path: src.indexOf('/') === 0 ? _getServiceAddress() + src : src, + } as UniApp.GetImageInfoSuccessData) // orientation和type是可选的,但GetImageInfoSuccessData定义的不对,暂时强制转换 + } + img.onerror = function () { + reject() + } + img.src = src }, GetImageInfoProtocol, GetImageInfoOptions diff --git a/packages/uni-h5/src/service/api/route/navigateBack.ts b/packages/uni-h5/src/service/api/route/navigateBack.ts index dfdea282565d710cf9e760709f42abb7c781534c..fea9622a0f26f807e2f9b5c946987f23259ec4d0 100644 --- a/packages/uni-h5/src/service/api/route/navigateBack.ts +++ b/packages/uni-h5/src/service/api/route/navigateBack.ts @@ -8,19 +8,18 @@ import { export const navigateBack = defineAsyncApi( API_NAVIGATE_BACK, - ({ delta }) => - new Promise((resolve, reject) => { - let canBack = true - const vm = getCurrentPageVm() - if (vm && vm.$callHook('onBackPress') === true) { - canBack = false - } - if (!canBack) { - return reject('onBackPress') - } - getApp().$router.go(-delta!) - resolve() - }), + ({ delta }, { resolve, reject }) => { + let canBack = true + const vm = getCurrentPageVm() + if (vm && vm.$callHook('onBackPress') === true) { + canBack = false + } + if (!canBack) { + return reject('onBackPress') + } + getApp().$router.go(-delta!) + return resolve() + }, NavigateBackProtocol, NavigateBackOptions ) diff --git a/packages/uni-h5/src/service/api/route/navigateTo.ts b/packages/uni-h5/src/service/api/route/navigateTo.ts index f87d9e4408ef64d99c89aff60ce011f54ca049f2..9ad35c9ab69f0b597ba3fc8a2246971c412c862b 100644 --- a/packages/uni-h5/src/service/api/route/navigateTo.ts +++ b/packages/uni-h5/src/service/api/route/navigateTo.ts @@ -8,7 +8,8 @@ import { navigate } from './utils' export const navigateTo = defineAsyncApi( API_NAVIGATE_TO, - ({ url }) => navigate(API_NAVIGATE_TO, url), + ({ url }, { resolve, reject }) => + navigate(API_NAVIGATE_TO, url).then(resolve).catch(reject), NavigateToProtocol, NavigateToOptions ) diff --git a/packages/uni-h5/src/service/api/route/reLaunch.ts b/packages/uni-h5/src/service/api/route/reLaunch.ts index 984c07663a41a2c6f72f47bd7407558a0df6dbf2..b3060af239539ea6663918329b9795c400c3f578 100644 --- a/packages/uni-h5/src/service/api/route/reLaunch.ts +++ b/packages/uni-h5/src/service/api/route/reLaunch.ts @@ -8,7 +8,8 @@ import { navigate } from './utils' export const reLaunch = defineAsyncApi( API_RE_LAUNCH, - ({ url }) => navigate(API_RE_LAUNCH, url), + ({ url }, { resolve, reject }) => + navigate(API_RE_LAUNCH, url).then(resolve).catch(reject), ReLaunchProtocol, ReLaunchOptions ) diff --git a/packages/uni-h5/src/service/api/route/redirectTo.ts b/packages/uni-h5/src/service/api/route/redirectTo.ts index 5490edd46e0784a97e347dee1a18f4828de4237f..78e34fee162009ddc6cee13c603f90f9ab24c92e 100644 --- a/packages/uni-h5/src/service/api/route/redirectTo.ts +++ b/packages/uni-h5/src/service/api/route/redirectTo.ts @@ -8,7 +8,8 @@ import { navigate } from './utils' export const redirectTo = defineAsyncApi( API_REDIRECT_TO, - ({ url }) => navigate(API_REDIRECT_TO, url), + ({ url }, { resolve, reject }) => + navigate(API_REDIRECT_TO, url).then(resolve).catch(reject), RedirectToProtocol, RedirectToOptions ) diff --git a/packages/uni-h5/src/service/api/route/switchTab.ts b/packages/uni-h5/src/service/api/route/switchTab.ts index 708550d9d6aec9d4d2bababe8010b50ca46e3829..86dcace0f52cd8756225e6b995f9edb02ae10127 100644 --- a/packages/uni-h5/src/service/api/route/switchTab.ts +++ b/packages/uni-h5/src/service/api/route/switchTab.ts @@ -8,7 +8,8 @@ import { navigate } from './utils' export const switchTab = defineAsyncApi( API_SWITCH_TAB, - ({ url }) => navigate(API_SWITCH_TAB, url), + ({ url }, { resolve, reject }) => + navigate(API_SWITCH_TAB, url).then(resolve).catch(reject), SwitchTabProtocol, SwitchTabOptions ) diff --git a/packages/uni-mp-alipay/dist/uni.api.esm.js b/packages/uni-mp-alipay/dist/uni.api.esm.js index 22ee4c3b4d26f05a1bc9f7ab28aafcec8b349642..11dc6352db18d8a4e5ab46b1b00fbb2377f5c043 100644 --- a/packages/uni-mp-alipay/dist/uni.api.esm.js +++ b/packages/uni-mp-alipay/dist/uni.api.esm.js @@ -1,4 +1,4 @@ -import { isArray, hasOwn, isObject, capitalize, toRawType, makeMap, isPlainObject, isFunction, extend, isPromise, isString } from '@vue/shared'; +import { isArray, hasOwn, isObject, capitalize, toRawType, makeMap, isPlainObject, isPromise, isFunction, isString } from '@vue/shared'; function validateProtocolFail(name, msg) { const errMsg = `${name}:fail ${msg}`; @@ -140,115 +140,6 @@ function isBoolean(...args) { return args.some((elem) => elem.toLowerCase() === 'boolean'); } -function tryCatch(fn) { - return function () { - try { - return fn.apply(fn, arguments); - } - catch (e) { - // TODO - console.error(e); - } - }; -} - -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; -} -function findInvokeCallbackByName(name) { - for (const key in invokeCallbacks) { - if (invokeCallbacks[key].name === name) { - return true; - } - } - return false; -} -function removeKeepAliveApiCallback(name, callback) { - for (const key in invokeCallbacks) { - const item = invokeCallbacks[key]; - if (item.callback === callback && item.name === name) { - delete invokeCallbacks[key]; - } - } -} -function offKeepAliveApiCallback(name) { - UniServiceJSBridge.off('api.' + name); -} -function onKeepAliveApiCallback(name) { - UniServiceJSBridge.on('api.' + name, (res) => { - for (const key in invokeCallbacks) { - const opts = invokeCallbacks[key]; - if (opts.name === name) { - opts.callback(res); - } - } - }); -} -function createKeepAliveApiCallback(name, callback) { - return addInvokeCallback(invokeCallbackId++, name, callback, true); -} -function getApiCallbacks(args) { - const apiCallbacks = {}; - for (const name in args) { - const fn = args[name]; - if (isFunction(fn)) { - apiCallbacks[name] = tryCatch(fn); - delete args[name]; - } - } - return apiCallbacks; -} -function normalizeErrMsg(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(res.errMsg, name); - isFunction(beforeAll) && beforeAll(res); - if (res.errMsg === name + ':ok') { - isFunction(beforeSuccess) && beforeSuccess(res); - hasSuccess && success(res); - } - else { - hasFail && fail(res); - } - hasComplete && complete(res); - }); - return callbackId; -} - function handlePromise(promise) { if (__UNI_FEATURE_PROMISE__) { return promise @@ -260,11 +151,6 @@ function handlePromise(promise) { return promise; } -const API_TYPE_ON = 0; -const API_TYPE_OFF = 1; -const API_TYPE_TASK = 2; -const API_TYPE_SYNC = 3; -const API_TYPE_ASYNC = 4; function formatApiArgs(args, options) { const params = args[0]; if (!options || @@ -277,55 +163,9 @@ function formatApiArgs(args, options) { }); return args; } -function wrapperOnApi(name, fn) { - return (callback) => { - // 是否是首次调用on,如果是首次,需要初始化onMethod监听 - const isFirstInvokeOnApi = !findInvokeCallbackByName(name); - createKeepAliveApiCallback(name, callback); - if (isFirstInvokeOnApi) { - onKeepAliveApiCallback(name); - fn(); - } - }; -} -function wrapperOffApi(name, fn) { - return (callback) => { - name = name.replace('off', 'on'); - removeKeepAliveApiCallback(name, callback); - // 是否还存在监听,若已不存在,则移除onMethod监听 - const hasInvokeOnApi = findInvokeCallbackByName(name); - if (!hasInvokeOnApi) { - offKeepAliveApiCallback(name); - fn(); - } - }; -} -function invokeSuccess(id, name, res) { - return invokeCallback(id, extend(res || {}, { errMsg: name + ':ok' })); -} -function invokeFail(id, name, err) { - return invokeCallback(id, { errMsg: name + ':fail' + (err ? ' ' + err : '') }); -} -function wrapperTaskApi(name, fn, options) { - return (args) => { - const id = createAsyncApiCallback(name, args, options); - return fn(args, { - resolve: (res) => invokeSuccess(id, name, res), - reject: (err) => invokeFail(id, name, err), - }); - }; -} function wrapperSyncApi(fn) { return (...args) => fn.apply(null, args); } -function wrapperAsyncApi(name, fn, options) { - return (args) => { - const id = createAsyncApiCallback(name, args, options); - fn(args) - .then((res) => invokeSuccess(id, name, res)) - .catch((err) => invokeFail(id, name, err)); - }; -} function wrapperApi(fn, name, protocol, options) { return function (...args) { if ((process.env.NODE_ENV !== 'production')) { @@ -338,21 +178,7 @@ function wrapperApi(fn, name, protocol, options) { }; } function defineSyncApi(name, fn, protocol, options) { - return defineApi(API_TYPE_SYNC, name, fn, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options); -} -function defineApi(type, name, fn, protocol, options) { - switch (type) { - case API_TYPE_ON: - return wrapperApi(wrapperOnApi(name, fn), name, protocol, options); - case API_TYPE_OFF: - return wrapperApi(wrapperOffApi(name, fn), name, protocol, options); - case API_TYPE_TASK: - return wrapperApi(wrapperTaskApi(name, fn), name, protocol, options); - case API_TYPE_SYNC: - return wrapperApi(wrapperSyncApi(fn), name, protocol, options); - case API_TYPE_ASYNC: - return wrapperApi(wrapperAsyncApi(name, fn, options), name, protocol, options); - } + return wrapperApi(wrapperSyncApi(fn), name, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options); } function getBaseSystemInfo() { diff --git a/packages/uni-mp-baidu/dist/uni.api.esm.js b/packages/uni-mp-baidu/dist/uni.api.esm.js index 2e427c83b6172bfcd4cfdfc03ecf4273d0e60670..67b68706aba7af076273ede2716d4db076dfd408 100644 --- a/packages/uni-mp-baidu/dist/uni.api.esm.js +++ b/packages/uni-mp-baidu/dist/uni.api.esm.js @@ -1,4 +1,4 @@ -import { isArray, hasOwn, isObject, capitalize, toRawType, makeMap, isPlainObject, isFunction, extend, isPromise, isString } from '@vue/shared'; +import { isArray, hasOwn, isObject, capitalize, toRawType, makeMap, isPlainObject, isPromise, isFunction, isString } from '@vue/shared'; function validateProtocolFail(name, msg) { const errMsg = `${name}:fail ${msg}`; @@ -140,115 +140,6 @@ function isBoolean(...args) { return args.some((elem) => elem.toLowerCase() === 'boolean'); } -function tryCatch(fn) { - return function () { - try { - return fn.apply(fn, arguments); - } - catch (e) { - // TODO - console.error(e); - } - }; -} - -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; -} -function findInvokeCallbackByName(name) { - for (const key in invokeCallbacks) { - if (invokeCallbacks[key].name === name) { - return true; - } - } - return false; -} -function removeKeepAliveApiCallback(name, callback) { - for (const key in invokeCallbacks) { - const item = invokeCallbacks[key]; - if (item.callback === callback && item.name === name) { - delete invokeCallbacks[key]; - } - } -} -function offKeepAliveApiCallback(name) { - UniServiceJSBridge.off('api.' + name); -} -function onKeepAliveApiCallback(name) { - UniServiceJSBridge.on('api.' + name, (res) => { - for (const key in invokeCallbacks) { - const opts = invokeCallbacks[key]; - if (opts.name === name) { - opts.callback(res); - } - } - }); -} -function createKeepAliveApiCallback(name, callback) { - return addInvokeCallback(invokeCallbackId++, name, callback, true); -} -function getApiCallbacks(args) { - const apiCallbacks = {}; - for (const name in args) { - const fn = args[name]; - if (isFunction(fn)) { - apiCallbacks[name] = tryCatch(fn); - delete args[name]; - } - } - return apiCallbacks; -} -function normalizeErrMsg(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(res.errMsg, name); - isFunction(beforeAll) && beforeAll(res); - if (res.errMsg === name + ':ok') { - isFunction(beforeSuccess) && beforeSuccess(res); - hasSuccess && success(res); - } - else { - hasFail && fail(res); - } - hasComplete && complete(res); - }); - return callbackId; -} - function handlePromise(promise) { if (__UNI_FEATURE_PROMISE__) { return promise @@ -260,11 +151,6 @@ function handlePromise(promise) { return promise; } -const API_TYPE_ON = 0; -const API_TYPE_OFF = 1; -const API_TYPE_TASK = 2; -const API_TYPE_SYNC = 3; -const API_TYPE_ASYNC = 4; function formatApiArgs(args, options) { const params = args[0]; if (!options || @@ -277,55 +163,9 @@ function formatApiArgs(args, options) { }); return args; } -function wrapperOnApi(name, fn) { - return (callback) => { - // 是否是首次调用on,如果是首次,需要初始化onMethod监听 - const isFirstInvokeOnApi = !findInvokeCallbackByName(name); - createKeepAliveApiCallback(name, callback); - if (isFirstInvokeOnApi) { - onKeepAliveApiCallback(name); - fn(); - } - }; -} -function wrapperOffApi(name, fn) { - return (callback) => { - name = name.replace('off', 'on'); - removeKeepAliveApiCallback(name, callback); - // 是否还存在监听,若已不存在,则移除onMethod监听 - const hasInvokeOnApi = findInvokeCallbackByName(name); - if (!hasInvokeOnApi) { - offKeepAliveApiCallback(name); - fn(); - } - }; -} -function invokeSuccess(id, name, res) { - return invokeCallback(id, extend(res || {}, { errMsg: name + ':ok' })); -} -function invokeFail(id, name, err) { - return invokeCallback(id, { errMsg: name + ':fail' + (err ? ' ' + err : '') }); -} -function wrapperTaskApi(name, fn, options) { - return (args) => { - const id = createAsyncApiCallback(name, args, options); - return fn(args, { - resolve: (res) => invokeSuccess(id, name, res), - reject: (err) => invokeFail(id, name, err), - }); - }; -} function wrapperSyncApi(fn) { return (...args) => fn.apply(null, args); } -function wrapperAsyncApi(name, fn, options) { - return (args) => { - const id = createAsyncApiCallback(name, args, options); - fn(args) - .then((res) => invokeSuccess(id, name, res)) - .catch((err) => invokeFail(id, name, err)); - }; -} function wrapperApi(fn, name, protocol, options) { return function (...args) { if ((process.env.NODE_ENV !== 'production')) { @@ -338,21 +178,7 @@ function wrapperApi(fn, name, protocol, options) { }; } function defineSyncApi(name, fn, protocol, options) { - return defineApi(API_TYPE_SYNC, name, fn, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options); -} -function defineApi(type, name, fn, protocol, options) { - switch (type) { - case API_TYPE_ON: - return wrapperApi(wrapperOnApi(name, fn), name, protocol, options); - case API_TYPE_OFF: - return wrapperApi(wrapperOffApi(name, fn), name, protocol, options); - case API_TYPE_TASK: - return wrapperApi(wrapperTaskApi(name, fn), name, protocol, options); - case API_TYPE_SYNC: - return wrapperApi(wrapperSyncApi(fn), name, protocol, options); - case API_TYPE_ASYNC: - return wrapperApi(wrapperAsyncApi(name, fn, options), name, protocol, options); - } + return wrapperApi(wrapperSyncApi(fn), name, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options); } function getBaseSystemInfo() { diff --git a/packages/uni-mp-qq/dist/uni.api.esm.js b/packages/uni-mp-qq/dist/uni.api.esm.js index b3b466c60dad952cb7dfeaf57b602ab13e3172e5..36f6b26ad1d83e3aefd3b0133a87c597595d4e88 100644 --- a/packages/uni-mp-qq/dist/uni.api.esm.js +++ b/packages/uni-mp-qq/dist/uni.api.esm.js @@ -1,4 +1,4 @@ -import { isArray, hasOwn, isObject, capitalize, toRawType, makeMap, isPlainObject, isFunction, extend, isPromise, isString } from '@vue/shared'; +import { isArray, hasOwn, isObject, capitalize, toRawType, makeMap, isPlainObject, isPromise, isFunction, isString } from '@vue/shared'; function validateProtocolFail(name, msg) { const errMsg = `${name}:fail ${msg}`; @@ -140,115 +140,6 @@ function isBoolean(...args) { return args.some((elem) => elem.toLowerCase() === 'boolean'); } -function tryCatch(fn) { - return function () { - try { - return fn.apply(fn, arguments); - } - catch (e) { - // TODO - console.error(e); - } - }; -} - -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; -} -function findInvokeCallbackByName(name) { - for (const key in invokeCallbacks) { - if (invokeCallbacks[key].name === name) { - return true; - } - } - return false; -} -function removeKeepAliveApiCallback(name, callback) { - for (const key in invokeCallbacks) { - const item = invokeCallbacks[key]; - if (item.callback === callback && item.name === name) { - delete invokeCallbacks[key]; - } - } -} -function offKeepAliveApiCallback(name) { - UniServiceJSBridge.off('api.' + name); -} -function onKeepAliveApiCallback(name) { - UniServiceJSBridge.on('api.' + name, (res) => { - for (const key in invokeCallbacks) { - const opts = invokeCallbacks[key]; - if (opts.name === name) { - opts.callback(res); - } - } - }); -} -function createKeepAliveApiCallback(name, callback) { - return addInvokeCallback(invokeCallbackId++, name, callback, true); -} -function getApiCallbacks(args) { - const apiCallbacks = {}; - for (const name in args) { - const fn = args[name]; - if (isFunction(fn)) { - apiCallbacks[name] = tryCatch(fn); - delete args[name]; - } - } - return apiCallbacks; -} -function normalizeErrMsg(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(res.errMsg, name); - isFunction(beforeAll) && beforeAll(res); - if (res.errMsg === name + ':ok') { - isFunction(beforeSuccess) && beforeSuccess(res); - hasSuccess && success(res); - } - else { - hasFail && fail(res); - } - hasComplete && complete(res); - }); - return callbackId; -} - function handlePromise(promise) { if (__UNI_FEATURE_PROMISE__) { return promise @@ -260,11 +151,6 @@ function handlePromise(promise) { return promise; } -const API_TYPE_ON = 0; -const API_TYPE_OFF = 1; -const API_TYPE_TASK = 2; -const API_TYPE_SYNC = 3; -const API_TYPE_ASYNC = 4; function formatApiArgs(args, options) { const params = args[0]; if (!options || @@ -277,55 +163,9 @@ function formatApiArgs(args, options) { }); return args; } -function wrapperOnApi(name, fn) { - return (callback) => { - // 是否是首次调用on,如果是首次,需要初始化onMethod监听 - const isFirstInvokeOnApi = !findInvokeCallbackByName(name); - createKeepAliveApiCallback(name, callback); - if (isFirstInvokeOnApi) { - onKeepAliveApiCallback(name); - fn(); - } - }; -} -function wrapperOffApi(name, fn) { - return (callback) => { - name = name.replace('off', 'on'); - removeKeepAliveApiCallback(name, callback); - // 是否还存在监听,若已不存在,则移除onMethod监听 - const hasInvokeOnApi = findInvokeCallbackByName(name); - if (!hasInvokeOnApi) { - offKeepAliveApiCallback(name); - fn(); - } - }; -} -function invokeSuccess(id, name, res) { - return invokeCallback(id, extend(res || {}, { errMsg: name + ':ok' })); -} -function invokeFail(id, name, err) { - return invokeCallback(id, { errMsg: name + ':fail' + (err ? ' ' + err : '') }); -} -function wrapperTaskApi(name, fn, options) { - return (args) => { - const id = createAsyncApiCallback(name, args, options); - return fn(args, { - resolve: (res) => invokeSuccess(id, name, res), - reject: (err) => invokeFail(id, name, err), - }); - }; -} function wrapperSyncApi(fn) { return (...args) => fn.apply(null, args); } -function wrapperAsyncApi(name, fn, options) { - return (args) => { - const id = createAsyncApiCallback(name, args, options); - fn(args) - .then((res) => invokeSuccess(id, name, res)) - .catch((err) => invokeFail(id, name, err)); - }; -} function wrapperApi(fn, name, protocol, options) { return function (...args) { if ((process.env.NODE_ENV !== 'production')) { @@ -338,21 +178,7 @@ function wrapperApi(fn, name, protocol, options) { }; } function defineSyncApi(name, fn, protocol, options) { - return defineApi(API_TYPE_SYNC, name, fn, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options); -} -function defineApi(type, name, fn, protocol, options) { - switch (type) { - case API_TYPE_ON: - return wrapperApi(wrapperOnApi(name, fn), name, protocol, options); - case API_TYPE_OFF: - return wrapperApi(wrapperOffApi(name, fn), name, protocol, options); - case API_TYPE_TASK: - return wrapperApi(wrapperTaskApi(name, fn), name, protocol, options); - case API_TYPE_SYNC: - return wrapperApi(wrapperSyncApi(fn), name, protocol, options); - case API_TYPE_ASYNC: - return wrapperApi(wrapperAsyncApi(name, fn, options), name, protocol, options); - } + return wrapperApi(wrapperSyncApi(fn), name, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options); } function getBaseSystemInfo() { diff --git a/packages/uni-mp-toutiao/dist/uni.api.esm.js b/packages/uni-mp-toutiao/dist/uni.api.esm.js index 69c791ffe7b360e0dfa321f4e34249a655b97626..eaa50cdbb2c86739006d99c752753dc6199ea9de 100644 --- a/packages/uni-mp-toutiao/dist/uni.api.esm.js +++ b/packages/uni-mp-toutiao/dist/uni.api.esm.js @@ -1,4 +1,4 @@ -import { isArray, hasOwn, isObject, capitalize, toRawType, makeMap, isPlainObject, isFunction, extend, isPromise, isString } from '@vue/shared'; +import { isArray, hasOwn, isObject, capitalize, toRawType, makeMap, isPlainObject, isPromise, isFunction, isString } from '@vue/shared'; function validateProtocolFail(name, msg) { const errMsg = `${name}:fail ${msg}`; @@ -140,115 +140,6 @@ function isBoolean(...args) { return args.some((elem) => elem.toLowerCase() === 'boolean'); } -function tryCatch(fn) { - return function () { - try { - return fn.apply(fn, arguments); - } - catch (e) { - // TODO - console.error(e); - } - }; -} - -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; -} -function findInvokeCallbackByName(name) { - for (const key in invokeCallbacks) { - if (invokeCallbacks[key].name === name) { - return true; - } - } - return false; -} -function removeKeepAliveApiCallback(name, callback) { - for (const key in invokeCallbacks) { - const item = invokeCallbacks[key]; - if (item.callback === callback && item.name === name) { - delete invokeCallbacks[key]; - } - } -} -function offKeepAliveApiCallback(name) { - UniServiceJSBridge.off('api.' + name); -} -function onKeepAliveApiCallback(name) { - UniServiceJSBridge.on('api.' + name, (res) => { - for (const key in invokeCallbacks) { - const opts = invokeCallbacks[key]; - if (opts.name === name) { - opts.callback(res); - } - } - }); -} -function createKeepAliveApiCallback(name, callback) { - return addInvokeCallback(invokeCallbackId++, name, callback, true); -} -function getApiCallbacks(args) { - const apiCallbacks = {}; - for (const name in args) { - const fn = args[name]; - if (isFunction(fn)) { - apiCallbacks[name] = tryCatch(fn); - delete args[name]; - } - } - return apiCallbacks; -} -function normalizeErrMsg(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(res.errMsg, name); - isFunction(beforeAll) && beforeAll(res); - if (res.errMsg === name + ':ok') { - isFunction(beforeSuccess) && beforeSuccess(res); - hasSuccess && success(res); - } - else { - hasFail && fail(res); - } - hasComplete && complete(res); - }); - return callbackId; -} - function handlePromise(promise) { if (__UNI_FEATURE_PROMISE__) { return promise @@ -260,11 +151,6 @@ function handlePromise(promise) { return promise; } -const API_TYPE_ON = 0; -const API_TYPE_OFF = 1; -const API_TYPE_TASK = 2; -const API_TYPE_SYNC = 3; -const API_TYPE_ASYNC = 4; function formatApiArgs(args, options) { const params = args[0]; if (!options || @@ -277,55 +163,9 @@ function formatApiArgs(args, options) { }); return args; } -function wrapperOnApi(name, fn) { - return (callback) => { - // 是否是首次调用on,如果是首次,需要初始化onMethod监听 - const isFirstInvokeOnApi = !findInvokeCallbackByName(name); - createKeepAliveApiCallback(name, callback); - if (isFirstInvokeOnApi) { - onKeepAliveApiCallback(name); - fn(); - } - }; -} -function wrapperOffApi(name, fn) { - return (callback) => { - name = name.replace('off', 'on'); - removeKeepAliveApiCallback(name, callback); - // 是否还存在监听,若已不存在,则移除onMethod监听 - const hasInvokeOnApi = findInvokeCallbackByName(name); - if (!hasInvokeOnApi) { - offKeepAliveApiCallback(name); - fn(); - } - }; -} -function invokeSuccess(id, name, res) { - return invokeCallback(id, extend(res || {}, { errMsg: name + ':ok' })); -} -function invokeFail(id, name, err) { - return invokeCallback(id, { errMsg: name + ':fail' + (err ? ' ' + err : '') }); -} -function wrapperTaskApi(name, fn, options) { - return (args) => { - const id = createAsyncApiCallback(name, args, options); - return fn(args, { - resolve: (res) => invokeSuccess(id, name, res), - reject: (err) => invokeFail(id, name, err), - }); - }; -} function wrapperSyncApi(fn) { return (...args) => fn.apply(null, args); } -function wrapperAsyncApi(name, fn, options) { - return (args) => { - const id = createAsyncApiCallback(name, args, options); - fn(args) - .then((res) => invokeSuccess(id, name, res)) - .catch((err) => invokeFail(id, name, err)); - }; -} function wrapperApi(fn, name, protocol, options) { return function (...args) { if ((process.env.NODE_ENV !== 'production')) { @@ -338,21 +178,7 @@ function wrapperApi(fn, name, protocol, options) { }; } function defineSyncApi(name, fn, protocol, options) { - return defineApi(API_TYPE_SYNC, name, fn, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options); -} -function defineApi(type, name, fn, protocol, options) { - switch (type) { - case API_TYPE_ON: - return wrapperApi(wrapperOnApi(name, fn), name, protocol, options); - case API_TYPE_OFF: - return wrapperApi(wrapperOffApi(name, fn), name, protocol, options); - case API_TYPE_TASK: - return wrapperApi(wrapperTaskApi(name, fn), name, protocol, options); - case API_TYPE_SYNC: - return wrapperApi(wrapperSyncApi(fn), name, protocol, options); - case API_TYPE_ASYNC: - return wrapperApi(wrapperAsyncApi(name, fn, options), name, protocol, options); - } + return wrapperApi(wrapperSyncApi(fn), name, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options); } function getBaseSystemInfo() { diff --git a/packages/uni-mp-weixin/dist/uni.api.esm.js b/packages/uni-mp-weixin/dist/uni.api.esm.js index 3955b9b0df95540551c7d7fb9aa4812d0491ff22..c357360c506b8ddd1540de953ba5daec8d2a4a45 100644 --- a/packages/uni-mp-weixin/dist/uni.api.esm.js +++ b/packages/uni-mp-weixin/dist/uni.api.esm.js @@ -1,4 +1,4 @@ -import { isArray, hasOwn, isObject, capitalize, toRawType, makeMap, isPlainObject, isFunction, extend, isPromise, isString } from '@vue/shared'; +import { isArray, hasOwn, isObject, capitalize, toRawType, makeMap, isPlainObject, isPromise, isFunction, isString } from '@vue/shared'; function validateProtocolFail(name, msg) { const errMsg = `${name}:fail ${msg}`; @@ -140,115 +140,6 @@ function isBoolean(...args) { return args.some((elem) => elem.toLowerCase() === 'boolean'); } -function tryCatch(fn) { - return function () { - try { - return fn.apply(fn, arguments); - } - catch (e) { - // TODO - console.error(e); - } - }; -} - -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; -} -function findInvokeCallbackByName(name) { - for (const key in invokeCallbacks) { - if (invokeCallbacks[key].name === name) { - return true; - } - } - return false; -} -function removeKeepAliveApiCallback(name, callback) { - for (const key in invokeCallbacks) { - const item = invokeCallbacks[key]; - if (item.callback === callback && item.name === name) { - delete invokeCallbacks[key]; - } - } -} -function offKeepAliveApiCallback(name) { - UniServiceJSBridge.off('api.' + name); -} -function onKeepAliveApiCallback(name) { - UniServiceJSBridge.on('api.' + name, (res) => { - for (const key in invokeCallbacks) { - const opts = invokeCallbacks[key]; - if (opts.name === name) { - opts.callback(res); - } - } - }); -} -function createKeepAliveApiCallback(name, callback) { - return addInvokeCallback(invokeCallbackId++, name, callback, true); -} -function getApiCallbacks(args) { - const apiCallbacks = {}; - for (const name in args) { - const fn = args[name]; - if (isFunction(fn)) { - apiCallbacks[name] = tryCatch(fn); - delete args[name]; - } - } - return apiCallbacks; -} -function normalizeErrMsg(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(res.errMsg, name); - isFunction(beforeAll) && beforeAll(res); - if (res.errMsg === name + ':ok') { - isFunction(beforeSuccess) && beforeSuccess(res); - hasSuccess && success(res); - } - else { - hasFail && fail(res); - } - hasComplete && complete(res); - }); - return callbackId; -} - function handlePromise(promise) { if (__UNI_FEATURE_PROMISE__) { return promise @@ -260,11 +151,6 @@ function handlePromise(promise) { return promise; } -const API_TYPE_ON = 0; -const API_TYPE_OFF = 1; -const API_TYPE_TASK = 2; -const API_TYPE_SYNC = 3; -const API_TYPE_ASYNC = 4; function formatApiArgs(args, options) { const params = args[0]; if (!options || @@ -277,55 +163,9 @@ function formatApiArgs(args, options) { }); return args; } -function wrapperOnApi(name, fn) { - return (callback) => { - // 是否是首次调用on,如果是首次,需要初始化onMethod监听 - const isFirstInvokeOnApi = !findInvokeCallbackByName(name); - createKeepAliveApiCallback(name, callback); - if (isFirstInvokeOnApi) { - onKeepAliveApiCallback(name); - fn(); - } - }; -} -function wrapperOffApi(name, fn) { - return (callback) => { - name = name.replace('off', 'on'); - removeKeepAliveApiCallback(name, callback); - // 是否还存在监听,若已不存在,则移除onMethod监听 - const hasInvokeOnApi = findInvokeCallbackByName(name); - if (!hasInvokeOnApi) { - offKeepAliveApiCallback(name); - fn(); - } - }; -} -function invokeSuccess(id, name, res) { - return invokeCallback(id, extend(res || {}, { errMsg: name + ':ok' })); -} -function invokeFail(id, name, err) { - return invokeCallback(id, { errMsg: name + ':fail' + (err ? ' ' + err : '') }); -} -function wrapperTaskApi(name, fn, options) { - return (args) => { - const id = createAsyncApiCallback(name, args, options); - return fn(args, { - resolve: (res) => invokeSuccess(id, name, res), - reject: (err) => invokeFail(id, name, err), - }); - }; -} function wrapperSyncApi(fn) { return (...args) => fn.apply(null, args); } -function wrapperAsyncApi(name, fn, options) { - return (args) => { - const id = createAsyncApiCallback(name, args, options); - fn(args) - .then((res) => invokeSuccess(id, name, res)) - .catch((err) => invokeFail(id, name, err)); - }; -} function wrapperApi(fn, name, protocol, options) { return function (...args) { if ((process.env.NODE_ENV !== 'production')) { @@ -338,21 +178,7 @@ function wrapperApi(fn, name, protocol, options) { }; } function defineSyncApi(name, fn, protocol, options) { - return defineApi(API_TYPE_SYNC, name, fn, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options); -} -function defineApi(type, name, fn, protocol, options) { - switch (type) { - case API_TYPE_ON: - return wrapperApi(wrapperOnApi(name, fn), name, protocol, options); - case API_TYPE_OFF: - return wrapperApi(wrapperOffApi(name, fn), name, protocol, options); - case API_TYPE_TASK: - return wrapperApi(wrapperTaskApi(name, fn), name, protocol, options); - case API_TYPE_SYNC: - return wrapperApi(wrapperSyncApi(fn), name, protocol, options); - case API_TYPE_ASYNC: - return wrapperApi(wrapperAsyncApi(name, fn, options), name, protocol, options); - } + return wrapperApi(wrapperSyncApi(fn), name, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options); } function getBaseSystemInfo() { diff --git a/packages/uni-quickapp-webview/dist/uni.api.esm.js b/packages/uni-quickapp-webview/dist/uni.api.esm.js index d2e8ef69be5265b1ed2511d0866684214aa9f8d9..5be717be220708be865e154a9761e38d2d11696b 100644 --- a/packages/uni-quickapp-webview/dist/uni.api.esm.js +++ b/packages/uni-quickapp-webview/dist/uni.api.esm.js @@ -1,4 +1,4 @@ -import { isArray, hasOwn, isObject, capitalize, toRawType, makeMap, isPlainObject, isFunction, extend, isPromise, isString } from '@vue/shared'; +import { isArray, hasOwn, isObject, capitalize, toRawType, makeMap, isPlainObject, isPromise, isFunction, isString } from '@vue/shared'; function validateProtocolFail(name, msg) { const errMsg = `${name}:fail ${msg}`; @@ -140,115 +140,6 @@ function isBoolean(...args) { return args.some((elem) => elem.toLowerCase() === 'boolean'); } -function tryCatch(fn) { - return function () { - try { - return fn.apply(fn, arguments); - } - catch (e) { - // TODO - console.error(e); - } - }; -} - -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; -} -function findInvokeCallbackByName(name) { - for (const key in invokeCallbacks) { - if (invokeCallbacks[key].name === name) { - return true; - } - } - return false; -} -function removeKeepAliveApiCallback(name, callback) { - for (const key in invokeCallbacks) { - const item = invokeCallbacks[key]; - if (item.callback === callback && item.name === name) { - delete invokeCallbacks[key]; - } - } -} -function offKeepAliveApiCallback(name) { - UniServiceJSBridge.off('api.' + name); -} -function onKeepAliveApiCallback(name) { - UniServiceJSBridge.on('api.' + name, (res) => { - for (const key in invokeCallbacks) { - const opts = invokeCallbacks[key]; - if (opts.name === name) { - opts.callback(res); - } - } - }); -} -function createKeepAliveApiCallback(name, callback) { - return addInvokeCallback(invokeCallbackId++, name, callback, true); -} -function getApiCallbacks(args) { - const apiCallbacks = {}; - for (const name in args) { - const fn = args[name]; - if (isFunction(fn)) { - apiCallbacks[name] = tryCatch(fn); - delete args[name]; - } - } - return apiCallbacks; -} -function normalizeErrMsg(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(res.errMsg, name); - isFunction(beforeAll) && beforeAll(res); - if (res.errMsg === name + ':ok') { - isFunction(beforeSuccess) && beforeSuccess(res); - hasSuccess && success(res); - } - else { - hasFail && fail(res); - } - hasComplete && complete(res); - }); - return callbackId; -} - function handlePromise(promise) { if (__UNI_FEATURE_PROMISE__) { return promise @@ -260,11 +151,6 @@ function handlePromise(promise) { return promise; } -const API_TYPE_ON = 0; -const API_TYPE_OFF = 1; -const API_TYPE_TASK = 2; -const API_TYPE_SYNC = 3; -const API_TYPE_ASYNC = 4; function formatApiArgs(args, options) { const params = args[0]; if (!options || @@ -277,55 +163,9 @@ function formatApiArgs(args, options) { }); return args; } -function wrapperOnApi(name, fn) { - return (callback) => { - // 是否是首次调用on,如果是首次,需要初始化onMethod监听 - const isFirstInvokeOnApi = !findInvokeCallbackByName(name); - createKeepAliveApiCallback(name, callback); - if (isFirstInvokeOnApi) { - onKeepAliveApiCallback(name); - fn(); - } - }; -} -function wrapperOffApi(name, fn) { - return (callback) => { - name = name.replace('off', 'on'); - removeKeepAliveApiCallback(name, callback); - // 是否还存在监听,若已不存在,则移除onMethod监听 - const hasInvokeOnApi = findInvokeCallbackByName(name); - if (!hasInvokeOnApi) { - offKeepAliveApiCallback(name); - fn(); - } - }; -} -function invokeSuccess(id, name, res) { - return invokeCallback(id, extend(res || {}, { errMsg: name + ':ok' })); -} -function invokeFail(id, name, err) { - return invokeCallback(id, { errMsg: name + ':fail' + (err ? ' ' + err : '') }); -} -function wrapperTaskApi(name, fn, options) { - return (args) => { - const id = createAsyncApiCallback(name, args, options); - return fn(args, { - resolve: (res) => invokeSuccess(id, name, res), - reject: (err) => invokeFail(id, name, err), - }); - }; -} function wrapperSyncApi(fn) { return (...args) => fn.apply(null, args); } -function wrapperAsyncApi(name, fn, options) { - return (args) => { - const id = createAsyncApiCallback(name, args, options); - fn(args) - .then((res) => invokeSuccess(id, name, res)) - .catch((err) => invokeFail(id, name, err)); - }; -} function wrapperApi(fn, name, protocol, options) { return function (...args) { if ((process.env.NODE_ENV !== 'production')) { @@ -338,21 +178,7 @@ function wrapperApi(fn, name, protocol, options) { }; } function defineSyncApi(name, fn, protocol, options) { - return defineApi(API_TYPE_SYNC, name, fn, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options); -} -function defineApi(type, name, fn, protocol, options) { - switch (type) { - case API_TYPE_ON: - return wrapperApi(wrapperOnApi(name, fn), name, protocol, options); - case API_TYPE_OFF: - return wrapperApi(wrapperOffApi(name, fn), name, protocol, options); - case API_TYPE_TASK: - return wrapperApi(wrapperTaskApi(name, fn), name, protocol, options); - case API_TYPE_SYNC: - return wrapperApi(wrapperSyncApi(fn), name, protocol, options); - case API_TYPE_ASYNC: - return wrapperApi(wrapperAsyncApi(name, fn, options), name, protocol, options); - } + return wrapperApi(wrapperSyncApi(fn), name, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options); } function getBaseSystemInfo() {