{"version":3,"file":"cccteam-ccc-lib-resource-utils.mjs","sources":["../../../projects/ccc-lib/resource-utils/swr-cache.service.ts","../../../projects/ccc-lib/resource-utils/safe-resource.ts","../../../projects/ccc-lib/resource-utils/cccteam-ccc-lib-resource-utils.ts"],"sourcesContent":["import { Injectable } from '@angular/core';\n\n@Injectable({ providedIn: 'root' })\nexport class SwrCacheService {\n  private cache = new Map<string, unknown>();\n\n  private readonly MAX_CACHE_SIZE = 1000;\n\n  get<T>(key: string): T | undefined {\n    return this.cache.get(key) as T | undefined;\n  }\n\n  /** Set or update a cache entry\n   *\n   * Insertion order is preserved, so deletion of entries is needed\n   * both to ensure that existing entries are moved to the 'front' of the cache\n   * and to ensure new entries remove the oldest entries to maintain cache size\n   */\n  set<T>(key: string, value: T): void {\n    if (this.cache.has(key)) {\n      // Moves the entry to 'front' of cache when set\n      this.cache.delete(key);\n    }\n\n    if (this.cache.size >= this.MAX_CACHE_SIZE) {\n      // Deletes 'oldest' entry\n      this.cache.delete(this.cache.keys().next().value!);\n    }\n\n    this.cache.set(key, value);\n  }\n\n  delete(key: string): void {\n    this.cache.delete(key);\n  }\n\n  /** Remove all cache entries */\n  clear(): void {\n    this.cache.clear();\n  }\n}\n","import { httpResource, HttpResourceOptions, HttpResourceRef } from '@angular/common/http';\nimport {\n  computed,\n  effect,\n  inject,\n  linkedSignal,\n  ResourceRef,\n  ResourceSnapshot,\n  Signal,\n  untracked,\n} from '@angular/core';\nimport { rxResource, RxResourceOptions } from '@angular/core/rxjs-interop';\nimport { SwrCacheService } from './swr-cache.service';\n\nexport interface SafeResourceRef<T> {\n  safeValue: Signal<T | undefined>;\n  resource: ResourceRef<T | undefined>;\n}\n\n/**\n * Creates a {@link SafeResourceRef} that wraps an HTTP resource.\n *\n *\n * The purpose of this utility function is to prevent the resource from dropping the entire application\n * into an exception state when the resource.value() is read after it fails to load.\n *\n * If it does not have a value, it will return `undefined` (or the provided `defaultValue` if specified).\n */\nexport function safeHttpResource<T>(\n  url: () => string | undefined,\n  options?: HttpResourceOptions<T, unknown> | undefined,\n  defaultValue?: T,\n): SafeResourceRef<T> {\n  const resource = httpResource<T>(url, options);\n  const safeValue = computed(() => {\n    if (!resource.hasValue()) {\n      return defaultValue;\n    }\n    return resource.value();\n  });\n  return { safeValue, resource: resource as HttpResourceRef<T | undefined> };\n}\n\n/**\n * Creates a {@link SafeResourceRef} that wraps an RxJS resource.\n *\n * The purpose of this utility function is to prevent the resource from dropping the entire application\n * into an exception state when the resource.value() is read after it fails to load.\n *\n * If it does not have a value, it will return `undefined` (or the provided `defaultValue` if specified).\n */\nexport function safeRxResource<T, A = unknown>(options: RxResourceOptions<T, A>, defaultValue?: T): SafeResourceRef<T> {\n  const resource = rxResource<T, A>(options);\n\n  const safeValue = computed(() => {\n    if (!resource.hasValue()) {\n      return defaultValue;\n    }\n    return resource.value();\n  });\n  return { safeValue, resource: resource as ResourceRef<T | undefined> };\n}\n\n/**\n * Creates a {@link SafeResourceRef} that wraps an HTTP resource and preserves its previous value when it enters a loading state.\n *\n * The purpose of this utility function is to prevent the resource from dropping the entire application\n * into an exception state when the resource.value() is read after it fails to load, while also preserving the previous value during loading.\n *\n */\nexport function staleHttpResource<T>(\n  url: () => string | undefined,\n  options?: HttpResourceOptions<T, unknown> | undefined,\n  defaultValue?: T,\n): SafeResourceRef<T> {\n  const resource = httpResource<T>(url, options);\n  const safeValue = linkedSignal<ResourceSnapshot<T | undefined>, T | undefined>({\n    source: resource.snapshot as Signal<ResourceSnapshot<T | undefined>>,\n    computation: (snap, previous) => {\n      if (snap.status === 'resolved' || snap.status === 'local') {\n        return (snap.value as T) ?? defaultValue;\n      }\n      // Hold the last good value (SWR)\n      if (snap.status === 'loading' || snap.status === 'reloading') {\n        return previous?.value ?? defaultValue;\n      }\n      return defaultValue;\n    },\n  });\n  return { safeValue, resource: resource as HttpResourceRef<T | undefined> };\n}\n\n/**\n * Creates a {@link SafeResourceRef} that wraps an RxJS resource and preserves its previous value when it enters a loading state.\n *\n * The purpose of this utility function is to prevent the resource from dropping the entire application\n * into an exception state when the resource.value() is read after it fails to load, while also preserving the previous value during loading.\n *\n */\nexport function staleRxResource<T, A = unknown>(\n  options: RxResourceOptions<T, A>,\n  defaultValue?: T,\n): SafeResourceRef<T> {\n  const resource = rxResource<T, A>(options);\n\n  const safeValue = linkedSignal<ResourceSnapshot<T | undefined>, T | undefined>({\n    source: resource.snapshot as Signal<ResourceSnapshot<T | undefined>>,\n    computation: (snap, previous) => {\n      if (snap.status === 'resolved' || snap.status === 'local') {\n        return (snap.value as T) ?? defaultValue;\n      }\n      // Hold the last good value (SWR)\n      if (snap.status === 'loading' || snap.status === 'reloading') {\n        return previous?.value ?? defaultValue;\n      }\n      return defaultValue;\n    },\n  });\n  return { safeValue, resource: resource as ResourceRef<T | undefined> };\n}\n\n/**\n * Creates a {@link SafeResourceRef} that wraps an HTTP resource and implements a\n * 'stale while revalidate' caching strategy\n *\n * This wrapper exhibits the following behaviors\n *\n * When the primary resource's `hasValue()` is false\n * - If the global cache service contains this resource's data, the cached data is immediately returned.\n * - If not in cache, the input `defaultValue` is returned (may be `undefined`)\n *\n * When the primary resource's `hasValue()` is true\n * Once the HTTP resource has data, the `safeValue` signal updates with\n * the new value. Any consumers of the `safeValue` computed() will receive fresh data.\n * A side effect updates the global cache with the fresh value\n */\nexport function swrHttpResource<T>(\n  urlFn: () => string | undefined,\n  options?: HttpResourceOptions<T, unknown> | undefined,\n  defaultValue?: T,\n): SafeResourceRef<T> {\n  const cache = inject(SwrCacheService);\n  const resource = httpResource<T>(urlFn, options);\n\n  effect(() => {\n    const url = urlFn();\n    if (url !== undefined && resource.hasValue()) {\n      const value = resource.value();\n      untracked(() => cache.set(url, value));\n    }\n  });\n\n  const safeValue = computed(() => {\n    const url = urlFn();\n    if (url === undefined) {\n      return defaultValue;\n    }\n\n    if (resource.hasValue()) {\n      return resource.value();\n    }\n\n    const cached = cache.get(url) as T | undefined;\n    if (cached !== undefined) {\n      return cached;\n    }\n\n    return defaultValue;\n  });\n  return { safeValue, resource: resource as HttpResourceRef<T | undefined> };\n}\n\n/**\n * Creates a {@link SafeResourceRef} that wraps an RxJS resource and implements a\n * 'stale while revalidate' caching strategy.\n *\n * An input cache key is required since data cannot be inferred from input parameters alone\n *\n * This wrapper exhibits the following behaviors\n *\n * When the primary resource's `hasValue()` is false\n * - If the global cache service contains this resource's data, the cached data is immediately returned.\n * - If not in cache, the input `defaultValue` is returned (may be `undefined`)\n *\n * When the primary resource's `hasValue()` is true\n * Once the RxJS resource has data, the `safeValue` signal updates with\n * the new value. Any consumers of the `safeValue` computed() will receive fresh data.\n * A side effect updates the global cache with the fresh value\n */\nexport function swrRxResource<T, A = unknown>(\n  cacheKey: string,\n  options: RxResourceOptions<T, A>,\n  defaultValue?: T,\n): SafeResourceRef<T> {\n  const cache = inject(SwrCacheService);\n  const resource = rxResource<T, A>(options);\n  const fullCacheKey = computed(() => {\n    if (options.params) {\n      const params = options.params();\n      return params !== undefined ? cacheKey + JSON.stringify(params) : cacheKey;\n    }\n    return cacheKey;\n  });\n\n  effect(() => {\n    if (resource.hasValue()) {\n      const key = fullCacheKey();\n      const value = resource.value();\n      untracked(() => cache.set(key, value));\n    }\n  });\n\n  const safeValue = computed(() => {\n    if (resource.hasValue()) {\n      return resource.value();\n    }\n\n    const cached = cache.get(fullCacheKey()) as T | undefined;\n    if (cached !== undefined) {\n      return cached;\n    }\n\n    return defaultValue;\n  });\n\n  return { safeValue, resource: resource as ResourceRef<T | undefined> };\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;;;MAGa,eAAe,CAAA;AAClB,IAAA,KAAK,GAAG,IAAI,GAAG,EAAmB;IAEzB,cAAc,GAAG,IAAI;AAEtC,IAAA,GAAG,CAAI,GAAW,EAAA;QAChB,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAkB;IAC7C;AAEA;;;;;AAKG;IACH,GAAG,CAAI,GAAW,EAAE,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