{"version":3,"file":"oinit.mjs","sources":["../src/index.ts"],"sourcesContent":["export default function oi<T, P extends Array<any>>(\n  promise: (...param: P) => Promise<T>,\n  defaultValue?: T\n): OnceInit<T, P>;\n\nexport default function oi<T, P extends Array<any> = void[]>(\n  ...args: any[]\n): OnceInit<T, P> {\n  if (!(args[0] instanceof Function) || args.length > 2 || args.length < 1) {\n    throw new Error(\"Arguments of oi is not supported\");\n  }\n  const promise: (...param: P) => Promise<T> = args[0];\n  if (args.length === 1) {\n    return new (class extends OnceInit<T, P> {\n      protected initPromise = promise;\n    })();\n  }\n  const defaultValue: T = args[1];\n  return new (class extends OnceInit<T, P> {\n    protected initPromise = promise;\n  })(defaultValue);\n}\n\nexport abstract class OnceInit<T, P extends Array<any> = void[]> {\n  protected observe: T | void;\n  protected promise: Promise<T> | null = null;\n  constructor(defaultValue?: T) {\n    this.observe = defaultValue;\n  }\n  protected abstract initPromise(...param: P): Promise<T>;\n  protected initialized: boolean = false;\n\n  get target(): T | void {\n    if (!this.initialized && this.initPromise.length === 0) {\n      this.refresh(...([] as unknown as P));\n    }\n    return this.observe;\n  }\n  async init(...param: P): Promise<T | void> {\n    if (this.promise) {\n      return this.promise.finally(() => this.observe);\n    }\n    if (!this.initialized) {\n      await this.refresh(...param);\n    }\n    return this.target;\n  }\n\n  refresh = async (...param: P): Promise<T | void> => {\n    if (!this.promise) {\n      this.promise = this.initPromise(...param);\n      this.observe = await this.promise;\n      this.promise = null;\n      this.initialized = true;\n      return this.observe;\n    } else {\n      return this.promise.finally(() => {\n        return this.observe;\n      });\n    }\n  };\n}\n"],"names":["oi","args","Function","length","Error","promise","OnceInit","initPromise","defaultValue","constructor","observe","initialized","refresh","_this","finally","this","target","init","_this2"],"mappings":"SAKwBA,QACnBC,2BAEH,KAAMA,EAAK,aAAcC,WAAaD,EAAKE,OAAS,GAAKF,EAAKE,OAAS,EACrE,UAAUC,MAAM,oCAElB,MAAMC,EAAuCJ,EAAK,GAClD,GAAoB,IAAhBA,EAAKE,OACP,yBAA0BG,qCACdC,YAAcF,IAG5B,MAAMG,EAAkBP,EAAK,GAC7B,yBAA0BK,qCACdC,YAAcF,IACvBG,SAGiBF,EAGpBG,YAAYD,QAFFE,oBACAL,QAA6B,UAK7BM,aAAuB,OAkBjCC,iEACE,OAAKC,EAAKR,wBAODQ,EAAKR,QAAQS,QAAQ,IACnBD,EAAKH,WAPdG,EAAKR,QAAUQ,EAAKN,iDACCM,EAAKR,0BAG1B,OAHAQ,EAAKH,UACLG,EAAKR,QAAU,KACfQ,EAAKF,aAAc,EACZE,EAAKH,mDA3BdK,KAAKL,QAAUF,EAKbQ,aAIF,OAHKD,KAAKJ,aAA2C,IAA5BI,KAAKR,YAAYJ,QACxCY,KAAKH,eAEKF,QAERO,mBACAF,8BAMJ,SAAYC,OANZ,GAAIE,EAAKb,QACP,uBAAOa,EAAKb,QAAQS,QAAQ,IAAMI,EAAKR,iCAEpCQ,EAAKP,mCACFO,EAAKN,oGALL"}