@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 @@
1
+ {"version":3,"file":"computed.d.ts","sourceRoot":"","sources":["../src/computed.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAE9D,iFAAiF;AACjF,MAAM,WAAW,cAAc,CAAC,CAAC,CAAE,SAAQ,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,KAAK,GAAG,QAAQ,CAAC;CAAG;AAE/E,MAAM,MAAM,QAAQ,CAAC,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,GAAG,cAAc,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;AACtE,MAAM,MAAM,MAAM,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;AAEhD,MAAM,WAAW,eAAe,CAAC,CAAC;IACjC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,KAAK,OAAO,CAAC;CACjC;AAED,+EAA+E;AAC/E,MAAM,WAAW,QAAQ,CAAC,CAAC;IAC1B,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAC;IAC1B,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC;IAClC,EAAE,CAAC,GAAG,EAAE,MAAM,GAAG,CAAC,CAAC;IACnB,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,OAAO,CAAC;IAC5B,GAAG,IAAI,CAAC,CAAC;IACT,SAAS,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC;CAC9C;AAED,wBAAgB,QAAQ,CAAC,CAAC,EACzB,EAAE,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,CAAC,EACtB,IAAI,GAAE,eAAe,CAAC,CAAC,CAAM,GAC3B,QAAQ,CAAC,CAAC,CAAC,CAUb"}
@@ -0,0 +1,19 @@
1
+ export interface BffTransportOptions {
2
+ /** The `dataRoute` URL. Default `/_data`. */
3
+ url?: string;
4
+ /** Default: the global `fetch`, read at each call. */
5
+ fetch?: typeof fetch;
6
+ /** Extra headers of each call (e.g. a CSRF token). */
7
+ headers?: () => HeadersInit;
8
+ }
9
+ /**
10
+ * The browser's side of a `/_data` route, shared by the adapters' `bffTransport`:
11
+ * posts `{ id, variables }` (variables in the SSR codec, Dates included) and
12
+ * reads `{ data }` back. The route's refusals and the API's errors come back as
13
+ * `RivaRequestError` with the route's `code` and `status` (retryable for 5xx,
14
+ * 408 and 429); a failed fetch is a `RivaNetworkError`, an abort a
15
+ * `RivaAbortError`, a body that is not the route's JSON `RIVA_INVALID_RESPONSE`.
16
+ * Cookies of the same origin go with the call.
17
+ */
18
+ export declare function postData(opts: BffTransportOptions, id: string, variables: unknown, signal?: AbortSignal): Promise<unknown>;
19
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/data/index.ts"],"names":[],"mappings":"AASA,MAAM,WAAW,mBAAmB;IACnC,6CAA6C;IAC7C,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,sDAAsD;IACtD,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;IACrB,sDAAsD;IACtD,OAAO,CAAC,EAAE,MAAM,WAAW,CAAC;CAC5B;AAWD;;;;;;;;GAQG;AACH,wBAAsB,QAAQ,CAC7B,IAAI,EAAE,mBAAmB,EACzB,EAAE,EAAE,MAAM,EACV,SAAS,EAAE,OAAO,EAClB,MAAM,CAAC,EAAE,WAAW,GAClB,OAAO,CAAC,OAAO,CAAC,CA8ClB"}
@@ -0,0 +1,60 @@
1
+ import {
2
+ encode2,
3
+ decode2
4
+ } from "../chunks/index-asz1gnp1.js";
5
+ import {
6
+ RivaRequestError2,
7
+ isRetryableStatus2,
8
+ RivaHttpError2,
9
+ RivaNetworkError2,
10
+ RivaAbortError2
11
+ } from "../chunks/index-2g2s3y31.js";
12
+
13
+ // src/data/index.ts
14
+ var invalid = (message, status) => new RivaRequestError2("RIVA_INVALID_RESPONSE", message, {
15
+ retryable: false,
16
+ ...status !== undefined && { status }
17
+ });
18
+ var isRecord = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
19
+ async function postData(opts, id, variables, signal) {
20
+ let res;
21
+ let body;
22
+ try {
23
+ res = await (opts.fetch ?? fetch)(opts.url ?? "/_data", {
24
+ method: "POST",
25
+ credentials: "same-origin",
26
+ headers: {
27
+ ...opts.headers?.(),
28
+ "content-type": "application/json"
29
+ },
30
+ body: JSON.stringify({ id, variables: encode2(variables) }),
31
+ ...signal && { signal }
32
+ });
33
+ } catch (error) {
34
+ if (signal?.aborted || error?.name === "AbortError")
35
+ throw new RivaAbortError2;
36
+ throw new RivaNetworkError2(error instanceof Error ? error.message : undefined, { cause: error });
37
+ }
38
+ try {
39
+ body = await res.json();
40
+ } catch {
41
+ if (signal?.aborted)
42
+ throw new RivaAbortError2;
43
+ throw invalid(`The data route answered ${res.status} with a body that is not JSON.`, res.status);
44
+ }
45
+ if (!res.ok) {
46
+ const failure = isRecord(body) ? body["error"] : undefined;
47
+ if (isRecord(failure) && typeof failure["code"] === "string")
48
+ throw new RivaRequestError2(failure["code"], typeof failure["message"] === "string" ? failure["message"] : `HTTP ${res.status}`, { status: res.status, retryable: isRetryableStatus2(res.status) });
49
+ throw new RivaHttpError2(res.status);
50
+ }
51
+ if (!isRecord(body) || !("data" in body))
52
+ throw invalid('The data route answered without a "data" field.');
53
+ return decode2(body["data"]);
54
+ }
55
+ export {
56
+ postData
57
+ };
58
+
59
+ //# debugId=83F3D9C793DDB68B64756E2164756E21
60
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,10 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/data/index.ts"],
4
+ "sourcesContent": [
5
+ "import {\n\tisRetryableStatus,\n\tRivaAbortError,\n\tRivaHttpError,\n\tRivaNetworkError,\n\tRivaRequestError,\n} from '../errors';\nimport { decode, encode } from '../ssr/codec';\n\nexport interface BffTransportOptions {\n\t/** The `dataRoute` URL. Default `/_data`. */\n\turl?: string;\n\t/** Default: the global `fetch`, read at each call. */\n\tfetch?: typeof fetch;\n\t/** Extra headers of each call (e.g. a CSRF token). */\n\theaders?: () => HeadersInit;\n}\n\nconst invalid = (message: string, status?: number) =>\n\tnew RivaRequestError('RIVA_INVALID_RESPONSE', message, {\n\t\tretryable: false,\n\t\t...(status !== undefined && { status }),\n\t});\n\nconst isRecord = (v: unknown): v is Record<string, unknown> =>\n\ttypeof v === 'object' && v !== null && !Array.isArray(v);\n\n/**\n * The browser's side of a `/_data` route, shared by the adapters' `bffTransport`:\n * posts `{ id, variables }` (variables in the SSR codec, Dates included) and\n * reads `{ data }` back. The route's refusals and the API's errors come back as\n * `RivaRequestError` with the route's `code` and `status` (retryable for 5xx,\n * 408 and 429); a failed fetch is a `RivaNetworkError`, an abort a\n * `RivaAbortError`, a body that is not the route's JSON `RIVA_INVALID_RESPONSE`.\n * Cookies of the same origin go with the call.\n */\nexport async function postData(\n\topts: BffTransportOptions,\n\tid: string,\n\tvariables: unknown,\n\tsignal?: AbortSignal,\n): Promise<unknown> {\n\tlet res: Response;\n\tlet body: unknown;\n\ttry {\n\t\tres = await (opts.fetch ?? fetch)(opts.url ?? '/_data', {\n\t\t\tmethod: 'POST',\n\t\t\tcredentials: 'same-origin',\n\t\t\theaders: {\n\t\t\t\t...opts.headers?.(),\n\t\t\t\t'content-type': 'application/json',\n\t\t\t},\n\t\t\tbody: JSON.stringify({ id, variables: encode(variables) }),\n\t\t\t...(signal && { signal }),\n\t\t});\n\t} catch (error) {\n\t\tif (signal?.aborted || (error as Error)?.name === 'AbortError')\n\t\t\tthrow new RivaAbortError();\n\t\tthrow new RivaNetworkError(\n\t\t\terror instanceof Error ? error.message : undefined,\n\t\t\t{ cause: error },\n\t\t);\n\t}\n\ttry {\n\t\tbody = await res.json();\n\t} catch {\n\t\tif (signal?.aborted) throw new RivaAbortError();\n\t\tthrow invalid(\n\t\t\t`The data route answered ${res.status} with a body that is not JSON.`,\n\t\t\tres.status,\n\t\t);\n\t}\n\tif (!res.ok) {\n\t\tconst failure = isRecord(body) ? body['error'] : undefined;\n\t\tif (isRecord(failure) && typeof failure['code'] === 'string')\n\t\t\tthrow new RivaRequestError(\n\t\t\t\tfailure['code'],\n\t\t\t\ttypeof failure['message'] === 'string'\n\t\t\t\t\t? failure['message']\n\t\t\t\t\t: `HTTP ${res.status}`,\n\t\t\t\t{ status: res.status, retryable: isRetryableStatus(res.status) },\n\t\t\t);\n\t\tthrow new RivaHttpError(res.status);\n\t}\n\tif (!isRecord(body) || !('data' in body))\n\t\tthrow invalid('The data route answered without a \"data\" field.');\n\treturn decode(body['data']);\n}\n"
6
+ ],
7
+ "mappings": ";;;;;;;;;;;;;AAkBA,IAAM,UAAU,CAAC,SAAiB,WACjC,IAAI,kBAAiB,yBAAyB,SAAS;AAAA,EACtD,WAAW;AAAA,KACP,WAAW,aAAa,EAAE,OAAO;AACtC,CAAC;AAEF,IAAM,WAAW,CAAC,MACjB,OAAO,MAAM,YAAY,MAAM,QAAQ,CAAC,MAAM,QAAQ,CAAC;AAWjD,eAAe,QAAQ,CAC7B,MACA,IACA,WACA,QACmB;AAAA,EACnB,IAAI;AAAA,EACJ,IAAI;AAAA,EACJ,IAAI;AAAA,IACH,MAAM,OAAO,KAAK,SAAS,OAAO,KAAK,OAAO,UAAU;AAAA,MACvD,QAAQ;AAAA,MACR,aAAa;AAAA,MACb,SAAS;AAAA,WACL,KAAK,UAAU;AAAA,QAClB,gBAAgB;AAAA,MACjB;AAAA,MACA,MAAM,KAAK,UAAU,EAAE,IAAI,WAAW,QAAO,SAAS,EAAE,CAAC;AAAA,SACrD,UAAU,EAAE,OAAO;AAAA,IACxB,CAAC;AAAA,IACA,OAAO,OAAO;AAAA,IACf,IAAI,QAAQ,WAAY,OAAiB,SAAS;AAAA,MACjD,MAAM,IAAI;AAAA,IACX,MAAM,IAAI,kBACT,iBAAiB,QAAQ,MAAM,UAAU,WACzC,EAAE,OAAO,MAAM,CAChB;AAAA;AAAA,EAED,IAAI;AAAA,IACH,OAAO,MAAM,IAAI,KAAK;AAAA,IACrB,MAAM;AAAA,IACP,IAAI,QAAQ;AAAA,MAAS,MAAM,IAAI;AAAA,IAC/B,MAAM,QACL,2BAA2B,IAAI,wCAC/B,IAAI,MACL;AAAA;AAAA,EAED,IAAI,CAAC,IAAI,IAAI;AAAA,IACZ,MAAM,UAAU,SAAS,IAAI,IAAI,KAAK,WAAW;AAAA,IACjD,IAAI,SAAS,OAAO,KAAK,OAAO,QAAQ,YAAY;AAAA,MACnD,MAAM,IAAI,kBACT,QAAQ,SACR,OAAO,QAAQ,eAAe,WAC3B,QAAQ,aACR,QAAQ,IAAI,UACf,EAAE,QAAQ,IAAI,QAAQ,WAAW,mBAAkB,IAAI,MAAM,EAAE,CAChE;AAAA,IACD,MAAM,IAAI,eAAc,IAAI,MAAM;AAAA,EACnC;AAAA,EACA,IAAI,CAAC,SAAS,IAAI,KAAK,EAAE,UAAU;AAAA,IAClC,MAAM,QAAQ,iDAAiD;AAAA,EAChE,OAAO,QAAO,KAAK,OAAO;AAAA;",
8
+ "debugId": "83F3D9C793DDB68B64756E2164756E21",
9
+ "names": []
10
+ }
@@ -0,0 +1,12 @@
1
+ import type { RivaClient } from '../client';
2
+ /** One operation a `/_data` route may run for the browser. */
3
+ export interface DataOperation {
4
+ readonly kind: 'query' | 'mutation';
5
+ /** Runs it on the request's client, with the caller's variables (decoded). */
6
+ run(client: RivaClient, variables: unknown, signal: AbortSignal): Promise<unknown>;
7
+ }
8
+ /** A set of operations by id: an allowlist. An id it does not know is not run. */
9
+ export interface DataSource {
10
+ find(id: string): DataOperation | undefined;
11
+ }
12
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/data/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAE5C,8DAA8D;AAC9D,MAAM,WAAW,aAAa;IAC7B,QAAQ,CAAC,IAAI,EAAE,OAAO,GAAG,UAAU,CAAC;IACpC,8EAA8E;IAC9E,GAAG,CACF,MAAM,EAAE,UAAU,EAClB,SAAS,EAAE,OAAO,EAClB,MAAM,EAAE,WAAW,GACjB,OAAO,CAAC,OAAO,CAAC,CAAC;CACpB;AAED,kFAAkF;AAClF,MAAM,WAAW,UAAU;IAC1B,IAAI,CAAC,EAAE,EAAE,MAAM,GAAG,aAAa,GAAG,SAAS,CAAC;CAC5C"}
@@ -0,0 +1,5 @@
1
+ /** A duration: a number of milliseconds, or a string such as `"30s"`, `"5m"`, `"2h"`, `"250ms"`. */
2
+ export type Duration = number | `${number}${'ms' | 's' | 'm' | 'h'}`;
3
+ /** Converts a `Duration` to milliseconds. `Infinity` is allowed; anything else invalid throws `RIVA_DURATION`. */
4
+ export declare function toMs(d: Duration): number;
5
+ //# sourceMappingURL=duration.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"duration.d.ts","sourceRoot":"","sources":["../src/duration.ts"],"names":[],"mappings":"AAEA,oGAAoG;AACpG,MAAM,MAAM,QAAQ,GAAG,MAAM,GAAG,GAAG,MAAM,GAAG,IAAI,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,EAAE,CAAC;AAIrE,kHAAkH;AAClH,wBAAgB,IAAI,CAAC,CAAC,EAAE,QAAQ,GAAG,MAAM,CAQxC"}
@@ -0,0 +1,5 @@
1
+ import type { EffectFn } from './client';
2
+ import type { Unsubscribe } from './signal';
3
+ /** Re-runs `fn` when what it read changes. Client-only: a no-op on the server. */
4
+ export declare const effect: (fn: EffectFn) => Unsubscribe;
5
+ //# sourceMappingURL=effect.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"effect.d.ts","sourceRoot":"","sources":["../src/effect.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAEzC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAI5C,kFAAkF;AAClF,eAAO,MAAM,MAAM,OAAQ,QAAQ,KAAG,WACI,CAAC"}
@@ -0,0 +1,18 @@
1
+ import type { RivaClient } from '../client';
2
+ import type { Fields } from './identity';
3
+ import { type EntityPolicy } from './policy';
4
+ /** The entity as the queries read it (tracked inside a computed or effect); `undefined` when missing. */
5
+ export declare const readEntity: <T = Record<string, unknown>>(client: RivaClient, ref: string) => T | undefined;
6
+ /**
7
+ * Merges a patch into an entity (created when missing), or the object a
8
+ * function returns from its current value; every query reading it is
9
+ * notified once.
10
+ */
11
+ export declare function writeEntity<T extends Fields = Fields>(client: RivaClient, ref: string, patch: T | ((prev: Record<string, unknown> | undefined) => T)): void;
12
+ /** Removes an entity: queries reading it see the field as missing. `false` when absent. */
13
+ export declare function evict(client: RivaClient, ref: string): boolean;
14
+ /** Marks stale every query entry that reaches the entity, and refetches the observed ones. */
15
+ export declare function invalidateEntity(client: RivaClient, ref: string): void;
16
+ /** The ref (`Type:id`) the policy gives an object, or `undefined` when it is not an entity. */
17
+ export declare const identifyOf: (policy: EntityPolicy, obj: Fields) => string | undefined;
18
+ //# sourceMappingURL=api.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../src/entities/api.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAE5C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAGzC,OAAO,EAAE,KAAK,YAAY,EAAuB,MAAM,UAAU,CAAC;AAElE,yGAAyG;AACzG,eAAO,MAAM,UAAU,GAAI,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,UAC7C,UAAU,OACb,MAAM,KACT,CAAC,GAAG,SAGN,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,EACpD,MAAM,EAAE,UAAU,EAClB,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,KAAK,CAAC,CAAC,GAC3D,IAAI,CAWN;AAED,2FAA2F;AAC3F,wBAAgB,KAAK,CAAC,MAAM,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAI9D;AAED,8FAA8F;AAC9F,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI,CAetE;AAED,+FAA+F;AAC/F,eAAO,MAAM,UAAU,WACd,YAAY,OACf,MAAM,KACT,MAAM,GAAG,SAAiC,CAAC"}
@@ -0,0 +1,13 @@
1
+ import type { EntityStore } from './store';
2
+ /**
3
+ * Resolves the refs of a stored value through the entity signals: each signal read
4
+ * is tracked, so the caller (a computed) re-runs when an entity it reached
5
+ * changes. A missing entity is `undefined`; a ref met again inside itself is
6
+ * cut. An entity unchanged since the last read keeps its identity: its last
7
+ * value is in the store's shared memo, or, when a cut shaped it (it then
8
+ * depends on where the read started), in the reader's own `local` memo.
9
+ */
10
+ export declare function denormalize(store: EntityStore, v: unknown, local: Map<string, unknown>): unknown;
11
+ /** The fields of an entity as a reader sees them: tracked, with the optimistic layers over them. */
12
+ export declare function fieldsOf(store: EntityStore, ref: string): import("./store").Stored | undefined;
13
+ //# sourceMappingURL=denormalize.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"denormalize.d.ts","sourceRoot":"","sources":["../../src/entities/denormalize.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAE3C;;;;;;;GAOG;AACH,wBAAgB,WAAW,CAC1B,KAAK,EAAE,WAAW,EAClB,CAAC,EAAE,OAAO,EACV,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,GACzB,OAAO,CAET;AAQD,oGAAoG;AACpG,wBAAgB,QAAQ,CAAC,KAAK,EAAE,WAAW,EAAE,GAAG,EAAE,MAAM,wCAavD"}
@@ -0,0 +1,11 @@
1
+ export type Fields = Readonly<Record<string, unknown>>;
2
+ export type Identify = (obj: Fields) => string | undefined;
3
+ export interface EntitiesOptions {
4
+ /** Identity of a type, by `__typename`: a string or number, or `undefined` for none. */
5
+ keys?: Record<string, (obj: Fields) => string | number | undefined>;
6
+ /** Replaces the default identity: return the ref (`Type:id`), or `undefined` for a non-entity. */
7
+ identify?: Identify;
8
+ }
9
+ /** `Type:id` for an object with a string `__typename` and an `id` (or `_id`), or a `keys` entry. */
10
+ export declare function makeIdentify(opts?: EntitiesOptions): Identify;
11
+ //# sourceMappingURL=identity.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"identity.d.ts","sourceRoot":"","sources":["../../src/entities/identity.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,MAAM,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;AACvD,MAAM,MAAM,QAAQ,GAAG,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,GAAG,SAAS,CAAC;AAE3D,MAAM,WAAW,eAAe;IAC/B,wFAAwF;IACxF,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC,CAAC;IACpE,kGAAkG;IAClG,QAAQ,CAAC,EAAE,QAAQ,CAAC;CACpB;AAED,oGAAoG;AACpG,wBAAgB,YAAY,CAAC,IAAI,GAAE,eAAoB,GAAG,QAAQ,CAYjE"}
@@ -0,0 +1,6 @@
1
+ export { evict, identifyOf, invalidateEntity, readEntity, writeEntity, } from './api';
2
+ export type { EntitiesOptions, Fields, Identify } from './identity';
3
+ export { isPlain, isRef, type Ref } from './json';
4
+ export type { Normalized } from './normalize';
5
+ export { type EntityCodec, type EntityPolicy, type EntityReader, entities, } from './policy';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/entities/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,KAAK,EACL,UAAU,EACV,gBAAgB,EAChB,UAAU,EACV,WAAW,GACX,MAAM,OAAO,CAAC;AACf,YAAY,EAAE,eAAe,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACpE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,GAAG,EAAE,MAAM,QAAQ,CAAC;AAClD,YAAY,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EACN,KAAK,WAAW,EAChB,KAAK,YAAY,EACjB,KAAK,YAAY,EACjB,QAAQ,GACR,MAAM,UAAU,CAAC"}
@@ -0,0 +1,414 @@
1
+ import {
2
+ nodeOf
3
+ } from "../chunks/index-e4vzsfy1.js";
4
+ import {
5
+ signal2
6
+ } from "../chunks/index-y6bzw104.js";
7
+ import {
8
+ RivaError2
9
+ } from "../chunks/index-2g2s3y31.js";
10
+
11
+ // src/entities/json.ts
12
+ var isObject = (v) => typeof v === "object" && v !== null;
13
+ var isPlain = (v) => {
14
+ if (!isObject(v) || Array.isArray(v))
15
+ return false;
16
+ const proto = Object.getPrototypeOf(v);
17
+ return proto === Object.prototype || proto === null;
18
+ };
19
+ var isRef = (v) => isPlain(v) && typeof v["__ref"] === "string";
20
+ function equal(a, b) {
21
+ if (Object.is(a, b))
22
+ return true;
23
+ if (a instanceof Date)
24
+ return b instanceof Date && +a === +b;
25
+ if (!isObject(a) || !isObject(b))
26
+ return false;
27
+ if (Array.isArray(a)) {
28
+ return Array.isArray(b) && a.length === b.length && a.every((x, i) => equal(x, b[i]));
29
+ }
30
+ if (Array.isArray(b) || a instanceof Date || b instanceof Date)
31
+ return false;
32
+ const ka = Object.keys(a);
33
+ return ka.length === Object.keys(b).length && ka.every((k) => Object.hasOwn(b, k) && equal(a[k], b[k]));
34
+ }
35
+ function share(prev, next) {
36
+ if (prev === next || !isObject(prev) || !isObject(next))
37
+ return next;
38
+ if (prev instanceof Date || next instanceof Date)
39
+ return equal(prev, next) ? prev : next;
40
+ if (Array.isArray(prev) || Array.isArray(next)) {
41
+ if (!Array.isArray(prev) || !Array.isArray(next))
42
+ return next;
43
+ const out = next.map((x, i) => share(prev[i], x));
44
+ return out.length === prev.length && out.every((x, i) => x === prev[i]) ? prev : out;
45
+ }
46
+ const n = next;
47
+ const out = {};
48
+ const keys = Object.keys(n);
49
+ for (const k of keys)
50
+ out[k] = share(prev[k], n[k]);
51
+ return keys.length === Object.keys(prev).length && keys.every((k) => Object.hasOwn(prev, k) && out[k] === prev[k]) ? prev : out;
52
+ }
53
+ function refsIn(v, out = []) {
54
+ if (isRef(v))
55
+ out.push(v["__ref"]);
56
+ else if (Array.isArray(v))
57
+ for (const x of v)
58
+ refsIn(x, out);
59
+ else if (isPlain(v))
60
+ for (const k in v)
61
+ refsIn(v[k], out);
62
+ return out;
63
+ }
64
+
65
+ // src/entities/denormalize.ts
66
+ function denormalize(store, v, local) {
67
+ return walk(store, v, { path: new Set, cuts: 0, local });
68
+ }
69
+ function fieldsOf(store, ref) {
70
+ const a = store.signals.get(ref);
71
+ let fields = a ? store.client.get(a) : store.client.graph.track ? store.client.get(store.signalOf(ref)) : undefined;
72
+ for (const layer of store.skip ? [] : store.client.get(store.ov)) {
73
+ const patch = layer.get(ref);
74
+ if (patch)
75
+ fields = { ...fields, ...patch };
76
+ }
77
+ return fields;
78
+ }
79
+ function walk(store, v, c) {
80
+ if (isRef(v)) {
81
+ const ref = v["__ref"];
82
+ const fields = fieldsOf(store, ref);
83
+ if (!fields)
84
+ return;
85
+ if (c.path.has(ref)) {
86
+ c.cuts++;
87
+ return;
88
+ }
89
+ const cuts = c.cuts;
90
+ c.path.add(ref);
91
+ const out = walkFields(store, fields, c);
92
+ c.path.delete(ref);
93
+ const memo = c.cuts === cuts ? store.direct : c.local;
94
+ const kept = share(memo.get(ref), out);
95
+ memo.set(ref, kept);
96
+ return kept;
97
+ }
98
+ if (Array.isArray(v))
99
+ return v.map((x) => walk(store, x, c));
100
+ return isPlain(v) ? walkFields(store, v, c) : v;
101
+ }
102
+ function walkFields(store, fields, c) {
103
+ const out = {};
104
+ for (const k in fields) {
105
+ if (k.includes("("))
106
+ continue;
107
+ const x = walk(store, fields[k], c);
108
+ if (x !== undefined)
109
+ out[k] = x;
110
+ }
111
+ return out;
112
+ }
113
+
114
+ // src/entities/normalize.ts
115
+ var bad = (what) => {
116
+ throw new RivaError2("RIVA_CACHE", `Cannot cache the data: it holds ${what}. Use JSON-like values (a Date is allowed).`);
117
+ };
118
+ function fieldsOf2(store, obj, pending, path) {
119
+ const fields = {};
120
+ for (const k of Object.keys(obj)) {
121
+ const n = walk2(store, obj[k], pending, path);
122
+ if (n !== undefined)
123
+ fields[k] = n;
124
+ }
125
+ return fields;
126
+ }
127
+ function walk2(store, v, pending, path) {
128
+ if (v === undefined || v === null)
129
+ return v;
130
+ const type = typeof v;
131
+ if (type === "function" || type === "symbol" || type === "bigint")
132
+ bad(`a ${type}`);
133
+ if (type !== "object") {
134
+ if (type === "number" && !Number.isFinite(v))
135
+ bad(`the number ${v}`);
136
+ return v;
137
+ }
138
+ if (v instanceof Date)
139
+ return v;
140
+ if (!Array.isArray(v) && !isPlain(v))
141
+ return bad(`an instance of ${v.constructor?.name ?? "a class"}`);
142
+ if (path.includes(v))
143
+ bad("a circular reference");
144
+ path.push(v);
145
+ let out;
146
+ if (Array.isArray(v))
147
+ out = v.map((x) => walk2(store, x, pending, path) ?? null);
148
+ else {
149
+ const fields = fieldsOf2(store, v, pending, path);
150
+ const ref = store.identify(v);
151
+ if (ref === undefined)
152
+ out = fields;
153
+ else {
154
+ pending.set(ref, { ...pending.get(ref), ...fields });
155
+ out = { __ref: ref };
156
+ }
157
+ }
158
+ path.pop();
159
+ return out;
160
+ }
161
+ function normalize(store, data) {
162
+ const pending = new Map;
163
+ return { root: walk2(store, data, pending, []), pending };
164
+ }
165
+ function normalizeAt(store, ref, obj) {
166
+ if (!isPlain(obj))
167
+ bad("something other than an object for an entity");
168
+ const pending = new Map;
169
+ pending.set(ref, fieldsOf2(store, obj, pending, []));
170
+ return pending;
171
+ }
172
+
173
+ // src/entities/identity.ts
174
+ function makeIdentify(opts = {}) {
175
+ if (opts.identify)
176
+ return opts.identify;
177
+ const keys = opts.keys;
178
+ return (obj) => {
179
+ const type = obj["__typename"];
180
+ if (typeof type !== "string")
181
+ return;
182
+ const custom = keys && Object.hasOwn(keys, type) ? keys[type] : undefined;
183
+ const id = custom ? custom(obj) : obj["id"] ?? obj["_id"];
184
+ return typeof id === "string" || typeof id === "number" ? `${type}:${id}` : undefined;
185
+ };
186
+ }
187
+
188
+ // src/entities/store.ts
189
+ class EntityStore {
190
+ client;
191
+ signals = new Map;
192
+ roots = new Set;
193
+ pins = new Set;
194
+ direct = new Map;
195
+ directLocal = new Map;
196
+ ov = signal2([]);
197
+ skip = false;
198
+ identify = makeIdentify();
199
+ rules;
200
+ constructor(client) {
201
+ this.client = client;
202
+ }
203
+ signalOf(ref) {
204
+ let a = this.signals.get(ref);
205
+ if (!a) {
206
+ a = signal2(undefined, { name: ref });
207
+ this.signals.set(ref, a);
208
+ }
209
+ return a;
210
+ }
211
+ peek(ref) {
212
+ const a = this.signals.get(ref);
213
+ return a && this.client.untracked(() => this.client.get(a));
214
+ }
215
+ fields(ref) {
216
+ return fieldsOf(this, ref);
217
+ }
218
+ get empty() {
219
+ for (const ref of this.signals.keys())
220
+ if (this.peek(ref))
221
+ return false;
222
+ return true;
223
+ }
224
+ clear(ref) {
225
+ const had = this.peek(ref) !== undefined;
226
+ this.direct.delete(ref);
227
+ this.directLocal.delete(ref);
228
+ const a = this.signals.get(ref);
229
+ if (!a)
230
+ return false;
231
+ if (had)
232
+ this.client.set(a, undefined);
233
+ if (nodeOf(this.client.graph, a).refs === 0)
234
+ this.signals.delete(ref);
235
+ return had;
236
+ }
237
+ merge(ref, fields) {
238
+ const prev = this.peek(ref);
239
+ const changed = {};
240
+ let any = !prev;
241
+ for (const k in fields) {
242
+ if (prev && Object.hasOwn(prev, k) && equal(prev[k], fields[k]))
243
+ continue;
244
+ changed[k] = fields[k];
245
+ any = true;
246
+ }
247
+ if (any)
248
+ this.client.set(this.signalOf(ref), { ...prev, ...changed });
249
+ }
250
+ sweep() {
251
+ const live = new Set;
252
+ const todo = [...this.pins];
253
+ for (const root of this.roots)
254
+ todo.push(...root.refs);
255
+ for (let r = todo.pop();r !== undefined; r = todo.pop()) {
256
+ if (live.has(r))
257
+ continue;
258
+ live.add(r);
259
+ const fields = this.peek(r);
260
+ for (const k in fields)
261
+ todo.push(...refsIn(fields[k]));
262
+ }
263
+ this.client.batch(() => {
264
+ for (const ref of [...this.signals.keys()])
265
+ if (!live.has(ref))
266
+ this.clear(ref);
267
+ });
268
+ }
269
+ }
270
+
271
+ // src/entities/policy.ts
272
+ class EntityRoot {
273
+ value;
274
+ refs;
275
+ memo = new Map;
276
+ seen;
277
+ constructor(value, refs) {
278
+ this.value = value;
279
+ this.refs = refs;
280
+ }
281
+ }
282
+ var plain = { normalize, denormalize };
283
+ var KEY = {};
284
+ function storeOf(client) {
285
+ let s = client.slots.get(KEY);
286
+ if (!s) {
287
+ s = new EntityStore(client);
288
+ client.slots.set(KEY, s);
289
+ }
290
+ return s;
291
+ }
292
+ function bound(client, opts, identify) {
293
+ const store = storeOf(client);
294
+ const rules = { keys: opts?.keys, identify: opts?.identify };
295
+ if (store.rules) {
296
+ if (store.rules.keys !== rules.keys || store.rules.identify !== rules.identify)
297
+ throw new RivaError2("RIVA_CACHE", "This client already uses other entity identity rules. Share one entities() (or the same options objects) between its queries.");
298
+ } else if (rules.keys || rules.identify) {
299
+ if (!store.empty)
300
+ throw new RivaError2("RIVA_CACHE", "This client already uses other entity identity rules. Create its entities() policy before its first readEntity or writeEntity.");
301
+ store.rules = rules;
302
+ store.identify = identify;
303
+ } else
304
+ store.rules = rules;
305
+ return store;
306
+ }
307
+ function entities(opts, codec = plain) {
308
+ const identify = makeIdentify(opts);
309
+ return {
310
+ identify,
311
+ write(client, data, prev, vars) {
312
+ const store = bound(client, opts, identify);
313
+ return client.batch(() => {
314
+ const { root, pending } = codec.normalize(store, data, vars);
315
+ for (const [ref, fields] of pending)
316
+ store.merge(ref, fields);
317
+ const stored = new EntityRoot(root, refsIn(root));
318
+ if (prev) {
319
+ stored.memo = prev.memo;
320
+ stored.seen = prev.seen;
321
+ }
322
+ store.roots.add(stored);
323
+ return stored;
324
+ });
325
+ },
326
+ read(client, stored, committed) {
327
+ const store = bound(client, opts, identify);
328
+ store.skip = !!committed;
329
+ try {
330
+ const seen = share(stored.seen, codec.denormalize(store, stored.value, stored.memo));
331
+ if (!committed)
332
+ stored.seen = seen;
333
+ return seen;
334
+ } finally {
335
+ store.skip = false;
336
+ }
337
+ },
338
+ layer(client, data, vars) {
339
+ const { pending } = codec.normalize(bound(client, opts, identify), data, vars);
340
+ client.set(storeOf(client).ov, (ls) => [...ls, pending]);
341
+ return {
342
+ drop: () => client.set(storeOf(client).ov, (ls) => ls.filter((l) => l !== pending))
343
+ };
344
+ },
345
+ commit(client, data, vars) {
346
+ const store = bound(client, opts, identify);
347
+ const { pending } = codec.normalize(store, data, vars);
348
+ client.batch(() => {
349
+ for (const [ref, fields] of pending)
350
+ store.merge(ref, fields);
351
+ });
352
+ },
353
+ release(client, stored) {
354
+ const store = bound(client, opts, identify);
355
+ store.roots.delete(stored);
356
+ store.sweep();
357
+ }
358
+ };
359
+ }
360
+
361
+ // src/entities/api.ts
362
+ var readEntity = (client, ref) => {
363
+ const store = storeOf(client);
364
+ return denormalize(store, { __ref: ref }, store.directLocal);
365
+ };
366
+ function writeEntity(client, ref, patch) {
367
+ const store = storeOf(client);
368
+ const next = typeof patch === "function" ? patch(client.untracked(() => readEntity(client, ref))) : patch;
369
+ const pending = normalizeAt(store, ref, next);
370
+ client.batch(() => {
371
+ for (const [r, fields] of pending)
372
+ store.merge(r, fields);
373
+ store.pins.add(ref);
374
+ });
375
+ }
376
+ function evict(client, ref) {
377
+ const store = storeOf(client);
378
+ store.pins.delete(ref);
379
+ return store.clear(ref);
380
+ }
381
+ function invalidateEntity(client, ref) {
382
+ const store = storeOf(client);
383
+ client.queries.invalidateBy((stored) => {
384
+ if (!(stored instanceof EntityRoot))
385
+ return false;
386
+ const seen = new Set;
387
+ const todo = [...stored.refs];
388
+ for (let r = todo.pop();r !== undefined; r = todo.pop()) {
389
+ if (r === ref)
390
+ return true;
391
+ if (seen.has(r))
392
+ continue;
393
+ seen.add(r);
394
+ const fields = store.peek(r);
395
+ for (const k in fields)
396
+ todo.push(...refsIn(fields[k]));
397
+ }
398
+ return false;
399
+ });
400
+ }
401
+ var identifyOf = (policy, obj) => policy.identify(obj);
402
+ export {
403
+ entities,
404
+ evict,
405
+ identifyOf,
406
+ invalidateEntity,
407
+ isPlain,
408
+ isRef,
409
+ readEntity,
410
+ writeEntity
411
+ };
412
+
413
+ //# debugId=A91C161CDC696BAC64756E2164756E21
414
+ //# sourceMappingURL=index.js.map