hi-weak
v1.0.0
Published
Nodejs weak references
Downloads
2
Readme
HI-WEAK
API
Exports
- exports.create(obj, listener) - 创建一个 WeakRef
- exports.wrap(obj, listener) - 创建一个用 Proxy 包装的 WeakRef
- exports.isWeakRef(obj) - 判断
- exports.weakDict() - 创建一个 value 为 WeakRef 的 字典
- exports.runGC([callback]) - 主动调用 nodejs GC 垃圾回收
- exports.addGCListener(listener) - 添加 GC 监听函数
- exports.removeGCListener(listener) - 删除 GC 监听函数
WeakRef Object
- WeakRef.prototype.raw() - 返回原对象,当原对象被回收后,返回 undefined
EXAMPLE
const Weak = require('hi-weak');
let obj = {};
let ref = Weak.create(obj, ()=>{
console.log('-- obj to be recycled --');
});
console.log(ref.raw());
const Weak = require('hi-weak');
let obj = {name: 'hi-lib'};
let proxy = Weak.wrap(obj, ()=>{
console.log('-- obj to be recycled --');
});
console.log( proxy.name );
// print "hi-lib"
// test
obj = null;
Weak.runGC(()=>{
console.log( proxy.name );
// print "undefined"
});
const Weak = require('hi-weak');
let obj = {name: 'hi-lib'};
let dict = Weak.weakDict();
dict.obj = obj;
console.log(dict.obj);
// print "{name: 'hi-lib'}"
// test
obj = null;
Weak.runGC(()=>{
console.log('-- gc --');
console.log( dict.obj );
// print "undefined"
});