firemail

标题: defineLazyGetter [打印本页]

作者: hechengjin    时间: 2016-4-21 16:15
标题: defineLazyGetter


  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)









欢迎光临 firemail (http://firemail.wang:8088/) Powered by Discuz! X3