GC文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/gc-introduction-V5 只是粗略概述,是否有详细文档。
以下是代码参考:
interface RRIoTMessageListener {
(message: any): void;
}
class MessageHandlerRegistry {
private listeners: Map<string, Set<WeakRef<RRIoTMessageListener>>>;
private finalizationRegistry: FinalizationRegistry<WeakRef<RRIoTMessageListener>>;
constructor() {
this.listeners = new Map<string, Set<WeakRef<RRIoTMessageListener>>>();
this.finalizationRegistry = new FinalizationRegistry((weakRef) => {
// 遍历所有 topic,移除已经被垃圾回收的 listener
for (const [topic, listeners] of this.listeners.entries()) {
listeners.delete(weakRef);
if (listeners.size === 0) {
this.listeners.delete(topic);
}
}
});
}
registerMessageHandler(listener: RRIoTMessageListener, topic: string): void {
const weakRef = new WeakRef(listener);
if (!this.listeners.has(topic)) {
this.listeners.set(topic, new Set<WeakRef<RRIoTMessageListener>>());
}
this.listeners.get(topic)!.add(weakRef);
this.finalizationRegistry.register(listener, weakRef);
}
notifyListeners(topic: string, message: any): void {
if (this.listeners.has(topic)) {
const listeners = this.listeners.get(topic)!;
for (const weakRef of listeners) {
const listener = weakRef.deref();
if (listener) {
listener(message);
} else {
listeners.delete(weakRef); // 移除已被垃圾回收的 weakRef
}
}
if (listeners.size === 0) {
this.listeners.delete(topic);
}
}
}
}
直接赋值为空
class SmartDevice {
private deviceName: string;
constructor(deviceName: string) {
this.deviceName = deviceName;
}
onMessageReceived(message: any): void {
console.log(`${this.deviceName} received: ${message}`);
}
}
// 使用示例
const registry = new MessageHandlerRegistry();
const device1 = new SmartDevice('Device 1');
const device2 = new SmartDevice('Device 2');
// 注册 SmartDevice 实例
registry.registerMessageHandler(device1.onMessageReceived.bind(device1), 'topic1');
registry.registerMessageHandler(device2.onMessageReceived.bind(device2), 'topic1');
// 发送消息
registry.notifyListeners('topic1', 'Hello, World!');
// 输出:
// Device 1 received: Hello, World!
// Device 2 received: Hello, World!
// 模拟垃圾回收
device1 = null;
device2 = null;
// 假设垃圾回收已经发生
registry.notifyListeners('topic1', 'Hello again!');
// 不会有任何输出,因为 device1 和 device2 都已被销毁
const registry = new MessageHandlerRegistry();
{
const device = new SmartDevice('Smart Device');
const monitoringSystem = new MonitoringSystem('Monitoring System');
// 注册观察者
registry.registerMessageHandler(device);
registry.registerMessageHandler(monitoringSystem);
// 发送消息
registry.notifyDpsMessage('topic1', 'Hello, World!', 1);
registry.notifyDeviceOnlineMessage('device123', true);
// 输出:
// Smart Device received DPS message: Hello, World! on topic topic1 with QoS 1
// Monitoring System monitoring DPS message: Hello, World! on topic topic1 with QoS 1
// Smart Device detected device device123 is online
// Monitoring System detected device device123 is online
}
// 由于device和monitoringSystem在块作用域结束后没有其他引用,将被垃圾回收
// 假设垃圾回收已经发生
registry.notifyDpsMessage('topic1', 'Hello again!', 1);
registry.notifyDeviceOnlineMessage('device123', false);
// 不会有任何输出,因为 device 和 monitoringSystem 都已被销毁
class对象不需要时就自动释放了,使用的是GC自动回收机制,不是引用机制。参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/gc-introduction-V5