diff --git a/uni_modules/uni-memorywarning/utssdk/app-android/index.uts b/uni_modules/uni-memorywarning/utssdk/app-android/index.uts index 28bf366a9359e188f6f53ea5bba905746b8a192c..ec4a9106ec63f5468ba459e4566fe23449865f03 100644 --- a/uni_modules/uni-memorywarning/utssdk/app-android/index.uts +++ b/uni_modules/uni-memorywarning/utssdk/app-android/index.uts @@ -1,20 +1,20 @@ import { UTSAndroid } from "io.dcloud.uts" -import { OnMemoryWarning, OffMemoryWarning } from "../interface.uts" +import { OnMemoryWarning, OffMemoryWarning, MemoryWarningCallback, MemoryWarningCallbackResult } from "../interface.uts" -let listeners: UTSCallback[] = [] +let listeners: MemoryWarningCallback[] = [] const onAppTrimMemoryListener = (ret: number) => { listeners.forEach(listener => { - let res = { - level:ret - } - listener(res) + let res: MemoryWarningCallbackResult = { + level:ret + } + listener(res) }) } @Suppress("DEPRECATION") -export const onMemoryWarning : OnMemoryWarning = function (callback: UTSCallback) { +export const onMemoryWarning : OnMemoryWarning = function (callback: MemoryWarningCallback) { if (listeners.length == 0) { // 仅首次执行底层的实际监听 UTSAndroid.onAppTrimMemory(onAppTrimMemoryListener) @@ -28,7 +28,7 @@ export const onMemoryWarning : OnMemoryWarning = function (callback: UTSCallbac @Suppress("DEPRECATION") -export const offMemoryWarning : OffMemoryWarning = function (callback: UTSCallback | null) { +export const offMemoryWarning : OffMemoryWarning = function (callback: MemoryWarningCallback | null) { if(callback == null){ // 清除全部回调 diff --git a/uni_modules/uni-memorywarning/utssdk/app-ios/index.uts b/uni_modules/uni-memorywarning/utssdk/app-ios/index.uts index 60ee47f5749bf69643f221b9d3ac18f7badaa9f1..5f4f37bfdc3f0e3bf20a5c98d9a90852b1b0171b 100644 --- a/uni_modules/uni-memorywarning/utssdk/app-ios/index.uts +++ b/uni_modules/uni-memorywarning/utssdk/app-ios/index.uts @@ -1,58 +1,47 @@ import { NotificationCenter } from 'Foundation'; import { UIApplication } from "UIKit" import { Selector } from "ObjectiveC" -import { OnMemoryWarning, OffMemoryWarning } from "../interface.uts" +import { OnMemoryWarning, OffMemoryWarning, MemoryWarningCallback, MemoryWarningCallbackResult } from "../interface.uts" class MemoryWarningTool { - static listeners: UTSCallback[] = [] - + static listener: MemoryWarningCallback | null = null // 监听内存警告 - static listenMemoryWarning(callback: UTSCallback) { - + static listenMemoryWarning(callback: MemoryWarningCallback) { + // 只有首次才需要注册监听事件 - if (this.listeners.length == 0) { + if (this.listener == null) { // 注册监听内存警告通知事件及设置回调方法 // target-action 回调方法需要通过 Selector("方法名") 构建 const method = Selector("receiveMemoryWarning") NotificationCenter.default.addObserver(this, selector = method, name = UIApplication.didReceiveMemoryWarningNotification, object = null) } - this.listeners.push(callback) + this.listener = callback } - + // 内存警告回调的方法 // target-action 的方法前需要添加 @objc 前缀 @objc static receiveMemoryWarning() { // 触发回调 - this.listeners.forEach(listener => { - listener({}) - }) + let res: MemoryWarningCallbackResult = { + level: 0 + }; + this.listener?.(res); } - + // 移除监听事件 - static removeListen(callback: UTSCallback | null) { - // 移除所有监听 - if (callback == null) { - this.listeners = [] - // 移除监听事件 - NotificationCenter.default.removeObserver(this) - return - } - - // 清除指定回调 - const index = this.listeners.indexOf(callback!) - if (index > -1) { - this.listeners.splice(index, 1) - } + static removeListen(callback: MemoryWarningCallback | null) { + this.listener = null; + NotificationCenter.default.removeObserver(this) } } // 开启监听内存警告 -export const onMemoryWarning : OnMemoryWarning = function (callback: UTSCallback) { +export const onMemoryWarning : OnMemoryWarning = function (callback: MemoryWarningCallback) { MemoryWarningTool.listenMemoryWarning(callback) } // 关闭监听内存警告 -export const offMemoryWarning : OffMemoryWarning = function (callback: UTSCallback | null) { +export const offMemoryWarning : OffMemoryWarning = function (callback: MemoryWarningCallback | null) { MemoryWarningTool.removeListen(callback) -} \ No newline at end of file +} diff --git a/uni_modules/uni-memorywarning/utssdk/interface.uts b/uni_modules/uni-memorywarning/utssdk/interface.uts index 5e11ec45b24fb0ba56d204fc2a9eab50d255b807..e512998c7d5a3ffa7c75fc649fbb6e7c06e5318e 100644 --- a/uni_modules/uni-memorywarning/utssdk/interface.uts +++ b/uni_modules/uni-memorywarning/utssdk/interface.uts @@ -1,13 +1,27 @@ +/** + * uni.onMemoryWarning/uni.offMemoryWarning回调参数 + */ +export type MemoryWarningCallbackResult = { + /** + * 内存警告等级(仅安卓平台有效,iOS始终是0) + */ + level: number +} -export type OnMemoryWarning = (callback: UTSCallback) => void +/** + * uni.onMemoryWarning/uni.offMemoryWarning回调函数定义 + */ +export type MemoryWarningCallback = (res: MemoryWarningCallbackResult) => void + +export type OnMemoryWarning = (callback: MemoryWarningCallback) => void -export type OffMemoryWarning = (callback : UTSCallback | null) => void +export type OffMemoryWarning = (callback : MemoryWarningCallback | null) => void export interface Uni { /** * 开启监听内存警告 - * - * @param {UTSCallback} callback + * + * @param {MemoryWarningCallback} callback * @tutorial https://uniapp.dcloud.net.cn/api/system/memory.html#onmemorywarning * @uniPlatform { * "app": { @@ -27,11 +41,11 @@ export interface Uni { * @uniVueVersion 2,3 //支持的vue版本 * @autotest { expectCallback: true } */ - onMemoryWarning(callback: UTSCallback) : void, + onMemoryWarning(callback: MemoryWarningCallback) : void, /** * 取消监听内存不足告警事件 - * - * @param {UTSCallback} callback + * + * @param {MemoryWarningCallback} callback * @tutorial https://uniapp.dcloud.net.cn/api/system/memory.html#offmemorywarning * @uniPlatform { * "app": { @@ -51,5 +65,5 @@ export interface Uni { * @uniVueVersion 2,3 //支持的vue版本 * @autotest { expectCallback: true } */ - offMemoryWarning(callback : UTSCallback | null) : void -} \ No newline at end of file + offMemoryWarning(callback : MemoryWarningCallback | null) : void +}