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

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

上级 b0121dfd
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 = {
let res: MemoryWarningCallbackResult = {
level:ret
}
listener(res)
......@@ -14,7 +14,7 @@ const onAppTrimMemoryListener = (ret: number) => {
@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){
// 清除全部回调
......
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 = []
// 移除监听事件
static removeListen(callback: MemoryWarningCallback | null) {
this.listener = null;
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)
}
// 关闭监听内存警告
export const offMemoryWarning : OffMemoryWarning = function (callback: UTSCallback | null) {
export const offMemoryWarning : OffMemoryWarning = function (callback: MemoryWarningCallback | null) {
MemoryWarningTool.removeListen(callback)
}
/**
* uni.onMemoryWarning/uni.offMemoryWarning回调参数
*/
export type MemoryWarningCallbackResult = {
/**
* 内存警告等级(仅安卓平台有效,iOS始终是0)
*/
level: number
}
/**
* uni.onMemoryWarning/uni.offMemoryWarning回调函数定义
*/
export type MemoryWarningCallback = (res: MemoryWarningCallbackResult) => void
export type OnMemoryWarning = (callback: UTSCallback) => 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
offMemoryWarning(callback : MemoryWarningCallback | null) : void
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册