提交 5169abcb 编写于 作者: lizhongyi_'s avatar lizhongyi_

调整zuni-memorywarnin组件中的UTSCallback为普通callback

上级 b0121dfd
import { UTSAndroid } from "io.dcloud.uts" 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) => { const onAppTrimMemoryListener = (ret: number) => {
listeners.forEach(listener => { listeners.forEach(listener => {
let res = { let res: MemoryWarningCallbackResult = {
level:ret level:ret
} }
listener(res) listener(res)
}) })
} }
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
export const onMemoryWarning : OnMemoryWarning = function (callback: UTSCallback) { export const onMemoryWarning : OnMemoryWarning = function (callback: MemoryWarningCallback) {
if (listeners.length == 0) { if (listeners.length == 0) {
// 仅首次执行底层的实际监听 // 仅首次执行底层的实际监听
UTSAndroid.onAppTrimMemory(onAppTrimMemoryListener) UTSAndroid.onAppTrimMemory(onAppTrimMemoryListener)
...@@ -28,7 +28,7 @@ export const onMemoryWarning : OnMemoryWarning = function (callback: UTSCallbac ...@@ -28,7 +28,7 @@ export const onMemoryWarning : OnMemoryWarning = function (callback: UTSCallbac
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
export const offMemoryWarning : OffMemoryWarning = function (callback: UTSCallback | null) { export const offMemoryWarning : OffMemoryWarning = function (callback: MemoryWarningCallback | null) {
if(callback == null){ if(callback == null){
// 清除全部回调 // 清除全部回调
......
import { NotificationCenter } from 'Foundation'; import { NotificationCenter } from 'Foundation';
import { UIApplication } from "UIKit" import { UIApplication } from "UIKit"
import { Selector } from "ObjectiveC" import { Selector } from "ObjectiveC"
import { OnMemoryWarning, OffMemoryWarning } from "../interface.uts" import { OnMemoryWarning, OffMemoryWarning, MemoryWarningCallback, MemoryWarningCallbackResult } from "../interface.uts"
class MemoryWarningTool { 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("方法名") 构建 // target-action 回调方法需要通过 Selector("方法名") 构建
const method = Selector("receiveMemoryWarning") const method = Selector("receiveMemoryWarning")
NotificationCenter.default.addObserver(this, selector = method, name = UIApplication.didReceiveMemoryWarningNotification, object = null) NotificationCenter.default.addObserver(this, selector = method, name = UIApplication.didReceiveMemoryWarningNotification, object = null)
} }
this.listeners.push(callback) this.listener = callback
} }
// 内存警告回调的方法 // 内存警告回调的方法
// target-action 的方法前需要添加 @objc 前缀 // target-action 的方法前需要添加 @objc 前缀
@objc static receiveMemoryWarning() { @objc static receiveMemoryWarning() {
// 触发回调 // 触发回调
this.listeners.forEach(listener => { let res: MemoryWarningCallbackResult = {
listener({}) level: 0
}) };
this.listener?.(res);
} }
// 移除监听事件 // 移除监听事件
static removeListen(callback: UTSCallback | null) { static removeListen(callback: MemoryWarningCallback | null) {
// 移除所有监听 this.listener = null;
if (callback == null) { NotificationCenter.default.removeObserver(this)
this.listeners = []
// 移除监听事件
NotificationCenter.default.removeObserver(this)
return
}
// 清除指定回调
const index = this.listeners.indexOf(callback!)
if (index > -1) {
this.listeners.splice(index, 1)
}
} }
} }
// 开启监听内存警告 // 开启监听内存警告
export const onMemoryWarning : OnMemoryWarning = function (callback: UTSCallback) { export const onMemoryWarning : OnMemoryWarning = function (callback: MemoryWarningCallback) {
MemoryWarningTool.listenMemoryWarning(callback) MemoryWarningTool.listenMemoryWarning(callback)
} }
// 关闭监听内存警告 // 关闭监听内存警告
export const offMemoryWarning : OffMemoryWarning = function (callback: UTSCallback | null) { export const offMemoryWarning : OffMemoryWarning = function (callback: MemoryWarningCallback | null) {
MemoryWarningTool.removeListen(callback) MemoryWarningTool.removeListen(callback)
} }
\ No newline at end of file
/**
* 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 { export interface Uni {
/** /**
* 开启监听内存警告 * 开启监听内存警告
* *
* @param {UTSCallback} callback * @param {MemoryWarningCallback} callback
* @tutorial https://uniapp.dcloud.net.cn/api/system/memory.html#onmemorywarning * @tutorial https://uniapp.dcloud.net.cn/api/system/memory.html#onmemorywarning
* @uniPlatform { * @uniPlatform {
* "app": { * "app": {
...@@ -27,11 +41,11 @@ export interface Uni { ...@@ -27,11 +41,11 @@ export interface Uni {
* @uniVueVersion 2,3 //支持的vue版本 * @uniVueVersion 2,3 //支持的vue版本
* @autotest { expectCallback: true } * @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 * @tutorial https://uniapp.dcloud.net.cn/api/system/memory.html#offmemorywarning
* @uniPlatform { * @uniPlatform {
* "app": { * "app": {
...@@ -51,5 +65,5 @@ export interface Uni { ...@@ -51,5 +65,5 @@ export interface Uni {
* @uniVueVersion 2,3 //支持的vue版本 * @uniVueVersion 2,3 //支持的vue版本
* @autotest { expectCallback: true } * @autotest { expectCallback: true }
*/ */
offMemoryWarning(callback : UTSCallback | null) : void offMemoryWarning(callback : MemoryWarningCallback | null) : void
} }
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册