cross-state 1.7.6 → 1.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  const require_store = require('./store-CPqOf4y5.cjs');
2
2
  const require_propAccess = require('./propAccess-udIdjqwY.cjs');
3
- const require_scope = require('./scope-C1OD72bj.cjs');
3
+ const require_scope = require('./scope-Dcy6-LYw.cjs');
4
4
  const require_patchMethods = require('./patchMethods-BfyJt9me.cjs');
5
5
  const require_extendedJson = require('./extendedJson-BwYSvpOA.cjs');
6
6
  const require_persist = require('./persist-CwKzFRM7.cjs');
package/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { _ as ValueState, a as CacheFunction, c as CreateCacheResult, d as ResourceGroup, f as allResources, g as PendingState, h as ErrorState, i as CacheBundle, l as createCache, m as CacheState, n as createScope, o as CacheGetOptions, p as createResourceGroup, r as Cache, s as CacheOptions, t as Scope, u as Resource } from "./scope-DmXUMLhM.cjs";
1
+ import { _ as ValueState, a as CacheFunction, c as CreateCacheResult, d as ResourceGroup, f as allResources, g as PendingState, h as ErrorState, i as CacheBundle, l as createCache, m as CacheState, n as createScope, o as CacheGetOptions, p as createResourceGroup, r as Cache, s as CacheOptions, t as Scope, u as Resource } from "./scope-8uUaV6I8.cjs";
2
2
  import { B as Value, C as Selector, D as UpdateFunction, E as UpdateFrom, F as PathAsString, I as SettablePath, K as Constrain, L as SettablePathAsArray, M as KeyType, N as Path, O as Use, P as PathAsArray, R as SettablePathAsString, S as Listener, T as Update, _ as Connection, a as StoreOptions, b as Duration, c as arrayMethods, d as setMethods, f as AsyncConnectionActions, g as Cancel, h as CalculationActions, i as StoreMethods, l as mapMethods, m as BaseConnectionActions, o as StoreOptionsWithMethods, p as AsyncUpdateFunction, r as Store, s as createStore, t as BoundStoreMethods, u as recordMethods, v as ConnectionActions, w as SubscribeOptions, x as Effect, y as DisposableCancel, z as SettableValue } from "./store-21GsOOLS.cjs";
3
3
  import { n as Patch, r as diff } from "./diff-BZm7wZ6G.cjs";
4
4
  import { a as PersistStorageBase, c as PersistStorageWithListItems, i as PersistStorage, n as PersistOptions, o as PersistStorageWithKeys, r as persist, s as PersistStorageWithLength, t as Persist } from "./persist-CkZ7CzW8.cjs";
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { _ as ValueState, a as CacheFunction, c as CreateCacheResult, d as ResourceGroup, f as allResources, g as PendingState, h as ErrorState, i as CacheBundle, l as createCache, m as CacheState, n as createScope, o as CacheGetOptions, p as createResourceGroup, r as Cache, s as CacheOptions, t as Scope, u as Resource } from "./scope-CraK2KFK.js";
1
+ import { _ as ValueState, a as CacheFunction, c as CreateCacheResult, d as ResourceGroup, f as allResources, g as PendingState, h as ErrorState, i as CacheBundle, l as createCache, m as CacheState, n as createScope, o as CacheGetOptions, p as createResourceGroup, r as Cache, s as CacheOptions, t as Scope, u as Resource } from "./scope-qdOAlRWQ.js";
2
2
  import { B as Value, C as Selector, D as UpdateFunction, E as UpdateFrom, F as PathAsString, I as SettablePath, K as Constrain, L as SettablePathAsArray, M as KeyType, N as Path, O as Use, P as PathAsArray, R as SettablePathAsString, S as Listener, T as Update, _ as Connection, a as StoreOptions, b as Duration, c as arrayMethods, d as setMethods, f as AsyncConnectionActions, g as Cancel, h as CalculationActions, i as StoreMethods, l as mapMethods, m as BaseConnectionActions, o as StoreOptionsWithMethods, p as AsyncUpdateFunction, r as Store, s as createStore, t as BoundStoreMethods, u as recordMethods, v as ConnectionActions, w as SubscribeOptions, x as Effect, y as DisposableCancel, z as SettableValue } from "./store-Cq1PqvEo.js";
3
3
  import { n as Patch, r as diff } from "./diff-DxjoO6qz.js";
4
4
  import { a as PersistStorageBase, c as PersistStorageWithListItems, i as PersistStorage, n as PersistOptions, o as PersistStorageWithKeys, r as persist, s as PersistStorageWithLength, t as Persist } from "./persist-3Nff6j1f.js";
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { a as mapMethods, h as autobind, i as arrayMethods, m as calcDuration, n as createStore, o as recordMethods, s as setMethods, t as Store } from "./store-D4q5zD7q.js";
2
2
  import { a as set, c as deepEqual, l as shallowEqual, n as get, u as strictEqual } from "./propAccess-BxCKNeOj.js";
3
- import { a as defaultCacheOptions, c as allResources, d as hash, f as simpleHash, i as createCache, l as createResourceGroup, n as createScope, o as internalCreate, r as Cache, s as ResourceGroup, t as Scope, u as InstanceCache } from "./scope-IaRRyweB.js";
3
+ import { a as defaultCacheOptions, c as allResources, d as hash, f as simpleHash, i as createCache, l as createResourceGroup, n as createScope, o as internalCreate, r as Cache, s as ResourceGroup, t as Scope, u as InstanceCache } from "./scope-Cb_u0JYM.js";
4
4
  import { i as applyPatches, r as diff } from "./patchMethods-D-Vmx93m.js";
5
5
  import { i as toExtendedJsonString, n as fromExtendedJsonString, r as toExtendedJson, t as fromExtendedJson } from "./extendedJson-B1OEqZtF.js";
6
6
  import { t as persist } from "./persist-CZhDrupc.js";
@@ -1,5 +1,5 @@
1
1
  import { A as GetKeys, B as Value, C as Selector, F as PathAsString, G as MaybePromise, H as WildcardPathAsString, I as SettablePath, K as Constrain, N as Path$1, T as Update, U as WildcardValue, W as Object_, b as Duration, j as Join, r as Store } from "../store-21GsOOLS.cjs";
2
- import { a as useScope, c as UseCacheValue, d as useStore, i as ScopeProvider, l as useCache, n as scopeMethods, o as cacheMethods, r as ScopeProps, s as UseCacheArray, t as storeMethods, u as UseStoreOptions } from "../storeMethods-CBLGCsAe.cjs";
2
+ import { a as useScope, c as UseCacheValue, d as useStore, i as ScopeProvider, l as useCache, n as scopeMethods, o as cacheMethods, r as ScopeProps, s as UseCacheArray, t as storeMethods, u as UseStoreOptions } from "../storeMethods-BjuI0joo.cjs";
3
3
  import { Draft } from "mutative";
4
4
  import { Component, ComponentPropsWithoutRef, Context, FormEvent, FunctionComponent, HTMLProps, ReactNode } from "react";
5
5
 
@@ -1,5 +1,5 @@
1
1
  import { A as GetKeys, B as Value, C as Selector, F as PathAsString, G as MaybePromise, H as WildcardPathAsString, I as SettablePath, K as Constrain, N as Path$1, T as Update, U as WildcardValue, W as Object_, b as Duration, j as Join, r as Store } from "../store-Cq1PqvEo.js";
2
- import { a as useScope, c as UseCacheValue, d as useStore, i as ScopeProvider, l as useCache, n as scopeMethods, o as cacheMethods, r as ScopeProps, s as UseCacheArray, t as storeMethods, u as UseStoreOptions } from "../storeMethods-Djo5Nj38.js";
2
+ import { a as useScope, c as UseCacheValue, d as useStore, i as ScopeProvider, l as useCache, n as scopeMethods, o as cacheMethods, r as ScopeProps, s as UseCacheArray, t as storeMethods, u as UseStoreOptions } from "../storeMethods-CK9-Q17H.js";
3
3
  import { Component, ComponentPropsWithoutRef, Context, FormEvent, FunctionComponent, HTMLProps, ReactNode } from "react";
4
4
  import { Draft } from "mutative";
5
5
 
@@ -1,5 +1,5 @@
1
1
  const require_store = require('../store-CPqOf4y5.cjs');
2
- const require_scope = require('../scope-C1OD72bj.cjs');
2
+ const require_scope = require('../scope-Dcy6-LYw.cjs');
3
3
  const require_storeMethods = require('../storeMethods-BZb4k7Ma.cjs');
4
4
 
5
5
  //#region src/react/register.ts
@@ -1,4 +1,4 @@
1
- import { n as scopeMethods, o as cacheMethods, t as storeMethods } from "../storeMethods-CBLGCsAe.cjs";
1
+ import { n as scopeMethods, o as cacheMethods, t as storeMethods } from "../storeMethods-BjuI0joo.cjs";
2
2
 
3
3
  //#region src/react/register.d.ts
4
4
  type StoreMethods = typeof storeMethods;
@@ -1,4 +1,4 @@
1
- import { n as scopeMethods, o as cacheMethods, t as storeMethods } from "../storeMethods-Djo5Nj38.js";
1
+ import { n as scopeMethods, o as cacheMethods, t as storeMethods } from "../storeMethods-CK9-Q17H.js";
2
2
 
3
3
  //#region src/react/register.d.ts
4
4
  type StoreMethods = typeof storeMethods;
@@ -1,5 +1,5 @@
1
1
  import { h as autobind, t as Store } from "../store-D4q5zD7q.js";
2
- import { r as Cache, t as Scope } from "../scope-IaRRyweB.js";
2
+ import { r as Cache, t as Scope } from "../scope-Cb_u0JYM.js";
3
3
  import { n as scopeMethods, s as cacheMethods, t as storeMethods } from "../storeMethods-CWeGkXoz.js";
4
4
 
5
5
  //#region src/react/register.ts
@@ -31,11 +31,15 @@ declare class ResourceGroup {
31
31
  readonly name?: string | undefined;
32
32
  private refMap;
33
33
  private refSet;
34
+ private timer;
35
+ private registry;
34
36
  constructor(name?: string | undefined);
35
37
  add(resource: Resource): void;
36
38
  delete(resource: Resource): void;
37
39
  invalidateAll(): void;
38
40
  clearAll(): void;
41
+ cleanup(): void;
42
+ stop(): void;
39
43
  }
40
44
  declare const allResources: ResourceGroup;
41
45
  declare function createResourceGroup(name?: string): ResourceGroup;
@@ -77,6 +81,10 @@ interface CacheOptions<T, Args extends any[]> extends StoreOptions<Promise<T>> {
77
81
  */
78
82
  invalidateOnWindowFocus?: boolean;
79
83
  /**
84
+ * 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.
85
+ */
86
+ invalidateOnActivation?: boolean;
87
+ /**
80
88
  * If set, the cached value will be cleared when the cache is invalidated.
81
89
  * Without this option, the cache will keep the last value as stale until a new value becomes available.
82
90
  */
@@ -138,6 +146,7 @@ declare class Cache<T, Args extends any[] = []> extends Store<Promise<T>> {
138
146
  protected watchPromise(): void;
139
147
  protected setTimers(): void;
140
148
  protected watchFocus(): void;
149
+ protected onActivation(): void;
141
150
  }
142
151
  type CreateCacheResult<T, Args extends any[], TCache extends Cache<T, Args>> = [] extends Args ? CacheBundle<T, Args, TCache> & TCache : CacheBundle<T, Args, TCache>;
143
152
  interface InvalidationOptions<T, Args extends any[], TCache extends Cache<T, Args>> {
@@ -164,4 +173,4 @@ declare class Scope<T> {
164
173
  declare function createScope<T>(defaultValue: T): Scope<T>;
165
174
  //#endregion
166
175
  export { ValueState as _, CacheFunction as a, CreateCacheResult as c, ResourceGroup as d, allResources as f, PendingState as g, ErrorState as h, CacheBundle as i, createCache as l, CacheState as m, createScope as n, CacheGetOptions as o, createResourceGroup as p, Cache as r, CacheOptions as s, Scope as t, Resource as u };
167
- //# sourceMappingURL=scope-DmXUMLhM.d.cts.map
176
+ //# sourceMappingURL=scope-8uUaV6I8.d.cts.map
@@ -77,18 +77,24 @@ var ResourceGroup = class ResourceGroup {
77
77
  this.name = name;
78
78
  this.refMap = /* @__PURE__ */ new WeakMap();
79
79
  this.refSet = /* @__PURE__ */ new Set();
80
+ this.timer = setInterval(() => this.cleanup(), 6e4);
81
+ this.registry = new FinalizationRegistry((ref) => {
82
+ this.refSet.delete(ref);
83
+ });
80
84
  autobind(ResourceGroup);
81
85
  }
82
86
  add(resource) {
83
87
  const ref = new WeakRef(resource);
84
88
  this.refMap.set(resource, ref);
85
89
  this.refSet.add(ref);
90
+ this.registry.register(resource, ref, resource);
86
91
  }
87
92
  delete(resource) {
88
93
  const ref = this.refMap.get(resource);
89
94
  if (ref) {
90
95
  this.refMap.delete(resource);
91
96
  this.refSet.delete(ref);
97
+ this.registry.unregister(resource);
92
98
  }
93
99
  }
94
100
  invalidateAll() {
@@ -105,6 +111,14 @@ var ResourceGroup = class ResourceGroup {
105
111
  else this.refSet.delete(ref);
106
112
  }
107
113
  }
114
+ cleanup() {
115
+ console.log("clean", this.refSet);
116
+ for (const ref of this.refSet) if (!ref.deref()) this.refSet.delete(ref);
117
+ }
118
+ stop() {
119
+ clearInterval(this.timer);
120
+ this.refSet.clear();
121
+ }
108
122
  };
109
123
  const allResources = /* @__PURE__ */ new ResourceGroup();
110
124
  function createResourceGroup(name) {
@@ -128,6 +142,7 @@ var Cache = class Cache extends Store {
128
142
  autobind(Cache);
129
143
  this.watchPromise();
130
144
  this.watchFocus();
145
+ this.addEffect(this.onActivation);
131
146
  }
132
147
  get({ update = "whenStale", backgroundUpdate = false } = {}) {
133
148
  if (!this.calculatedValue?.check()) {
@@ -245,6 +260,9 @@ var Cache = class Cache extends Store {
245
260
  };
246
261
  document.addEventListener("visibilitychange", onFocus);
247
262
  }
263
+ onActivation() {
264
+ if (this.options.invalidateOnActivation) this.invalidate();
265
+ }
248
266
  };
249
267
  function mapValue(cache, _selector) {
250
268
  const selector = makeSelector(_selector);
@@ -330,4 +348,4 @@ function createScope(defaultValue) {
330
348
 
331
349
  //#endregion
332
350
  export { defaultCacheOptions as a, allResources as c, hash as d, simpleHash as f, createCache as i, createResourceGroup as l, createScope as n, internalCreate as o, Cache as r, ResourceGroup as s, Scope as t, InstanceCache as u };
333
- //# sourceMappingURL=scope-IaRRyweB.js.map
351
+ //# sourceMappingURL=scope-Cb_u0JYM.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scope-Cb_u0JYM.js","names":["hash: unique symbol","factory: (...args: Args) => T","cacheTime?: number","name?: string","allResources: ResourceGroup","args: Args","options: CacheOptions<T, Args>","derivedFromCache?: {\n cache: Cache<any, any>;\n selectors: (Selector<any, any> | AnyPath)[];\n }","options","baseInstance: CacheBundle<T, Args, TCache> & TCache","defaultCacheOptions: CacheOptions<any, any>","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 console.log('clean', this.refSet);\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,MAAM,WAAW;;AAIpC,SAAgB,WAAW,OAAwB;AACjD,KAAI,gBAAgB,MAAM,CACxB,QAAO,MAAM,OAAO;AAGtB,KAAI,iBAAiB,IACnB,QAAO,KAAK,CAAC,GAAG,MAAM,CAAC,IAAI,WAAW,CAAC,MAAM,CAAC,KAAK,IAAI,CAAC;AAG1D,KAAI,iBAAiB,IACnB,QAAO,KAAK,CAAC,GAAG,MAAM,SAAS,CAAC,CAAC,IAAI,WAAW,CAAC,MAAM,CAA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MAAM,YAAY,EAAE,eAAe,SAA+C,EAAE,KAAK;AACvF,OAAK,MAAM,YAAY,cAAc,QAAQ,CAC3C,KAAI,OAAO,SAAS,CAClB,UAAS,OAAO;;CAKtB,MAAM,qBAAqB;AACzB,SAAO,cAAc,QAAQ;;AAG/B,gBAAe,IAAI,MACjB,OAAO,aAAa,QAAW;EAC7B;EACA;EACA;EACA;EACD,CAAC,EACF;EACE,MAAM,SAAS,UAAU,UAAU;AACjC,UAAO,IAAI,GAAI,SAA6B;;EAE9C,IAAI,QAAQ,GAAG,UAAU;AACvB,OAAI,QAAQ,IAAI,QAAQ,EAAE,CACxB,QAAO,QAAQ,IAAI,QAAQ,GAAG,SAAS;GAGzC,MAAM,YAAY,IAAI,GAAI,EAAE,CAAqB;AACjD,UAAO,QAAQ,IAAI,WAAW,GAAG,UAAU;;EAE9C,CACF;CAED,MAAM,SAAS,MAAM,QAAQ,cAAc,GACvC,gBACA,gBACE,CAAC,cAAc,GACf,EAAE;AAER,MAAK,MAAM,SAAS,OAAO,OAAO,aAAa,CAC7C,OAAM,IAAI,aAAa;AAGzB,QAAO;;AAGT,MAAaC,sBAA8C;CACzD,yBAAyB;CACzB,kBAAkB,EAAE,MAAM,GAAG;CAC7B,QAAQ,EAAE,cAAc,GAAG;CAC3B,QAAQ;CACT;AAED,MAAaC,cACK,uBAAO,OAAO,QAAQ,EACpC,gBAAgB,qBACjB,CAAC;;;;ACjfJ,IAAa,QAAb,MAAa,MAAS;CACpB,YAAY,AAAgBC,cAAiB;EAAjB;AAC1B,WAAS,MAAM;;;AAInB,SAAgB,YAAe,cAA2B;AACxD,QAAO,IAAI,MAAM,aAAa"}
@@ -77,18 +77,24 @@ var ResourceGroup = class ResourceGroup {
77
77
  this.name = name;
78
78
  this.refMap = /* @__PURE__ */ new WeakMap();
79
79
  this.refSet = /* @__PURE__ */ new Set();
80
+ this.timer = setInterval(() => this.cleanup(), 6e4);
81
+ this.registry = new FinalizationRegistry((ref) => {
82
+ this.refSet.delete(ref);
83
+ });
80
84
  require_store.autobind(ResourceGroup);
81
85
  }
82
86
  add(resource) {
83
87
  const ref = new WeakRef(resource);
84
88
  this.refMap.set(resource, ref);
85
89
  this.refSet.add(ref);
90
+ this.registry.register(resource, ref, resource);
86
91
  }
87
92
  delete(resource) {
88
93
  const ref = this.refMap.get(resource);
89
94
  if (ref) {
90
95
  this.refMap.delete(resource);
91
96
  this.refSet.delete(ref);
97
+ this.registry.unregister(resource);
92
98
  }
93
99
  }
94
100
  invalidateAll() {
@@ -105,6 +111,14 @@ var ResourceGroup = class ResourceGroup {
105
111
  else this.refSet.delete(ref);
106
112
  }
107
113
  }
114
+ cleanup() {
115
+ console.log("clean", this.refSet);
116
+ for (const ref of this.refSet) if (!ref.deref()) this.refSet.delete(ref);
117
+ }
118
+ stop() {
119
+ clearInterval(this.timer);
120
+ this.refSet.clear();
121
+ }
108
122
  };
109
123
  const allResources = /* @__PURE__ */ new ResourceGroup();
110
124
  function createResourceGroup(name) {
@@ -128,6 +142,7 @@ var Cache = class Cache extends require_store.Store {
128
142
  require_store.autobind(Cache);
129
143
  this.watchPromise();
130
144
  this.watchFocus();
145
+ this.addEffect(this.onActivation);
131
146
  }
132
147
  get({ update = "whenStale", backgroundUpdate = false } = {}) {
133
148
  if (!this.calculatedValue?.check()) {
@@ -245,6 +260,9 @@ var Cache = class Cache extends require_store.Store {
245
260
  };
246
261
  document.addEventListener("visibilitychange", onFocus);
247
262
  }
263
+ onActivation() {
264
+ if (this.options.invalidateOnActivation) this.invalidate();
265
+ }
248
266
  };
249
267
  function mapValue(cache, _selector) {
250
268
  const selector = require_store.makeSelector(_selector);
@@ -401,4 +419,4 @@ Object.defineProperty(exports, 'simpleHash', {
401
419
  return simpleHash;
402
420
  }
403
421
  });
404
- //# sourceMappingURL=scope-C1OD72bj.cjs.map
422
+ //# sourceMappingURL=scope-Dcy6-LYw.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scope-Dcy6-LYw.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 console.log('clean', this.refSet);\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,MAAM,WAAW;;AAIpC,SAAgB,WAAW,OAAwB;AACjD,KAAI,gBAAgB,MAAM,CACxB,QAAO,MAAM,OAAO;AAGtB,KAAI,iBAAiB,IACnB,QAAO,KAAK,CAAC,GAAG,MAAM,CAAC,IAAI,WAAW,CAAC,MAAM,CAAC,KAAK,IAAI,CAAC;AAG1D,KAAI,iBAAiB,IACnB,QAAO,KAAK,CAAC,GAAG,MAAM,SAAS,CAAC,CAAC,IAAI,WAAW,CAAC,MAAM,CAAC,KAAK,IAAI,CAAC;AAGpE,KAAI,MAAM,QAAQ,MAAM,CACtB,QAAO,IAAI,MAAM,IAAI,WAAW,CAAC,KAAK,IAAI,CAAC;AAG7C,KAAIC,iCAAc,MAAM,CACtB,QAAO,KAAK,OAAO,QAAQ,MAAM,CAAC,IAAI,WAAW,CAAC,MAAM,CAAC,KAAK,IAAI,CAAC;AAGrE,QAAO,KAAK,UAAU,MAAM;;;;;ACrC9B,IAAa,gBAAb,MAAiE;CAO/D,YACE,AAAgBC,SAChB,AAAgBC,WAChB;EAFgB;EACA;+BARF,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,GAAG,UAAU;AACvB,OAAI,QAAQ,IAAI,QAAQ,EAAE,CACxB,QAAO,QAAQ,IAAI,QAAQ,GAAG,SAAS;GAGzC,MAAM,YAAY,IAAI,GAAI,EAAE,CAAqB;AACjD,UAAO,QAAQ,IAAI,WAAW,GAAG,UAAU;;EAE9C,CACF;CAED,MAAM,SAAS,MAAM,QAAQ,cAAc,GACvC,gBACA,gBACE,CAAC,cAAc,GACf,EAAE;AAER,MAAK,MAAM,SAAS,OAAO,OAAO,aAAa,CAC7C,OAAM,IAAI,aAAa;AAGzB,QAAO;;AAGT,MAAaI,sBAA8C;CACzD,yBAAyB;CACzB,kBAAkB,EAAE,MAAM,GAAG;CAC7B,QAAQ,EAAE,cAAc,GAAG;CAC3B,QAAQC;CACT;AAED,MAAaC,cACK,uBAAO,OAAO,QAAQ,EACpC,gBAAgB,qBACjB,CAAC;;;;ACjfJ,IAAa,QAAb,MAAa,MAAS;CACpB,YAAY,AAAgBC,cAAiB;EAAjB;AAC1B,yBAAS,MAAM;;;AAInB,SAAgB,YAAe,cAA2B;AACxD,QAAO,IAAI,MAAM,aAAa"}
@@ -31,11 +31,15 @@ declare class ResourceGroup {
31
31
  readonly name?: string | undefined;
32
32
  private refMap;
33
33
  private refSet;
34
+ private timer;
35
+ private registry;
34
36
  constructor(name?: string | undefined);
35
37
  add(resource: Resource): void;
36
38
  delete(resource: Resource): void;
37
39
  invalidateAll(): void;
38
40
  clearAll(): void;
41
+ cleanup(): void;
42
+ stop(): void;
39
43
  }
40
44
  declare const allResources: ResourceGroup;
41
45
  declare function createResourceGroup(name?: string): ResourceGroup;
@@ -77,6 +81,10 @@ interface CacheOptions<T, Args extends any[]> extends StoreOptions<Promise<T>> {
77
81
  */
78
82
  invalidateOnWindowFocus?: boolean;
79
83
  /**
84
+ * 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.
85
+ */
86
+ invalidateOnActivation?: boolean;
87
+ /**
80
88
  * If set, the cached value will be cleared when the cache is invalidated.
81
89
  * Without this option, the cache will keep the last value as stale until a new value becomes available.
82
90
  */
@@ -138,6 +146,7 @@ declare class Cache<T, Args extends any[] = []> extends Store<Promise<T>> {
138
146
  protected watchPromise(): void;
139
147
  protected setTimers(): void;
140
148
  protected watchFocus(): void;
149
+ protected onActivation(): void;
141
150
  }
142
151
  type CreateCacheResult<T, Args extends any[], TCache extends Cache<T, Args>> = [] extends Args ? CacheBundle<T, Args, TCache> & TCache : CacheBundle<T, Args, TCache>;
143
152
  interface InvalidationOptions<T, Args extends any[], TCache extends Cache<T, Args>> {
@@ -164,4 +173,4 @@ declare class Scope<T> {
164
173
  declare function createScope<T>(defaultValue: T): Scope<T>;
165
174
  //#endregion
166
175
  export { ValueState as _, CacheFunction as a, CreateCacheResult as c, ResourceGroup as d, allResources as f, PendingState as g, ErrorState as h, CacheBundle as i, createCache as l, CacheState as m, createScope as n, CacheGetOptions as o, createResourceGroup as p, Cache as r, CacheOptions as s, Scope as t, Resource as u };
167
- //# sourceMappingURL=scope-CraK2KFK.d.ts.map
176
+ //# sourceMappingURL=scope-qdOAlRWQ.d.ts.map
@@ -1,4 +1,4 @@
1
- import { m as CacheState, r as Cache, t as Scope } from "./scope-DmXUMLhM.cjs";
1
+ import { m as CacheState, r as Cache, t as Scope } from "./scope-8uUaV6I8.cjs";
2
2
  import { B as Value, C as Selector, K as Constrain, N as Path, T as Update, r as Store, w as SubscribeOptions } from "./store-21GsOOLS.cjs";
3
3
  import { Context, ReactNode } from "react";
4
4
 
@@ -120,4 +120,4 @@ declare const storeMethods: {
120
120
  };
121
121
  //#endregion
122
122
  export { useScope as a, UseCacheValue as c, useStore as d, ScopeProvider as i, useCache as l, scopeMethods as n, cacheMethods as o, ScopeProps as r, UseCacheArray as s, storeMethods as t, UseStoreOptions as u };
123
- //# sourceMappingURL=storeMethods-CBLGCsAe.d.cts.map
123
+ //# sourceMappingURL=storeMethods-BjuI0joo.d.cts.map
@@ -1,4 +1,4 @@
1
- import { m as CacheState, r as Cache, t as Scope } from "./scope-CraK2KFK.js";
1
+ import { m as CacheState, r as Cache, t as Scope } from "./scope-qdOAlRWQ.js";
2
2
  import { B as Value, C as Selector, K as Constrain, N as Path, T as Update, r as Store, w as SubscribeOptions } from "./store-Cq1PqvEo.js";
3
3
  import { Context, ReactNode } from "react";
4
4
 
@@ -120,4 +120,4 @@ declare const storeMethods: {
120
120
  };
121
121
  //#endregion
122
122
  export { useScope as a, UseCacheValue as c, useStore as d, ScopeProvider as i, useCache as l, scopeMethods as n, cacheMethods as o, ScopeProps as r, UseCacheArray as s, storeMethods as t, UseStoreOptions as u };
123
- //# sourceMappingURL=storeMethods-Djo5Nj38.d.ts.map
123
+ //# sourceMappingURL=storeMethods-CK9-Q17H.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cross-state",
3
- "version": "1.7.6",
3
+ "version": "1.8.1",
4
4
  "description": "(React) state library",
5
5
  "license": "ISC",
6
6
  "repository": "schummar/cross-state",
@@ -1 +0,0 @@
1
- {"version":3,"file":"scope-C1OD72bj.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\n private refSet = new Set<WeakRef<Resource>>();\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 }\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 }\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\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 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 }\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\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,MAAM,WAAW;;AAIpC,SAAgB,WAAW,OAAwB;AACjD,KAAI,gBAAgB,MAAM,CACxB,QAAO,MAAM,OAAO;AAGtB,KAAI,iBAAiB,IACnB,QAAO,KAAK,CAAC,GAAG,MAAM,CAAC,IAAI,WAAW,CAAC,MAAM,CAAC,KAAK,IAAI,CAAC;AAG1D,KAAI,iBAAiB,IACnB,QAAO,KAAK,CAAC,GAAG,MAAM,SAAS,CAAC,CAAC,IAAI,WAAW,CAAC,MAAM,CAAC,KAAK,IAAI,CAAC;AAGpE,KAAI,MAAM,QAAQ,MAAM,CACtB,QAAO,IAAI,MAAM,IAAI,WAAW,CAAC,KAAK,IAAI,CA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,cAAc,GACvC,gBACA,gBACE,CAAC,cAAc,GACf,EAAE;AAER,MAAK,MAAM,SAAS,OAAO,OAAO,aAAa,CAC7C,OAAM,IAAI,aAAa;AAGzB,QAAO;;AAGT,MAAaI,sBAA8C;CACzD,yBAAyB;CACzB,kBAAkB,EAAE,MAAM,GAAG;CAC7B,QAAQ,EAAE,cAAc,GAAG;CAC3B,QAAQC;CACT;AAED,MAAaC,cACK,uBAAO,OAAO,QAAQ,EACpC,gBAAgB,qBACjB,CAAC;;;;ACreJ,IAAa,QAAb,MAAa,MAAS;CACpB,YAAY,AAAgBC,cAAiB;EAAjB;AAC1B,yBAAS,MAAM;;;AAInB,SAAgB,YAAe,cAA2B;AACxD,QAAO,IAAI,MAAM,aAAa"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"scope-IaRRyweB.js","names":["hash: unique symbol","factory: (...args: Args) => T","cacheTime?: number","name?: string","allResources: ResourceGroup","args: Args","options: CacheOptions<T, Args>","derivedFromCache?: {\n cache: Cache<any, any>;\n selectors: (Selector<any, any> | AnyPath)[];\n }","options","baseInstance: CacheBundle<T, Args, TCache> & TCache","defaultCacheOptions: CacheOptions<any, any>","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\n private refSet = new Set<WeakRef<Resource>>();\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 }\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 }\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\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 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 }\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\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,MAAM,WAAW;;AAIpC,SAAgB,WAAW,OAAwB;AACjD,KAAI,gBAAgB,MAAM,CACxB,QAAO,MAAM,OAAO;AAGtB,KAAI,iBAAiB,IACnB,QAAO,KAAK,CAAC,GAAG,MAAM,CAAC,IAAI,WAAW,CAAC,MAAM,CAAC,KAAK,IAAI,CAAC;AAG1D,KAAI,iBAAiB,IACnB,QAAO,KAAK,CAAC,GAAG,MAAM,SAAS,CAAC,CAAC,IAAI,WAAW,CAAC,MAAM,CAAC,KAAK,IAAI,CAAC;AAGpE,KAAI,MAAM,QAAQ,MAAM,CACtB,QAAO,IAAI,MAAM,IAAI,WAAW,CAAC,KAAK,IAAI,CAAC;AAG7C,KAAI,cAAc,MAAM,CACtB,QAAO,KAAK,OAAO,QAAQ,MAAM,CAAC,IAAI,WAAW,CAAC,MAAM,CAAC,KAAK,IAAI,CAAC;AAGrE,QAAO,KAAK,UAAU,MAAM;;;;;ACrC9B,IAAa,gBAAb,MAAiE;CAO/D,YACE,AAAgBC,SAChB,AA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