Skip to content

盘点api问题统一修正 - #2467

Open
wangxiaokou wants to merge 15 commits into
masterfrom
fix-api-proxy-error
Open

盘点api问题统一修正#2467
wangxiaokou wants to merge 15 commits into
masterfrom
fix-api-proxy-error

Conversation

@wangxiaokou

Copy link
Copy Markdown
Collaborator
  1. Bug 修复

location/index.web.js

  • getLocation 返回的 speed 字段错误赋值为 coords.accuracy,修正为 coords.speed

socket/SocketTask.js

  • close() 未将 code/reason 传给底层 WebSocket.close(),修正为 this._socket.close(code, reason)
  • onClose 回调在有 _closeData 时传的是原始 CloseEvent,修正为始终传 {code, reason} 结构
  • new window.WebSocket 改为 new WebSocket(去掉多余的 window. 前缀)

socket/index.web.js

  • 去掉了 connectSocket 中的 SSR 环境判断(原来在非浏览器环境直接 return,导致部分场景无法使用)

set-navigation-bar/index.web.js

  • setNavigationBarColor 每次调用都 createElement + appendChild,导致 meta[name="theme-color"] 标签不断累积;修正为先
    querySelector 查找已有标签,不存在时才创建

create-intersection-observer/IntersectionObserver.js

  • isInit 是模块级变量,多个实例共享,第一个实例触发后所有新实例的初始化逻辑都会出错;修正为将 isInit 改为实例属性
    this._isInit

system/index.ios.js

  • deviceOrientation 判断逻辑反了(宽>高应为 landscape,原来返回 portrait)
  • getSystemInfo 成功回调的 errMsg 写的是 setStorage:ok,修正为 getSystemInfo:ok

  1. off 系列 API 补全"不传参数移除所有"语义

原来部分 off 方法不传 callback 时行为不一致或无效,统一对齐微信规范(不传 = 移除全部):
涉及文件:app/index.web.js 补全了
offUnhandledRejection、offError、offAppShow、offAppHide、offLazyLoadError;window/index.web.js 和 rnWindow.js 补全了 web
和 RN 两端的 offWindowResize;keyboard/index.ios.js 补全了
offKeyboardHeightChange;device/network/onNetworkStatusChange.js 补全了 web 端的
offNetworkStatusChange,同时修复了单个移除时遗漏 fnMap.delete 的问题;rnNetwork.js 将 RN 端 offNetworkStatusChange
的空参判断从 === undefined 改为 == null,与 null 传参场景对齐。


  1. audio/index.web.js 事件系统重写

原实现用单个 audio.cb 属性共享所有事件回调,多次注册会互相覆盖,且 offXxx 逻辑基本失效。重写为:

  • 每个事件独立维护回调数组 eventCallbacks[eventName]
  • onXxx(cb) → push + addEventListener
  • offXxx(cb) → 移除指定;offXxx() → 移除全部
  • 移除 'Stop' 出 eventNames(HTML5 Audio 无原生 stop 事件)
  • 新增 _stopCbs 数组,stop() 执行时手动调用所有 stop 回调,onStop/offStop 同样支持多回调和"不传参数移除全部"

  1. storage/rnStorage.js
  • RN 环境下 removeStorageSync 和 clearStorageSync
    原来直接调用异步方法(AsyncStorage.removeItem/clear),同步语义完全错误;修正为 envError 占位,明确告知不支持

Comment on lines +38 to +41
Keyboard.removeAllListeners('keyboardDidShow')
Keyboard.removeAllListeners('keyboardDidHide')
hasListener = false
return

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这几行代码没有意义,复用下面的逻辑不就可以了

@hiyuki hiyuki left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

本次改动涉及 Web/RN 对外 API 行为,但未同步 docs-vitepress/ 文档;其中 RN storage、window、keyboard、network 行为变化也需要按仓库约束同步 .agents/skills/mpx2rn/。另外当前两组 unit check 均失败,建议补充本次 off/close 行为的核心单测并修复 CI 后再合入。

if (callbacks.length === 0) {
if (callbacks.length === 0 || callback == null) {
callbacks.length = 0
Keyboard.removeAllListeners('keyboardDidShow')

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] 不要清除其他模块注册的 Keyboard 监听

Keyboard.removeAllListeners() 会删除整个 React Native Keyboard emitter 上该事件的全部订阅,包括业务代码和第三方库注册的监听。现在无参 offKeyboardHeightChange() 会稳定触发这个副作用。建议保存本模块两次 Keyboard.addListener() 返回的 subscription,并在此处只调用这些 subscription 的 remove()

}
if (this._closeData) {
this._closeCb(event)
this._closeCb(this._closeData)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] onClose 应返回底层真实关闭结果

这里用请求关闭时保存的 _closeData 覆盖了真实 CloseEvent。例如调用 close({ code: 1000 }) 后连接在关闭握手期间异常断开,浏览器可能返回 1006,当前实现仍会上报 1000close() 可以继续把 code/reason 传给底层,但 onclose 应始终根据 event.codeevent.reason 构造回调参数。

}
if (callbackFn == null) {
// 不传 callback 时清除所有监听
fnMap.forEach((proxyCallback, originalCallback) => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] 空参 off 无法移除同一 callback 的重复注册

onNetworkStatusChange 每次都会创建并注册新的 proxyCallback,但 fnMap.set(callbackFn, proxyCallback) 只保留最后一个。同一 callback 注册多次后,这里的遍历只能移除最后一个 wrapper,之前的监听会永久残留,因此没有实现“移除全部”。建议为每个原始 callback 保存全部 wrapper,或重复注册前先移除旧 wrapper。

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

Successfully merging this pull request may close these issues.

2 participants