From 20c70da8fa34387b67ac5aa50f61e786d59cbcfb Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Mon, 16 Mar 2020 13:27:33 +0800 Subject: [PATCH] fix(v3): es7.promise.finally https://ask.dcloud.net.cn/question/87814 --- packages/uni-app-plus/dist/index.js | 29 ++++++++++--------- packages/uni-mp-alipay/dist/index.js | 29 ++++++++++--------- packages/uni-mp-baidu/dist/index.js | 29 ++++++++++--------- packages/uni-mp-qq/dist/index.js | 29 ++++++++++--------- packages/uni-mp-toutiao/dist/index.js | 29 ++++++++++--------- packages/uni-mp-weixin/dist/index.js | 29 ++++++++++--------- .../lib/platforms/app-plus/define-pages.js | 13 ++++++++- src/core/helpers/promise.js | 29 ++++++++++--------- 8 files changed, 123 insertions(+), 93 deletions(-) diff --git a/packages/uni-app-plus/dist/index.js b/packages/uni-app-plus/dist/index.js index 7aa090e08..2d04e0847 100644 --- a/packages/uni-app-plus/dist/index.js +++ b/packages/uni-app-plus/dist/index.js @@ -235,13 +235,15 @@ const SYNC_API_RE = const CONTEXT_API_RE = /^create|Manager$/; +const ASYNC_API = ['createBLEConnection']; + const CALLBACK_API_RE = /^on/; function isContextApi (name) { return CONTEXT_API_RE.test(name) } function isSyncApi (name) { - return SYNC_API_RE.test(name) + return SYNC_API_RE.test(name) && ASYNC_API.indexOf(name) === -1 } function isCallbackApi (name) { @@ -266,6 +268,19 @@ function shouldPromise (name) { return true } +/* eslint-disable no-extend-native */ +if (!Promise.prototype.finally) { + Promise.prototype.finally = function (callback) { + const promise = this.constructor; + return this.then( + value => promise.resolve(callback()).then(() => value), + reason => promise.resolve(callback()).then(() => { + throw reason + }) + ) + }; +} + function promisify (name, api) { if (!shouldPromise(name)) { return api @@ -279,18 +294,6 @@ function promisify (name, api) { success: resolve, fail: reject }), ...params); - /* eslint-disable no-extend-native */ - if (!Promise.prototype.finally) { - Promise.prototype.finally = function (callback) { - const promise = this.constructor; - return this.then( - value => promise.resolve(callback()).then(() => value), - reason => promise.resolve(callback()).then(() => { - throw reason - }) - ) - }; - } }))) } } diff --git a/packages/uni-mp-alipay/dist/index.js b/packages/uni-mp-alipay/dist/index.js index ede7974ea..2b64d7f50 100644 --- a/packages/uni-mp-alipay/dist/index.js +++ b/packages/uni-mp-alipay/dist/index.js @@ -235,13 +235,15 @@ const SYNC_API_RE = const CONTEXT_API_RE = /^create|Manager$/; +const ASYNC_API = ['createBLEConnection']; + const CALLBACK_API_RE = /^on/; function isContextApi (name) { return CONTEXT_API_RE.test(name) } function isSyncApi (name) { - return SYNC_API_RE.test(name) + return SYNC_API_RE.test(name) && ASYNC_API.indexOf(name) === -1 } function isCallbackApi (name) { @@ -266,6 +268,19 @@ function shouldPromise (name) { return true } +/* eslint-disable no-extend-native */ +if (!Promise.prototype.finally) { + Promise.prototype.finally = function (callback) { + const promise = this.constructor; + return this.then( + value => promise.resolve(callback()).then(() => value), + reason => promise.resolve(callback()).then(() => { + throw reason + }) + ) + }; +} + function promisify (name, api) { if (!shouldPromise(name)) { return api @@ -279,18 +294,6 @@ function promisify (name, api) { success: resolve, fail: reject }), ...params); - /* eslint-disable no-extend-native */ - if (!Promise.prototype.finally) { - Promise.prototype.finally = function (callback) { - const promise = this.constructor; - return this.then( - value => promise.resolve(callback()).then(() => value), - reason => promise.resolve(callback()).then(() => { - throw reason - }) - ) - }; - } }))) } } diff --git a/packages/uni-mp-baidu/dist/index.js b/packages/uni-mp-baidu/dist/index.js index ca718f756..7859d3040 100644 --- a/packages/uni-mp-baidu/dist/index.js +++ b/packages/uni-mp-baidu/dist/index.js @@ -235,13 +235,15 @@ const SYNC_API_RE = const CONTEXT_API_RE = /^create|Manager$/; +const ASYNC_API = ['createBLEConnection']; + const CALLBACK_API_RE = /^on/; function isContextApi (name) { return CONTEXT_API_RE.test(name) } function isSyncApi (name) { - return SYNC_API_RE.test(name) + return SYNC_API_RE.test(name) && ASYNC_API.indexOf(name) === -1 } function isCallbackApi (name) { @@ -266,6 +268,19 @@ function shouldPromise (name) { return true } +/* eslint-disable no-extend-native */ +if (!Promise.prototype.finally) { + Promise.prototype.finally = function (callback) { + const promise = this.constructor; + return this.then( + value => promise.resolve(callback()).then(() => value), + reason => promise.resolve(callback()).then(() => { + throw reason + }) + ) + }; +} + function promisify (name, api) { if (!shouldPromise(name)) { return api @@ -279,18 +294,6 @@ function promisify (name, api) { success: resolve, fail: reject }), ...params); - /* eslint-disable no-extend-native */ - if (!Promise.prototype.finally) { - Promise.prototype.finally = function (callback) { - const promise = this.constructor; - return this.then( - value => promise.resolve(callback()).then(() => value), - reason => promise.resolve(callback()).then(() => { - throw reason - }) - ) - }; - } }))) } } diff --git a/packages/uni-mp-qq/dist/index.js b/packages/uni-mp-qq/dist/index.js index 9ed02a0ec..6cac8aba2 100644 --- a/packages/uni-mp-qq/dist/index.js +++ b/packages/uni-mp-qq/dist/index.js @@ -235,13 +235,15 @@ const SYNC_API_RE = const CONTEXT_API_RE = /^create|Manager$/; +const ASYNC_API = ['createBLEConnection']; + const CALLBACK_API_RE = /^on/; function isContextApi (name) { return CONTEXT_API_RE.test(name) } function isSyncApi (name) { - return SYNC_API_RE.test(name) + return SYNC_API_RE.test(name) && ASYNC_API.indexOf(name) === -1 } function isCallbackApi (name) { @@ -266,6 +268,19 @@ function shouldPromise (name) { return true } +/* eslint-disable no-extend-native */ +if (!Promise.prototype.finally) { + Promise.prototype.finally = function (callback) { + const promise = this.constructor; + return this.then( + value => promise.resolve(callback()).then(() => value), + reason => promise.resolve(callback()).then(() => { + throw reason + }) + ) + }; +} + function promisify (name, api) { if (!shouldPromise(name)) { return api @@ -279,18 +294,6 @@ function promisify (name, api) { success: resolve, fail: reject }), ...params); - /* eslint-disable no-extend-native */ - if (!Promise.prototype.finally) { - Promise.prototype.finally = function (callback) { - const promise = this.constructor; - return this.then( - value => promise.resolve(callback()).then(() => value), - reason => promise.resolve(callback()).then(() => { - throw reason - }) - ) - }; - } }))) } } diff --git a/packages/uni-mp-toutiao/dist/index.js b/packages/uni-mp-toutiao/dist/index.js index f0458258b..666b02e8a 100644 --- a/packages/uni-mp-toutiao/dist/index.js +++ b/packages/uni-mp-toutiao/dist/index.js @@ -235,13 +235,15 @@ const SYNC_API_RE = const CONTEXT_API_RE = /^create|Manager$/; +const ASYNC_API = ['createBLEConnection']; + const CALLBACK_API_RE = /^on/; function isContextApi (name) { return CONTEXT_API_RE.test(name) } function isSyncApi (name) { - return SYNC_API_RE.test(name) + return SYNC_API_RE.test(name) && ASYNC_API.indexOf(name) === -1 } function isCallbackApi (name) { @@ -266,6 +268,19 @@ function shouldPromise (name) { return true } +/* eslint-disable no-extend-native */ +if (!Promise.prototype.finally) { + Promise.prototype.finally = function (callback) { + const promise = this.constructor; + return this.then( + value => promise.resolve(callback()).then(() => value), + reason => promise.resolve(callback()).then(() => { + throw reason + }) + ) + }; +} + function promisify (name, api) { if (!shouldPromise(name)) { return api @@ -279,18 +294,6 @@ function promisify (name, api) { success: resolve, fail: reject }), ...params); - /* eslint-disable no-extend-native */ - if (!Promise.prototype.finally) { - Promise.prototype.finally = function (callback) { - const promise = this.constructor; - return this.then( - value => promise.resolve(callback()).then(() => value), - reason => promise.resolve(callback()).then(() => { - throw reason - }) - ) - }; - } }))) } } diff --git a/packages/uni-mp-weixin/dist/index.js b/packages/uni-mp-weixin/dist/index.js index d59bed555..2192aa80d 100644 --- a/packages/uni-mp-weixin/dist/index.js +++ b/packages/uni-mp-weixin/dist/index.js @@ -235,13 +235,15 @@ const SYNC_API_RE = const CONTEXT_API_RE = /^create|Manager$/; +const ASYNC_API = ['createBLEConnection']; + const CALLBACK_API_RE = /^on/; function isContextApi (name) { return CONTEXT_API_RE.test(name) } function isSyncApi (name) { - return SYNC_API_RE.test(name) + return SYNC_API_RE.test(name) && ASYNC_API.indexOf(name) === -1 } function isCallbackApi (name) { @@ -266,6 +268,19 @@ function shouldPromise (name) { return true } +/* eslint-disable no-extend-native */ +if (!Promise.prototype.finally) { + Promise.prototype.finally = function (callback) { + const promise = this.constructor; + return this.then( + value => promise.resolve(callback()).then(() => value), + reason => promise.resolve(callback()).then(() => { + throw reason + }) + ) + }; +} + function promisify (name, api) { if (!shouldPromise(name)) { return api @@ -279,18 +294,6 @@ function promisify (name, api) { success: resolve, fail: reject }), ...params); - /* eslint-disable no-extend-native */ - if (!Promise.prototype.finally) { - Promise.prototype.finally = function (callback) { - const promise = this.constructor; - return this.then( - value => promise.resolve(callback()).then(() => value), - reason => promise.resolve(callback()).then(() => { - throw reason - }) - ) - }; - } }))) } } diff --git a/packages/webpack-uni-pages-loader/lib/platforms/app-plus/define-pages.js b/packages/webpack-uni-pages-loader/lib/platforms/app-plus/define-pages.js index 6816cb345..44362c10b 100644 --- a/packages/webpack-uni-pages-loader/lib/platforms/app-plus/define-pages.js +++ b/packages/webpack-uni-pages-loader/lib/platforms/app-plus/define-pages.js @@ -10,7 +10,18 @@ function generatePageCode (pages, pageOptions) { module.exports = function definePages (appJson) { return { name: 'define-pages.js', - content: ` + content: ` +if (!Promise.prototype.finally) { + Promise.prototype.finally = function(callback) { + const promise = this.constructor + return this.then( + value => promise.resolve(callback()).then(() => value), + reason => promise.resolve(callback()).then(() => { + throw reason + }) + ) + } +} if(uni.restoreGlobal){ uni.restoreGlobal(weex,plus,setTimeout,clearTimeout,setInterval,clearInterval) } diff --git a/src/core/helpers/promise.js b/src/core/helpers/promise.js index 181aaa42c..dba09f091 100644 --- a/src/core/helpers/promise.js +++ b/src/core/helpers/promise.js @@ -12,9 +12,9 @@ const SYNC_API_RE = const CONTEXT_API_RE = /^create|Manager$/ -const TASK_APIS = ['request', 'downloadFile', 'uploadFile', 'connectSocket'] +const TASK_APIS = ['request', 'downloadFile', 'uploadFile', 'connectSocket'] -const ASYNC_API = ['createBLEConnection'] +const ASYNC_API = ['createBLEConnection'] const CALLBACK_API_RE = /^on/ @@ -51,6 +51,19 @@ export function shouldPromise (name) { return true } +/* eslint-disable no-extend-native */ +if (!Promise.prototype.finally) { + Promise.prototype.finally = function (callback) { + const promise = this.constructor + return this.then( + value => promise.resolve(callback()).then(() => value), + reason => promise.resolve(callback()).then(() => { + throw reason + }) + ) + } +} + export function promisify (name, api) { if (!shouldPromise(name)) { return api @@ -64,18 +77,6 @@ export function promisify (name, api) { success: resolve, fail: reject }), ...params) - /* eslint-disable no-extend-native */ - if (!Promise.prototype.finally) { - Promise.prototype.finally = function (callback) { - const promise = this.constructor - return this.then( - value => promise.resolve(callback()).then(() => value), - reason => promise.resolve(callback()).then(() => { - throw reason - }) - ) - } - } }))) } } -- GitLab