nowity 1.0.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 (192) hide show
  1. package/README.md +751 -0
  2. package/dist/api/contract.d.ts +186 -0
  3. package/dist/api/contract.d.ts.map +1 -0
  4. package/dist/api/contract.js +251 -0
  5. package/dist/api/errors.d.ts +50 -0
  6. package/dist/api/errors.d.ts.map +1 -0
  7. package/dist/api/errors.js +50 -0
  8. package/dist/api/index.d.ts +13 -0
  9. package/dist/api/index.d.ts.map +1 -0
  10. package/dist/api/index.js +32 -0
  11. package/dist/client/confirmed-store.d.ts +33 -0
  12. package/dist/client/confirmed-store.d.ts.map +1 -0
  13. package/dist/client/confirmed-store.js +123 -0
  14. package/dist/client/connection.d.ts +102 -0
  15. package/dist/client/connection.d.ts.map +1 -0
  16. package/dist/client/connection.js +285 -0
  17. package/dist/client/list.d.ts +102 -0
  18. package/dist/client/list.d.ts.map +1 -0
  19. package/dist/client/list.js +257 -0
  20. package/dist/client/live-query.d.ts +153 -0
  21. package/dist/client/live-query.d.ts.map +1 -0
  22. package/dist/client/live-query.js +244 -0
  23. package/dist/client/nowity-client.d.ts +188 -0
  24. package/dist/client/nowity-client.d.ts.map +1 -0
  25. package/dist/client/nowity-client.js +580 -0
  26. package/dist/client/null-transport.d.ts +16 -0
  27. package/dist/client/null-transport.d.ts.map +1 -0
  28. package/dist/client/null-transport.js +17 -0
  29. package/dist/client/overlay.d.ts +44 -0
  30. package/dist/client/overlay.d.ts.map +1 -0
  31. package/dist/client/overlay.js +90 -0
  32. package/dist/client/query-source.d.ts +78 -0
  33. package/dist/client/query-source.d.ts.map +1 -0
  34. package/dist/client/query-source.js +57 -0
  35. package/dist/client/ssr-source.d.ts +70 -0
  36. package/dist/client/ssr-source.d.ts.map +1 -0
  37. package/dist/client/ssr-source.js +134 -0
  38. package/dist/client/transport.d.ts +19 -0
  39. package/dist/client/transport.d.ts.map +1 -0
  40. package/dist/client/transport.js +2 -0
  41. package/dist/client/ws-transport.d.ts +30 -0
  42. package/dist/client/ws-transport.d.ts.map +1 -0
  43. package/dist/client/ws-transport.js +89 -0
  44. package/dist/client.d.ts +47 -0
  45. package/dist/client.d.ts.map +1 -0
  46. package/dist/client.js +80 -0
  47. package/dist/core/diff.d.ts +14 -0
  48. package/dist/core/diff.d.ts.map +1 -0
  49. package/dist/core/diff.js +56 -0
  50. package/dist/core/normalize.d.ts +16 -0
  51. package/dist/core/normalize.d.ts.map +1 -0
  52. package/dist/core/normalize.js +67 -0
  53. package/dist/core/protocol.d.ts +215 -0
  54. package/dist/core/protocol.d.ts.map +1 -0
  55. package/dist/core/protocol.js +14 -0
  56. package/dist/core/ssr-bridge.d.ts +69 -0
  57. package/dist/core/ssr-bridge.d.ts.map +1 -0
  58. package/dist/core/ssr-bridge.js +40 -0
  59. package/dist/core/types.d.ts +96 -0
  60. package/dist/core/types.d.ts.map +1 -0
  61. package/dist/core/types.js +17 -0
  62. package/dist/index.d.ts +47 -0
  63. package/dist/index.d.ts.map +1 -0
  64. package/dist/index.js +98 -0
  65. package/dist/ironbean/client.d.ts +7 -0
  66. package/dist/ironbean/client.d.ts.map +1 -0
  67. package/dist/ironbean/client.js +38 -0
  68. package/dist/ironbean/server.d.ts +42 -0
  69. package/dist/ironbean/server.d.ts.map +1 -0
  70. package/dist/ironbean/server.js +62 -0
  71. package/dist/ironbean/tokens.d.ts +26 -0
  72. package/dist/ironbean/tokens.d.ts.map +1 -0
  73. package/dist/ironbean/tokens.js +23 -0
  74. package/dist/next/config.d.ts +7 -0
  75. package/dist/next/config.d.ts.map +1 -0
  76. package/dist/next/config.js +77 -0
  77. package/dist/next/live.d.ts +69 -0
  78. package/dist/next/live.d.ts.map +1 -0
  79. package/dist/next/live.js +70 -0
  80. package/dist/next/serve.d.ts +44 -0
  81. package/dist/next/serve.d.ts.map +1 -0
  82. package/dist/next/serve.js +107 -0
  83. package/dist/next/server.d.ts +62 -0
  84. package/dist/next/server.d.ts.map +1 -0
  85. package/dist/next/server.js +127 -0
  86. package/dist/react/connection-context.d.ts +4 -0
  87. package/dist/react/connection-context.d.ts.map +1 -0
  88. package/dist/react/connection-context.js +28 -0
  89. package/dist/react/grid-list.d.ts +34 -0
  90. package/dist/react/grid-list.d.ts.map +1 -0
  91. package/dist/react/grid-list.js +62 -0
  92. package/dist/react/hydrate.d.ts +22 -0
  93. package/dist/react/hydrate.d.ts.map +1 -0
  94. package/dist/react/hydrate.js +18 -0
  95. package/dist/react/index.d.ts +35 -0
  96. package/dist/react/index.d.ts.map +1 -0
  97. package/dist/react/index.js +51 -0
  98. package/dist/react/list.d.ts +31 -0
  99. package/dist/react/list.d.ts.map +1 -0
  100. package/dist/react/list.js +35 -0
  101. package/dist/react/prerendered.d.ts +33 -0
  102. package/dist/react/prerendered.d.ts.map +1 -0
  103. package/dist/react/prerendered.js +165 -0
  104. package/dist/react/provider.d.ts +64 -0
  105. package/dist/react/provider.d.ts.map +1 -0
  106. package/dist/react/provider.js +130 -0
  107. package/dist/react/refusals.d.ts +31 -0
  108. package/dist/react/refusals.d.ts.map +1 -0
  109. package/dist/react/refusals.js +51 -0
  110. package/dist/react/restricted.d.ts +76 -0
  111. package/dist/react/restricted.d.ts.map +1 -0
  112. package/dist/react/restricted.js +126 -0
  113. package/dist/react/use-list.d.ts +42 -0
  114. package/dist/react/use-list.d.ts.map +1 -0
  115. package/dist/react/use-list.js +56 -0
  116. package/dist/react/use-query.d.ts +66 -0
  117. package/dist/react/use-query.d.ts.map +1 -0
  118. package/dist/react/use-query.js +126 -0
  119. package/dist/react/virtual.d.ts +60 -0
  120. package/dist/react/virtual.d.ts.map +1 -0
  121. package/dist/react/virtual.js +131 -0
  122. package/dist/server/api/auth.d.ts +37 -0
  123. package/dist/server/api/auth.d.ts.map +1 -0
  124. package/dist/server/api/auth.js +54 -0
  125. package/dist/server/api/bridge.d.ts +28 -0
  126. package/dist/server/api/bridge.d.ts.map +1 -0
  127. package/dist/server/api/bridge.js +330 -0
  128. package/dist/server/api/collector.d.ts +25 -0
  129. package/dist/server/api/collector.d.ts.map +1 -0
  130. package/dist/server/api/collector.js +184 -0
  131. package/dist/server/api/configure.d.ts +31 -0
  132. package/dist/server/api/configure.d.ts.map +1 -0
  133. package/dist/server/api/configure.js +67 -0
  134. package/dist/server/api/execution.d.ts +77 -0
  135. package/dist/server/api/execution.d.ts.map +1 -0
  136. package/dist/server/api/execution.js +97 -0
  137. package/dist/server/api/list.d.ts +141 -0
  138. package/dist/server/api/list.d.ts.map +1 -0
  139. package/dist/server/api/list.js +281 -0
  140. package/dist/server/api/prerender.d.ts +32 -0
  141. package/dist/server/api/prerender.d.ts.map +1 -0
  142. package/dist/server/api/prerender.js +69 -0
  143. package/dist/server/api/read-keys.d.ts +128 -0
  144. package/dist/server/api/read-keys.d.ts.map +1 -0
  145. package/dist/server/api/read-keys.js +464 -0
  146. package/dist/server/api/read-set.d.ts +103 -0
  147. package/dist/server/api/read-set.d.ts.map +1 -0
  148. package/dist/server/api/read-set.js +210 -0
  149. package/dist/server/api/runtime.d.ts +36 -0
  150. package/dist/server/api/runtime.d.ts.map +1 -0
  151. package/dist/server/api/runtime.js +49 -0
  152. package/dist/server/api/tracked-client.d.ts +6 -0
  153. package/dist/server/api/tracked-client.d.ts.map +1 -0
  154. package/dist/server/api/tracked-client.js +56 -0
  155. package/dist/server/change-source/change-source.d.ts +41 -0
  156. package/dist/server/change-source/change-source.d.ts.map +1 -0
  157. package/dist/server/change-source/change-source.js +2 -0
  158. package/dist/server/change-source/outbox-change-source.d.ts +106 -0
  159. package/dist/server/change-source/outbox-change-source.d.ts.map +1 -0
  160. package/dist/server/change-source/outbox-change-source.js +237 -0
  161. package/dist/server/change-source/wal-change-source.d.ts +85 -0
  162. package/dist/server/change-source/wal-change-source.d.ts.map +1 -0
  163. package/dist/server/change-source/wal-change-source.js +193 -0
  164. package/dist/server/db.d.ts +51 -0
  165. package/dist/server/db.d.ts.map +1 -0
  166. package/dist/server/db.js +53 -0
  167. package/dist/server/evaluate.d.ts +39 -0
  168. package/dist/server/evaluate.d.ts.map +1 -0
  169. package/dist/server/evaluate.js +61 -0
  170. package/dist/server/invalidation-index.d.ts +64 -0
  171. package/dist/server/invalidation-index.d.ts.map +1 -0
  172. package/dist/server/invalidation-index.js +0 -0
  173. package/dist/server/mutator.d.ts +59 -0
  174. package/dist/server/mutator.d.ts.map +1 -0
  175. package/dist/server/mutator.js +77 -0
  176. package/dist/server/query.d.ts +113 -0
  177. package/dist/server/query.d.ts.map +1 -0
  178. package/dist/server/query.js +35 -0
  179. package/dist/server/setup.d.ts +30 -0
  180. package/dist/server/setup.d.ts.map +1 -0
  181. package/dist/server/setup.js +107 -0
  182. package/dist/server/sql/setup.sql +215 -0
  183. package/dist/server/ssr.d.ts +29 -0
  184. package/dist/server/ssr.d.ts.map +1 -0
  185. package/dist/server/ssr.js +43 -0
  186. package/dist/server/sync-engine.d.ts +256 -0
  187. package/dist/server/sync-engine.d.ts.map +1 -0
  188. package/dist/server/sync-engine.js +805 -0
  189. package/dist/server/ws-server.d.ts +56 -0
  190. package/dist/server/ws-server.d.ts.map +1 -0
  191. package/dist/server/ws-server.js +241 -0
  192. package/package.json +81 -0
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RefusalContext = void 0;
4
+ exports.useRefusalScope = useRefusalScope;
5
+ exports.refuse = refuse;
6
+ exports.waitOn = waitOn;
7
+ /**
8
+ * How a refusal reaches the boundary that renders it.
9
+ *
10
+ * In a browser it does not need this: the hook throws, and `<Restricted>` is an
11
+ * error boundary that catches. A server render has no such thing — React's
12
+ * streaming renderer has no commit phase and therefore no error boundaries, and
13
+ * a component that throws takes the nearest *Suspense* boundary with it, which
14
+ * is written into the HTML as "render this part again in the browser".
15
+ *
16
+ * Nothing above the throw can help, either: the server renderer retries the
17
+ * component that suspended, never its ancestors, so a boundary cannot re-render
18
+ * itself into a fallback the way it could in a browser.
19
+ *
20
+ * What it *can* do is read. The fallback of a boundary that errored is rendered
21
+ * afterwards and goes into the HTML, so the refusal is left here on the way out
22
+ * and picked up there — one baton, written immediately before the throw and read
23
+ * immediately after it. That is the whole of this module, and it is what keeps a
24
+ * refused panel a rendered refusal on the server rather than a hole the browser
25
+ * has to fill in.
26
+ */
27
+ const react_1 = require("react");
28
+ const errors_1 = require("../api/errors");
29
+ exports.RefusalContext = (0, react_1.createContext)(null);
30
+ /** The nearest boundary's baton, or null where there is no boundary above. */
31
+ function useRefusalScope() {
32
+ return (0, react_1.useContext)(exports.RefusalContext);
33
+ }
34
+ /**
35
+ * Leave a refusal for the boundary above, and hand it back to be thrown.
36
+ *
37
+ * Written for every refusal, not only on a server: which of the two paths picks
38
+ * it up is React's decision, not ours, and a baton nobody collects is cleared by
39
+ * the boundary's next render.
40
+ */
41
+ function refuse(scope, problem) {
42
+ if (scope && (0, errors_1.isForbidden)(problem))
43
+ scope.refusal = problem;
44
+ return problem;
45
+ }
46
+ /** The same, for a wait: what the boundary's fallback should suspend on instead. */
47
+ function waitOn(scope, settled) {
48
+ if (scope)
49
+ scope.pending = settled;
50
+ return settled;
51
+ }
@@ -0,0 +1,76 @@
1
+ import { Component, type ReactNode } from "react";
2
+ import { type Forbidden } from "../api/errors";
3
+ export interface RestrictedProps {
4
+ /**
5
+ * Shown while the subtree may not be read.
6
+ *
7
+ * A function is handed the refusal itself, whose message is whatever the
8
+ * endpoint said when it threw — the one thing about "no access" that a
9
+ * boundary would otherwise put out of reach.
10
+ */
11
+ fallback?: ReactNode | ((refusal: Forbidden) => ReactNode);
12
+ children: ReactNode;
13
+ }
14
+ /**
15
+ * `<Suspense>` for permissions: where "you may not see this" is rendered.
16
+ *
17
+ * ```tsx
18
+ * <Live fallback={<Spinner />}>
19
+ * <Restricted fallback={<p>No access to this project.</p>}>
20
+ * <Documents projectId={id} />
21
+ * </Restricted>
22
+ * </Live>
23
+ * ```
24
+ *
25
+ * A refused query throws, the way a slow one suspends, so a component that reads
26
+ * data states what it renders *with* data and nothing else. Where the refusal is
27
+ * caught is then a decision about the page — one boundary around a panel, or one
28
+ * around everything — rather than a branch repeated in every component that
29
+ * happens to read something.
30
+ *
31
+ * What makes that safe is the part a plain error boundary cannot do: **it comes
32
+ * back**. Permissions are granted as often as they are taken away, and an
33
+ * ordinary boundary stays caught once it has caught something, which is why this
34
+ * was `undefined` for a while. A refusal from a live query carries the way out —
35
+ * the subscription stays registered and denied, watching the very table that
36
+ * would grant access — so this waits on it and un-catches itself the moment the
37
+ * query is readable again. Add someone to a project and their open page fills in.
38
+ *
39
+ * Anything that is not a refusal is rethrown for the next boundary up, because a
40
+ * broken query is a fault and this is not where faults are handled. A `Forbidden`
41
+ * thrown by application code rather than by a query has nothing to watch, so it
42
+ * latches like any caught error — that is the honest answer, since nothing about
43
+ * it can change on its own.
44
+ *
45
+ * It is not a `<Suspense>`, and still needs one above it: after it resets, the
46
+ * children read a query that is loading again and suspend. `<Live>` is the usual
47
+ * one, and is where waiting belongs anyway.
48
+ *
49
+ * ---
50
+ *
51
+ * The `<Suspense>` *inside* it is not that boundary and is not for waiting. It is
52
+ * the whole of how this works on a server, where React has no error boundaries
53
+ * at all — see {@link RefusalScope}. A refusal takes the nearest Suspense
54
+ * boundary with it, so this puts one right here, and its fallback renders the
55
+ * refusal from the baton: the damage stops at this panel instead of costing the
56
+ * page its prerender, and "no access" is in the HTML rather than a hole. When
57
+ * what happened was an ordinary wait rather than a refusal, that fallback
58
+ * suspends in turn, which hands the subtree to the next boundary up — so the
59
+ * page's own spinner is still what a slow query shows.
60
+ */
61
+ export declare class Restricted extends Component<RestrictedProps, {
62
+ refusal: unknown;
63
+ }> {
64
+ state: {
65
+ refusal: unknown;
66
+ };
67
+ private readonly scope;
68
+ private stopWatching;
69
+ static getDerivedStateFromError(refusal: unknown): {
70
+ refusal: unknown;
71
+ };
72
+ componentDidCatch(refusal: unknown): void;
73
+ componentWillUnmount(): void;
74
+ render(): ReactNode;
75
+ }
76
+ //# sourceMappingURL=restricted.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"restricted.d.ts","sourceRoot":"","sources":["../../src/react/restricted.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAY,KAAK,SAAS,EAAE,MAAM,OAAO,CAAC;AAC5D,OAAO,EAAe,KAAK,SAAS,EAAE,MAAM,eAAe,CAAC;AAI5D,MAAM,WAAW,eAAe;IAC5B;;;;;;OAMG;IACH,QAAQ,CAAC,EAAE,SAAS,GAAG,CAAC,CAAC,OAAO,EAAE,SAAS,KAAK,SAAS,CAAC,CAAC;IAC3D,QAAQ,EAAE,SAAS,CAAC;CACvB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AACH,qBAAa,UAAW,SAAQ,SAAS,CAAC,eAAe,EAAE;IAAE,OAAO,EAAE,OAAO,CAAA;CAAE,CAAC;IACnE,KAAK,EAAE;QAAE,OAAO,EAAE,OAAO,CAAA;KAAE,CAAqB;IACzD,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAkD;IACxE,OAAO,CAAC,YAAY,CAA6B;IAEjD,MAAM,CAAC,wBAAwB,CAAC,OAAO,EAAE,OAAO,GAAG;QAAE,OAAO,EAAE,OAAO,CAAA;KAAE;IAQ9D,iBAAiB,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI;IASzC,oBAAoB,IAAI,IAAI;IAK5B,MAAM,IAAI,SAAS;CAsB/B"}
@@ -0,0 +1,126 @@
1
+ "use strict";
2
+ "use client";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.Restricted = void 0;
5
+ const jsx_runtime_1 = require("react/jsx-runtime");
6
+ const react_1 = require("react");
7
+ const errors_1 = require("../api/errors");
8
+ const refusals_1 = require("./refusals");
9
+ /**
10
+ * `<Suspense>` for permissions: where "you may not see this" is rendered.
11
+ *
12
+ * ```tsx
13
+ * <Live fallback={<Spinner />}>
14
+ * <Restricted fallback={<p>No access to this project.</p>}>
15
+ * <Documents projectId={id} />
16
+ * </Restricted>
17
+ * </Live>
18
+ * ```
19
+ *
20
+ * A refused query throws, the way a slow one suspends, so a component that reads
21
+ * data states what it renders *with* data and nothing else. Where the refusal is
22
+ * caught is then a decision about the page — one boundary around a panel, or one
23
+ * around everything — rather than a branch repeated in every component that
24
+ * happens to read something.
25
+ *
26
+ * What makes that safe is the part a plain error boundary cannot do: **it comes
27
+ * back**. Permissions are granted as often as they are taken away, and an
28
+ * ordinary boundary stays caught once it has caught something, which is why this
29
+ * was `undefined` for a while. A refusal from a live query carries the way out —
30
+ * the subscription stays registered and denied, watching the very table that
31
+ * would grant access — so this waits on it and un-catches itself the moment the
32
+ * query is readable again. Add someone to a project and their open page fills in.
33
+ *
34
+ * Anything that is not a refusal is rethrown for the next boundary up, because a
35
+ * broken query is a fault and this is not where faults are handled. A `Forbidden`
36
+ * thrown by application code rather than by a query has nothing to watch, so it
37
+ * latches like any caught error — that is the honest answer, since nothing about
38
+ * it can change on its own.
39
+ *
40
+ * It is not a `<Suspense>`, and still needs one above it: after it resets, the
41
+ * children read a query that is loading again and suspend. `<Live>` is the usual
42
+ * one, and is where waiting belongs anyway.
43
+ *
44
+ * ---
45
+ *
46
+ * The `<Suspense>` *inside* it is not that boundary and is not for waiting. It is
47
+ * the whole of how this works on a server, where React has no error boundaries
48
+ * at all — see {@link RefusalScope}. A refusal takes the nearest Suspense
49
+ * boundary with it, so this puts one right here, and its fallback renders the
50
+ * refusal from the baton: the damage stops at this panel instead of costing the
51
+ * page its prerender, and "no access" is in the HTML rather than a hole. When
52
+ * what happened was an ordinary wait rather than a refusal, that fallback
53
+ * suspends in turn, which hands the subtree to the next boundary up — so the
54
+ * page's own spinner is still what a slow query shows.
55
+ */
56
+ class Restricted extends react_1.Component {
57
+ constructor() {
58
+ super(...arguments);
59
+ this.state = { refusal: null };
60
+ this.scope = { refusal: null, pending: null };
61
+ this.stopWatching = null;
62
+ }
63
+ static getDerivedStateFromError(refusal) {
64
+ // Everything is caught and sorted out in `render`, which is the only
65
+ // place a boundary can decline one: rethrowing from the render React
66
+ // retries is what sends it up, and there is no other way to say "not
67
+ // mine".
68
+ return { refusal };
69
+ }
70
+ componentDidCatch(refusal) {
71
+ if (!(0, errors_1.isForbidden)(refusal))
72
+ return;
73
+ this.stopWatching =
74
+ refusal.onReadable?.(() => {
75
+ this.stopWatching = null;
76
+ this.setState({ refusal: null });
77
+ }) ?? null;
78
+ }
79
+ componentWillUnmount() {
80
+ this.stopWatching?.();
81
+ this.stopWatching = null;
82
+ }
83
+ render() {
84
+ const { refusal } = this.state;
85
+ if (refusal) {
86
+ if (!(0, errors_1.isForbidden)(refusal))
87
+ throw refusal;
88
+ return render(this.props.fallback, refusal);
89
+ }
90
+ // The baton is cleared here rather than where it is read, because this
91
+ // render is the boundary starting again — a mount, or access having come
92
+ // back. A refusal left over from the last one would otherwise be shown
93
+ // while the query it belonged to is merely loading.
94
+ this.scope.refusal = null;
95
+ this.scope.pending = null;
96
+ return ((0, jsx_runtime_1.jsx)(refusals_1.RefusalContext.Provider, { value: this.scope, children: (0, jsx_runtime_1.jsx)(react_1.Suspense, { fallback: (0, jsx_runtime_1.jsx)(Refused, { scope: this.scope, fallback: this.props.fallback }), children: this.props.children }) }));
97
+ }
98
+ }
99
+ exports.Restricted = Restricted;
100
+ /**
101
+ * The inner boundary's fallback: a refusal if that is what happened, and
102
+ * otherwise not this boundary's business.
103
+ */
104
+ function Refused({ scope, fallback, }) {
105
+ if (scope.refusal)
106
+ return render(fallback, scope.refusal);
107
+ // A wait, then — and waiting is rendered by the page, not here. Suspending
108
+ // from a fallback hands the subtree to the next boundary up, which is the
109
+ // `<Live>` that was put there for exactly this.
110
+ //
111
+ // On the query's own promise, and taken off the baton as it is thrown. A
112
+ // promise that never settles would do the same thing for the browser and
113
+ // deadlock a server render: the fallback is rendered once for the wait and
114
+ // again for the refusal that follows it, and the second of those never
115
+ // happens if the first left the segment waiting forever.
116
+ const pending = scope.pending;
117
+ scope.pending = null;
118
+ if (pending)
119
+ throw pending;
120
+ // Neither refused nor waiting: the subtree suspended on something that is
121
+ // not a query, and the boundary above is the one that should say so.
122
+ return null;
123
+ }
124
+ function render(fallback, refusal) {
125
+ return typeof fallback === "function" ? fallback(refusal) : (fallback ?? null);
126
+ }
@@ -0,0 +1,42 @@
1
+ import { type LiveList } from "../client/list";
2
+ import type { ContractRef } from "./use-query";
3
+ /**
4
+ * Every `@list` endpoint of a contract, as the store that pages it.
5
+ *
6
+ * `all(projectId): Promise<Document[]>` becomes `all(projectId): LiveList<Document>`
7
+ * — the whole collection as a thing you can index into, rather than an array you
8
+ * would have had to receive in full before you could index into anything.
9
+ */
10
+ export type Listed<T> = {
11
+ [K in keyof T]: T[K] extends (...args: infer A) => Promise<Array<infer R>> ? (...args: A) => LiveList<R> : never;
12
+ };
13
+ export interface UseListOptions {
14
+ /**
15
+ * How many rows one window holds. 50 unless the items are unusually large or
16
+ * unusually cheap.
17
+ *
18
+ * It is the unit of everything: of the subscriptions the browser opens, of
19
+ * what a scroll costs, and of how much of the collection arrives at once. It
20
+ * is a client-side decision on purpose — the endpoint has no opinion about
21
+ * viewports, which is the whole point of it having no paging arguments.
22
+ */
23
+ pageSize?: number;
24
+ }
25
+ /**
26
+ * Read a contract's collections.
27
+ *
28
+ * ```tsx
29
+ * const documents = useList(DocumentApi).all(projectId);
30
+ * return <GridList of={documents} itemHeight={72}>{(row) => <Card row={row} />}</GridList>;
31
+ * ```
32
+ *
33
+ * The same three states {@link useQuerySuspense} has, resolved the same way:
34
+ * **not yet** suspends — a list waits for its own size, and nothing else —
35
+ * **not allowed** throws `Forbidden` for a `<Restricted>` boundary, and
36
+ * **broken** throws. What comes back is the store, not the rows: the rows arrive
37
+ * a window at a time, as something scrolls them into view.
38
+ *
39
+ * Call it during render, unconditionally, the way a hook is called.
40
+ */
41
+ export declare function useList<T extends object>(token: ContractRef<T>, options?: UseListOptions): Listed<T>;
42
+ //# sourceMappingURL=use-list.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-list.d.ts","sourceRoot":"","sources":["../../src/react/use-list.ts"],"names":[],"mappings":"AAEA,OAAO,EAAW,KAAK,QAAQ,EAAqB,MAAM,gBAAgB,CAAC;AAI3E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE/C;;;;;;GAMG;AACH,MAAM,MAAM,MAAM,CAAC,CAAC,IAAI;KACnB,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,MAAM,CAAC,KAAK,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,GACpE,CAAC,GAAG,IAAI,EAAE,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,GAC3B,KAAK;CACd,CAAC;AAEF,MAAM,WAAW,cAAc;IAC3B;;;;;;;;OAQG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;CACrB;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,OAAO,CAAC,CAAC,SAAS,MAAM,EACpC,KAAK,EAAE,WAAW,CAAC,CAAC,CAAC,EACrB,OAAO,GAAE,cAAmB,GAC7B,MAAM,CAAC,CAAC,CAAC,CAiBX"}
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useList = useList;
4
+ const react_1 = require("react");
5
+ const contract_1 = require("../api/contract");
6
+ const list_1 = require("../client/list");
7
+ const connection_context_1 = require("./connection-context");
8
+ const refusals_1 = require("./refusals");
9
+ /**
10
+ * Read a contract's collections.
11
+ *
12
+ * ```tsx
13
+ * const documents = useList(DocumentApi).all(projectId);
14
+ * return <GridList of={documents} itemHeight={72}>{(row) => <Card row={row} />}</GridList>;
15
+ * ```
16
+ *
17
+ * The same three states {@link useQuerySuspense} has, resolved the same way:
18
+ * **not yet** suspends — a list waits for its own size, and nothing else —
19
+ * **not allowed** throws `Forbidden` for a `<Restricted>` boundary, and
20
+ * **broken** throws. What comes back is the store, not the rows: the rows arrive
21
+ * a window at a time, as something scrolls them into view.
22
+ *
23
+ * Call it during render, unconditionally, the way a hook is called.
24
+ */
25
+ function useList(token, options = {}) {
26
+ const source = (0, connection_context_1.useQuerySource)();
27
+ const scope = (0, refusals_1.useRefusalScope)();
28
+ const contract = (0, contract_1.contractNameOf)(token);
29
+ const pageSize = options.pageSize ?? list_1.DEFAULT_PAGE_SIZE;
30
+ return new Proxy({}, {
31
+ get(_target, method) {
32
+ if (typeof method !== "string")
33
+ return undefined;
34
+ return (...args) => read(source, scope, `${contract}.${method}`, args, pageSize);
35
+ },
36
+ has: () => true,
37
+ });
38
+ }
39
+ function read(source, scope, endpoint, args, pageSize) {
40
+ const list = (0, list_1.listFor)(source, endpoint, args, pageSize);
41
+ (0, react_1.useEffect)(() => {
42
+ // Held on commit rather than on render, so a render Suspense threw away
43
+ // leaves nothing behind: the windows it opened are collected by the
44
+ // connection's grace period like any other unheld subscription.
45
+ const active = (0, list_1.listFor)(source, endpoint, args, pageSize);
46
+ active.retain();
47
+ return () => active.release();
48
+ // `list.key` is the endpoint, the arguments by value, and the page size.
49
+ // eslint-disable-next-line react-hooks/exhaustive-deps
50
+ }, [source, list.key]);
51
+ if (list.loading)
52
+ throw (0, refusals_1.waitOn)(scope, list.settled);
53
+ if (list.problem)
54
+ throw (0, refusals_1.refuse)(scope, list.problem);
55
+ return list;
56
+ }
@@ -0,0 +1,66 @@
1
+ /** A contract, as a token or as the name it travels under. */
2
+ export type ContractRef<T> = string | (abstract new (...args: any[]) => T);
3
+ /**
4
+ * Every endpoint of a contract, read as a value.
5
+ *
6
+ * `byProject(id): Promise<Document[]>` becomes `byProject(id): Document[]` — the
7
+ * promise is what the hook waits on, so what is left is the data.
8
+ */
9
+ export type Suspended<T> = {
10
+ [K in keyof T]: T[K] extends (...args: infer A) => Promise<infer R> ? (...args: A) => R : never;
11
+ };
12
+ /**
13
+ * Read a contract's queries for as long as the component is mounted.
14
+ *
15
+ * ```tsx
16
+ * const Documents = observer(({ projectId }: { projectId: string }) => {
17
+ * const rows = useQuerySuspense(DocumentApi).byProject(projectId);
18
+ * if (!rows) return <p>No access to this project.</p>;
19
+ * return <ul>{rows.map((row) => <Row key={row.id} row={row} />)}</ul>;
20
+ * });
21
+ * ```
22
+ *
23
+ * The hook hands back the contract itself, so reading is the call the server
24
+ * implements — same method, same arguments, same types. What it does with the
25
+ * three things that can be true of a query is the part React needs, and none of
26
+ * them is a value:
27
+ *
28
+ * - **not yet** suspends, so waiting is a `<Live>` or `<Suspense>` fallback;
29
+ * - **not allowed** throws {@link Forbidden}, so "no access" is a
30
+ * {@link Restricted} fallback — a boundary that, unlike an ordinary error
31
+ * boundary, comes back when access does;
32
+ * - **broken** throws, because a query that fails is a fault, and faults belong
33
+ * to an error boundary.
34
+ *
35
+ * All three are boundaries rather than branches on purpose: what a component
36
+ * says is what it renders *with* data, and where each of the other two is caught
37
+ * is a decision about the page instead of a condition repeated in every
38
+ * component that happens to read something.
39
+ *
40
+ * Wrap the component in `observer`: without it the component reads the objects
41
+ * but subscribes to nothing, and the field-level tracking that makes this worth
42
+ * doing never happens.
43
+ *
44
+ * Arguments are compared by value and are the subscription's identity, so
45
+ * passing the same ones twice — from two components, or on every render — shares
46
+ * one subscription rather than opening a second.
47
+ *
48
+ * Call the methods during render, unconditionally, the way a hook is called:
49
+ * each call subscribes, and React needs those in the same order every time.
50
+ * {@link useService} is the contract as it is, for mutations.
51
+ */
52
+ export declare function useQuerySuspense<T extends object>(token: ContractRef<T>): Suspended<T>;
53
+ /**
54
+ * The contract as the browser implements it: methods that return promises.
55
+ *
56
+ * For mutations — `documents.rename(id, title)` is an ordinary call that rejects
57
+ * if the server refuses it — and for the rare read that happens in an event
58
+ * handler rather than in render.
59
+ *
60
+ * ```tsx
61
+ * const documents = useService(DocumentApi);
62
+ * <button onClick={() => documents.rename(row.id, draft)}>Save</button>
63
+ * ```
64
+ */
65
+ export declare function useService<T extends object>(token: ContractRef<T>): T;
66
+ //# sourceMappingURL=use-query.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-query.d.ts","sourceRoot":"","sources":["../../src/react/use-query.ts"],"names":[],"mappings":"AAMA,8DAA8D;AAC9D,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI,MAAM,GAAG,CAAC,QAAQ,MAAM,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC;AAE3E;;;;;GAKG;AACH,MAAM,MAAM,SAAS,CAAC,CAAC,IAAI;KACtB,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,MAAM,CAAC,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC,GAC7D,CAAC,GAAG,IAAI,EAAE,CAAC,KAAK,CAAC,GACjB,KAAK;CACd,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,MAAM,EAAE,KAAK,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,CAUtF;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,UAAU,CAAC,CAAC,SAAS,MAAM,EAAE,KAAK,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,CAAC,CAarE"}
@@ -0,0 +1,126 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useQuerySuspense = useQuerySuspense;
4
+ exports.useService = useService;
5
+ const react_1 = require("react");
6
+ const contract_1 = require("../api/contract");
7
+ const connection_context_1 = require("./connection-context");
8
+ const refusals_1 = require("./refusals");
9
+ /**
10
+ * Read a contract's queries for as long as the component is mounted.
11
+ *
12
+ * ```tsx
13
+ * const Documents = observer(({ projectId }: { projectId: string }) => {
14
+ * const rows = useQuerySuspense(DocumentApi).byProject(projectId);
15
+ * if (!rows) return <p>No access to this project.</p>;
16
+ * return <ul>{rows.map((row) => <Row key={row.id} row={row} />)}</ul>;
17
+ * });
18
+ * ```
19
+ *
20
+ * The hook hands back the contract itself, so reading is the call the server
21
+ * implements — same method, same arguments, same types. What it does with the
22
+ * three things that can be true of a query is the part React needs, and none of
23
+ * them is a value:
24
+ *
25
+ * - **not yet** suspends, so waiting is a `<Live>` or `<Suspense>` fallback;
26
+ * - **not allowed** throws {@link Forbidden}, so "no access" is a
27
+ * {@link Restricted} fallback — a boundary that, unlike an ordinary error
28
+ * boundary, comes back when access does;
29
+ * - **broken** throws, because a query that fails is a fault, and faults belong
30
+ * to an error boundary.
31
+ *
32
+ * All three are boundaries rather than branches on purpose: what a component
33
+ * says is what it renders *with* data, and where each of the other two is caught
34
+ * is a decision about the page instead of a condition repeated in every
35
+ * component that happens to read something.
36
+ *
37
+ * Wrap the component in `observer`: without it the component reads the objects
38
+ * but subscribes to nothing, and the field-level tracking that makes this worth
39
+ * doing never happens.
40
+ *
41
+ * Arguments are compared by value and are the subscription's identity, so
42
+ * passing the same ones twice — from two components, or on every render — shares
43
+ * one subscription rather than opening a second.
44
+ *
45
+ * Call the methods during render, unconditionally, the way a hook is called:
46
+ * each call subscribes, and React needs those in the same order every time.
47
+ * {@link useService} is the contract as it is, for mutations.
48
+ */
49
+ function useQuerySuspense(token) {
50
+ return endpoints(token, (source, scope, name, args) => {
51
+ // Every read here is observable, and each one is reached on the path that
52
+ // returns a value — so a subscription revoked long after it resolved
53
+ // re-renders the component that was showing it, into the refusal.
54
+ const entry = subscribe(source, name, args);
55
+ if (entry.loading)
56
+ throw (0, refusals_1.waitOn)(scope, entry.settled);
57
+ if (entry.problem)
58
+ throw (0, refusals_1.refuse)(scope, entry.problem);
59
+ return entry.value;
60
+ });
61
+ }
62
+ /**
63
+ * The contract as the browser implements it: methods that return promises.
64
+ *
65
+ * For mutations — `documents.rename(id, title)` is an ordinary call that rejects
66
+ * if the server refuses it — and for the rare read that happens in an event
67
+ * handler rather than in render.
68
+ *
69
+ * ```tsx
70
+ * const documents = useService(DocumentApi);
71
+ * <button onClick={() => documents.rename(row.id, draft)}>Save</button>
72
+ * ```
73
+ */
74
+ function useService(token) {
75
+ // From the browser's connection, which is the same object the container
76
+ // hands out:
77
+ // the provider bound every contract to exactly this proxy, as a singleton,
78
+ // so `getBean(DocumentApi)` in a service and `useService(DocumentApi)` in a
79
+ // component are the same service.
80
+ //
81
+ // Asked here rather than through the container because a component must get
82
+ // the *browser's* half whatever process it renders in. A page rendered on
83
+ // the server, or a headless check running both halves, has a container that
84
+ // answers with the implementation — which talks to the database, outside any
85
+ // snapshot, as nothing a component may call.
86
+ return (0, connection_context_1.useQuerySource)().contract(nameOf(token));
87
+ }
88
+ function nameOf(token) {
89
+ return (0, contract_1.contractNameOf)(token);
90
+ }
91
+ /**
92
+ * The contract, with every method replaced by one that reads instead of calls.
93
+ *
94
+ * A proxy rather than an enumerated object: an abstract method emits no runtime
95
+ * property, so there is nothing on the class to enumerate.
96
+ */
97
+ function endpoints(token, read) {
98
+ const source = (0, connection_context_1.useQuerySource)();
99
+ const scope = (0, refusals_1.useRefusalScope)();
100
+ const contract = nameOf(token);
101
+ return new Proxy({}, {
102
+ get(_target, method) {
103
+ if (typeof method !== "string")
104
+ return undefined;
105
+ return (...args) => read(source, scope, `${contract}.${method}`, args);
106
+ },
107
+ has: () => true,
108
+ });
109
+ }
110
+ /** The shared entry for one query, held for as long as the component is mounted. */
111
+ function subscribe(source, name, args) {
112
+ const entry = source.ensure(name, args);
113
+ (0, react_1.useEffect)(() => {
114
+ // Retained on commit, not on render: a render React throws away — which
115
+ // is what suspending does — never runs this, and the entry it created is
116
+ // collected by the grace period instead of leaking a subscription for the
117
+ // session's lifetime. It is also why a server render can leave both of
118
+ // these empty: it never commits anything.
119
+ const active = source.ensure(name, args);
120
+ source.retain(active);
121
+ return () => source.release(active);
122
+ // `entry.key` is `name` plus the arguments by value.
123
+ // eslint-disable-next-line react-hooks/exhaustive-deps
124
+ }, [source, entry.key]);
125
+ return entry;
126
+ }
@@ -0,0 +1,60 @@
1
+ /**
2
+ * The geometry both {@link GridList} and {@link List} are made of.
3
+ *
4
+ * Internal: nothing exported here reaches `nowity/client`. What a person picks
5
+ * between is a grid and a list, and the difference between those two is one
6
+ * function — how many items fit across — so that is the only thing this takes
7
+ * from either of them. Everything else, from measuring the box to deciding
8
+ * which windows stay open, is the same work said once.
9
+ */
10
+ import { type CSSProperties, type JSX, type ReactNode } from "react";
11
+ import type { LiveList } from "../client/list";
12
+ /** What a grid and a list share, which is everything except how wide a row is. */
13
+ export interface VirtualProps<T> {
14
+ /** The collection, from `useList(SomeApi).something()`. */
15
+ of: LiveList<T>;
16
+ /** One item. Called with `undefined` for a row whose window is still loading. */
17
+ children: (item: T | undefined, index: number) => ReactNode;
18
+ /** Row height in pixels. Uniform, because that is what makes the geometry arithmetic. */
19
+ itemHeight: number;
20
+ /** Space between items, in pixels. */
21
+ gap?: number;
22
+ /** Extra rows rendered above and below the viewport. */
23
+ overscan?: number;
24
+ /** Height of the scrolling box. Ignored when `windowScroll` is set. */
25
+ height?: number | string;
26
+ /** Scroll with the page rather than inside a box of its own. */
27
+ windowScroll?: boolean;
28
+ /**
29
+ * How many items to render before the viewport has been measured — which is
30
+ * every server render, and the first paint of every browser one.
31
+ *
32
+ * It is what puts a screenful of rows in the HTML instead of an empty box:
33
+ * a server render asks the collection for exactly these, so the first window
34
+ * leaves with the page.
35
+ */
36
+ initialCount?: number;
37
+ /** Rendered instead of the rows when the collection is empty. */
38
+ empty?: ReactNode;
39
+ className?: string;
40
+ style?: CSSProperties;
41
+ }
42
+ interface VirtualLayout {
43
+ /** How many items fit across a box this wide. Called with 0 before measuring. */
44
+ columnsOf: (width: number) => number;
45
+ /** The same answer in CSS, for the paint that happens before any measurement. */
46
+ template: string;
47
+ }
48
+ /**
49
+ * An `observer` in its own right, and not only because its wrappers are.
50
+ *
51
+ * `observer` tracks the render it wraps, and `<List>` renders an *element* —
52
+ * everything below is a separate render as far as MobX is concerned. So the one
53
+ * observable this reads, `list.total`, has to be tracked here or a collection
54
+ * that grows would only redraw when something else in the tree happened to be
55
+ * watching it. A row appearing in a list nobody prints a count beside is exactly
56
+ * that case.
57
+ */
58
+ export declare const Virtual: <T>(props: VirtualProps<T> & VirtualLayout) => JSX.Element;
59
+ export {};
60
+ //# sourceMappingURL=virtual.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"virtual.d.ts","sourceRoot":"","sources":["../../src/react/virtual.tsx"],"names":[],"mappings":"AAEA;;;;;;;;GAQG;AACH,OAAO,EAIH,KAAK,aAAa,EAClB,KAAK,GAAG,EACR,KAAK,SAAS,EAEjB,MAAM,OAAO,CAAC;AAEf,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE/C,kFAAkF;AAClF,MAAM,WAAW,YAAY,CAAC,CAAC;IAC3B,2DAA2D;IAC3D,EAAE,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC;IAChB,iFAAiF;IACjF,QAAQ,EAAE,CAAC,IAAI,EAAE,CAAC,GAAG,SAAS,EAAE,KAAK,EAAE,MAAM,KAAK,SAAS,CAAC;IAC5D,yFAAyF;IACzF,UAAU,EAAE,MAAM,CAAC;IACnB,sCAAsC;IACtC,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,wDAAwD;IACxD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,uEAAuE;IACvE,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACzB,gEAAgE;IAChE,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB;;;;;;;OAOG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,iEAAiE;IACjE,KAAK,CAAC,EAAE,SAAS,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,aAAa,CAAC;CACzB;AAED,UAAU,aAAa;IACnB,iFAAiF;IACjF,SAAS,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC;IACrC,iFAAiF;IACjF,QAAQ,EAAE,MAAM,CAAC;CACpB;AAED;;;;;;;;;GASG;AACH,eAAO,MAAM,OAAO,EA+Fd,CAAC,CAAC,EAAE,KAAK,EAAE,YAAY,CAAC,CAAC,CAAC,GAAG,aAAa,KAAK,GAAG,CAAC,OAAO,CAAC"}