function handlers(module_name) {
  function warn(prop) {
    console.warn(`TODO implement ${module_name}.${prop} in @nanoapp/nstd/node-libs.`)
  }

  return {
    get: function(dones, prop) {
      if (prop == "then") {
        return dones[prop];
      }
      return prop in dones ? dones[prop] : () => warn(prop)
    }
  }
}

export function todo(name, dones) {
  return new Proxy(dones, handlers(name))
}

export function sleep(millisecond) {
  return new Promise((resolve)=>{
    setTimeout(resolve, millisecond)
  })
}

// 类似process.env, os.homedir都是同步API,目前只能通过cache的形式，在模块初始化时就开始载入并存储起来。
export class MockCache {
  constructor(name) {
    this.name = name;
    this.cache = {}

    this._all = []
  }

  wait_all() {
    return Promise.all(this._all)
  }

  build(key, init) {
    this._all.push(init);

    init.then((v)=>{
      this.cache[key] = v
    })
    return () => {
      const v = this.cache[key]
      if (v === null) {
        throw `Please use "await ${this.name}.__init__()" for init ${this.name}.${field} for @nanoapp/nstd's nodejs mock`
      } else {
        return v
      }
    }
  }
}
