diff --git a/uni_modules/uni-memorywarning/utssdk/app-android/index.uts b/uni_modules/uni-memorywarning/utssdk/app-android/index.uts index 5839e4db13b3e624d2b648c67c34085abdc9330b..b364d0431a189b41ce5eb855e5d4a475b8b54889 100644 --- a/uni_modules/uni-memorywarning/utssdk/app-android/index.uts +++ b/uni_modules/uni-memorywarning/utssdk/app-android/index.uts @@ -1,50 +1,44 @@ import { UTSAndroid } from "io.dcloud.uts" -let listeners : UTSCallback[] = [] +let listeners: UTSCallback[] = [] -const onAppTrimMemoryListener = (res : number) => { - listeners.forEach(listener => { - listener(res) - }) +const onAppTrimMemoryListener = (res: number) => { + listeners.forEach(listener => { + listener(res) + }) } @Suppress("DEPRECATION") -export function onMemoryWarning(callback : (res : number) => void) { - if (listeners.length == 0) { +export function onMemoryWarning(callback: (res: number) => void) { + if (listeners.length == 0) { // 仅首次执行底层的实际监听 - UTSAndroid.onAppTrimMemory((res : number) => { - onAppTrimMemoryListener(res) - }) - UTSAndroid.onAppActivityDestroy(() => { + UTSAndroid.onAppTrimMemory(onAppTrimMemoryListener) + UTSAndroid.onAppActivityDestroy(()=>{ // listeners 默认是静态常量周期,activity 销毁时,需要手动清空 listeners = [] }) - } - listeners.push(callback) + } + listeners.push(callback) } - @Suppress("DEPRECATION") -export function offMemoryWarning(callback : UTSCallback | null = null) { - - if (callback == null) { +export function offMemoryWarning(callback: UTSCallback | null = null) { + + if(callback == null){ // 清除全部回调 listeners = [] UTSAndroid.offAppTrimMemory(null); return } - + // 清除指定回调 - const index = listeners.indexOf(callback) - if (index > -1) { - listeners.splice(index, 1) - } - if (listeners.length == 0) { - // 当用户不再监听时,移除底层实际监听 - UTSAndroid.offAppTrimMemory((res : number) => { - onAppTrimMemoryListener(res) - }) - } + const index = listeners.indexOf(callback) + if (index > -1) { + listeners.splice(index, 1) + } + if (listeners.length == 0) { + // 当用户不再监听时,移除底层实际监听 + UTSAndroid.offAppTrimMemory(onAppTrimMemoryListener) + } } -