Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
uni-api
提交
5169abcb
U
uni-api
项目概览
DCloud
/
uni-api
通知
671
Star
23
Fork
12
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
3
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-api
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
3
Issue
3
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
5169abcb
编写于
11月 09, 2023
作者:
lizhongyi_
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
调整zuni-memorywarnin组件中的UTSCallback为普通callback
上级
b0121dfd
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
49 addition
and
46 deletion
+49
-46
uni_modules/uni-memorywarning/utssdk/app-android/index.uts
uni_modules/uni-memorywarning/utssdk/app-android/index.uts
+8
-8
uni_modules/uni-memorywarning/utssdk/app-ios/index.uts
uni_modules/uni-memorywarning/utssdk/app-ios/index.uts
+18
-29
uni_modules/uni-memorywarning/utssdk/interface.uts
uni_modules/uni-memorywarning/utssdk/interface.uts
+23
-9
未找到文件。
uni_modules/uni-memorywarning/utssdk/app-android/index.uts
浏览文件 @
5169abcb
import { UTSAndroid } from "io.dcloud.uts"
import { OnMemoryWarning, OffMemoryWarning } from "../interface.uts"
import { OnMemoryWarning, OffMemoryWarning
, MemoryWarningCallback, MemoryWarningCallbackResult
} from "../interface.uts"
let listeners:
UTS
Callback[] = []
let listeners:
MemoryWarning
Callback[] = []
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:
UTS
Callback) {
export const onMemoryWarning : OnMemoryWarning = function (callback:
MemoryWarning
Callback) {
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:
UTS
Callback | null) {
export const offMemoryWarning : OffMemoryWarning = function (callback:
MemoryWarning
Callback | null) {
if(callback == null){
// 清除全部回调
...
...
uni_modules/uni-memorywarning/utssdk/app-ios/index.uts
浏览文件 @
5169abcb
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:
UTS
Callback) {
static listenMemoryWarning(callback:
MemoryWarning
Callback) {
// 只有首次才需要注册监听事件
if (this.listener
s.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:
UTS
Callback) {
export const onMemoryWarning : OnMemoryWarning = function (callback:
MemoryWarning
Callback) {
MemoryWarningTool.listenMemoryWarning(callback)
}
// 关闭监听内存警告
export const offMemoryWarning : OffMemoryWarning = function (callback:
UTS
Callback | null) {
export const offMemoryWarning : OffMemoryWarning = function (callback:
MemoryWarning
Callback | null) {
MemoryWarningTool.removeListen(callback)
}
\ No newline at end of file
}
uni_modules/uni-memorywarning/utssdk/interface.uts
浏览文件 @
5169abcb
/**
* 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 :
UTS
Callback | null) => void
export type OffMemoryWarning = (callback :
MemoryWarning
Callback | 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:
UTS
Callback) : void,
onMemoryWarning(callback:
MemoryWarning
Callback) : 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
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录