@uni-helper/uni-promises
Version:
uni-app Promise 化的 API
617 lines (482 loc) • 23 kB
JavaScript
'use strict';
var __defProp$3 = Object.defineProperty;
var __defProps$3 = Object.defineProperties;
var __getOwnPropDescs$3 = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols$3 = Object.getOwnPropertySymbols;
var __hasOwnProp$3 = Object.prototype.hasOwnProperty;
var __propIsEnum$3 = Object.prototype.propertyIsEnumerable;
var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues$3 = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp$3.call(b, prop))
__defNormalProp$3(a, prop, b[prop]);
if (__getOwnPropSymbols$3)
for (var prop of __getOwnPropSymbols$3(b)) {
if (__propIsEnum$3.call(b, prop))
__defNormalProp$3(a, prop, b[prop]);
}
return a;
};
var __spreadProps$3 = (a, b) => __defProps$3(a, __getOwnPropDescs$3(b));
function noop() {
}
function promisify(callback) {
return (...args) => {
const options = args[0];
return new Promise(
(resolve, reject) => {
callback(__spreadProps$3(__spreadValues$3({}, options), {
success: (result) => {
var _a;
(_a = options == null ? void 0 : options.success) == null ? void 0 : _a.call(options, result);
resolve(result);
},
fail: (error) => {
var _a;
(_a = options == null ? void 0 : options.fail) == null ? void 0 : _a.call(options, error);
reject(error);
}
}));
}
);
};
}
function mountTaskMethodToPromise(task, promise) {
if (!task || !promise)
return;
for (const fn of [
"onHeadersReceived",
"offHeadersReceived",
"onChunkReceived",
"offChunkReceived",
"onProgressUpdate",
"offProgressUpdate"
]) {
if (fn in task) {
promise[fn] = task[fn].bind(task);
}
}
promise.abort = () => {
task == null ? void 0 : task.abort();
return promise;
};
}
const addPhoneContact = promisify(uni.addPhoneContact);
const authorize = promisify(uni.authorize);
const checkIsSoterEnrolledInDevice = promisify(uni.checkIsSoterEnrolledInDevice);
const checkIsSupportSoterAuthentication = promisify(uni.checkIsSupportSoterAuthentication);
const checkSession = promisify(uni.checkSession);
const chooseAddress = promisify(uni.chooseAddress);
const chooseFile = promisify(uni.chooseFile);
const chooseImage = promisify(uni.chooseImage);
const chooseInvoice = promisify(uni.chooseInvoice);
const chooseInvoiceTitle = promisify(uni.chooseInvoiceTitle);
const chooseLocation = promisify(uni.chooseLocation);
const chooseMedia = promisify(uni.chooseMedia);
const chooseMessageFile = promisify(uni.chooseMessageFile);
const chooseVideo = promisify(uni.chooseVideo);
const clearStorage = promisify(uni.clearStorage);
const closeBLEConnection = promisify(uni.closeBLEConnection);
const closeBluetoothAdapter = promisify(uni.closeBluetoothAdapter);
const closePreviewImage = promisify(uni.closePreviewImage);
const closeSocket = () => promisify(uni.closeSocket);
const compressImage = promisify(uni.compressImage);
const compressVideo = promisify(uni.compressVideo);
const connectSocket = () => promisify(uni.connectSocket);
const createBLEConnection = promisify(uni.createBLEConnection);
const createCameraContext = () => uni.createCameraContext();
const createCanvasContext = (canvasId, componentInstance) => uni.createCanvasContext(canvasId, componentInstance);
const createInnerAudioContext = () => uni.createInnerAudioContext();
const createLivePlayerContext = (livePlayerId, componentInstance) => uni.createLivePlayerContext(livePlayerId, componentInstance);
const createLivePusherContext = (livePusherId, componentInstance) => uni.createLivePusherContext(livePusherId, componentInstance);
const createMapContext = (mapId, componentInstance) => {
return new Promise((resolve, reject) => {
try {
const context = uni.createMapContext(mapId, componentInstance);
context.getCenterLocation = promisify(context.getCenterLocation.bind(context));
context.moveToLocation = promisify(context.moveToLocation.bind(context));
context.translateMarker = promisify(context.translateMarker.bind(context));
context.includePoints = promisify(context.includePoints.bind(context));
context.getRegion = promisify(context.getRegion.bind(context));
context.getScale = promisify(context.getScale.bind(context));
context.addCustomLayer = promisify(context.addCustomLayer.bind(context));
context.addGroundOverlay = promisify(context.addGroundOverlay.bind(context));
context.addMarkers = promisify(context.addMarkers.bind(context));
context.fromScreenLocation = promisify(context.fromScreenLocation.bind(context));
context.initMarkerCluster = promisify(context.initMarkerCluster.bind(context));
context.moveAlong = promisify(context.moveAlong.bind(context));
context.openMapApp = promisify(context.openMapApp.bind(context));
context.removeCustomLayer = promisify(context.removeCustomLayer.bind(context));
context.removeGroundOverlay = promisify(context.removeGroundOverlay.bind(context));
context.removeMarkers = promisify(context.removeMarkers.bind(context));
context.setCenterOffset = promisify(context.setCenterOffset.bind(context));
context.toScreenLocation = promisify(context.toScreenLocation.bind(context));
context.updateGroundOverlay = promisify(context.updateGroundOverlay.bind(context));
resolve(context);
} catch (error) {
reject(error);
}
});
};
const createMediaContainer = () => uni.createMediaContainer();
const createPushMessage = promisify(uni.createPushMessage);
const createSelectorQuery = () => uni.createSelectorQuery();
const createVideoContext = (videoId, componentInstance) => uni.createVideoContext(videoId, componentInstance);
var __defProp$2 = Object.defineProperty;
var __defProps$2 = Object.defineProperties;
var __getOwnPropDescs$2 = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols$2 = Object.getOwnPropertySymbols;
var __hasOwnProp$2 = Object.prototype.hasOwnProperty;
var __propIsEnum$2 = Object.prototype.propertyIsEnumerable;
var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues$2 = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp$2.call(b, prop))
__defNormalProp$2(a, prop, b[prop]);
if (__getOwnPropSymbols$2)
for (var prop of __getOwnPropSymbols$2(b)) {
if (__propIsEnum$2.call(b, prop))
__defNormalProp$2(a, prop, b[prop]);
}
return a;
};
var __spreadProps$2 = (a, b) => __defProps$2(a, __getOwnPropDescs$2(b));
function downloadFile(urlOrOptions, options) {
var _a, _b, _c;
const _options = typeof urlOrOptions === "string" ? __spreadProps$2(__spreadValues$2({}, options), { url: urlOrOptions, complete: (_a = options == null ? void 0 : options.complete) != null ? _a : noop }) : __spreadProps$2(__spreadValues$2(__spreadValues$2({}, urlOrOptions), options), {
complete: (_c = (_b = urlOrOptions == null ? void 0 : urlOrOptions.complete) != null ? _b : options == null ? void 0 : options.complete) != null ? _c : noop
});
const { success, fail } = _options;
let task;
const promise = new Promise((resolve, reject) => {
_options.success = (r) => {
success == null ? void 0 : success(r);
resolve(r);
};
_options.fail = (e) => {
fail == null ? void 0 : fail(e);
reject(e);
};
task = uni.downloadFile(_options);
});
mountTaskMethodToPromise(task, promise);
return promise;
}
const _getApp = () => new Promise((resolve, reject) => {
try {
const app = getApp();
resolve(app);
} catch (error) {
reject(error);
}
});
const getBackgroundAudioManager = () => uni.getBackgroundAudioManager();
const getBatteryInfo = promisify(uni.getBatteryInfo);
const getBLEDeviceCharacteristics = promisify(uni.getBLEDeviceCharacteristics);
const getBLEDeviceRSSI = promisify(uni.getBLEDeviceRSSI);
const getBLEDeviceServices = promisify(uni.getBLEDeviceServices);
const getBluetoothAdapterState = promisify(uni.getBluetoothAdapterState);
const getBluetoothDevices = promisify(uni.getBluetoothDevices);
const getCheckBoxState = promisify(uni.getCheckBoxState);
const getClipboardData = promisify(uni.getClipboardData);
const getConnectedBluetoothDevices = promisify(uni.getConnectedBluetoothDevices);
const getExtConfig = promisify(uni.getExtConfig);
const getFileInfo = promisify(uni.getFileInfo);
const getImageInfo = promisify(uni.getImageInfo);
const getLocation = promisify(uni.getLocation);
const getNetworkType = promisify(uni.getNetworkType);
const getProvider = promisify(uni.getProvider);
const getPushClientId = promisify(uni.getPushClientId);
const getRecordManager = () => uni.getRecorderManager();
const getSavedFileInfo = promisify(uni.getSavedFileInfo);
const getSavedFileList = promisify(uni.getSavedFileList);
const getScreenBrightness = promisify(uni.getScreenBrightness);
const getSelectedTextRange = promisify(uni.getSelectedTextRange);
const getSetting = promisify(uni.getSetting);
const getStorage = promisify(uni.getStorage);
const getStorageInfo = promisify(uni.getStorageInfo);
const getSystemInfo = promisify(uni.getSystemInfo);
const getUserInfo = promisify(uni.getUserInfo);
const getUserProfile = promisify(uni.getUserProfile);
const getVideoInfo = promisify(uni.getVideoInfo);
const hideHomeButton = promisify(uni.hideHomeButton);
const hideNavigationBarLoading = promisify(uni.hideNavigationBarLoading);
const hideShareMenu = promisify(uni.hideShareMenu);
const hideTabBar = promisify(uni.hideTabBar);
const hideTabBarRedDot = promisify(uni.hideTabBarRedDot);
const loadFontFace = promisify(uni.loadFontFace);
const login = promisify(uni.login);
const makePhoneCall = promisify(uni.makePhoneCall);
const navigateBack = promisify(uni.navigateBack);
const navigateBackMiniProgram = promisify(uni.navigateBackMiniProgram);
const navigateTo = promisify(uni.navigateTo);
const navigateToMiniProgram = promisify(uni.navigateToMiniProgram);
const notifyBLECharacteristicValueChange = promisify(uni.notifyBLECharacteristicValueChange);
const openAppAuthorizeSetting = promisify(uni.openAppAuthorizeSetting);
const openBluetoothAdapter = promisify(uni.openBluetoothAdapter);
const openDocument = promisify(uni.openDocument);
const openLocation = promisify(uni.openLocation);
const openVideoEditor = promisify(uni.openVideoEditor);
const pageScrollTo = promisify(uni.pageScrollTo);
const preLogin = promisify(uni.preLogin);
const previewImage = promisify(uni.previewImage);
const readBLECharacteristicValue = promisify(uni.readBLECharacteristicValue);
const redirectTo = promisify(uni.redirectTo);
const reLaunch = promisify(uni.reLaunch);
const removeSavedFile = promisify(uni.removeSavedFile);
const removeStorage = promisify(uni.removeStorage);
const removeTabBarBadge = promisify(uni.removeTabBarBadge);
var __defProp$1 = Object.defineProperty;
var __defProps$1 = Object.defineProperties;
var __getOwnPropDescs$1 = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues$1 = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp$1.call(b, prop))
__defNormalProp$1(a, prop, b[prop]);
if (__getOwnPropSymbols$1)
for (var prop of __getOwnPropSymbols$1(b)) {
if (__propIsEnum$1.call(b, prop))
__defNormalProp$1(a, prop, b[prop]);
}
return a;
};
var __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b));
function request(urlOrOptions, options) {
var _a, _b, _c;
const _options = typeof urlOrOptions === "string" ? __spreadProps$1(__spreadValues$1({}, options), { url: urlOrOptions, complete: (_a = options == null ? void 0 : options.complete) != null ? _a : noop }) : __spreadProps$1(__spreadValues$1(__spreadValues$1({}, urlOrOptions), options), {
complete: (_c = (_b = urlOrOptions == null ? void 0 : urlOrOptions.complete) != null ? _b : options == null ? void 0 : options.complete) != null ? _c : noop
});
const { success, fail } = _options;
let task;
const promise = new Promise((resolve, reject) => {
_options.success = (r) => {
success == null ? void 0 : success(r);
resolve(r);
};
_options.fail = (e) => {
fail == null ? void 0 : fail(e);
reject(e);
};
task = uni.request(_options);
});
mountTaskMethodToPromise(task, promise);
return promise;
}
const requestPayment = promisify(uni.requestPayment);
const requestSubscribeMessage = promisify(uni.requestSubscribeMessage);
const saveFile = promisify(uni.saveFile);
const saveImageToPhotosAlbum = promisify(uni.saveImageToPhotosAlbum);
const saveVideoToPhotosAlbum = promisify(uni.saveVideoToPhotosAlbum);
const scanCode = promisify(uni.scanCode);
const sendSocketMessage = () => promisify(uni.sendSocketMessage);
const setBackgroundColor = promisify(uni.setBackgroundColor);
const setBackgroundTextStyle = promisify(uni.setBackgroundTextStyle);
const setBLEMTU = promisify(uni.setBLEMTU);
const setClipboardData = promisify(uni.setClipboardData);
const setEnableDebug = promisify(uni.setEnableDebug);
const setKeepScreenOn = promisify(uni.setKeepScreenOn);
const setNavigationBarColor = promisify(uni.setNavigationBarColor);
const setNavigationBarTitle = promisify(uni.setNavigationBarTitle);
const setScreenBrightness = promisify(uni.setScreenBrightness);
const setStorage = promisify(uni.setStorage);
const setTabBarBadge = promisify(uni.setTabBarBadge);
const setTabBarItem = promisify(uni.setTabBarItem);
const setTabBarStyle = promisify(uni.setTabBarStyle);
const share = promisify(uni.share);
const shareWithSystem = promisify(uni.shareWithSystem);
const showActionSheet = promisify(uni.showActionSheet);
const showLoading = promisify(uni.showLoading);
const showModal = promisify(uni.showModal);
const showNavigationBarLoading = promisify(uni.showNavigationBarLoading);
const showShareMenu = promisify(uni.showShareMenu);
const showTabBar = promisify(uni.showTabBar);
const showTabBarRedDot = promisify(uni.showTabBarRedDot);
const showToast = promisify(uni.showToast);
const startBluetoothDevicesDiscovery = promisify(uni.startBluetoothDevicesDiscovery);
const startPullDownRefresh = promisify(uni.startPullDownRefresh);
const startSoterAuthentication = promisify(uni.startSoterAuthentication);
const stopBluetoothDevicesDiscovery = promisify(uni.stopBluetoothDevicesDiscovery);
const switchTab = promisify(uni.switchTab);
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
function uploadFile(urlOrOptions, options) {
var _a, _b, _c;
const _options = typeof urlOrOptions === "string" ? __spreadProps(__spreadValues({}, options), { url: urlOrOptions, complete: (_a = options == null ? void 0 : options.complete) != null ? _a : noop }) : __spreadProps(__spreadValues(__spreadValues({}, urlOrOptions), options), {
complete: (_c = (_b = urlOrOptions == null ? void 0 : urlOrOptions.complete) != null ? _b : options == null ? void 0 : options.complete) != null ? _c : noop
});
const { success, fail } = _options;
let task;
const promise = new Promise((resolve, reject) => {
_options.success = (r) => {
success == null ? void 0 : success(r);
resolve(r);
};
_options.fail = (e) => {
fail == null ? void 0 : fail(e);
reject(e);
};
task = uni.uploadFile(_options);
});
mountTaskMethodToPromise(task, promise);
return promise;
}
const vibrate = promisify(uni.vibrate);
const vibrateLong = promisify(uni.vibrateLong);
const vibrateShort = promisify(uni.vibrateShort);
const writeBLECharacteristicValue = promisify(uni.writeBLECharacteristicValue);
exports.addPhoneContact = addPhoneContact;
exports.authorize = authorize;
exports.checkIsSoterEnrolledInDevice = checkIsSoterEnrolledInDevice;
exports.checkIsSupportSoterAuthentication = checkIsSupportSoterAuthentication;
exports.checkSession = checkSession;
exports.chooseAddress = chooseAddress;
exports.chooseFile = chooseFile;
exports.chooseImage = chooseImage;
exports.chooseInvoice = chooseInvoice;
exports.chooseInvoiceTitle = chooseInvoiceTitle;
exports.chooseLocation = chooseLocation;
exports.chooseMedia = chooseMedia;
exports.chooseMessageFile = chooseMessageFile;
exports.chooseVideo = chooseVideo;
exports.clearStorage = clearStorage;
exports.closeBLEConnection = closeBLEConnection;
exports.closeBluetoothAdapter = closeBluetoothAdapter;
exports.closePreviewImage = closePreviewImage;
exports.closeSocket = closeSocket;
exports.compressImage = compressImage;
exports.compressVideo = compressVideo;
exports.connectSocket = connectSocket;
exports.createBLEConnection = createBLEConnection;
exports.createCameraContext = createCameraContext;
exports.createCanvasContext = createCanvasContext;
exports.createInnerAudioContext = createInnerAudioContext;
exports.createLivePlayerContext = createLivePlayerContext;
exports.createLivePusherContext = createLivePusherContext;
exports.createMapContext = createMapContext;
exports.createMediaContainer = createMediaContainer;
exports.createPushMessage = createPushMessage;
exports.createSelectorQuery = createSelectorQuery;
exports.createVideoContext = createVideoContext;
exports.downloadFile = downloadFile;
exports.getApp = _getApp;
exports.getBLEDeviceCharacteristics = getBLEDeviceCharacteristics;
exports.getBLEDeviceRSSI = getBLEDeviceRSSI;
exports.getBLEDeviceServices = getBLEDeviceServices;
exports.getBackgroundAudioManager = getBackgroundAudioManager;
exports.getBatteryInfo = getBatteryInfo;
exports.getBluetoothAdapterState = getBluetoothAdapterState;
exports.getBluetoothDevices = getBluetoothDevices;
exports.getCheckBoxState = getCheckBoxState;
exports.getClipboardData = getClipboardData;
exports.getConnectedBluetoothDevices = getConnectedBluetoothDevices;
exports.getExtConfig = getExtConfig;
exports.getFileInfo = getFileInfo;
exports.getImageInfo = getImageInfo;
exports.getLocation = getLocation;
exports.getNetworkType = getNetworkType;
exports.getProvider = getProvider;
exports.getPushClientId = getPushClientId;
exports.getRecordManager = getRecordManager;
exports.getSavedFileInfo = getSavedFileInfo;
exports.getSavedFileList = getSavedFileList;
exports.getScreenBrightness = getScreenBrightness;
exports.getSelectedTextRange = getSelectedTextRange;
exports.getSetting = getSetting;
exports.getStorage = getStorage;
exports.getStorageInfo = getStorageInfo;
exports.getSystemInfo = getSystemInfo;
exports.getUserInfo = getUserInfo;
exports.getUserProfile = getUserProfile;
exports.getVideoInfo = getVideoInfo;
exports.hideHomeButton = hideHomeButton;
exports.hideNavigationBarLoading = hideNavigationBarLoading;
exports.hideShareMenu = hideShareMenu;
exports.hideTabBar = hideTabBar;
exports.hideTabBarRedDot = hideTabBarRedDot;
exports.loadFontFace = loadFontFace;
exports.login = login;
exports.makePhoneCall = makePhoneCall;
exports.mountTaskMethodToPromise = mountTaskMethodToPromise;
exports.navigateBack = navigateBack;
exports.navigateBackMiniProgram = navigateBackMiniProgram;
exports.navigateTo = navigateTo;
exports.navigateToMiniProgram = navigateToMiniProgram;
exports.noop = noop;
exports.notifyBLECharacteristicValueChange = notifyBLECharacteristicValueChange;
exports.openAppAuthorizeSetting = openAppAuthorizeSetting;
exports.openBluetoothAdapter = openBluetoothAdapter;
exports.openDocument = openDocument;
exports.openLocation = openLocation;
exports.openVideoEditor = openVideoEditor;
exports.pageScrollTo = pageScrollTo;
exports.preLogin = preLogin;
exports.previewImage = previewImage;
exports.promisify = promisify;
exports.reLaunch = reLaunch;
exports.readBLECharacteristicValue = readBLECharacteristicValue;
exports.redirectTo = redirectTo;
exports.removeSavedFile = removeSavedFile;
exports.removeStorage = removeStorage;
exports.removeTabBarBadge = removeTabBarBadge;
exports.request = request;
exports.requestPayment = requestPayment;
exports.requestSubscribeMessage = requestSubscribeMessage;
exports.saveFile = saveFile;
exports.saveImageToPhotosAlbum = saveImageToPhotosAlbum;
exports.saveVideoToPhotosAlbum = saveVideoToPhotosAlbum;
exports.scanCode = scanCode;
exports.sendSocketMessage = sendSocketMessage;
exports.setBLEMTU = setBLEMTU;
exports.setBackgroundColor = setBackgroundColor;
exports.setBackgroundTextStyle = setBackgroundTextStyle;
exports.setClipboardData = setClipboardData;
exports.setEnableDebug = setEnableDebug;
exports.setKeepScreenOn = setKeepScreenOn;
exports.setNavigationBarColor = setNavigationBarColor;
exports.setNavigationBarTitle = setNavigationBarTitle;
exports.setScreenBrightness = setScreenBrightness;
exports.setStorage = setStorage;
exports.setTabBarBadge = setTabBarBadge;
exports.setTabBarItem = setTabBarItem;
exports.setTabBarStyle = setTabBarStyle;
exports.share = share;
exports.shareWithSystem = shareWithSystem;
exports.showActionSheet = showActionSheet;
exports.showLoading = showLoading;
exports.showModal = showModal;
exports.showNavigationBarLoading = showNavigationBarLoading;
exports.showShareMenu = showShareMenu;
exports.showTabBar = showTabBar;
exports.showTabBarRedDot = showTabBarRedDot;
exports.showToast = showToast;
exports.startBluetoothDevicesDiscovery = startBluetoothDevicesDiscovery;
exports.startPullDownRefresh = startPullDownRefresh;
exports.startSoterAuthentication = startSoterAuthentication;
exports.stopBluetoothDevicesDiscovery = stopBluetoothDevicesDiscovery;
exports.switchTab = switchTab;
exports.uploadFile = uploadFile;
exports.vibrate = vibrate;
exports.vibrateLong = vibrateLong;
exports.vibrateShort = vibrateShort;
exports.writeBLECharacteristicValue = writeBLECharacteristicValue;