提交 492e5909 编写于 作者: 杜庆泉's avatar 杜庆泉

手动修复内存监控 组件 3.7.6之后编译器新语法

上级 6d53d9ea
import { UTSAndroid } from "io.dcloud.uts" import { UTSAndroid } from "io.dcloud.uts"
let listeners: UTSCallback[] = [] let listeners : UTSCallback[] = []
const onAppTrimMemoryListener = (res: number) => { const onAppTrimMemoryListener = (res : number) => {
listeners.forEach(listener => { listeners.forEach(listener => {
listener(res) listener(res)
}) })
...@@ -10,11 +10,13 @@ const onAppTrimMemoryListener = (res: number) => { ...@@ -10,11 +10,13 @@ const onAppTrimMemoryListener = (res: number) => {
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
export function onMemoryWarning(callback: (res: number) => void) { export function onMemoryWarning(callback : (res : number) => void) {
if (listeners.length == 0) { if (listeners.length == 0) {
// 仅首次执行底层的实际监听 // 仅首次执行底层的实际监听
UTSAndroid.onAppTrimMemory(onAppTrimMemoryListener) UTSAndroid.onAppTrimMemory((res : number) => {
UTSAndroid.onAppActivityDestroy(()=>{ onAppTrimMemoryListener(res)
})
UTSAndroid.onAppActivityDestroy(() => {
// listeners 默认是静态常量周期,activity 销毁时,需要手动清空 // listeners 默认是静态常量周期,activity 销毁时,需要手动清空
listeners = [] listeners = []
}) })
...@@ -22,10 +24,11 @@ export function onMemoryWarning(callback: (res: number) => void) { ...@@ -22,10 +24,11 @@ export function onMemoryWarning(callback: (res: number) => void) {
listeners.push(callback) listeners.push(callback)
} }
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
export function offMemoryWarning(callback: UTSCallback | null = null) { export function offMemoryWarning(callback : UTSCallback | null = null) {
if(callback == null){ if (callback == null) {
// 清除全部回调 // 清除全部回调
listeners = [] listeners = []
UTSAndroid.offAppTrimMemory(null); UTSAndroid.offAppTrimMemory(null);
...@@ -39,6 +42,9 @@ export function offMemoryWarning(callback: UTSCallback | null = null) { ...@@ -39,6 +42,9 @@ export function offMemoryWarning(callback: UTSCallback | null = null) {
} }
if (listeners.length == 0) { if (listeners.length == 0) {
// 当用户不再监听时,移除底层实际监听 // 当用户不再监听时,移除底层实际监听
UTSAndroid.offAppTrimMemory(onAppTrimMemoryListener) UTSAndroid.offAppTrimMemory((res : number) => {
onAppTrimMemoryListener(res)
})
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册