|
const { defineLazyGetter } = require('./LazyGetter.js')
exports.defineLazyGetter = (aObject, aName, aLambda) => {
if (Object.prototype.hasOwnProperty.call(aObject, aName)) {
/* TODO: using global variable to make sure not to be called multiple times */
console.log(`Define ${aName} on an object multiple times ${components.stack.caller.caller.filename}`)
return
}
return Object.defineProperty(aObject, aName, {
get: function () {
// Redefine this accessor property as a data property.
// Delete it first, to rule out "too much recursion" in case aObject is
// a proxy whose defineProperty handler might unwittingly trigger this
// getter again.
delete aObject[aName];
let value = aLambda.apply(aObject);
Object.defineProperty(aObject, aName, {
value,
writable: true,
configurable: true,
enumerable: true
});
return value;
},
configurable: true,
enumerable: true
});
}
Globals.app.defineLazyGetter = (name, lambda) => {
return defineLazyGetter(Globals.app, name, lambda)
}
// 调用
Globals.app.defineLazyGetter('messageCache', () => new Map())
// 使用
Globals.app.messageCache.has(msgURI)
Globals.app.messageCache.get(msgURI)
Globals.app.messageCache.set(msgURI, result)
|
|