Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions packages/api-proxy/src/platform/api/app/index.web.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ function onUnhandledRejection (callback) {
}

function offUnhandledRejection (callback) {
if (callback == null) {
global.__mpxAppCbs.rejection.length = 0
return
}
off(global.__mpxAppCbs.rejection, callback)
}

Expand All @@ -34,6 +38,10 @@ function onError (callback) {
}

function offError (callback) {
if (callback == null) {
global.__mpxAppCbs.error.length = 0
return
}
off(global.__mpxAppCbs.error, callback)
}

Expand All @@ -44,6 +52,10 @@ function onAppShow (callback) {
}

function offAppShow (callback) {
if (callback == null) {
global.__mpxAppCbs.show.length = 0
return
}
off(global.__mpxAppCbs.show, callback)
}

Expand All @@ -54,6 +66,10 @@ function onAppHide (callback) {
}

function offAppHide (callback) {
if (callback == null) {
global.__mpxAppCbs.hide.length = 0
return
}
off(global.__mpxAppCbs.hide, callback)
}

Expand All @@ -65,6 +81,10 @@ function onLazyLoadError (callback) {

function offLazyLoadError (callback) {
if (isReact) {
if (callback == null) {
global.__mpxAppCbs.lazyLoad.length = 0
return
}
off(global.__mpxAppCbs.lazyLoad, callback)
}
}
Expand Down
75 changes: 59 additions & 16 deletions packages/api-proxy/src/platform/api/audio/index.web.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@ export const createInnerAudioContext = () => {

__audio.pause = () => audio.pause()

const _stopCbs = []

__audio.stop = () => {
__audio.pause()
__audio.seek(0)
_stopping = true // 打开屏蔽开关,后续 pause 事件的 wrapper 会看到这个标志
audio.pause()
audio.currentTime = 0
setTimeout(() => {
_stopping = false // pause 事件已经派发完,关掉开关
_stopCbs.forEach(cb => cb())
}, 0)
}

__audio.seek = value => {
Expand Down Expand Up @@ -49,27 +56,63 @@ export const createInnerAudioContext = () => {
'Seeking',
'TimeUpdate',
'Waiting',
'Stop',
'Error'
]
const eventListeners = [
['on', audio.addEventListener],
['off', audio.removeEventListener]
]

let _stopping = false

const eventCallbacks = {}
eventNames.forEach(eventName => {
eventListeners.forEach(([eventNameItem, listenerFn]) => {
Object.defineProperty(__audio, `${eventNameItem}${eventName}`, {
get () {
return (callback = audio.cb) => {
if (eventNameItem !== 'off') {
audio.cb = callback
const nativeName = eventName.toLowerCase()
eventCallbacks[nativeName] = []

Object.defineProperty(__audio, `on${eventName}`, {
get () {
return (cb) => {
const wrapper = nativeName === 'pause'
? (e) => { if (!_stopping) cb(e) }
: cb
eventCallbacks[nativeName].push({ cb, wrapper })
audio.addEventListener(nativeName, wrapper)
}
}
})

Object.defineProperty(__audio, `off${eventName}`, {
get () {
return (cb) => {
if (cb == null) {
eventCallbacks[nativeName].forEach(({ wrapper }) => audio.removeEventListener(nativeName, wrapper))
eventCallbacks[nativeName] = []
} else {
const idx = eventCallbacks[nativeName].findIndex(item => item.cb === cb)
if (idx > -1) {
audio.removeEventListener(nativeName, eventCallbacks[nativeName][idx].wrapper)
eventCallbacks[nativeName].splice(idx, 1)
}
// debugger
return listenerFn.call(audio, eventName.toLowerCase(), audio.cb)
}
}
})
}
})
})

Object.defineProperty(__audio, 'onStop', {
get () {
return (cb) => { _stopCbs.push(cb) }
}
})
Object.defineProperty(__audio, 'offStop', {
get () {
return (cb) => {
if (cb == null) {
_stopCbs.length = 0
} else {
const idx = _stopCbs.indexOf(cb)
if (idx > -1) _stopCbs.splice(idx, 1)
}
}
}
})

return __audio
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { nextTick } from '../next-tick'
import { parseDataset, warn } from '@mpxjs/utils'

let isInit = true

class WebIntersectionObserver {
constructor (_component, options) {
this._component = _component
Expand All @@ -14,6 +12,7 @@ class WebIntersectionObserver {
this._rootMargin = ''
this._disconnected = false
this._minThreshold = this.getMinThreshold()
this._isFirst = true
}

initObserver () {
Expand All @@ -25,7 +24,7 @@ class WebIntersectionObserver {
return new IntersectionObserver((entries, observer) => {
const initialRatio = this._options.initialRatio || 0
entries.forEach(entry => {
if (!isInit || (isInit && (entry.intersectionRatio !== initialRatio && (this._minThreshold <= entry.intersectionRatio)))) {
if (!this._isFirst || (this._isFirst && (entry.intersectionRatio !== initialRatio && (this._minThreshold <= entry.intersectionRatio)))) {
Object.defineProperties(entry, {
id: {
get () {
Expand Down Expand Up @@ -57,7 +56,7 @@ class WebIntersectionObserver {
this._callback && this._callback(entry)
}
})
isInit = false
this._isFirst = false
}, {
root: this._root || null,
rootMargin: this._rootMargin,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,20 @@ export function offNetworkStatusChange (callbackFn) {
throwSSRWarning('offNetworkStatusChange API is running in non browser environments')
return
}
if (callbackFn == null) {
// 不传 callback 时清除所有监听
fnMap.forEach((proxyCallback, originalCallback) => {
if (navigator.connection) {
navigator.connection.removeEventListener('change', proxyCallback)
}
})
fnMap.clear()
oldObserveList.clear()
return
}
if (navigator.connection) {
navigator.connection.removeEventListener('change', fnMap.get(callbackFn))
fnMap.delete(callbackFn)
} else {
oldObserveList.has(callbackFn) && oldObserveList.delete(callbackFn)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const onNetworkStatusChange = function (callback) {
const offNetworkStatusChange = function (callback) {
if (callback && typeof callback === 'function') {
_callbacks.delete(callback)
} else if (callback === undefined) {
} else if (callback == null) {
_callbacks.clear()
_unsubscribe && _unsubscribe()
_unsubscribe = null
Expand Down
8 changes: 8 additions & 0 deletions packages/api-proxy/src/platform/api/keyboard/index.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ const onKeyboardHeightChange = function (callback) {
callbacks.push(callback)
}
const offKeyboardHeightChange = function (callback) {
if (callback == null) {
// 不传 callback 时清除所有监听
callbacks.length = 0
Keyboard.removeAllListeners('keyboardDidShow')
Keyboard.removeAllListeners('keyboardDidHide')
hasListener = false
return
}
const index = callbacks.indexOf(callback)
if (index > -1) {
callbacks.splice(index, 1)
Expand Down
2 changes: 1 addition & 1 deletion packages/api-proxy/src/platform/api/location/index.web.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const getLocation = function (options = {}) {
errMsg: 'getLocation:ok',
latitude: coords.latitude,
longitude: coords.longitude,
speed: coords.accuracy
speed: coords.speed
}
defineUnsupportedProps(result, ['horizontalAccuracy', 'verticalAccuracy'])
successHandle(result, success, complete)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ function setNavigationBarColor (options = {}) {
return
}
const { backgroundColor, success, complete } = options
const meta = document.createElement('meta')
meta.setAttribute('name', 'theme-color')
let meta = document.querySelector('meta[name="theme-color"]')
if (!meta) {
meta = document.createElement('meta')
meta.setAttribute('name', 'theme-color')
document.head.appendChild(meta)
}
meta.setAttribute('content', backgroundColor)
document.head.appendChild(meta)
successHandle({ errMsg: 'setNavigationBarColor:ok' }, success, complete)
}

Expand Down
8 changes: 4 additions & 4 deletions packages/api-proxy/src/platform/api/socket/SocketTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class SocketTask {
this._closeData = null

if (protocols && protocols.length > 0) {
this._socket = new window.WebSocket(url, protocols)
this._socket = new WebSocket(url, protocols)
} else {
this._socket = new window.WebSocket(url)
this._socket = new WebSocket(url)
}
this.addListener(this._socket)
socketTasks.add(this._socket)
Expand Down Expand Up @@ -62,7 +62,7 @@ class SocketTask {
reason
}
try {
this._socket.close()
this._socket.close(code, reason)
const res = { errMsg: 'closeSocket:ok' }
successHandle(res, success, complete)
} catch (err) {
Expand Down Expand Up @@ -90,7 +90,7 @@ class SocketTask {
return
}
if (this._closeData) {
this._closeCb(event)
this._closeCb(this._closeData)
} else {
this._closeCb({ code: event.code, reason: event.reason })
}
Expand Down
6 changes: 1 addition & 5 deletions packages/api-proxy/src/platform/api/socket/index.web.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { warn, successHandle, failHandle, isBrowser, throwSSRWarning } from '../../../common/js'
import { warn, successHandle, failHandle } from '../../../common/js'
import SocketTask from './SocketTask'

function connectSocket (options = { url: '' }) {
if (!isBrowser) {
throwSSRWarning('connectSocket API is running in non browser environments')
return
}
const { url, protocols, success, fail, complete } = options

try {
Expand Down
8 changes: 2 additions & 6 deletions packages/api-proxy/src/platform/api/storage/rnStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ function removeStorage (options = {}) {
})
}

function removeStorageSync (key) {
AsyncStorage.removeItem(key)
}
const removeStorageSync = envError('removeStorageSync')

function clearStorage (options = {}) {
const { success, fail, complete } = options
Expand All @@ -121,9 +119,7 @@ function clearStorage (options = {}) {
})
}

function clearStorageSync () {
AsyncStorage.clear()
}
const clearStorageSync = envError('clearStorageSync')

export {
setStorage,
Expand Down
4 changes: 2 additions & 2 deletions packages/api-proxy/src/platform/api/system/index.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const getSystemInfoSync = function () {
model: DeviceInfo.getModel(),
system: `${DeviceInfo.getSystemName()} ${DeviceInfo.getSystemVersion()}`,
platform: DeviceInfo.isEmulatorSync() ? 'emulator' : DeviceInfo.getSystemName().toLowerCase(),
deviceOrientation: screenWidth > screenHeight ? 'portrait' : 'landscape',
deviceOrientation: screenWidth > screenHeight ? 'landscape' : 'portrait',
fontSizeSetting: PixelRatio.getFontScale()
}
Object.assign(result, windowInfo)
Expand Down Expand Up @@ -46,7 +46,7 @@ const getSystemInfo = function (options = {}) {
try {
const systemInfo = getSystemInfoSync()
Object.assign(systemInfo, {
errMsg: 'setStorage:ok'
errMsg: 'getSystemInfo:ok'
})
successHandle(systemInfo, success, complete)
} catch (err) {
Expand Down
5 changes: 5 additions & 0 deletions packages/api-proxy/src/platform/api/window/index.web.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ function onWindowResize (callback) {
}

function offWindowResize (callback) {
if (callback == null) {
// 不传 callback 时清除所有监听
callbacks.length = 0
return
}
const index = callbacks.indexOf(callback)
if (index > -1) {
callbacks.splice(index, 1)
Expand Down
6 changes: 6 additions & 0 deletions packages/api-proxy/src/platform/api/window/rnWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ function onWindowResize (callback) {
}

function offWindowResize (callback) {
if (callback == null) {
// 不传 callback 时清除所有监听
callbacks.length = 0
removeListener()
return
}
const index = callbacks.indexOf(callback)
if (index > -1) {
callbacks.splice(index, 1)
Expand Down
Loading