Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

异步函数 uni.createBLEConnection 没有被 Promisify #1543

Closed
benlypan opened this issue Apr 9, 2020 · 4 comments
Closed

异步函数 uni.createBLEConnection 没有被 Promisify #1543

benlypan opened this issue Apr 9, 2020 · 4 comments

Comments

@benlypan
Copy link

benlypan commented Apr 9, 2020

问题描述
[问题描述:尽可能简洁清晰地把问题描述清楚]
uni.createBLEConnection 是一个异步函数,框架没有把他 Promisify,返回的不是 Promise

复现步骤
[复现问题的步骤]

console.log(uni.createBLEConnection());

可以看到返回值不是 Promise

预期结果
[使用简洁清晰的语言描述你希望生效的预期结果]

实际结果
[这里请贴上你的报错截图或文字]

系统信息:

  • 发行平台: 微信小程序
  • 操作系统: 全平台
  • HBuilderX版本 [如使用HBuilderX,则需提供 HBuilderX 版本号]
  • uni-app版本 v2.6.9
  • 设备信息 全平台

补充信息
[可选]
[根据你的分析,出现这个问题的原因可能在哪里?]
@dcloudio/uni-mp-weixin/index.js 中判断是否被 Promisify 的逻辑如下

const SYNC_API_RE =
  /^\$|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/;

const CONTEXT_API_RE = /^create|Manager$/;

const ASYNC_API = ['createBLEConnection'];

const CALLBACK_API_RE = /^on|^off/;

function isContextApi (name) {
  return CONTEXT_API_RE.test(name)
}
function isSyncApi (name) {
  return SYNC_API_RE.test(name) && ASYNC_API.indexOf(name) === -1
}

function isCallbackApi (name) {
  return CALLBACK_API_RE.test(name) && name !== 'onPush'
}

function handlePromise (promise) {
  return promise.then(data => {
    return [null, data]
  })
    .catch(err => [err])
}

function shouldPromise (name) {
  if (
    isContextApi(name) ||
    isSyncApi(name) ||
    isCallbackApi(name)
  ) {
    return false
  }
  return true
}

可以看到,在 shouldPromise 中执行 isContextApi(name) 时,因为 createBLEConnectioncreate 开头的,满足了 isContextApi 的条件,返回了 true, 导致 shouldPromise 返回了 false。代码中存在 const ASYNC_API = ['createBLEConnection']; 片段,可以看出考虑了这个函数的 Promisify,却有逻辑漏洞。将 isContextApi 函数修改为以下逻辑可修复错误

function isContextApi (name) {
  return CONTEXT_API_RE.test(name) && ASYNC_API.indexOf(name) === -1
}
@Wangyaqi
Copy link
Contributor

Wangyaqi commented Apr 9, 2020

你用的什么版本的HBuilderX?这个问题在2.6.6-alpha已经修复了

@benlypan
Copy link
Author

benlypan commented Apr 9, 2020

@Wangyaqi 用的是 cli 版的,npm run info 信息如下

> node node_modules/@dcloudio/vue-cli-plugin-uni/commands/info.js

uni-app v2.6.9
uni-app cli v2.0.0-26920200403001

Environment Info:

  System:
    OS: Linux 5.5 Arch Linux undefined
    CPU: (4) x64 Intel(R) Core(TM) i7-6600U CPU @ 2.60GHz
  Binaries:
    Node: 13.11.0 - /usr/bin/node
    Yarn: 1.22.4 - /usr/bin/yarn
    npm: 6.14.3 - /usr/bin/npm
  Browsers:
    Chrome: Not Found
    Firefox: Not Found
  npmPackages:
    @dcloudio/uni-app-plus: ^2.0.0-26920200403001 => 2.0.0-26920200403001 
    @dcloudio/uni-app-plus-nvue:  0.0.1 
    @dcloudio/uni-app-plus-nvue-v8:  0.0.1 
    @dcloudio/uni-cli-shared: ^2.0.0-26920200403001 => 2.0.0-26920200403001 
    @dcloudio/uni-h5: ^2.0.0-26920200403001 => 2.0.0-26920200403001 
    @dcloudio/uni-helper-json: * => 1.0.5 
    @dcloudio/uni-migration: ^2.0.0-26920200403001 => 2.0.0-26920200403001 
    @dcloudio/uni-mp-alipay: ^2.0.0-26920200403001 => 2.0.0-26920200403001 
    @dcloudio/uni-mp-baidu: ^2.0.0-26920200403001 => 2.0.0-26920200403001 
    @dcloudio/uni-mp-qq: ^2.0.0-26920200403001 => 2.0.0-26920200403001 
    @dcloudio/uni-mp-toutiao: ^2.0.0-26920200403001 => 2.0.0-26920200403001 
    @dcloudio/uni-mp-weixin: ^2.0.0-26920200403001 => 2.0.0-26920200403001 
    @dcloudio/uni-stat: ^2.0.0-26920200403001 => 2.0.0-26920200403001 
    @dcloudio/uni-template-compiler: ^2.0.0-26920200403001 => 2.0.0-26920200403001 
    @dcloudio/vue-cli-plugin-hbuilderx: ^2.0.0-26920200403001 => 2.0.0-26920200403001 
    @dcloudio/vue-cli-plugin-uni: ^2.0.0-26920200403001 => 2.0.0-26920200403001 
    @dcloudio/vue-cli-plugin-uni-optimize: ^2.0.0-26920200403001 => 2.0.0-26920200403001 
    @dcloudio/webpack-uni-mp-loader: ^2.0.0-26920200403001 => 2.0.0-26920200403001 
    @dcloudio/webpack-uni-nvue-loader:  0.0.1 
    @dcloudio/webpack-uni-pages-loader: ^2.0.0-26920200403001 => 2.0.0-26920200403001 
    @vue/babel-helper-vue-jsx-merge-props:  1.0.0 
    @vue/babel-plugin-transform-vue-jsx:  1.1.2 
    @vue/babel-preset-app:  4.3.1 
    @vue/babel-preset-jsx:  1.1.2 
    @vue/babel-sugar-functional-vue:  1.1.2 
    @vue/babel-sugar-inject-h:  1.1.2 
    @vue/babel-sugar-v-model:  1.1.2 
    @vue/babel-sugar-v-on:  1.1.2 
    @vue/cli-overlay:  4.3.1 
    @vue/cli-plugin-babel: ^4.3.0 => 4.3.1 
    @vue/cli-plugin-router:  4.3.1 
    @vue/cli-plugin-vuex:  4.3.1 
    @vue/cli-service: ^4.3.0 => 4.3.1 
    @vue/cli-shared-utils:  4.3.1 
    @vue/component-compiler-utils:  3.1.2 (3.1.2)
    @vue/preload-webpack-plugin:  1.1.1 
    @vue/web-component-wrapper:  1.2.0 
    mpvue-page-factory:  1.0.1 
    mpvue-template-compiler:  1.0.13 
    uni-h5-vue:  2.6.10 
    uni-mp-vue:  2.6.10 
    vue: ^2.6.11 => 2.6.11 
    vue-hot-reload-api:  2.3.4 
    vue-loader:  15.9.1 (15.9.1)
    vue-router:  3.0.1 
    vue-style-loader:  4.1.2 
    vue-template-compiler: ^2.6.11 => 2.6.11 
    vue-template-es2015-compiler:  1.9.1 
    vuex: ^3.0.1 => 3.1.3 
  npmGlobalPackages:
    @vue/cli: 4.1.1

@benlypan
Copy link
Author

benlypan commented Apr 9, 2020

而且我看这个仓库里 master, alpha, dev 分支下 packages/uni-mp-weixin/dist/index.js 代码也还都是有问题的

@Wangyaqi
Copy link
Contributor

Wangyaqi commented Apr 9, 2020

了解,现在改一下,上次这个修改是不回调的问题

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants