From 5169abcb6416488fe91108f9327a3456655518f1 Mon Sep 17 00:00:00 2001 From: lizhongyi Date: Thu, 9 Nov 2023 20:30:12 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4zuni-memorywarnin=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=E4=B8=AD=E7=9A=84UTSCallback=E4=B8=BA=E6=99=AE?= =?UTF-8?q?=E9=80=9Acallback?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../utssdk/app-android/index.uts | 16 +++---- .../utssdk/app-ios/index.uts | 47 +++++++------------ .../uni-memorywarning/utssdk/interface.uts | 32 +++++++++---- 3 files changed, 49 insertions(+), 46 deletions(-) diff --git a/uni_modules/uni-memorywarning/utssdk/app-android/index.uts b/uni_modules/uni-memorywarning/utssdk/app-android/index.uts index 28bf366..ec4a910 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 60ee47f..5f4f37b 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 5e11ec4..e512998 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 +} -- GitLab