@softistx/riva 0.0.0-stage → 0.1.0

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.
Files changed (138) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +168 -2
  3. package/dist/batch.d.ts +5 -0
  4. package/dist/batch.d.ts.map +1 -0
  5. package/dist/chunks/index-2g2s3y31.js +58 -0
  6. package/dist/chunks/index-2g2s3y31.js.map +10 -0
  7. package/dist/chunks/index-46sbdyt0.js +84 -0
  8. package/dist/chunks/index-46sbdyt0.js.map +12 -0
  9. package/dist/chunks/index-5xnfwj57.js +21 -0
  10. package/dist/chunks/index-5xnfwj57.js.map +10 -0
  11. package/dist/chunks/index-asz1gnp1.js +50 -0
  12. package/dist/chunks/index-asz1gnp1.js.map +10 -0
  13. package/dist/chunks/index-e4vzsfy1.js +253 -0
  14. package/dist/chunks/index-e4vzsfy1.js.map +11 -0
  15. package/dist/chunks/index-tn8ankny.js +137 -0
  16. package/dist/chunks/index-tn8ankny.js.map +10 -0
  17. package/dist/chunks/index-vz93xwnj.js +35 -0
  18. package/dist/chunks/index-vz93xwnj.js.map +10 -0
  19. package/dist/chunks/index-y6bzw104.js +53 -0
  20. package/dist/chunks/index-y6bzw104.js.map +11 -0
  21. package/dist/client.d.ts +48 -0
  22. package/dist/client.d.ts.map +1 -0
  23. package/dist/computed.d.ts +21 -0
  24. package/dist/computed.d.ts.map +1 -0
  25. package/dist/data/index.d.ts +19 -0
  26. package/dist/data/index.d.ts.map +1 -0
  27. package/dist/data/index.js +60 -0
  28. package/dist/data/index.js.map +10 -0
  29. package/dist/data/types.d.ts +12 -0
  30. package/dist/data/types.d.ts.map +1 -0
  31. package/dist/duration.d.ts +5 -0
  32. package/dist/duration.d.ts.map +1 -0
  33. package/dist/effect.d.ts +5 -0
  34. package/dist/effect.d.ts.map +1 -0
  35. package/dist/entities/api.d.ts +18 -0
  36. package/dist/entities/api.d.ts.map +1 -0
  37. package/dist/entities/denormalize.d.ts +13 -0
  38. package/dist/entities/denormalize.d.ts.map +1 -0
  39. package/dist/entities/identity.d.ts +11 -0
  40. package/dist/entities/identity.d.ts.map +1 -0
  41. package/dist/entities/index.d.ts +6 -0
  42. package/dist/entities/index.d.ts.map +1 -0
  43. package/dist/entities/index.js +414 -0
  44. package/dist/entities/index.js.map +16 -0
  45. package/dist/entities/json.d.ts +15 -0
  46. package/dist/entities/json.d.ts.map +1 -0
  47. package/dist/entities/normalize.d.ts +11 -0
  48. package/dist/entities/normalize.d.ts.map +1 -0
  49. package/dist/entities/policy.d.ts +41 -0
  50. package/dist/entities/policy.d.ts.map +1 -0
  51. package/dist/entities/store.d.ts +47 -0
  52. package/dist/entities/store.d.ts.map +1 -0
  53. package/dist/env.d.ts +15 -0
  54. package/dist/env.d.ts.map +1 -0
  55. package/dist/errors.d.ts +38 -0
  56. package/dist/errors.d.ts.map +1 -0
  57. package/dist/flush.d.ts +11 -0
  58. package/dist/flush.d.ts.map +1 -0
  59. package/dist/graph.d.ts +47 -0
  60. package/dist/graph.d.ts.map +1 -0
  61. package/dist/index.d.ts +14 -0
  62. package/dist/index.d.ts.map +1 -0
  63. package/dist/index.js +502 -0
  64. package/dist/index.js.map +18 -0
  65. package/dist/infinite/index.d.ts +3 -0
  66. package/dist/infinite/index.d.ts.map +1 -0
  67. package/dist/infinite/index.js +207 -0
  68. package/dist/infinite/index.js.map +13 -0
  69. package/dist/infinite/infinite.d.ts +8 -0
  70. package/dist/infinite/infinite.d.ts.map +1 -0
  71. package/dist/infinite/more.d.ts +19 -0
  72. package/dist/infinite/more.d.ts.map +1 -0
  73. package/dist/infinite/refetch.d.ts +16 -0
  74. package/dist/infinite/refetch.d.ts.map +1 -0
  75. package/dist/infinite/slots.d.ts +22 -0
  76. package/dist/infinite/slots.d.ts.map +1 -0
  77. package/dist/infinite/types.d.ts +68 -0
  78. package/dist/infinite/types.d.ts.map +1 -0
  79. package/dist/key.d.ts +3 -0
  80. package/dist/key.d.ts.map +1 -0
  81. package/dist/mutation-run.d.ts +5 -0
  82. package/dist/mutation-run.d.ts.map +1 -0
  83. package/dist/mutation.d.ts +53 -0
  84. package/dist/mutation.d.ts.map +1 -0
  85. package/dist/query-cache.d.ts +33 -0
  86. package/dist/query-cache.d.ts.map +1 -0
  87. package/dist/query-entry.d.ts +85 -0
  88. package/dist/query-entry.d.ts.map +1 -0
  89. package/dist/query-observe.d.ts +9 -0
  90. package/dist/query-observe.d.ts.map +1 -0
  91. package/dist/query-run.d.ts +12 -0
  92. package/dist/query-run.d.ts.map +1 -0
  93. package/dist/query.d.ts +100 -0
  94. package/dist/query.d.ts.map +1 -0
  95. package/dist/retry.d.ts +18 -0
  96. package/dist/retry.d.ts.map +1 -0
  97. package/dist/scope.d.ts +16 -0
  98. package/dist/scope.d.ts.map +1 -0
  99. package/dist/signal.d.ts +22 -0
  100. package/dist/signal.d.ts.map +1 -0
  101. package/dist/ssr/codec.d.ts +10 -0
  102. package/dist/ssr/codec.d.ts.map +1 -0
  103. package/dist/ssr/dehydrate.d.ts +37 -0
  104. package/dist/ssr/dehydrate.d.ts.map +1 -0
  105. package/dist/ssr/hydrate.d.ts +20 -0
  106. package/dist/ssr/hydrate.d.ts.map +1 -0
  107. package/dist/ssr/index.d.ts +5 -0
  108. package/dist/ssr/index.d.ts.map +1 -0
  109. package/dist/ssr/index.js +136 -0
  110. package/dist/ssr/index.js.map +11 -0
  111. package/dist/store/def.d.ts +31 -0
  112. package/dist/store/def.d.ts.map +1 -0
  113. package/dist/store/index.d.ts +3 -0
  114. package/dist/store/index.d.ts.map +1 -0
  115. package/dist/store/index.js +215 -0
  116. package/dist/store/index.js.map +14 -0
  117. package/dist/store/instance.d.ts +14 -0
  118. package/dist/store/instance.d.ts.map +1 -0
  119. package/dist/store/run.d.ts +12 -0
  120. package/dist/store/run.d.ts.map +1 -0
  121. package/dist/store/store.d.ts +10 -0
  122. package/dist/store/store.d.ts.map +1 -0
  123. package/dist/store/types.d.ts +70 -0
  124. package/dist/store/types.d.ts.map +1 -0
  125. package/dist/store/view.d.ts +10 -0
  126. package/dist/store/view.d.ts.map +1 -0
  127. package/docs/README.md +19 -0
  128. package/docs/guide/data.md +36 -0
  129. package/docs/guide/entities.md +90 -0
  130. package/docs/guide/errors.md +31 -0
  131. package/docs/guide/infinite.md +62 -0
  132. package/docs/guide/mutations.md +73 -0
  133. package/docs/guide/queries.md +93 -0
  134. package/docs/guide/signals.md +72 -0
  135. package/docs/guide/ssr.md +65 -0
  136. package/docs/guide/store.md +88 -0
  137. package/docs/troubleshooting.md +343 -0
  138. package/package.json +84 -5
@@ -0,0 +1,16 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/entities/json.ts", "../src/entities/denormalize.ts", "../src/entities/normalize.ts", "../src/entities/identity.ts", "../src/entities/store.ts", "../src/entities/policy.ts", "../src/entities/api.ts"],
4
+ "sourcesContent": [
5
+ "/** A reference to an entity, as stored in place of it. */\nexport interface Ref {\n\t// biome-ignore lint/style/useNamingConvention: the wire name of a reference\n\t__ref: string;\n}\n\nexport const isObject = (v: unknown): v is Record<string, unknown> =>\n\ttypeof v === 'object' && v !== null;\n\n/** A plain object: a literal, or one with no prototype. */\nexport const isPlain = (v: unknown): v is Record<string, unknown> => {\n\tif (!isObject(v) || Array.isArray(v)) return false;\n\tconst proto = Object.getPrototypeOf(v);\n\treturn proto === Object.prototype || proto === null;\n};\n\nexport const isRef = (v: unknown): v is Ref =>\n\tisPlain(v) && typeof v['__ref'] === 'string';\n\n/** Deep equality over JSON data and dates. */\nexport function equal(a: unknown, b: unknown): boolean {\n\tif (Object.is(a, b)) return true;\n\tif (a instanceof Date) return b instanceof Date && +a === +b;\n\tif (!isObject(a) || !isObject(b)) return false;\n\tif (Array.isArray(a)) {\n\t\treturn (\n\t\t\tArray.isArray(b) &&\n\t\t\ta.length === b.length &&\n\t\t\ta.every((x, i) => equal(x, b[i]))\n\t\t);\n\t}\n\tif (Array.isArray(b) || a instanceof Date || b instanceof Date) return false;\n\tconst ka = Object.keys(a);\n\treturn (\n\t\tka.length === Object.keys(b).length &&\n\t\tka.every((k) => Object.hasOwn(b, k) && equal(a[k], b[k]))\n\t);\n}\n\n/** Keeps `prev`'s identity (and its parts') wherever `next` equals it. */\nexport function share(prev: unknown, next: unknown): unknown {\n\tif (prev === next || !isObject(prev) || !isObject(next)) return next;\n\tif (prev instanceof Date || next instanceof Date)\n\t\treturn equal(prev, next) ? prev : next;\n\tif (Array.isArray(prev) || Array.isArray(next)) {\n\t\tif (!Array.isArray(prev) || !Array.isArray(next)) return next;\n\t\tconst out = next.map((x, i) => share(prev[i], x));\n\t\treturn out.length === prev.length && out.every((x, i) => x === prev[i])\n\t\t\t? prev\n\t\t\t: out;\n\t}\n\tconst n = next as Record<string, unknown>;\n\tconst out: Record<string, unknown> = {};\n\tconst keys = Object.keys(n);\n\tfor (const k of keys) out[k] = share(prev[k], n[k]);\n\treturn keys.length === Object.keys(prev).length &&\n\t\tkeys.every((k) => Object.hasOwn(prev, k) && out[k] === prev[k])\n\t\t? prev\n\t\t: out;\n}\n\n/** The entity refs inside a normalized value (duplicates kept). */\nexport function refsIn(v: unknown, out: string[] = []): string[] {\n\tif (isRef(v)) out.push(v['__ref']);\n\telse if (Array.isArray(v)) for (const x of v) refsIn(x, out);\n\telse if (isPlain(v)) for (const k in v) refsIn(v[k], out);\n\treturn out;\n}\n",
6
+ "import { isPlain, isRef, share } from './json';\nimport type { EntityStore } from './store';\n\n/**\n * Resolves the refs of a stored value through the entity signals: each signal read\n * is tracked, so the caller (a computed) re-runs when an entity it reached\n * changes. A missing entity is `undefined`; a ref met again inside itself is\n * cut. An entity unchanged since the last read keeps its identity: its last\n * value is in the store's shared memo, or, when a cut shaped it (it then\n * depends on where the read started), in the reader's own `local` memo.\n */\nexport function denormalize(\n\tstore: EntityStore,\n\tv: unknown,\n\tlocal: Map<string, unknown>,\n): unknown {\n\treturn walk(store, v, { path: new Set(), cuts: 0, local });\n}\n\ninterface Ctx {\n\tpath: Set<string>;\n\tcuts: number;\n\tlocal: Map<string, unknown>;\n}\n\n/** The fields of an entity as a reader sees them: tracked, with the optimistic layers over them. */\nexport function fieldsOf(store: EntityStore, ref: string) {\n\tconst a = store.signals.get(ref);\n\t// A missing entity is only given a signal when the read is tracked.\n\tlet fields = a\n\t\t? store.client.get(a)\n\t\t: store.client.graph.track\n\t\t\t? store.client.get(store.signalOf(ref))\n\t\t\t: undefined;\n\tfor (const layer of store.skip ? [] : store.client.get(store.ov)) {\n\t\tconst patch = layer.get(ref);\n\t\tif (patch) fields = { ...fields, ...patch };\n\t}\n\treturn fields;\n}\n\nfunction walk(store: EntityStore, v: unknown, c: Ctx): unknown {\n\tif (isRef(v)) {\n\t\tconst ref = v['__ref'];\n\t\tconst fields = fieldsOf(store, ref);\n\t\tif (!fields) return;\n\t\tif (c.path.has(ref)) {\n\t\t\tc.cuts++;\n\t\t\treturn;\n\t\t}\n\t\tconst cuts = c.cuts;\n\t\tc.path.add(ref);\n\t\tconst out = walkFields(store, fields, c);\n\t\tc.path.delete(ref);\n\t\tconst memo = c.cuts === cuts ? store.direct : c.local;\n\t\tconst kept = share(memo.get(ref), out);\n\t\tmemo.set(ref, kept);\n\t\treturn kept;\n\t}\n\tif (Array.isArray(v)) return v.map((x) => walk(store, x, c));\n\treturn isPlain(v) ? walkFields(store, v, c) : v;\n}\n\nfunction walkFields(\n\tstore: EntityStore,\n\tfields: Record<string, unknown>,\n\tc: Ctx,\n): Record<string, unknown> {\n\tconst out: Record<string, unknown> = {};\n\tfor (const k in fields) {\n\t\t// A GraphQL argument field (`friends({\"first\":1})`) is not part of a plain result.\n\t\tif (k.includes('(')) continue;\n\t\tconst x = walk(store, fields[k], c);\n\t\tif (x !== undefined) out[k] = x;\n\t}\n\treturn out;\n}\n",
7
+ "import { RivaError } from '../errors';\nimport type { Fields } from './identity';\nimport { isPlain, type Ref } from './json';\nimport type { EntityStore, Stored } from './store';\n\nconst bad = (what: string): never => {\n\tthrow new RivaError(\n\t\t'RIVA_CACHE',\n\t\t`Cannot cache the data: it holds ${what}. Use JSON-like values (a Date is allowed).`,\n\t);\n};\n\nexport interface Normalized {\n\t/** The data with each entity replaced by its ref. */\n\troot: unknown;\n\t/** The fields to merge, by entity; nothing is merged until the whole walk succeeded. */\n\tpending: Map<string, Stored>;\n}\n\nfunction fieldsOf(\n\tstore: EntityStore,\n\tobj: Record<string, unknown>,\n\tpending: Map<string, Stored>,\n\tpath: unknown[],\n): Stored {\n\tconst fields: Stored = {};\n\tfor (const k of Object.keys(obj)) {\n\t\tconst n = walk(store, obj[k], pending, path);\n\t\tif (n !== undefined) fields[k] = n;\n\t}\n\treturn fields;\n}\n\nfunction walk(\n\tstore: EntityStore,\n\tv: unknown,\n\tpending: Map<string, Stored>,\n\tpath: unknown[],\n): unknown {\n\tif (v === undefined || v === null) return v;\n\tconst type = typeof v;\n\tif (type === 'function' || type === 'symbol' || type === 'bigint')\n\t\tbad(`a ${type}`);\n\tif (type !== 'object') {\n\t\tif (type === 'number' && !Number.isFinite(v)) bad(`the number ${v}`);\n\t\treturn v;\n\t}\n\tif (v instanceof Date) return v;\n\tif (!Array.isArray(v) && !isPlain(v))\n\t\treturn bad(\n\t\t\t`an instance of ${(v as object).constructor?.name ?? 'a class'}`,\n\t\t);\n\tif (path.includes(v)) bad('a circular reference');\n\tpath.push(v);\n\tlet out: unknown;\n\tif (Array.isArray(v))\n\t\tout = v.map((x) => walk(store, x, pending, path) ?? null);\n\telse {\n\t\tconst fields = fieldsOf(store, v, pending, path);\n\t\tconst ref = store.identify(v as Fields);\n\t\tif (ref === undefined) out = fields;\n\t\telse {\n\t\t\tpending.set(ref, { ...pending.get(ref), ...fields });\n\t\t\tout = { __ref: ref } satisfies Ref;\n\t\t}\n\t}\n\tpath.pop();\n\treturn out;\n}\n\nexport function normalize(store: EntityStore, data: unknown): Normalized {\n\tconst pending = new Map<string, Stored>();\n\treturn { root: walk(store, data, pending, []), pending };\n}\n\n/** The fields of `obj` merged into the entity `ref` (nested entities are normalized too). */\nexport function normalizeAt(\n\tstore: EntityStore,\n\tref: string,\n\tobj: unknown,\n): Map<string, Stored> {\n\tif (!isPlain(obj)) bad('something other than an object for an entity');\n\tconst pending = new Map<string, Stored>();\n\tpending.set(\n\t\tref,\n\t\tfieldsOf(store, obj as Record<string, unknown>, pending, []),\n\t);\n\treturn pending;\n}\n",
8
+ "export type Fields = Readonly<Record<string, unknown>>;\nexport type Identify = (obj: Fields) => string | undefined;\n\nexport interface EntitiesOptions {\n\t/** Identity of a type, by `__typename`: a string or number, or `undefined` for none. */\n\tkeys?: Record<string, (obj: Fields) => string | number | undefined>;\n\t/** Replaces the default identity: return the ref (`Type:id`), or `undefined` for a non-entity. */\n\tidentify?: Identify;\n}\n\n/** `Type:id` for an object with a string `__typename` and an `id` (or `_id`), or a `keys` entry. */\nexport function makeIdentify(opts: EntitiesOptions = {}): Identify {\n\tif (opts.identify) return opts.identify;\n\tconst keys = opts.keys;\n\treturn (obj) => {\n\t\tconst type = obj['__typename'];\n\t\tif (typeof type !== 'string') return;\n\t\tconst custom = keys && Object.hasOwn(keys, type) ? keys[type] : undefined;\n\t\tconst id = custom ? custom(obj) : (obj['id'] ?? obj['_id']);\n\t\treturn typeof id === 'string' || typeof id === 'number'\n\t\t\t? `${type}:${id}`\n\t\t\t: undefined;\n\t};\n}\n",
9
+ "import type { RivaClient } from '../client';\nimport { nodeOf } from '../graph';\nimport { type Signal, signal } from '../signal';\nimport { fieldsOf } from './denormalize';\nimport { type Identify, makeIdentify } from './identity';\nimport { equal, refsIn } from './json';\n\nexport type Stored = Record<string, unknown>;\n\n/** The options a store's identity rules came from, compared by reference. */\nexport interface Rules {\n\tkeys: unknown;\n\tidentify: unknown;\n}\n\n/** The entities of one client: one signal per entity, kept alive by roots. */\nexport class EntityStore {\n\treadonly signals = new Map<string, Signal<Stored | undefined>>();\n\t/** The stored data of every live query entry (anything with `refs`). */\n\treadonly roots = new Set<{ refs: string[] }>();\n\t/** Entities written by hand, kept until evicted. */\n\treadonly pins = new Set<string>();\n\t/** The last denormalized value of each entity that no cut cycle shaped. */\n\treadonly direct = new Map<string, unknown>();\n\t/** The same for direct reads of entities a cut shaped. */\n\treadonly directLocal = new Map<string, unknown>();\n\t/** The optimistic layers of mutations: field patches by entity, oldest first. */\n\treadonly ov = signal<Map<string, Stored>[]>([]);\n\t/** Set while a committed read runs: the layers are skipped. */\n\tskip = false;\n\tidentify: Identify = makeIdentify();\n\t/** Set by the first policy that uses the store. */\n\trules: Rules | undefined;\n\n\tconstructor(readonly client: RivaClient) {}\n\n\t/** The signal of an entity (created empty when missing, so a tracked read of it is notified). */\n\tsignalOf(ref: string): Signal<Stored | undefined> {\n\t\tlet a = this.signals.get(ref);\n\t\tif (!a) {\n\t\t\ta = signal<Stored | undefined>(undefined, { name: ref });\n\t\t\tthis.signals.set(ref, a);\n\t\t}\n\t\treturn a;\n\t}\n\n\t/** The stored fields, untracked; creates nothing. */\n\tpeek(ref: string): Stored | undefined {\n\t\tconst a = this.signals.get(ref);\n\t\treturn a && this.client.untracked(() => this.client.get(a));\n\t}\n\n\t/** The fields as a reader sees them: tracked, the optimistic layers over them. */\n\tfields(ref: string): Stored | undefined {\n\t\treturn fieldsOf(this, ref);\n\t}\n\n\t/** Whether any entity holds fields. */\n\tget empty(): boolean {\n\t\tfor (const ref of this.signals.keys()) if (this.peek(ref)) return false;\n\t\treturn true;\n\t}\n\n\t/** Empties an entity; its signal stays while observed, so readers are notified. */\n\tclear(ref: string): boolean {\n\t\tconst had = this.peek(ref) !== undefined;\n\t\tthis.direct.delete(ref);\n\t\tthis.directLocal.delete(ref);\n\t\tconst a = this.signals.get(ref);\n\t\tif (!a) return false;\n\t\tif (had) this.client.set(a, undefined);\n\t\tif (nodeOf(this.client.graph, a).refs === 0) this.signals.delete(ref);\n\t\treturn had;\n\t}\n\n\t/** Merges fields into an entity: only a changed field changes the signal. */\n\tmerge(ref: string, fields: Stored): void {\n\t\tconst prev = this.peek(ref);\n\t\tconst changed: Stored = {};\n\t\tlet any = !prev;\n\t\tfor (const k in fields) {\n\t\t\tif (prev && Object.hasOwn(prev, k) && equal(prev[k], fields[k])) continue;\n\t\t\tchanged[k] = fields[k];\n\t\t\tany = true;\n\t\t}\n\t\tif (any) this.client.set(this.signalOf(ref), { ...prev, ...changed });\n\t}\n\n\t/** Removes every entity no live root or pin reaches (cycles included). */\n\tsweep(): void {\n\t\tconst live = new Set<string>();\n\t\tconst todo = [...this.pins];\n\t\tfor (const root of this.roots) todo.push(...root.refs);\n\t\tfor (let r = todo.pop(); r !== undefined; r = todo.pop()) {\n\t\t\tif (live.has(r)) continue;\n\t\t\tlive.add(r);\n\t\t\tconst fields = this.peek(r);\n\t\t\tfor (const k in fields) todo.push(...refsIn(fields[k]));\n\t\t}\n\t\tthis.client.batch(() => {\n\t\t\tfor (const ref of [...this.signals.keys()])\n\t\t\t\tif (!live.has(ref)) this.clear(ref);\n\t\t});\n\t}\n}\n",
10
+ "import type { RivaClient } from '../client';\nimport { RivaError } from '../errors';\nimport type { CachePolicy } from '../query';\nimport { denormalize } from './denormalize';\nimport { type EntitiesOptions, type Fields, makeIdentify } from './identity';\nimport { refsIn, share } from './json';\nimport { type Normalized, normalize } from './normalize';\nimport { EntityStore } from './store';\n\n/** What an entry stores: the normalized data and the entities it holds. */\nexport class EntityRoot {\n\t/** What a reader last saw, to keep identities between reads. */\n\tmemo = new Map<string, unknown>();\n\tseen: unknown;\n\tconstructor(\n\t\treadonly value: unknown,\n\t\treadonly refs: string[],\n\t) {}\n}\n\n/** A cache policy that also tells whether an object is an entity. */\nexport interface EntityPolicy extends CachePolicy<EntityRoot> {\n\tidentify(obj: Fields): string | undefined;\n}\n\n/** What a codec sees of the entity store: identity and fields, nothing to write. */\nexport interface EntityReader {\n\tidentify(obj: Fields): string | undefined;\n\t/** The committed fields of an entity, untracked. */\n\tpeek(ref: string): Record<string, unknown> | undefined;\n\t/** The fields as a reader sees them: tracked, the optimistic layers over them. */\n\tfields(ref: string): Record<string, unknown> | undefined;\n}\n\n/**\n * How data becomes entities and back, for a policy that knows the shape of the\n * data (a GraphQL selection). `denormalize` runs in a computed: read each\n * entity through `fields`, so what it reads is tracked.\n */\nexport interface EntityCodec {\n\t/** `vars` are the variables of the query or mutation that gave `data`. */\n\tnormalize(store: EntityReader, data: unknown, vars: unknown): Normalized;\n\tdenormalize(\n\t\tstore: EntityReader,\n\t\troot: unknown,\n\t\tlocal: Map<string, unknown>,\n\t): unknown;\n}\n\nconst plain: EntityCodec = { normalize, denormalize };\n\nconst KEY = {};\n\n/** The entity store of a client, created on first use. */\nexport function storeOf(client: RivaClient): EntityStore {\n\tlet s = client.slots.get(KEY) as EntityStore | undefined;\n\tif (!s) {\n\t\ts = new EntityStore(client);\n\t\tclient.slots.set(KEY, s);\n\t}\n\treturn s;\n}\n\n/**\n * The client's store, bound to the identity rules of `opts`. The first policy\n * binds it (an unbound store only while it is empty, unless the rules are the\n * default ones); a policy with other rules is refused.\n */\nfunction bound(\n\tclient: RivaClient,\n\topts: EntitiesOptions | undefined,\n\tidentify: EntityStore['identify'],\n): EntityStore {\n\tconst store = storeOf(client);\n\tconst rules = { keys: opts?.keys, identify: opts?.identify };\n\tif (store.rules) {\n\t\tif (\n\t\t\tstore.rules.keys !== rules.keys ||\n\t\t\tstore.rules.identify !== rules.identify\n\t\t)\n\t\t\tthrow new RivaError(\n\t\t\t\t'RIVA_CACHE',\n\t\t\t\t'This client already uses other entity identity rules. Share one entities() (or the same options objects) between its queries.',\n\t\t\t);\n\t} else if (rules.keys || rules.identify) {\n\t\tif (!store.empty)\n\t\t\tthrow new RivaError(\n\t\t\t\t'RIVA_CACHE',\n\t\t\t\t'This client already uses other entity identity rules. Create its entities() policy before its first readEntity or writeEntity.',\n\t\t\t);\n\t\tstore.rules = rules;\n\t\tstore.identify = identify;\n\t} else store.rules = rules;\n\treturn store;\n}\n\n/** Stores query data as shared entities: `query({ cache: entities(), … })`. */\nexport function entities(\n\topts?: EntitiesOptions,\n\tcodec: EntityCodec = plain,\n): EntityPolicy {\n\tconst identify = makeIdentify(opts);\n\treturn {\n\t\tidentify,\n\t\twrite(client, data, prev, vars) {\n\t\t\tconst store = bound(client, opts, identify);\n\t\t\treturn client.batch(() => {\n\t\t\t\tconst { root, pending } = codec.normalize(store, data, vars);\n\t\t\t\tfor (const [ref, fields] of pending) store.merge(ref, fields);\n\t\t\t\tconst stored = new EntityRoot(root, refsIn(root));\n\t\t\t\tif (prev) {\n\t\t\t\t\tstored.memo = prev.memo;\n\t\t\t\t\tstored.seen = prev.seen;\n\t\t\t\t}\n\t\t\t\tstore.roots.add(stored);\n\t\t\t\treturn stored;\n\t\t\t});\n\t\t},\n\t\tread(client, stored, committed) {\n\t\t\tconst store = bound(client, opts, identify);\n\t\t\tstore.skip = !!committed;\n\t\t\ttry {\n\t\t\t\tconst seen = share(\n\t\t\t\t\tstored.seen,\n\t\t\t\t\tcodec.denormalize(store, stored.value, stored.memo),\n\t\t\t\t);\n\t\t\t\tif (!committed) stored.seen = seen;\n\t\t\t\treturn seen;\n\t\t\t} finally {\n\t\t\t\tstore.skip = false;\n\t\t\t}\n\t\t},\n\t\tlayer(client, data, vars) {\n\t\t\tconst { pending } = codec.normalize(\n\t\t\t\tbound(client, opts, identify),\n\t\t\t\tdata,\n\t\t\t\tvars,\n\t\t\t);\n\t\t\tclient.set(storeOf(client).ov, (ls) => [...ls, pending]);\n\t\t\treturn {\n\t\t\t\tdrop: () =>\n\t\t\t\t\tclient.set(storeOf(client).ov, (ls) =>\n\t\t\t\t\t\tls.filter((l) => l !== pending),\n\t\t\t\t\t),\n\t\t\t};\n\t\t},\n\t\tcommit(client, data, vars) {\n\t\t\tconst store = bound(client, opts, identify);\n\t\t\tconst { pending } = codec.normalize(store, data, vars);\n\t\t\tclient.batch(() => {\n\t\t\t\tfor (const [ref, fields] of pending) store.merge(ref, fields);\n\t\t\t});\n\t\t},\n\t\trelease(client, stored) {\n\t\t\tconst store = bound(client, opts, identify);\n\t\t\tstore.roots.delete(stored);\n\t\t\tstore.sweep();\n\t\t},\n\t};\n}\n",
11
+ "import type { RivaClient } from '../client';\nimport { denormalize } from './denormalize';\nimport type { Fields } from './identity';\nimport { refsIn } from './json';\nimport { normalizeAt } from './normalize';\nimport { type EntityPolicy, EntityRoot, storeOf } from './policy';\n\n/** The entity as the queries read it (tracked inside a computed or effect); `undefined` when missing. */\nexport const readEntity = <T = Record<string, unknown>>(\n\tclient: RivaClient,\n\tref: string,\n): T | undefined => {\n\tconst store = storeOf(client);\n\treturn denormalize(store, { __ref: ref }, store.directLocal) as T | undefined;\n};\n\n/**\n * Merges a patch into an entity (created when missing), or the object a\n * function returns from its current value; every query reading it is\n * notified once.\n */\nexport function writeEntity<T extends Fields = Fields>(\n\tclient: RivaClient,\n\tref: string,\n\tpatch: T | ((prev: Record<string, unknown> | undefined) => T),\n): void {\n\tconst store = storeOf(client);\n\tconst next =\n\t\ttypeof patch === 'function'\n\t\t\t? patch(client.untracked(() => readEntity(client, ref)))\n\t\t\t: patch;\n\tconst pending = normalizeAt(store, ref, next);\n\tclient.batch(() => {\n\t\tfor (const [r, fields] of pending) store.merge(r, fields);\n\t\tstore.pins.add(ref);\n\t});\n}\n\n/** Removes an entity: queries reading it see the field as missing. `false` when absent. */\nexport function evict(client: RivaClient, ref: string): boolean {\n\tconst store = storeOf(client);\n\tstore.pins.delete(ref);\n\treturn store.clear(ref);\n}\n\n/** Marks stale every query entry that reaches the entity, and refetches the observed ones. */\nexport function invalidateEntity(client: RivaClient, ref: string): void {\n\tconst store = storeOf(client);\n\tclient.queries.invalidateBy((stored) => {\n\t\tif (!(stored instanceof EntityRoot)) return false;\n\t\tconst seen = new Set<string>();\n\t\tconst todo = [...stored.refs];\n\t\tfor (let r = todo.pop(); r !== undefined; r = todo.pop()) {\n\t\t\tif (r === ref) return true;\n\t\t\tif (seen.has(r)) continue;\n\t\t\tseen.add(r);\n\t\t\tconst fields = store.peek(r);\n\t\t\tfor (const k in fields) todo.push(...refsIn(fields[k]));\n\t\t}\n\t\treturn false;\n\t});\n}\n\n/** The ref (`Type:id`) the policy gives an object, or `undefined` when it is not an entity. */\nexport const identifyOf = (\n\tpolicy: EntityPolicy,\n\tobj: Fields,\n): string | undefined => policy.identify(obj);\n"
12
+ ],
13
+ "mappings": ";;;;;;;;;;;AAMO,IAAM,WAAW,CAAC,MACxB,OAAO,MAAM,YAAY,MAAM;AAGzB,IAAM,UAAU,CAAC,MAA6C;AAAA,EACpE,IAAI,CAAC,SAAS,CAAC,KAAK,MAAM,QAAQ,CAAC;AAAA,IAAG,OAAO;AAAA,EAC7C,MAAM,QAAQ,OAAO,eAAe,CAAC;AAAA,EACrC,OAAO,UAAU,OAAO,aAAa,UAAU;AAAA;AAGzC,IAAM,QAAQ,CAAC,MACrB,QAAQ,CAAC,KAAK,OAAO,EAAE,aAAa;AAG9B,SAAS,KAAK,CAAC,GAAY,GAAqB;AAAA,EACtD,IAAI,OAAO,GAAG,GAAG,CAAC;AAAA,IAAG,OAAO;AAAA,EAC5B,IAAI,aAAa;AAAA,IAAM,OAAO,aAAa,QAAQ,CAAC,MAAM,CAAC;AAAA,EAC3D,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,SAAS,CAAC;AAAA,IAAG,OAAO;AAAA,EACzC,IAAI,MAAM,QAAQ,CAAC,GAAG;AAAA,IACrB,OACC,MAAM,QAAQ,CAAC,KACf,EAAE,WAAW,EAAE,UACf,EAAE,MAAM,CAAC,GAAG,MAAM,MAAM,GAAG,EAAE,EAAE,CAAC;AAAA,EAElC;AAAA,EACA,IAAI,MAAM,QAAQ,CAAC,KAAK,aAAa,QAAQ,aAAa;AAAA,IAAM,OAAO;AAAA,EACvE,MAAM,KAAK,OAAO,KAAK,CAAC;AAAA,EACxB,OACC,GAAG,WAAW,OAAO,KAAK,CAAC,EAAE,UAC7B,GAAG,MAAM,CAAC,MAAM,OAAO,OAAO,GAAG,CAAC,KAAK,MAAM,EAAE,IAAI,EAAE,EAAE,CAAC;AAAA;AAKnD,SAAS,K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AmB;AAAA,EACvE,MAAM,QAAQ,QAAQ,MAAM;AAAA,EAC5B,OAAO,QAAQ,aAAa,CAAC,WAAW;AAAA,IACvC,IAAI,EAAE,kBAAkB;AAAA,MAAa,OAAO;AAAA,IAC5C,MAAM,OAAO,IAAI;AAAA,IACjB,MAAM,OAAO,CAAC,GAAG,OAAO,IAAI;AAAA,IAC5B,SAAS,IAAI,KAAK,IAAI,EAAG,MAAM,WAAW,IAAI,KAAK,IAAI,GAAG;AAAA,MACzD,IAAI,MAAM;AAAA,QAAK,OAAO;AAAA,MACtB,IAAI,KAAK,IAAI,CAAC;AAAA,QAAG;AAAA,MACjB,KAAK,IAAI,CAAC;AAAA,MACV,MAAM,SAAS,MAAM,KAAK,CAAC;AAAA,MAC3B,WAAW,KAAK;AAAA,QAAQ,KAAK,KAAK,GAAG,OAAO,OAAO,EAAE,CAAC;AAAA,IACvD;AAAA,IACA,OAAO;AAAA,GACP;AAAA;AAIK,IAAM,aAAa,CACzB,QACA,QACwB,OAAO,SAAS,GAAG;",
14
+ "debugId": "A91C161CDC696BAC64756E2164756E21",
15
+ "names": []
16
+ }
@@ -0,0 +1,15 @@
1
+ /** A reference to an entity, as stored in place of it. */
2
+ export interface Ref {
3
+ __ref: string;
4
+ }
5
+ export declare const isObject: (v: unknown) => v is Record<string, unknown>;
6
+ /** A plain object: a literal, or one with no prototype. */
7
+ export declare const isPlain: (v: unknown) => v is Record<string, unknown>;
8
+ export declare const isRef: (v: unknown) => v is Ref;
9
+ /** Deep equality over JSON data and dates. */
10
+ export declare function equal(a: unknown, b: unknown): boolean;
11
+ /** Keeps `prev`'s identity (and its parts') wherever `next` equals it. */
12
+ export declare function share(prev: unknown, next: unknown): unknown;
13
+ /** The entity refs inside a normalized value (duplicates kept). */
14
+ export declare function refsIn(v: unknown, out?: string[]): string[];
15
+ //# sourceMappingURL=json.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../src/entities/json.ts"],"names":[],"mappings":"AAAA,0DAA0D;AAC1D,MAAM,WAAW,GAAG;IAEnB,KAAK,EAAE,MAAM,CAAC;CACd;AAED,eAAO,MAAM,QAAQ,MAAO,OAAO,KAAG,CAAC,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAC7B,CAAC;AAErC,2DAA2D;AAC3D,eAAO,MAAM,OAAO,MAAO,OAAO,KAAG,CAAC,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAI/D,CAAC;AAEF,eAAO,MAAM,KAAK,MAAO,OAAO,KAAG,CAAC,IAAI,GACK,CAAC;AAE9C,8CAA8C;AAC9C,wBAAgB,KAAK,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,OAAO,CAiBrD;AAED,0EAA0E;AAC1E,wBAAgB,KAAK,CAAC,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,GAAG,OAAO,CAmB3D;AAED,mEAAmE;AACnE,wBAAgB,MAAM,CAAC,CAAC,EAAE,OAAO,EAAE,GAAG,GAAE,MAAM,EAAO,GAAG,MAAM,EAAE,CAK/D"}
@@ -0,0 +1,11 @@
1
+ import type { EntityStore, Stored } from './store';
2
+ export interface Normalized {
3
+ /** The data with each entity replaced by its ref. */
4
+ root: unknown;
5
+ /** The fields to merge, by entity; nothing is merged until the whole walk succeeded. */
6
+ pending: Map<string, Stored>;
7
+ }
8
+ export declare function normalize(store: EntityStore, data: unknown): Normalized;
9
+ /** The fields of `obj` merged into the entity `ref` (nested entities are normalized too). */
10
+ export declare function normalizeAt(store: EntityStore, ref: string, obj: unknown): Map<string, Stored>;
11
+ //# sourceMappingURL=normalize.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"normalize.d.ts","sourceRoot":"","sources":["../../src/entities/normalize.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AASnD,MAAM,WAAW,UAAU;IAC1B,qDAAqD;IACrD,IAAI,EAAE,OAAO,CAAC;IACd,wFAAwF;IACxF,OAAO,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC7B;AAqDD,wBAAgB,SAAS,CAAC,KAAK,EAAE,WAAW,EAAE,IAAI,EAAE,OAAO,GAAG,UAAU,CAGvE;AAED,6FAA6F;AAC7F,wBAAgB,WAAW,CAC1B,KAAK,EAAE,WAAW,EAClB,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,OAAO,GACV,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAQrB"}
@@ -0,0 +1,41 @@
1
+ import type { RivaClient } from '../client';
2
+ import type { CachePolicy } from '../query';
3
+ import { type EntitiesOptions, type Fields } from './identity';
4
+ import { type Normalized } from './normalize';
5
+ import { EntityStore } from './store';
6
+ /** What an entry stores: the normalized data and the entities it holds. */
7
+ export declare class EntityRoot {
8
+ readonly value: unknown;
9
+ readonly refs: string[];
10
+ /** What a reader last saw, to keep identities between reads. */
11
+ memo: Map<string, unknown>;
12
+ seen: unknown;
13
+ constructor(value: unknown, refs: string[]);
14
+ }
15
+ /** A cache policy that also tells whether an object is an entity. */
16
+ export interface EntityPolicy extends CachePolicy<EntityRoot> {
17
+ identify(obj: Fields): string | undefined;
18
+ }
19
+ /** What a codec sees of the entity store: identity and fields, nothing to write. */
20
+ export interface EntityReader {
21
+ identify(obj: Fields): string | undefined;
22
+ /** The committed fields of an entity, untracked. */
23
+ peek(ref: string): Record<string, unknown> | undefined;
24
+ /** The fields as a reader sees them: tracked, the optimistic layers over them. */
25
+ fields(ref: string): Record<string, unknown> | undefined;
26
+ }
27
+ /**
28
+ * How data becomes entities and back, for a policy that knows the shape of the
29
+ * data (a GraphQL selection). `denormalize` runs in a computed: read each
30
+ * entity through `fields`, so what it reads is tracked.
31
+ */
32
+ export interface EntityCodec {
33
+ /** `vars` are the variables of the query or mutation that gave `data`. */
34
+ normalize(store: EntityReader, data: unknown, vars: unknown): Normalized;
35
+ denormalize(store: EntityReader, root: unknown, local: Map<string, unknown>): unknown;
36
+ }
37
+ /** The entity store of a client, created on first use. */
38
+ export declare function storeOf(client: RivaClient): EntityStore;
39
+ /** Stores query data as shared entities: `query({ cache: entities(), … })`. */
40
+ export declare function entities(opts?: EntitiesOptions, codec?: EntityCodec): EntityPolicy;
41
+ //# sourceMappingURL=policy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"policy.d.ts","sourceRoot":"","sources":["../../src/entities/policy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAE5C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAE5C,OAAO,EAAE,KAAK,eAAe,EAAE,KAAK,MAAM,EAAgB,MAAM,YAAY,CAAC;AAE7E,OAAO,EAAE,KAAK,UAAU,EAAa,MAAM,aAAa,CAAC;AACzD,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAEtC,2EAA2E;AAC3E,qBAAa,UAAU;IAKrB,QAAQ,CAAC,KAAK,EAAE,OAAO;IACvB,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE;IALxB,gEAAgE;IAChE,IAAI,uBAA8B;IAClC,IAAI,EAAE,OAAO,CAAC;IACd,YACU,KAAK,EAAE,OAAO,EACd,IAAI,EAAE,MAAM,EAAE,EACpB;CACJ;AAED,qEAAqE;AACrE,MAAM,WAAW,YAAa,SAAQ,WAAW,CAAC,UAAU,CAAC;IAC5D,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;CAC1C;AAED,oFAAoF;AACpF,MAAM,WAAW,YAAY;IAC5B,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;IAC1C,oDAAoD;IACpD,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAAC;IACvD,kFAAkF;IAClF,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAAC;CACzD;AAED;;;;GAIG;AACH,MAAM,WAAW,WAAW;IAC3B,0EAA0E;IAC1E,SAAS,CAAC,KAAK,EAAE,YAAY,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,GAAG,UAAU,CAAC;IACzE,WAAW,CACV,KAAK,EAAE,YAAY,EACnB,IAAI,EAAE,OAAO,EACb,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,GACzB,OAAO,CAAC;CACX;AAMD,0DAA0D;AAC1D,wBAAgB,OAAO,CAAC,MAAM,EAAE,UAAU,GAAG,WAAW,CAOvD;AAmCD,+EAA+E;AAC/E,wBAAgB,QAAQ,CACvB,IAAI,CAAC,EAAE,eAAe,EACtB,KAAK,GAAE,WAAmB,GACxB,YAAY,CA2Dd"}
@@ -0,0 +1,47 @@
1
+ import type { RivaClient } from '../client';
2
+ import { type Signal } from '../signal';
3
+ import { type Identify } from './identity';
4
+ export type Stored = Record<string, unknown>;
5
+ /** The options a store's identity rules came from, compared by reference. */
6
+ export interface Rules {
7
+ keys: unknown;
8
+ identify: unknown;
9
+ }
10
+ /** The entities of one client: one signal per entity, kept alive by roots. */
11
+ export declare class EntityStore {
12
+ readonly client: RivaClient;
13
+ readonly signals: Map<string, Signal<Stored | undefined>>;
14
+ /** The stored data of every live query entry (anything with `refs`). */
15
+ readonly roots: Set<{
16
+ refs: string[];
17
+ }>;
18
+ /** Entities written by hand, kept until evicted. */
19
+ readonly pins: Set<string>;
20
+ /** The last denormalized value of each entity that no cut cycle shaped. */
21
+ readonly direct: Map<string, unknown>;
22
+ /** The same for direct reads of entities a cut shaped. */
23
+ readonly directLocal: Map<string, unknown>;
24
+ /** The optimistic layers of mutations: field patches by entity, oldest first. */
25
+ readonly ov: Signal<Map<string, Stored>[]>;
26
+ /** Set while a committed read runs: the layers are skipped. */
27
+ skip: boolean;
28
+ identify: Identify;
29
+ /** Set by the first policy that uses the store. */
30
+ rules: Rules | undefined;
31
+ constructor(client: RivaClient);
32
+ /** The signal of an entity (created empty when missing, so a tracked read of it is notified). */
33
+ signalOf(ref: string): Signal<Stored | undefined>;
34
+ /** The stored fields, untracked; creates nothing. */
35
+ peek(ref: string): Stored | undefined;
36
+ /** The fields as a reader sees them: tracked, the optimistic layers over them. */
37
+ fields(ref: string): Stored | undefined;
38
+ /** Whether any entity holds fields. */
39
+ get empty(): boolean;
40
+ /** Empties an entity; its signal stays while observed, so readers are notified. */
41
+ clear(ref: string): boolean;
42
+ /** Merges fields into an entity: only a changed field changes the signal. */
43
+ merge(ref: string, fields: Stored): void;
44
+ /** Removes every entity no live root or pin reaches (cycles included). */
45
+ sweep(): void;
46
+ }
47
+ //# sourceMappingURL=store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../src/entities/store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAE5C,OAAO,EAAE,KAAK,MAAM,EAAU,MAAM,WAAW,CAAC;AAEhD,OAAO,EAAE,KAAK,QAAQ,EAAgB,MAAM,YAAY,CAAC;AAGzD,MAAM,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAE7C,6EAA6E;AAC7E,MAAM,WAAW,KAAK;IACrB,IAAI,EAAE,OAAO,CAAC;IACd,QAAQ,EAAE,OAAO,CAAC;CAClB;AAED,8EAA8E;AAC9E,qBAAa,WAAW;IAkBX,QAAQ,CAAC,MAAM,EAAE,UAAU;IAjBvC,QAAQ,CAAC,OAAO,0CAAiD;IACjE,wEAAwE;IACxE,QAAQ,CAAC,KAAK;cAAmB,MAAM,EAAE;OAAM;IAC/C,oDAAoD;IACpD,QAAQ,CAAC,IAAI,cAAqB;IAClC,2EAA2E;IAC3E,QAAQ,CAAC,MAAM,uBAA8B;IAC7C,0DAA0D;IAC1D,QAAQ,CAAC,WAAW,uBAA8B;IAClD,iFAAiF;IACjF,QAAQ,CAAC,EAAE,gCAAqC;IAChD,+DAA+D;IAC/D,IAAI,UAAS;IACb,QAAQ,EAAE,QAAQ,CAAkB;IACpC,mDAAmD;IACnD,KAAK,EAAE,KAAK,GAAG,SAAS,CAAC;IAEzB,YAAqB,MAAM,EAAE,UAAU,EAAI;IAE3C,iGAAiG;IACjG,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,GAAG,SAAS,CAAC,CAOhD;IAED,qDAAqD;IACrD,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAGpC;IAED,kFAAkF;IAClF,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAEtC;IAED,uCAAuC;IACvC,IAAI,KAAK,IAAI,OAAO,CAGnB;IAED,mFAAmF;IACnF,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAS1B;IAED,6EAA6E;IAC7E,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CAUvC;IAED,0EAA0E;IAC1E,KAAK,IAAI,IAAI,CAcZ;CACD"}
package/dist/env.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ /** Time source, injectable so tests and non-browser hosts control timers. */
2
+ export interface Scheduler {
3
+ now(): number;
4
+ setTimeout(fn: () => void, ms: number): unknown;
5
+ clearTimeout(handle: unknown): void;
6
+ }
7
+ /** Window events that trigger refetches; each returns its own unsubscribe. */
8
+ export interface ClientEvents {
9
+ onFocus(cb: () => void): () => void;
10
+ onReconnect(cb: () => void): () => void;
11
+ }
12
+ export declare const defaultScheduler: Scheduler;
13
+ /** Browser listeners (visibilitychange when visible, focus, online); nothing on the server. */
14
+ export declare const defaultEvents: ClientEvents;
15
+ //# sourceMappingURL=env.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"env.d.ts","sourceRoot":"","sources":["../src/env.ts"],"names":[],"mappings":"AAEA,6EAA6E;AAC7E,MAAM,WAAW,SAAS;IACzB,GAAG,IAAI,MAAM,CAAC;IACd,UAAU,CAAC,EAAE,EAAE,MAAM,IAAI,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC;IAChD,YAAY,CAAC,MAAM,EAAE,OAAO,GAAG,IAAI,CAAC;CACpC;AAED,8EAA8E;AAC9E,MAAM,WAAW,YAAY;IAC5B,OAAO,CAAC,EAAE,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC;IACpC,WAAW,CAAC,EAAE,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC;CACxC;AAED,eAAO,MAAM,gBAAgB,EAAE,SAI9B,CAAC;AAkBF,+FAA+F;AAC/F,eAAO,MAAM,aAAa,EAAE,YAY3B,CAAC"}
@@ -0,0 +1,38 @@
1
+ /** Base of every error riva throws. Defined once here so `instanceof` holds across packages. */
2
+ export declare class RivaError extends Error {
3
+ readonly code: string;
4
+ constructor(code: string, message: string, options?: ErrorOptions);
5
+ }
6
+ /** A value was read with no `RivaClient` in scope. */
7
+ export declare class RivaScopeError extends RivaError {
8
+ constructor(message: string);
9
+ }
10
+ /** A request failed. `retryable` tells the query engine whether trying again can help. */
11
+ export declare class RivaRequestError extends RivaError {
12
+ readonly status: number | undefined;
13
+ readonly retryable: boolean;
14
+ constructor(code: string, message: string, options: {
15
+ retryable: boolean;
16
+ status?: number;
17
+ cause?: unknown;
18
+ });
19
+ }
20
+ /** Whether a failed HTTP status is worth retrying: 5xx, 408 and 429. */
21
+ export declare const isRetryableStatus: (status: number) => boolean;
22
+ /** An HTTP error status. Retryable for 5xx, 408 and 429. */
23
+ export declare class RivaHttpError extends RivaRequestError {
24
+ constructor(status: number, message?: string, cause?: {
25
+ cause?: unknown;
26
+ });
27
+ }
28
+ /** The network failed before any response. Always retryable. */
29
+ export declare class RivaNetworkError extends RivaRequestError {
30
+ constructor(message?: string, cause?: {
31
+ cause?: unknown;
32
+ });
33
+ }
34
+ /** A request was aborted. Never retried. */
35
+ export declare class RivaAbortError extends RivaError {
36
+ constructor(message?: string);
37
+ }
38
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,gGAAgG;AAChG,qBAAa,SAAU,SAAQ,KAAK;IACnC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,YAAY,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY,EAIhE;CACD;AAED,sDAAsD;AACtD,qBAAa,cAAe,SAAQ,SAAS;IAC5C,YAAY,OAAO,EAAE,MAAM,EAG1B;CACD;AAED,0FAA0F;AAC1F,qBAAa,gBAAiB,SAAQ,SAAS;IAC9C,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;IACpC,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;IAC5B,YACC,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,EACf,OAAO,EAAE;QAAE,SAAS,EAAE,OAAO,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,OAAO,CAAA;KAAE,EAMjE;CACD;AAED,wEAAwE;AACxE,eAAO,MAAM,iBAAiB,WAAY,MAAM,KAAG,OACD,CAAC;AAEnD,4DAA4D;AAC5D,qBAAa,aAAc,SAAQ,gBAAgB;IAClD,YACC,MAAM,EAAE,MAAM,EACd,OAAO,SAAmB,EAC1B,KAAK,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,OAAO,CAAA;KAAE,EAQ3B;CACD;AAED,gEAAgE;AAChE,qBAAa,gBAAiB,SAAQ,gBAAgB;IACrD,YAAY,OAAO,SAAkB,EAAE,KAAK,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,OAAO,CAAA;KAAE,EAGjE;CACD;AAED,4CAA4C;AAC5C,qBAAa,cAAe,SAAQ,SAAS;IAC5C,YAAY,OAAO,SAA6B,EAG/C;CACD"}
@@ -0,0 +1,11 @@
1
+ import type { Graph, Node } from './graph';
2
+ /** Queues every node with listeners reachable from `n`: each node once, no recursion. */
3
+ export declare function collect(g: Graph, n: Node): void;
4
+ /**
5
+ * Notifies the queued nodes' listeners, each in its own client. Every
6
+ * notification runs; errors are collected and rethrown after (one as is,
7
+ * several as an AggregateError). A node re-running over 100 times in one
8
+ * flush is a loop.
9
+ */
10
+ export declare function flush(g: Graph, refresh: (g: Graph, n: Node) => void): void;
11
+ //# sourceMappingURL=flush.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"flush.d.ts","sourceRoot":"","sources":["../src/flush.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAK3C,yFAAyF;AACzF,wBAAgB,OAAO,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,IAAI,GAAG,IAAI,CAW/C;AAED;;;;;GAKG;AACH,wBAAgB,KAAK,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,IAAI,KAAK,IAAI,GAAG,IAAI,CA+C1E"}
@@ -0,0 +1,47 @@
1
+ import type { RivaClient } from './client';
2
+ import type { Computed, Readable } from './computed';
3
+ import type { Listener, Signal } from './signal';
4
+ type Def = Signal<unknown> | Computed<unknown>;
5
+ /** The per-client node of a definition: its value and its links. */
6
+ export interface Node {
7
+ def: Def;
8
+ value: unknown;
9
+ /** Bumped each time the value changes. */
10
+ ver: number;
11
+ /** The graph epoch this computed was last verified at. */
12
+ stamp: number;
13
+ /** Live observers plus listeners: links to sources exist only while > 0. */
14
+ refs: number;
15
+ observers: Set<Node>;
16
+ /** Sources read by the last evaluation, with the version seen. */
17
+ deps: Map<Node, number>;
18
+ listeners: Set<Listener<unknown>>;
19
+ seen: number;
20
+ busy: boolean;
21
+ init: boolean;
22
+ }
23
+ export interface Graph {
24
+ client: RivaClient;
25
+ nodes: WeakMap<Def, Node>;
26
+ /** Bumped on every write: a computed verified at this epoch is fresh. */
27
+ epoch: number;
28
+ track: Map<Node, number> | undefined;
29
+ depth: number;
30
+ flushing: boolean;
31
+ pending: Set<Node>;
32
+ /** The signals that travel in an SSR snapshot, by name; null once a name is taken twice. @internal */
33
+ named: Map<string, Node | null>;
34
+ /** Values of hydrated signals not created yet, by name. @internal */
35
+ seeds?: Map<string, unknown>;
36
+ }
37
+ export declare const createGraph: (client: RivaClient) => Graph;
38
+ export declare function nodeOf(g: Graph, def: Def): Node;
39
+ export declare function untracked<R>(g: Graph, fn: () => R): R;
40
+ /** Brings a computed up to date, re-evaluating only when a source changed. */
41
+ export declare function refresh(g: Graph, n: Node): void;
42
+ export declare function read<T>(g: Graph, def: Readable<T>): T;
43
+ export declare function write<T>(g: Graph, def: Signal<T>, value: T | ((prev: T) => T)): void;
44
+ export declare function subscribe<T>(g: Graph, def: Readable<T>, listener: Listener<T>): () => void;
45
+ export declare function batch<R>(g: Graph, fn: () => R): R;
46
+ export {};
47
+ //# sourceMappingURL=graph.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graph.d.ts","sourceRoot":"","sources":["../src/graph.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAC3C,OAAO,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAIrD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAEjD,KAAK,GAAG,GAAG,MAAM,CAAC,OAAO,CAAC,GAAG,QAAQ,CAAC,OAAO,CAAC,CAAC;AAE/C,oEAAoE;AACpE,MAAM,WAAW,IAAI;IACpB,GAAG,EAAE,GAAG,CAAC;IACT,KAAK,EAAE,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C,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,IAAI,GAAG,IAAI,CAyB/C;AAED,wBAAgB,IAAI,CAAC,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,GAAG,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,CAKrD;AAID,wBAAgB,KAAK,CAAC,CAAC,EACtB,CAAC,EAAE,KAAK,EACR,GAAG,EAAE,MAAM,CAAC,CAAC,CAAC,EACd,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,GACzB,IAAI,CAYN;AAED,wBAAgB,SAAS,CAAC,CAAC,EAC1B,CAAC,EAAE,KAAK,EACR,GAAG,EAAE,QAAQ,CAAC,CAAC,CAAC,EAChB,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,GACnB,MAAM,IAAI,CAuBZ;AAED,wBAAgB,KAAK,CAAC,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,CA2BjD"}
@@ -0,0 +1,14 @@
1
+ export { batch, untracked } from './batch';
2
+ export { type ClientOptions, createRivaClient, RivaClient, type SubscribeOptions, } from './client';
3
+ export { type Computed, type ComputedOptions, computed, type Getter, type Readable, type ReadonlySignal, } from './computed';
4
+ export type { DataOperation, DataSource } from './data/types';
5
+ export type { Duration } from './duration';
6
+ export { effect } from './effect';
7
+ export type { ClientEvents, Scheduler } from './env';
8
+ export { isRetryableStatus, RivaAbortError, RivaError, RivaHttpError, RivaNetworkError, RivaRequestError, RivaScopeError, } from './errors';
9
+ export { stableKey } from './key';
10
+ export { at, type Invalidation, type Mutation, type MutationOptions, mutation, type RunOptions, type Target, type Tx, } from './mutation';
11
+ export { type CachePolicy, type EnsureOptions, type FetchContext, type Query, type QueryOptions, type QueryState, type QueryStatus, query, type VarsArgs, } from './query';
12
+ export { setDefaultClient } from './scope';
13
+ export { type Listener, type Signal, type SignalOptions, signal, type Unsubscribe, } from './signal';
14
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAC3C,OAAO,EACN,KAAK,aAAa,EAClB,gBAAgB,EAChB,UAAU,EACV,KAAK,gBAAgB,GACrB,MAAM,UAAU,CAAC;AAClB,OAAO,EACN,KAAK,QAAQ,EACb,KAAK,eAAe,EACpB,QAAQ,EACR,KAAK,MAAM,EACX,KAAK,QAAQ,EACb,KAAK,cAAc,GACnB,MAAM,YAAY,CAAC;AACpB,YAAY,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC9D,YAAY,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAC3C,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,YAAY,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AACrD,OAAO,EACN,iBAAiB,EACjB,cAAc,EACd,SAAS,EACT,aAAa,EACb,gBAAgB,EAChB,gBAAgB,EAChB,cAAc,GACd,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAClC,OAAO,EACN,EAAE,EACF,KAAK,YAAY,EACjB,KAAK,QAAQ,EACb,KAAK,eAAe,EACpB,QAAQ,EACR,KAAK,UAAU,EACf,KAAK,MAAM,EACX,KAAK,EAAE,GACP,MAAM,YAAY,CAAC;AACpB,OAAO,EACN,KAAK,WAAW,EAChB,KAAK,aAAa,EAClB,KAAK,YAAY,EACjB,KAAK,KAAK,EACV,KAAK,YAAY,EACjB,KAAK,UAAU,EACf,KAAK,WAAW,EAChB,KAAK,EACL,KAAK,QAAQ,GACb,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAC3C,OAAO,EACN,KAAK,QAAQ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