{"version":3,"file":"createAsyncStore.cjs","names":["ASYNC_STORE"],"sources":["../../../../src/createReduxAsyncStore/temp/createAsyncStore/createAsyncStore.ts"],"sourcesContent":["import { setReadOnly } from '@pastweb/tools';\nimport { ASYNC_STORE } from './constants';\nimport type { AsyncStoreOptions } from './types';\n\n/**\n * Creates an asynchronous store with the given options.\n *\n * @template T The type of the store to be created.\n * @param {AsyncStoreOptions} options The options for creating the asynchronous store.\n * @param {string} options.storeName The name of the store. This is required for better error debugging.\n * @param {number} [options.timeout=20000] The timeout limit in milliseconds for initializing the store.\n * @returns {T} The created asynchronous store.\n * @throws Will throw an error if the `storeName` option is not set.\n *\n * @example\n * const storeOptions = {\n *   storeName: 'myStore',\n *   timeout: 30000,\n * };\n * const myStore = createAsyncStore(storeOptions);\n */\nexport function createAsyncStore<T>(options: AsyncStoreOptions): T {\n  const { name, timeout = 20000 } = options;\n  \n  if (!name) {\n    throw Error('The \"name\" option must be set for a better error debugging.');\n  }\n  \n  let storeReadyInt: any = null;\n  \n  const asyncStore = {\n    options,\n    isStoreReady: false,\n    isReady: new Promise(checkReady),\n    setStoreReady,\n    init,\n  };\n\n\n  /**\n   * Checks if the store is ready at intervals and resolves the promise when it is ready.\n   *\n   * @param {(value: boolean) => void} resolve The resolve function of the promise.\n   */\n  function checkReady(resolve: (value: boolean) => void) {\n    storeReadyInt = setInterval(() => {\n      if (asyncStore.isStoreReady) {\n        clearInterval(storeReadyInt);\n        resolve(true);\n        return;    \n      }\n    }, 16);\n  }\n\n  /**\n   * Sets the store as ready.\n   */\n  function setStoreReady(): void {\n    asyncStore.isStoreReady = true;\n  }\n\n  /**\n   * Initializes the store and sets a timeout to throw an error if the init function is not implemented or if the initialization time is longer that the timeout value.\n   */\n  function init() {\n    setTimeout(() => {\n      throw Error(`The \"init\" function must be set to call the \"setStoreReady\" in \"${name}\", or the the store it is not initialized under ${timeout / 1000} seconds.`);\n    }, timeout);\n  }\n\n  Object.defineProperty(asyncStore, ASYNC_STORE, {\n    value: true,\n    enumerable: false,\n    writable: false,\n    configurable: false,\n  });\n\n  setReadOnly(asyncStore, ['setStoreReady', 'isReady']);\n\n  return asyncStore as T;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAqBA,SAAgB,iBAAoB,SAA+B;CACjE,MAAM,EAAE,MAAM,UAAU,QAAU;CAElC,IAAI,CAAC,MACH,MAAM,MAAM,+DAA6D;CAG3E,IAAI,gBAAqB;CAEzB,MAAM,aAAa;EACjB;EACA,cAAc;EACd,SAAS,IAAI,QAAQ,UAAU;EAC/B;EACA;CACF;;;;;;CAQA,SAAS,WAAW,SAAmC;EACrD,gBAAgB,kBAAkB;GAChC,IAAI,WAAW,cAAc;IAC3B,cAAc,aAAa;IAC3B,QAAQ,IAAI;IACZ;GACF;EACF,GAAG,EAAE;CACP;;;;CAKA,SAAS,gBAAsB;EAC7B,WAAW,eAAe;CAC5B;;;;CAKA,SAAS,OAAO;EACd,iBAAiB;GACf,MAAM,MAAM,mEAAmE,KAAK,kDAAkD,UAAU,IAAK,UAAU;EACjK,GAAG,OAAO;CACZ;CAEA,OAAO,eAAe,YAAYA,8DAAAA,aAAa;EAC7C,OAAO;EACP,YAAY;EACZ,UAAU;EACV,cAAc;CAChB,CAAC;CAED,CAAA,GAAA,eAAA,YAAA,CAAY,YAAY,CAAC,iBAAiB,SAAS,CAAC;CAEpD,OAAO;AACT"}