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

uts-memory plugin basic done.

上级 bf7484bc
...@@ -33,7 +33,8 @@ ...@@ -33,7 +33,8 @@
"uni_modules": { "uni_modules": {
"uni-ext-api":{ "uni-ext-api":{
"uni": { "uni": {
"onMemoryWarning": "onMemoryWarning" "onMemoryWarning": "onMemoryWarning",
"offMemoryWarning":"offMemoryWarning"
}, },
"mp-weixin":false, "mp-weixin":false,
"mp-alipay":false, "mp-alipay":false,
......
import { onAppTrimMemory } from "io.dcloud.uts.android"; import { onAppTrimMemory, offAppTrimMemory ,onAppActivityDestroy} from "io.dcloud.uts.android"
export function onMemoryWarning(callback: (res:Number) => void) { let listeners: UTSCallback[] = []
// console.log("here");
onAppTrimMemory((level:Number) => { const onAppTrimMemoryListener = (res: number) => {
console.log(level); listeners.forEach(listener => {
callback(level); console.log("listener " + listener);
}); listener(res)
// return ; })
} }
export function onMemoryWarning(callback: (res: number) => void) {
export function offMemoryWarning(success: (res: any) => void) { if (listeners.length === 0) {
// TODO 没有实现卸载监听 // 仅首次执行底层的实际监听
let ret = { onAppTrimMemory(onAppTrimMemoryListener)
code:0 onAppActivityDestroy(()=>{
// listeners 默认是静态常量周期,activity 销毁时,需要手动清空
listeners = []
})
} }
success(ret); listeners.push(callback)
} }
export function offMemoryWarning(callback: ((res: number) => void) | null) {
if(callback == null){
// 清除全部回调
listeners = []
return
}
// 清除指定回调
const index = listeners.indexOf(callback)
if (index > -1) {
listeners.splice(index, 1)
}
if (listeners.length === 0) {
// 当用户不再监听时,移除底层实际监听
offAppTrimMemory(onAppTrimMemoryListener)
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册