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

使用raf库的cancel方法,会因为@tarojs/runtime的window出现不兼容而Illegal invocation #7749

Closed
303394539 opened this issue Sep 27, 2020 · 2 comments · Fixed by #8444
Labels
F-react Framework - React T-weapp Target - 编译到微信小程序 V-3 Version - 3.x
Milestone

Comments

@303394539
Copy link

相关平台

微信小程序

小程序基础库: 2.13.0
使用框架: React

复现步骤

import raf from 'raf';
raf.cancal() //可重现。

期望结果

与raf库不冲突,可正常使用raf.cancel

实际结果

raf.cancal(); //Illegal invocation

环境信息

Taro CLI 3.0.11 environment info:
    System:
      OS: macOS 10.14.6
      Shell: 3.2.57 - /bin/bash
    Binaries:
      Node: 10.15.3 - ~/.nvm/versions/node/v10.15.3/bin/node
      Yarn: 1.22.5 - ~/.nvm/versions/node/v10.15.3/bin/yarn
      npm: 6.4.1 - ~/.nvm/versions/node/v10.15.3/bin/npm
    npmPackages:
      babel-preset-taro: ^3.0.11 => 3.0.13 

补充信息

分析原因是因为在非h5版本中,raf内部通过@tarojs/runtime的window获取到cancelAnimationFrame为clearTimeout,因为raf使用了caf.apply(window, arguments)实现cancel方法,而这时的window缺失clearTimeout的引用,造成Illegal invocation。

代码源头:

taro/packages/taro-runtime/src/bom/window.ts

if (process.env.TARO_ENV !== 'h5') {
  (window as any).requestAnimationFrame = raf;
  (window as any).cancelAnimationFrame = caf;
  (window as any).Date = Date;
  (window as any).setTimeout = setTimeout
}

raf/index.js

module.exports.cancel = function() {
  caf.apply(root, arguments)
}
@taro-bot2 taro-bot2 bot added F-react Framework - React T-weapp Target - 编译到微信小程序 V-3 Version - 3.x labels Sep 27, 2020
@luckyadam
Copy link
Member

可以直接从 @tarojs/runtime 里引入 requestAnimationFrame 来使用

@303394539
Copy link
Author

303394539 commented Sep 28, 2020

自己使用raf有兼容问题可以换个方式,只是目前使用的另一个第三方的东西内部使用到了raf,现在来说Taro和这个第三方的东西都是外部的,出现了raf的不兼容,这样就不好办了。我目前使用alias覆盖了raf的引入倒是暂时解决了,不过这个兼容问题看能不能官方兼容一下

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-react Framework - React T-weapp Target - 编译到微信小程序 V-3 Version - 3.x
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants