{"version":3,"file":"scope-DkX72a-2.cjs","names":["hash: unique symbol","isPlainObject","factory: (...args: Args) => T","cacheTime?: number","name?: string","allResources: ResourceGroup","Store","args: Args","options: CacheOptions<T, Args>","derivedFromCache?: {\n      cache: Cache<any, any>;\n      selectors: (Selector<any, any> | AnyPath)[];\n    }","createStore","calculatedValue","PromiseWithState","calcDuration","makeSelector","options","baseInstance: CacheBundle<T, Args, TCache> & TCache","defaultCacheOptions: CacheOptions<any, any>","deepEqual","createCache: typeof create & { defaultOptions: CacheOptions<any, any> }","defaultValue: T"],"sources":["../src/lib/hash.ts","../src/lib/instanceCache.ts","../src/core/resourceGroup.ts","../src/core/cache.ts","../src/core/scope.ts"],"sourcesContent":["import { isPlainObject } from '@lib/helpers';\n\nexport interface Hashable {\n  [hash](): string;\n}\n\nexport const hash: unique symbol = Symbol('hash');\n\nfunction hasHashFunction(value: unknown): value is Hashable {\n  return (\n    typeof value === 'object' &&\n    value !== null &&\n    hash in value &&\n    typeof (value as any)[hash] === 'function' &&\n    (value as any)[hash].length === 0\n  );\n}\n\nexport function simpleHash(value: unknown): string {\n  if (hasHashFunction(value)) {\n    return value[hash]();\n  }\n\n  if (value instanceof Set) {\n    return `s[${[...value].map(simpleHash).sort().join(',')}]`;\n  }\n\n  if (value instanceof Map) {\n    return `m[${[...value.entries()].map(simpleHash).sort().join(',')}]`;\n  }\n\n  if (Array.isArray(value)) {\n    return `[${value.map(simpleHash).join(',')}]`;\n  }\n\n  if (isPlainObject(value)) {\n    return `o[${Object.entries(value).map(simpleHash).sort().join(',')}]`;\n  }\n\n  return JSON.stringify(value);\n}\n","import { simpleHash } from './hash';\n\nexport class InstanceCache<Args extends any[], T extends object> {\n  private cache = new Map<string, { t: number; ref?: T; weakRef: WeakRef<T> }>();\n\n  private interval = this.cacheTime\n    ? setInterval(() => this.cleanup(), Math.max(this.cacheTime / 10, 1))\n    : undefined;\n\n  constructor(\n    public readonly factory: (...args: Args) => T,\n    public readonly cacheTime?: number,\n  ) {}\n\n  cleanup(): void {\n    const cutoff = this.now() - (this.cacheTime ?? 0);\n\n    for (const [key, entry] of this.cache.entries()) {\n      if (entry.ref && entry.t <= cutoff) {\n        delete entry.ref;\n      }\n\n      if (!entry.ref && !entry.weakRef?.deref()) {\n        this.cache.delete(key);\n      }\n    }\n  }\n\n  get(...args: Args): T {\n    return this.getWithKey(args, args);\n  }\n\n  getWithKey(args: Args, cacheKey: unknown): T {\n    const key = simpleHash(cacheKey);\n    let entry = this.cache.get(key);\n    let value = entry?.ref ?? entry?.weakRef?.deref();\n\n    if (!entry || !value) {\n      value = this.factory(...args);\n      entry = {\n        t: this.now(),\n        ref: value,\n        weakRef: new WeakRef(value),\n      };\n\n      this.cache.set(key, entry);\n    } else {\n      entry.t = this.now();\n      entry.ref ??= value;\n    }\n\n    return value;\n  }\n\n  values(): T[] {\n    return [...this.cache.values()]\n      .map((entry) => entry.ref ?? entry.weakRef?.deref())\n      .filter((value): value is T => !!value);\n  }\n\n  stop(): void {\n    if (this.interval) {\n      clearInterval(this.interval);\n    }\n  }\n\n  stats(): { count: number; withRef: number; withWeakRef: number } {\n    return {\n      count: this.cache.size,\n      withRef: [...this.cache.values()].filter((x) => !!x.ref).length,\n      withWeakRef: [...this.cache.values()].filter((x) => !!x.weakRef?.deref()).length,\n    };\n  }\n\n  private now() {\n    return performance.now();\n  }\n}\n","import { autobind } from '@lib/autobind';\n\nexport interface Resource {\n  invalidateAll(): void;\n  clearAll(): void;\n}\n\nexport class ResourceGroup {\n  private refMap = new WeakMap<Resource, WeakRef<Resource>>();\n  private refSet = new Set<WeakRef<Resource>>();\n  private timer = setInterval(() => this.cleanup(), 60_000);\n\n  private registry = new FinalizationRegistry<WeakRef<Resource>>((ref) => {\n    this.refSet.delete(ref);\n  });\n\n  constructor(public readonly name?: string) {\n    autobind(ResourceGroup);\n  }\n\n  add(resource: Resource): void {\n    const ref = new WeakRef(resource);\n    this.refMap.set(resource, ref);\n    this.refSet.add(ref);\n    this.registry.register(resource, ref, resource);\n  }\n\n  delete(resource: Resource): void {\n    const ref = this.refMap.get(resource);\n    if (ref) {\n      this.refMap.delete(resource);\n      this.refSet.delete(ref);\n      this.registry.unregister(resource);\n    }\n  }\n\n  invalidateAll(): void {\n    for (const ref of this.refSet) {\n      const resource = ref.deref();\n      if (resource) {\n        resource.invalidateAll();\n      } else {\n        this.refSet.delete(ref);\n      }\n    }\n  }\n\n  clearAll(): void {\n    for (const ref of this.refSet) {\n      const resource = ref.deref();\n      if (resource) {\n        resource.clearAll();\n      } else {\n        this.refSet.delete(ref);\n      }\n    }\n  }\n\n  cleanup(): void {\n    for (const ref of this.refSet) {\n      if (!ref.deref()) {\n        this.refSet.delete(ref);\n      }\n    }\n  }\n\n  stop(): void {\n    clearInterval(this.timer);\n    this.refSet.clear();\n  }\n}\n\nexport const allResources: ResourceGroup = /* @__PURE__ */ new ResourceGroup();\n\nexport function createResourceGroup(name?: string): ResourceGroup {\n  return new ResourceGroup(name);\n}\n","import { autobind } from '@lib/autobind';\nimport type { CacheState, ErrorState, ValueState } from '@lib/cacheState';\nimport { calcDuration } from '@lib/duration';\nimport { calculatedValue } from '@lib/calculatedValue';\nimport type { Constrain } from '@lib/constrain';\nimport { deepEqual } from '@lib/equals';\nimport { InstanceCache } from '@lib/instanceCache';\nimport { makeSelector } from '@lib/makeSelector';\nimport { type MaybePromise } from '@lib/maybePromise';\nimport type { AnyPath, Path, Value } from '@lib/path';\nimport { PromiseWithState } from '@lib/promiseWithState';\nimport type { Duration, Selector } from './commonTypes';\nimport { allResources, type ResourceGroup } from './resourceGroup';\nimport { Store, createStore, type Calculate, type StoreOptions } from './store';\n\nexport interface CacheGetOptions {\n  /**\n   * How to handle the cache when getting the value.\n   * - `whenMissing`: Only fetch a new value if there is no cached value.\n   * - `whenStale`: Fetch a new value if there is no cached value or if the cached value is stale.\n   * - `force`: Always fetch a new value, regardless of the cache state.\n   */\n  update?: 'whenMissing' | 'whenStale' | 'force';\n\n  /**\n   * If set to `true`, the cache will be updated in the background.\n   * This means that a stale value will be returned immediately, if available, while the new value is being fetched.\n   */\n  backgroundUpdate?: boolean;\n}\n\nexport interface CacheFunction<T, Args extends any[] = []> {\n  (...args: Args): Promise<T> | Calculate<Promise<T>>;\n}\n\nexport interface CacheOptions<T, Args extends any[]> extends StoreOptions<Promise<T>> {\n  /**\n   * How long to keep the cache entry before it is considered stale.\n   * If set to `undefined` or `null`, the cache entry will never be invalidated automatically.\n   *\n   * @example\n   * ```typescript\n   * createCache(fetchData, {\n   *   invalidateAfter: { seconds: 10 },\n   * });\n   * ```\n   */\n  invalidateAfter?: Duration | ((state: ValueState<T> | ErrorState) => Duration | null) | null;\n\n  /**\n   * If set, the cache will be invalidated when the window gets focused.\n   * This is useful for caches that are used in a browser environment and might become stale when the user switches tabs.\n   */\n  invalidateOnWindowFocus?: boolean;\n\n  /**\n   * If set, the cache will be invalidated when it becomes active - e.g. when it is subscribed to or a component using the cache mounts.\n   */\n  invalidateOnActivation?: boolean;\n\n  /**\n   * If set, the cached value will be cleared when the cache is invalidated.\n   * Without this option, the cache will keep the last value as stale until a new value becomes available.\n   */\n  clearOnInvalidate?: boolean;\n\n  /**\n   * If set, cache entries will be cleared after approximately the specified duration.\n   * This is useful for long lived pages or applications and helps to prevent memory leaks.\n   * The exact time when the entry is cleared is not guaranteed, since it will be cleared during garbage collection.\n   */\n  clearUnusedAfter?: Duration | null;\n\n  /**\n   * Add the cache to the specified resource group(s).\n   * This allows you to invalidate or clear multiple caches that belong to the same group.\n   * All caches are always added to the `allResources` group.\n   */\n  resourceGroup?: ResourceGroup | ResourceGroup[];\n\n  /**\n   * Function to generate a custom cache key based on the provided arguments.\n   * This allows you to control how cache entries are identified and reused.\n   * By default, the arguments array is used as the cache key.\n   *\n   * @example\n   * ```typescript\n   * // Will use the same instance when provided with `undefined`, `{ num: 0 }`, `{ bool: false }`, etc.\n   * createCache((filter?: { num?: number, bool?: boolean }) => fetchData(filter), {\n   *   getCacheKey: (filter?) => ({\n   *     num: filter?.num ?? 0,\n   *     bool: filter?.bool ?? false,\n   *   }),\n   * });\n   * ```\n   */\n  getCacheKey?: (...args: Args) => unknown;\n}\n\nexport class Cache<T, Args extends any[] = []> extends Store<Promise<T>> {\n  readonly state: Store<CacheState<T>> = createStore<CacheState<T>>({\n    status: 'pending',\n    isStale: true,\n    isUpdating: false,\n    isConnected: false,\n  });\n\n  protected stalePromise?: Promise<T>;\n\n  protected invalidationTimer?: ReturnType<typeof setTimeout>;\n\n  constructor(\n    getter: Calculate<Promise<T>>,\n    public readonly args: Args,\n    public readonly options: CacheOptions<T, Args> = {},\n    public readonly derivedFromCache?: {\n      cache: Cache<any, any>;\n      selectors: (Selector<any, any> | AnyPath)[];\n    },\n  ) {\n    super(getter, options, undefined);\n    autobind(Cache);\n\n    this.watchPromise();\n    this.watchFocus();\n    this.addEffect(this.onActivation);\n  }\n\n  get({ update = 'whenStale', backgroundUpdate = false }: CacheGetOptions = {}): Promise<T> {\n    if (!this.calculatedValue?.check()) {\n      this.calculatedValue?.stop();\n      this.calculatedValue = undefined;\n    }\n\n    const promise = this.calculatedValue?.value;\n    const stalePromise = this.stalePromise;\n\n    if (\n      (update === 'whenMissing' && !promise && !stalePromise) ||\n      (update === 'whenStale' && !promise) ||\n      update === 'force'\n    ) {\n      this.calculatedValue = calculatedValue(this, this.notify);\n      this.notify();\n\n      if ((!promise && !stalePromise) || !backgroundUpdate) {\n        return this.calculatedValue.value;\n      }\n    }\n\n    if (!promise || (stalePromise && backgroundUpdate)) {\n      return stalePromise!;\n    }\n\n    return promise;\n  }\n\n  updateValue(value: MaybePromise<T> | ((value: T | undefined) => T)): void {\n    if (value instanceof Function) {\n      value = value(this.state.get().value);\n    }\n    this.set(PromiseWithState.resolve(value));\n  }\n\n  updateError(error: unknown): void {\n    this.set(PromiseWithState.reject(error));\n  }\n\n  invalidate(recursive?: boolean): void {\n    const { clearOnInvalidate } = this.options;\n\n    if (clearOnInvalidate) {\n      return this.clear(recursive);\n    }\n\n    const { status, isStale, isUpdating } = this.state.get();\n    if (status !== 'pending' && !isStale && !isUpdating) {\n      this.stalePromise = this.calculatedValue?.value;\n    }\n\n    this.state.set((state) => ({\n      ...state,\n      isStale: true,\n      isUpdating: false,\n    }));\n\n    super.invalidate(recursive);\n  }\n\n  clear(recursive?: boolean): void {\n    this.state.set({\n      status: 'pending',\n      isStale: true,\n      isUpdating: false,\n      isConnected: false,\n    });\n    delete this.stalePromise;\n\n    super.invalidate(recursive);\n  }\n\n  mapValue<S>(selector: Selector<T, S>): Cache<S, Args>;\n\n  mapValue<const P extends AnyPath>(\n    selector: P extends Path<T> ? P : Path<T>,\n  ): Cache<Value<T, P>, Args>;\n\n  mapValue(selector: Selector<any, any> | AnyPath) {\n    return mapValue(this, selector);\n  }\n\n  protected watchPromise(): void {\n    this.subscribe(\n      async (promise) => {\n        if (promise instanceof PromiseWithState && promise.state.status !== 'pending') {\n          promise.catch(() => undefined);\n\n          this.state.set((state) => ({\n            ...promise.state,\n            isStale: false,\n            isUpdating: false,\n            isConnected: state.isConnected,\n          }));\n\n          delete this.stalePromise;\n          this.setTimers();\n          return;\n        }\n\n        this.state.set((state) => ({\n          ...state,\n          isUpdating: true,\n        }));\n\n        this.setTimers();\n\n        try {\n          const value = await promise;\n\n          if (promise !== this.calculatedValue?.value) {\n            return;\n          }\n\n          this.state.set((state) => ({\n            status: 'value',\n            value,\n            isStale: false,\n            isUpdating: false,\n            isConnected: state.isConnected,\n          }));\n          delete this.stalePromise;\n          this.setTimers();\n        } catch (error) {\n          if (promise !== this.calculatedValue?.value) {\n            return;\n          }\n\n          this.state.set((state) => ({\n            status: 'error',\n            error,\n            isStale: false,\n            isUpdating: false,\n            isConnected: state.isConnected,\n          }));\n          delete this.stalePromise;\n          this.setTimers();\n        }\n      },\n      { passive: true },\n    );\n  }\n\n  protected setTimers(): void {\n    if (this.invalidationTimer) {\n      clearTimeout(this.invalidationTimer);\n    }\n    this.invalidationTimer = undefined;\n\n    const state = this.state.get();\n\n    if (state.status === 'pending' || state.isStale) {\n      return;\n    }\n\n    let { invalidateAfter } = this.options;\n    const ref = new WeakRef(this);\n\n    if (invalidateAfter instanceof Function) {\n      invalidateAfter = invalidateAfter(state);\n    }\n\n    if (invalidateAfter !== null && invalidateAfter !== undefined) {\n      this.invalidationTimer = setTimeout(\n        () => ref?.deref()?.invalidate(),\n        calcDuration(invalidateAfter),\n      );\n    }\n  }\n\n  protected watchFocus(): void {\n    const { invalidateOnWindowFocus } = this.options;\n\n    if (\n      !invalidateOnWindowFocus ||\n      typeof document === 'undefined' ||\n      typeof document.addEventListener === 'undefined'\n    ) {\n      return;\n    }\n\n    const ref = new WeakRef(this);\n\n    const onFocus = () => {\n      const that = ref?.deref();\n      if (!that) {\n        document.removeEventListener('visibilitychange', onFocus);\n        return;\n      }\n\n      if (!document.hidden && !that.state.get().isConnected) {\n        that.invalidate();\n      }\n    };\n\n    document.addEventListener('visibilitychange', onFocus);\n  }\n\n  protected onActivation(): void {\n    if (this.options.invalidateOnActivation) {\n      this.invalidate();\n    }\n  }\n}\n\nfunction mapValue<T, S, Args extends any[]>(\n  cache: Cache<T, Args>,\n  _selector: Selector<T, S> | AnyPath,\n): Cache<S, Args> {\n  const selector = makeSelector(_selector);\n  const derivedFromCache = {\n    cache: cache.derivedFromCache ? cache.derivedFromCache.cache : cache,\n    selectors: cache.derivedFromCache\n      ? [...cache.derivedFromCache.selectors, _selector]\n      : [_selector],\n  };\n\n  return new Cache<S, Args>(\n    async ({ use }) => {\n      const value = await use(cache);\n      return selector(value);\n    },\n    cache.args,\n    {\n      equals: cache.options.equals,\n    },\n    derivedFromCache,\n  );\n}\n\nexport type CreateCacheResult<\n  T,\n  Args extends any[],\n  TCache extends Cache<T, Args>,\n> = [] extends Args ? CacheBundle<T, Args, TCache> & TCache : CacheBundle<T, Args, TCache>;\n\nexport interface InvalidationOptions<T, Args extends any[], TCache extends Cache<T, Args>> {\n  filter?: (cache: TCache) => boolean;\n}\n\nexport type CacheBundle<T, Args extends any[], TCache extends Cache<T, Args>> = {\n  (...args: Args): TCache;\n  mapCache<S>(selector: Selector<T, S>): CreateCacheResult<S, Args, Cache<S, Args>>;\n  mapValue<const P>(\n    selector: Constrain<P, Path<T>>,\n  ): CreateCacheResult<Value<T, P>, Args, Cache<Value<T, P>, Args>>;\n  invalidateAll: (options?: InvalidationOptions<T, Args, TCache>) => void;\n  clearAll: (options?: InvalidationOptions<T, Args, TCache>) => void;\n  getInstances: () => TCache[];\n};\n\nfunction create<T, Args extends any[] = []>(\n  cacheFunction: CacheFunction<T, Args>,\n  options?: NoInfer<CacheOptions<T, Args>>,\n): CreateCacheResult<T, Args, Cache<T, Args>> {\n  return internalCreate<T, Args, Cache<T, Args>>(\n    (args, options) =>\n      new Cache(\n        (helpers) => {\n          const result = cacheFunction.apply(helpers, args);\n\n          if (result instanceof Function) {\n            return result(helpers);\n          }\n\n          return result;\n        },\n        args,\n        options,\n        undefined,\n      ),\n    options,\n  );\n}\n\nexport function internalCreate<T, Args extends any[], TCache extends Cache<T, Args>>(\n  factory: (args: Args, options: CacheOptions<T, Args>) => TCache,\n  options?: CacheOptions<T, Args>,\n): CreateCacheResult<T, Args, TCache> {\n  options = { ...createCache.defaultOptions, ...options };\n  const { clearUnusedAfter, resourceGroup } = options ?? {};\n\n  let baseInstance: CacheBundle<T, Args, TCache> & TCache;\n\n  const instanceCache = new InstanceCache<Args, TCache>(\n    (...args) => factory(args, options),\n    clearUnusedAfter ? calcDuration(clearUnusedAfter) : undefined,\n  );\n\n  function get(...args: Args) {\n    const sliceAfter = args.lastIndexOf(undefined);\n    if (sliceAfter !== -1) {\n      args = args.slice(0, sliceAfter) as Args;\n    }\n\n    const cacheKey = options?.getCacheKey ? options.getCacheKey(...args) : args;\n    return instanceCache.getWithKey(args, cacheKey);\n  }\n\n  const mapCache = (selector: any) => {\n    return internalCreate<any, Args, Cache<any, Args>>((args: Args) =>\n      mapValue(baseInstance(...args), selector),\n    );\n  };\n\n  const invalidateAll = ({ filter = () => true }: InvalidationOptions<T, Args, TCache> = {}) => {\n    for (const instance of instanceCache.values()) {\n      if (filter(instance)) {\n        instance.invalidate();\n      }\n    }\n  };\n\n  const clearAll = ({ filter = () => true }: InvalidationOptions<T, Args, TCache> = {}) => {\n    for (const instance of instanceCache.values()) {\n      if (filter(instance)) {\n        instance.clear();\n      }\n    }\n  };\n\n  const getInstances = () => {\n    return instanceCache.values();\n  };\n\n  baseInstance = new Proxy(\n    Object.assign(() => undefined, {\n      mapCache,\n      invalidateAll,\n      clearAll,\n      getInstances,\n    }),\n    {\n      apply(_target, _thisArg, argArray) {\n        return get(...(argArray as unknown as Args));\n      },\n      get(target, p, receiver) {\n        if (Reflect.has(target, p)) {\n          return Reflect.get(target, p, receiver);\n        }\n\n        const baseCache = get(...([] as unknown as Args));\n        return Reflect.get(baseCache, p, baseCache);\n      },\n    },\n  ) as unknown as CacheBundle<T, Args, TCache> & TCache;\n\n  const groups = Array.isArray(resourceGroup)\n    ? resourceGroup\n    : resourceGroup\n      ? [resourceGroup]\n      : [];\n\n  for (const group of groups.concat(allResources)) {\n    group.add(baseInstance);\n  }\n\n  return baseInstance;\n}\n\nexport const defaultCacheOptions: CacheOptions<any, any> = {\n  invalidateOnWindowFocus: true,\n  clearUnusedAfter: { days: 1 },\n  retain: { milliseconds: 1 },\n  equals: deepEqual,\n};\n\nexport const createCache: typeof create & { defaultOptions: CacheOptions<any, any> } =\n  /* @__PURE__ */ Object.assign(create, {\n    defaultOptions: defaultCacheOptions,\n  });\n","import { autobind } from '@lib/autobind';\n\nexport class Scope<T> {\n  constructor(public readonly defaultValue: T) {\n    autobind(Scope);\n  }\n}\n\nexport function createScope<T>(defaultValue: T): Scope<T> {\n  return new Scope(defaultValue);\n}\n"],"mappings":";;;;AAMA,MAAaA,OAAsB,OAAO,OAAO;AAEjD,SAAS,gBAAgB,OAAmC;AAC1D,QACE,OAAO,UAAU,YACjB,UAAU,QACV,QAAQ,SACR,OAAQ,MAAc,UAAU,cAC/B,MAAc,M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