@party-stack/ontology-react 0.0.0-stage → 0.2.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.
- package/LICENSE +21 -0
- package/README.md +194 -2
- package/lib/attachmentPromiseCache.d.ts +30 -0
- package/lib/attachmentPromiseCache.d.ts.map +1 -0
- package/lib/attachmentPromiseCache.js +84 -0
- package/lib/attachmentPromiseCache.js.map +1 -0
- package/lib/attachmentResources.d.ts +9 -0
- package/lib/attachmentResources.d.ts.map +1 -0
- package/lib/attachmentResources.js +15 -0
- package/lib/attachmentResources.js.map +1 -0
- package/lib/attachments.d.ts +19 -0
- package/lib/attachments.d.ts.map +1 -0
- package/lib/attachments.js +79 -0
- package/lib/attachments.js.map +1 -0
- package/lib/authentication.d.ts +15 -0
- package/lib/authentication.d.ts.map +1 -0
- package/lib/authentication.js +48 -0
- package/lib/authentication.js.map +1 -0
- package/lib/context.d.ts +9 -0
- package/lib/context.d.ts.map +1 -0
- package/lib/context.js +16 -0
- package/lib/context.js.map +1 -0
- package/lib/index.d.ts +40 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +167 -0
- package/lib/index.js.map +1 -0
- package/lib/options.d.ts +9 -0
- package/lib/options.d.ts.map +1 -0
- package/lib/options.js +38 -0
- package/lib/options.js.map +1 -0
- package/lib/pins.d.ts +12 -0
- package/lib/pins.d.ts.map +1 -0
- package/lib/pins.js +40 -0
- package/lib/pins.js.map +1 -0
- package/lib/resource.d.ts +36 -0
- package/lib/resource.d.ts.map +1 -0
- package/lib/resource.js +105 -0
- package/lib/resource.js.map +1 -0
- package/package.json +44 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Robert Fidler
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,195 @@
|
|
|
1
|
-
#
|
|
1
|
+
# React ontologies
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Let the provider open and close a view, or supply an existing one. Query components can use module-level collection
|
|
4
|
+
descriptors; they do not need to pass a client or call an ontology hook.
|
|
5
|
+
|
|
6
|
+
```tsx
|
|
7
|
+
import { OntologyProvider } from "@party-stack/ontology-react";
|
|
8
|
+
import { httpClient } from "@party-stack/ontology/http";
|
|
9
|
+
import { webRuntime } from "@party-stack/web-runtime";
|
|
10
|
+
import { Suspense } from "react";
|
|
11
|
+
|
|
12
|
+
const options = {
|
|
13
|
+
client: httpClient("/api/ontology/rpc"),
|
|
14
|
+
runtime: webRuntime({ storage: "memory" }),
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
<Suspense fallback={<p>Opening…</p>}>
|
|
18
|
+
<OntologyProvider {...options}>
|
|
19
|
+
<Issues />
|
|
20
|
+
</OntologyProvider>
|
|
21
|
+
</Suspense>;
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Construction inputs are lazy. Keep object-valued props stable between renders. The provider
|
|
25
|
+
acquires after mounting, suspends through your parent boundary while opening, and
|
|
26
|
+
closes the view when options change or it unmounts. Startup failures reach the
|
|
27
|
+
nearest error boundary. No browser resources open during server rendering.
|
|
28
|
+
|
|
29
|
+
For a view acquired elsewhere:
|
|
30
|
+
|
|
31
|
+
```tsx
|
|
32
|
+
import { OntologyProvider } from "@party-stack/ontology-react";
|
|
33
|
+
import { eq, useLiveSuspenseQuery } from "@tanstack/react-db";
|
|
34
|
+
import { objects } from "./ontology/generated/collections";
|
|
35
|
+
import type { AppOntology } from "./ontology/generated/types";
|
|
36
|
+
|
|
37
|
+
function Issues() {
|
|
38
|
+
const { data } = useLiveSuspenseQuery({
|
|
39
|
+
query: (q) => q.from({ issue: objects.Issue }).where(({ issue }) => eq(issue.status, "Open")),
|
|
40
|
+
});
|
|
41
|
+
return (
|
|
42
|
+
<ul>
|
|
43
|
+
{data.map((issue) => (
|
|
44
|
+
<li key={issue.id}>{issue.title}</li>
|
|
45
|
+
))}
|
|
46
|
+
</ul>
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
<Suspense fallback={<p>Loading issues…</p>}>
|
|
51
|
+
<OntologyProvider ontology={ontology}>
|
|
52
|
+
<Issues />
|
|
53
|
+
</OntologyProvider>
|
|
54
|
+
</Suspense>;
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
The provider borrows an existing view. Its owner calls `ontology.cleanup()`.
|
|
58
|
+
`ontology generate` emits `generated/collections.ts`. You can also declare descriptors
|
|
59
|
+
manually with a primary-key map, such as `ontologyCollections<AppOntology>({ Issue: "id" })`.
|
|
60
|
+
Descriptors and `ontology.objects` resolve to the same client-owned collections.
|
|
61
|
+
The helper also returns `cachedObjects`, which reads the local cache
|
|
62
|
+
without acquiring remote demand.
|
|
63
|
+
|
|
64
|
+
## Select an ontology from a server
|
|
65
|
+
|
|
66
|
+
Supply one shared client for the server, then select a target by name. Names can
|
|
67
|
+
be static aliases such as `connect`, or application-defined keys that include a
|
|
68
|
+
saved connection ID.
|
|
69
|
+
|
|
70
|
+
```tsx
|
|
71
|
+
import { OntologyProvider, OntologyEnvironmentProvider } from "@party-stack/ontology-react";
|
|
72
|
+
import { workerClient } from "@party-stack/ontology/worker";
|
|
73
|
+
import { webRuntime } from "@party-stack/web-runtime";
|
|
74
|
+
|
|
75
|
+
const client = workerClient(
|
|
76
|
+
() =>
|
|
77
|
+
new SharedWorker(new URL("./ontology.worker.ts", import.meta.url), {
|
|
78
|
+
type: "module",
|
|
79
|
+
name: "app",
|
|
80
|
+
})
|
|
81
|
+
);
|
|
82
|
+
const options = { runtime: webRuntime({ storage: "memory" }) };
|
|
83
|
+
|
|
84
|
+
<OntologyEnvironmentProvider client={client} {...options}>
|
|
85
|
+
<Suspense fallback={<p>Opening…</p>}>
|
|
86
|
+
<OntologyProvider ontology={selectedOntology}>
|
|
87
|
+
<Issues />
|
|
88
|
+
</OntologyProvider>
|
|
89
|
+
</Suspense>
|
|
90
|
+
</OntologyEnvironmentProvider>;
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
A named `OntologyProvider` can override defaults with direct props, such as
|
|
94
|
+
`label="Issues"` or `demand={issueDemand}`. Nested groups such as `cache` and
|
|
95
|
+
`writes` are replaced as a group. Keep object-valued inputs stable between renders.
|
|
96
|
+
|
|
97
|
+
Changing the selected name closes the previous tab view. The server owns its
|
|
98
|
+
persistent replicas independently. For HTTP, use
|
|
99
|
+
`httpClient("/api/ontology/rpc")` instead. The environment opens the client once
|
|
100
|
+
and closes it on unmount; selected views borrow its transport.
|
|
101
|
+
|
|
102
|
+
Use `useOntology<AppOntology>()` for actions, attachments and offline pins. The
|
|
103
|
+
provider supplies TanStack DB's `DbProvider`; collection descriptors need no
|
|
104
|
+
client argument. Authentication uses ordinary actions on the `connect` ontology;
|
|
105
|
+
there is no installation-specific React context or account subscription protocol.
|
|
106
|
+
|
|
107
|
+
## Authentication
|
|
108
|
+
|
|
109
|
+
`AuthenticationProvider` specializes the ordinary ontology provider for Connect. It owns
|
|
110
|
+
its query view and the Promise command client, borrowing the environment's shared
|
|
111
|
+
transport and runtime. Acquisition suspends; unmount closes both resources.
|
|
112
|
+
|
|
113
|
+
```tsx
|
|
114
|
+
import { AuthenticationProvider, useAuthentication } from "@party-stack/ontology-react/authentication";
|
|
115
|
+
|
|
116
|
+
<OntologyEnvironmentProvider client={client} {...options}>
|
|
117
|
+
<Suspense fallback={<p>Loading accounts…</p>}>
|
|
118
|
+
<AuthenticationProvider>
|
|
119
|
+
<AccountSwitcher />
|
|
120
|
+
</AuthenticationProvider>
|
|
121
|
+
</Suspense>
|
|
122
|
+
</OntologyEnvironmentProvider>;
|
|
123
|
+
|
|
124
|
+
function SignInButton() {
|
|
125
|
+
const authentication = useAuthentication();
|
|
126
|
+
async function signIn() {
|
|
127
|
+
await authentication
|
|
128
|
+
.oauth({ applicationId: "app", redirectUrl })
|
|
129
|
+
.signIn({ browserPresentation: "popup" });
|
|
130
|
+
}
|
|
131
|
+
return <button onClick={signIn}>Sign in</button>;
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
`authenticationObjects`, exported from `@party-stack/ontology/authentication`,
|
|
136
|
+
contains typed collection descriptors for `OAuthConnection`, `ApiKeyConnection`,
|
|
137
|
+
`ExternalSessionConnection`, and service/application configuration. Use them with
|
|
138
|
+
`useLiveSuspenseQuery` inside `AuthenticationProvider` to build a reactive profile
|
|
139
|
+
switcher. Selection belongs to the app; commands come from `useAuthentication()`.
|
|
140
|
+
`signOut(account)` retains an inactive account; `forget(account)` removes its saved
|
|
141
|
+
row. Both accept `{ kind: "oauth" | "api-key" | "external-session", id }`, also
|
|
142
|
+
returned by sign-in and one-shot inventory commands. Application replicas and
|
|
143
|
+
queued actions remain until explicitly removed.
|
|
144
|
+
The descriptors resolve against this view. Authentication commands run
|
|
145
|
+
directly against Connect's ordinary backend actions; they never enter the local
|
|
146
|
+
replica outbox. Sign-in starts from the event handler, not provider construction.
|
|
147
|
+
|
|
148
|
+
## Component fragments and files
|
|
149
|
+
|
|
150
|
+
Fragment definitions and hooks are re-exported from `@party-stack/db-fragments`.
|
|
151
|
+
Import root query hooks directly from `@tanstack/react-db` and configure retention
|
|
152
|
+
through its native query options. A fragment's `.include` uses a native `include`;
|
|
153
|
+
`useFragment(ref)` returns `{ data }`, where `data` is the selected row or `undefined` if it no longer exists. Plural
|
|
154
|
+
fragments use `defineListFragment` and `useListFragment`. The parent query owns
|
|
155
|
+
membership and lifetime. See the Notes app for nested editor/folder/attachment
|
|
156
|
+
fragments, or [the fragment package](../db-fragments/README.md) for a small example.
|
|
157
|
+
Arguments are inferred from defaults such as `(row, { includeBody = false })`.
|
|
158
|
+
Use a parameter annotation such as `(row, args: Args)` for required arguments or
|
|
159
|
+
constrained types, and bind them with `fragment.include(row, args)`.
|
|
160
|
+
|
|
161
|
+
```tsx
|
|
162
|
+
import { useAttachmentMetadata, useAttachmentUrl } from "@party-stack/ontology-react";
|
|
163
|
+
import { Suspense } from "react";
|
|
164
|
+
import type { attachment } from "@party-stack/ontology/values";
|
|
165
|
+
|
|
166
|
+
function AttachmentImage({ file }: { file: attachment }) {
|
|
167
|
+
const url = useAttachmentUrl(file);
|
|
168
|
+
const metadata = useAttachmentMetadata(file, { select: ["name", "size"] });
|
|
169
|
+
return <img src={url} alt={metadata?.name ?? "Attachment"} />;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
<Suspense fallback={<span>Loading image…</span>}>
|
|
173
|
+
<AttachmentImage file={file} />
|
|
174
|
+
</Suspense>;
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
Both attachment hooks suspend initially and throw failures to the nearest error
|
|
178
|
+
boundary. They accept `enabled: false`, returning an undefined value without
|
|
179
|
+
starting a read. Matching requests share stable Promises within the provider.
|
|
180
|
+
Settled reads are collected after five inactive minutes, revoking their object
|
|
181
|
+
URLs. Pending reads remain owned by the provider even without a committed reader.
|
|
182
|
+
Closing the provider aborts pending reads and revokes all its cached URLs. This
|
|
183
|
+
request lifetime is separate from the replica's stored attachment bytes.
|
|
184
|
+
|
|
185
|
+
```tsx
|
|
186
|
+
const { status, set, clear } = useOfflinePin();
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
Pins use ordinary TanStack DB queries: bind a named descriptor with
|
|
190
|
+
`ontology.pins(downloadFolder)`, query its typed `.collection`, and call `.set(inputs)`
|
|
191
|
+
or `.remove(inputs)` directly. Joins on `pin.inputs.folderId` can add download
|
|
192
|
+
status to a folder list. No dedicated React pin hook is needed.
|
|
193
|
+
|
|
194
|
+
Attachment hooks return the URL or selected metadata directly. Loading suspends and
|
|
195
|
+
failures reach the error boundary; disabled or absent attachments return `undefined`.
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
//#region src/attachmentPromiseCache.d.ts
|
|
2
|
+
/** Stable in-flight reads belong to a view, including renders which never commit.
|
|
3
|
+
* Settled reads stay reusable for five inactive minutes; expiry releases their
|
|
4
|
+
* object URLs. Pending reads are cancelled when the view owner closes.
|
|
5
|
+
*/
|
|
6
|
+
export declare class AttachmentPromiseCache {
|
|
7
|
+
private readonly inactiveTime;
|
|
8
|
+
private readonly entries;
|
|
9
|
+
private disposed;
|
|
10
|
+
constructor(inactiveTime?: number);
|
|
11
|
+
get<A>(key: string, open: (signal: AbortSignal) => Promise<A>, close?: (value: A) => void): Entry<A>;
|
|
12
|
+
subscribe<A>(entry: Entry<A>, listener: () => void): () => void;
|
|
13
|
+
dispose(): void;
|
|
14
|
+
private collectLater;
|
|
15
|
+
private release;
|
|
16
|
+
}
|
|
17
|
+
export type Entry<A> = {
|
|
18
|
+
readonly key: string;
|
|
19
|
+
readonly controller: AbortController;
|
|
20
|
+
readonly promise: Promise<A>;
|
|
21
|
+
readonly listeners: Set<() => void>;
|
|
22
|
+
readonly close?: (value: A) => void;
|
|
23
|
+
settled: boolean;
|
|
24
|
+
disposed: boolean;
|
|
25
|
+
hasValue: boolean;
|
|
26
|
+
value?: A;
|
|
27
|
+
timer?: ReturnType<typeof setTimeout>;
|
|
28
|
+
};
|
|
29
|
+
//#endregion
|
|
30
|
+
//# sourceMappingURL=attachmentPromiseCache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attachmentPromiseCache.d.ts","names":[],"sources":["../src/attachmentPromiseCache.ts"],"mappings":";;;;;qBAIa;mBAIoB;mBAHZ;UACT;EAER,YAA6B;EAE7B,IAAI,GAAG,aAAa,OAAO,QAAQ,gBAAgB,QAAQ,IAAI,SAAS,OAAO,aAAa,MAAM;EA4ClG,UAAU,GAAG,OAAO,MAAM,IAAI;EAU9B;UAOQ;UAUA;;YASA,MAAM;WACL;WACA,YAAY;WACZ,SAAS,QAAQ;WACjB,WAAW;WACX,SAAS,OAAO;EACzB;EACA;EACA;EACA,QAAQ;EACR,QAAQ,kBAAkB"}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
//#region src/attachmentPromiseCache.ts
|
|
2
|
+
/** Stable in-flight reads belong to a view, including renders which never commit.
|
|
3
|
+
* Settled reads stay reusable for five inactive minutes; expiry releases their
|
|
4
|
+
* object URLs. Pending reads are cancelled when the view owner closes.
|
|
5
|
+
*/
|
|
6
|
+
var AttachmentPromiseCache = class {
|
|
7
|
+
inactiveTime;
|
|
8
|
+
entries = /* @__PURE__ */ new Map();
|
|
9
|
+
disposed = false;
|
|
10
|
+
constructor(inactiveTime = 3e5) {
|
|
11
|
+
this.inactiveTime = inactiveTime;
|
|
12
|
+
}
|
|
13
|
+
get(key, open, close) {
|
|
14
|
+
if (this.disposed) throw new Error("The attachment view has closed.");
|
|
15
|
+
const found = this.entries.get(key);
|
|
16
|
+
if (found) return found;
|
|
17
|
+
const controller = new AbortController();
|
|
18
|
+
const entry = {
|
|
19
|
+
key,
|
|
20
|
+
controller,
|
|
21
|
+
listeners: /* @__PURE__ */ new Set(),
|
|
22
|
+
settled: false,
|
|
23
|
+
disposed: false,
|
|
24
|
+
hasValue: false,
|
|
25
|
+
close,
|
|
26
|
+
promise: Promise.resolve().then(() => {
|
|
27
|
+
if (controller.signal.aborted) throw new DOMException("The attachment view has closed.", "AbortError");
|
|
28
|
+
return open(controller.signal);
|
|
29
|
+
}).then((value) => {
|
|
30
|
+
entry.settled = true;
|
|
31
|
+
if (entry.disposed) {
|
|
32
|
+
close?.(value);
|
|
33
|
+
throw new DOMException("The attachment view has closed.", "AbortError");
|
|
34
|
+
}
|
|
35
|
+
entry.value = value;
|
|
36
|
+
entry.hasValue = true;
|
|
37
|
+
this.collectLater(key, entry);
|
|
38
|
+
return value;
|
|
39
|
+
}, (error) => {
|
|
40
|
+
entry.settled = true;
|
|
41
|
+
this.collectLater(key, entry);
|
|
42
|
+
throw error;
|
|
43
|
+
})
|
|
44
|
+
};
|
|
45
|
+
entry.promise.catch(() => {});
|
|
46
|
+
this.entries.set(key, entry);
|
|
47
|
+
return entry;
|
|
48
|
+
}
|
|
49
|
+
subscribe(entry, listener) {
|
|
50
|
+
if (entry.timer !== void 0) clearTimeout(entry.timer);
|
|
51
|
+
entry.timer = void 0;
|
|
52
|
+
entry.listeners.add(listener);
|
|
53
|
+
return () => {
|
|
54
|
+
entry.listeners.delete(listener);
|
|
55
|
+
this.collectLater(entry.key, entry);
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
dispose() {
|
|
59
|
+
if (this.disposed) return;
|
|
60
|
+
this.disposed = true;
|
|
61
|
+
for (const entry of this.entries.values()) this.release(entry);
|
|
62
|
+
this.entries.clear();
|
|
63
|
+
}
|
|
64
|
+
collectLater(key, entry) {
|
|
65
|
+
if (!entry.settled || entry.disposed || entry.listeners.size || entry.timer !== void 0) return;
|
|
66
|
+
entry.timer = setTimeout(() => {
|
|
67
|
+
entry.timer = void 0;
|
|
68
|
+
if (entry.listeners.size || entry.disposed) return;
|
|
69
|
+
if (this.entries.get(key) === entry) this.entries.delete(key);
|
|
70
|
+
this.release(entry);
|
|
71
|
+
}, this.inactiveTime);
|
|
72
|
+
}
|
|
73
|
+
release(entry) {
|
|
74
|
+
if (entry.disposed) return;
|
|
75
|
+
entry.disposed = true;
|
|
76
|
+
if (entry.timer !== void 0) clearTimeout(entry.timer);
|
|
77
|
+
entry.controller.abort();
|
|
78
|
+
if (entry.hasValue) entry.close?.(entry.value);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
//#endregion
|
|
82
|
+
export { AttachmentPromiseCache };
|
|
83
|
+
|
|
84
|
+
//# sourceMappingURL=attachmentPromiseCache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attachmentPromiseCache.js","names":[],"sources":["../src/attachmentPromiseCache.ts"],"sourcesContent":["/** Stable in-flight reads belong to a view, including renders which never commit.\n * Settled reads stay reusable for five inactive minutes; expiry releases their\n * object URLs. Pending reads are cancelled when the view owner closes.\n */\nexport class AttachmentPromiseCache {\n private readonly entries = new Map<string, Entry<unknown>>();\n private disposed = false;\n\n constructor(private readonly inactiveTime = 5 * 60_000) {}\n\n get<A>(key: string, open: (signal: AbortSignal) => Promise<A>, close?: (value: A) => void): Entry<A> {\n if (this.disposed) throw new Error(\"The attachment view has closed.\");\n const found = this.entries.get(key);\n if (found) return found as Entry<A>;\n const controller = new AbortController();\n const entry: Entry<A> = {\n key,\n controller,\n listeners: new Set(),\n settled: false,\n disposed: false,\n hasValue: false,\n close,\n promise: Promise.resolve()\n .then(() => {\n if (controller.signal.aborted)\n throw new DOMException(\"The attachment view has closed.\", \"AbortError\");\n return open(controller.signal);\n })\n .then(\n (value) => {\n entry.settled = true;\n if (entry.disposed) {\n close?.(value);\n throw new DOMException(\"The attachment view has closed.\", \"AbortError\");\n }\n entry.value = value;\n entry.hasValue = true;\n this.collectLater(key, entry);\n return value;\n },\n (error: unknown) => {\n entry.settled = true;\n this.collectLater(key, entry);\n throw error;\n }\n ),\n };\n // A failed read may no longer have a Suspense reader after navigation.\n void entry.promise.catch(() => {});\n this.entries.set(key, entry as Entry<unknown>);\n return entry;\n }\n\n subscribe<A>(entry: Entry<A>, listener: () => void) {\n if (entry.timer !== undefined) clearTimeout(entry.timer);\n entry.timer = undefined;\n entry.listeners.add(listener);\n return () => {\n entry.listeners.delete(listener);\n this.collectLater(entry.key, entry);\n };\n }\n\n dispose() {\n if (this.disposed) return;\n this.disposed = true;\n for (const entry of this.entries.values()) this.release(entry);\n this.entries.clear();\n }\n\n private collectLater<A>(key: string, entry: Entry<A>) {\n if (!entry.settled || entry.disposed || entry.listeners.size || entry.timer !== undefined) return;\n entry.timer = setTimeout(() => {\n entry.timer = undefined;\n if (entry.listeners.size || entry.disposed) return;\n if (this.entries.get(key) === entry) this.entries.delete(key);\n this.release(entry);\n }, this.inactiveTime);\n }\n\n private release<A>(entry: Entry<A>) {\n if (entry.disposed) return;\n entry.disposed = true;\n if (entry.timer !== undefined) clearTimeout(entry.timer);\n entry.controller.abort();\n if (entry.hasValue) entry.close?.(entry.value!);\n }\n}\n\nexport type Entry<A> = {\n readonly key: string;\n readonly controller: AbortController;\n readonly promise: Promise<A>;\n readonly listeners: Set<() => void>;\n readonly close?: (value: A) => void;\n settled: boolean;\n disposed: boolean;\n hasValue: boolean;\n value?: A;\n timer?: ReturnType<typeof setTimeout>;\n};\n"],"mappings":";;;;;AAIA,IAAa,yBAAb,MAAoC;CAIH;CAH7B,0BAA2B,IAAI,IAA4B;CAC3D,WAAmB;CAEnB,YAAY,eAAgC,KAAY;EAA3B,KAAA,eAAA;CAA4B;CAEzD,IAAO,KAAa,MAA2C,OAAsC;EACjG,IAAI,KAAK,UAAU,MAAM,IAAI,MAAM,iCAAiC;EACpE,MAAM,QAAQ,KAAK,QAAQ,IAAI,GAAG;EAClC,IAAI,OAAO,OAAO;EAClB,MAAM,aAAa,IAAI,gBAAgB;EACvC,MAAM,QAAkB;GACpB;GACA;GACA,2BAAW,IAAI,IAAI;GACnB,SAAS;GACT,UAAU;GACV,UAAU;GACV;GACA,SAAS,QAAQ,QAAQ,CAAC,CACrB,WAAW;IACR,IAAI,WAAW,OAAO,SAClB,MAAM,IAAI,aAAa,mCAAmC,YAAY;IAC1E,OAAO,KAAK,WAAW,MAAM;GACjC,CAAC,CAAC,CACD,MACI,UAAU;IACP,MAAM,UAAU;IAChB,IAAI,MAAM,UAAU;KAChB,QAAQ,KAAK;KACb,MAAM,IAAI,aAAa,mCAAmC,YAAY;IAC1E;IACA,MAAM,QAAQ;IACd,MAAM,WAAW;IACjB,KAAK,aAAa,KAAK,KAAK;IAC5B,OAAO;GACX,IACC,UAAmB;IAChB,MAAM,UAAU;IAChB,KAAK,aAAa,KAAK,KAAK;IAC5B,MAAM;GACV,CACJ;EACR;EAEA,MAAW,QAAQ,YAAY,CAAC,CAAC;EACjC,KAAK,QAAQ,IAAI,KAAK,KAAuB;EAC7C,OAAO;CACX;CAEA,UAAa,OAAiB,UAAsB;EAChD,IAAI,MAAM,UAAU,KAAA,GAAW,aAAa,MAAM,KAAK;EACvD,MAAM,QAAQ,KAAA;EACd,MAAM,UAAU,IAAI,QAAQ;EAC5B,aAAa;GACT,MAAM,UAAU,OAAO,QAAQ;GAC/B,KAAK,aAAa,MAAM,KAAK,KAAK;EACtC;CACJ;CAEA,UAAU;EACN,IAAI,KAAK,UAAU;EACnB,KAAK,WAAW;EAChB,KAAK,MAAM,SAAS,KAAK,QAAQ,OAAO,GAAG,KAAK,QAAQ,KAAK;EAC7D,KAAK,QAAQ,MAAM;CACvB;CAEA,aAAwB,KAAa,OAAiB;EAClD,IAAI,CAAC,MAAM,WAAW,MAAM,YAAY,MAAM,UAAU,QAAQ,MAAM,UAAU,KAAA,GAAW;EAC3F,MAAM,QAAQ,iBAAiB;GAC3B,MAAM,QAAQ,KAAA;GACd,IAAI,MAAM,UAAU,QAAQ,MAAM,UAAU;GAC5C,IAAI,KAAK,QAAQ,IAAI,GAAG,MAAM,OAAO,KAAK,QAAQ,OAAO,GAAG;GAC5D,KAAK,QAAQ,KAAK;EACtB,GAAG,KAAK,YAAY;CACxB;CAEA,QAAmB,OAAiB;EAChC,IAAI,MAAM,UAAU;EACpB,MAAM,WAAW;EACjB,IAAI,MAAM,UAAU,KAAA,GAAW,aAAa,MAAM,KAAK;EACvD,MAAM,WAAW,MAAM;EACvB,IAAI,MAAM,UAAU,MAAM,QAAQ,MAAM,KAAM;CAClD;AACJ"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { AttachmentPromiseCache } from "./attachmentPromiseCache.js";
|
|
2
|
+
//#region src/attachmentResources.d.ts
|
|
3
|
+
export declare const AttachmentResourcesContext: import("react").Context<AttachmentPromiseCache | undefined>;
|
|
4
|
+
export declare function makeAttachmentResources(): {
|
|
5
|
+
cache: AttachmentPromiseCache;
|
|
6
|
+
cleanup: () => Promise<void>;
|
|
7
|
+
};
|
|
8
|
+
//#endregion
|
|
9
|
+
//# sourceMappingURL=attachmentResources.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attachmentResources.d.ts","names":[],"sources":["../src/attachmentResources.ts"],"mappings":";;qBAGa,4CAA0B,QAAA;wBAEvB;SAAA;iBAAA"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { AttachmentPromiseCache } from "./attachmentPromiseCache.js";
|
|
2
|
+
import { createContext } from "react";
|
|
3
|
+
//#region src/attachmentResources.ts
|
|
4
|
+
const AttachmentResourcesContext = createContext(void 0);
|
|
5
|
+
function makeAttachmentResources() {
|
|
6
|
+
const cache = new AttachmentPromiseCache();
|
|
7
|
+
return {
|
|
8
|
+
cache,
|
|
9
|
+
cleanup: async () => cache.dispose()
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
//#endregion
|
|
13
|
+
export { AttachmentResourcesContext, makeAttachmentResources };
|
|
14
|
+
|
|
15
|
+
//# sourceMappingURL=attachmentResources.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attachmentResources.js","names":[],"sources":["../src/attachmentResources.ts"],"sourcesContent":["import { createContext } from \"react\";\nimport { AttachmentPromiseCache } from \"./attachmentPromiseCache.js\";\n\nexport const AttachmentResourcesContext = createContext<AttachmentPromiseCache | undefined>(undefined);\n\nexport function makeAttachmentResources() {\n const cache = new AttachmentPromiseCache();\n return { cache, cleanup: async () => cache.dispose() };\n}\n"],"mappings":";;;AAGA,MAAa,6BAA6B,cAAkD,KAAA,CAAS;AAErG,SAAgB,0BAA0B;CACtC,MAAM,QAAQ,IAAI,uBAAuB;CACzC,OAAO;EAAE;EAAO,SAAS,YAAY,MAAM,QAAQ;CAAE;AACzD"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { AttachmentMetadata } from "@party-stack/ontology";
|
|
2
|
+
import { attachment } from "@party-stack/ontology/values";
|
|
3
|
+
//#region src/attachments.d.ts
|
|
4
|
+
/** Suspends for bytes and throws initial failures to the nearest error boundary.
|
|
5
|
+
* Identical reads share an object URL within this view. Unused settled reads are
|
|
6
|
+
* released after five minutes; closing the view aborts reads and revokes all URLs.
|
|
7
|
+
*/
|
|
8
|
+
export declare function useAttachmentUrl(value: attachment | undefined, options?: {
|
|
9
|
+
enabled?: boolean;
|
|
10
|
+
}): string | undefined;
|
|
11
|
+
/** Suspends until the selected metadata is available. Selection order does not
|
|
12
|
+
* create duplicate reads, and changing the source creates a separate request.
|
|
13
|
+
*/
|
|
14
|
+
export declare function useAttachmentMetadata<const Fields extends readonly ("size" | "type" | "name")[]>(value: attachment | undefined, options: {
|
|
15
|
+
select: Fields;
|
|
16
|
+
enabled?: boolean;
|
|
17
|
+
}): Pick<AttachmentMetadata, Fields[number]> | undefined;
|
|
18
|
+
//#endregion
|
|
19
|
+
//# sourceMappingURL=attachments.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attachments.d.ts","names":[],"sources":["../src/attachments.ts"],"mappings":";;;;;;;wBA4DgB,iBAAiB,OAAO,wBAAwB;EAAW;;;;;wBAgB3D,4BAA4B,sDACxC,OAAO,wBACP;EAAW,QAAQ;EAAQ;IAAmB,KAAA,oBAAA"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { AttachmentResourcesContext } from "./attachmentResources.js";
|
|
3
|
+
import { useOntology } from "./context.js";
|
|
4
|
+
import { use, useCallback, useContext, useMemo, useSyncExternalStore } from "react";
|
|
5
|
+
//#region src/attachments.ts
|
|
6
|
+
const noSubscription = () => () => {};
|
|
7
|
+
const noEntry = () => void 0;
|
|
8
|
+
function useAttachmentValue(value) {
|
|
9
|
+
const source = value?.source;
|
|
10
|
+
const id = value?.id;
|
|
11
|
+
const type = value?.type;
|
|
12
|
+
const objectType = source?.objectType;
|
|
13
|
+
const primaryKey = source?.primaryKey;
|
|
14
|
+
const property = source?.property;
|
|
15
|
+
return useMemo(() => id === void 0 ? void 0 : {
|
|
16
|
+
id,
|
|
17
|
+
...type === void 0 ? {} : { type },
|
|
18
|
+
...objectType === void 0 || primaryKey === void 0 || property === void 0 ? {} : { source: {
|
|
19
|
+
objectType,
|
|
20
|
+
primaryKey,
|
|
21
|
+
property
|
|
22
|
+
} }
|
|
23
|
+
}, [
|
|
24
|
+
id,
|
|
25
|
+
type,
|
|
26
|
+
objectType,
|
|
27
|
+
primaryKey,
|
|
28
|
+
property
|
|
29
|
+
]);
|
|
30
|
+
}
|
|
31
|
+
function useAttachmentRead(key, open, close) {
|
|
32
|
+
const cache = useContext(AttachmentResourcesContext);
|
|
33
|
+
const getSnapshot = useCallback(() => cache && key !== void 0 ? cache.get(key, open, close) : void 0, [
|
|
34
|
+
cache,
|
|
35
|
+
key,
|
|
36
|
+
open,
|
|
37
|
+
close
|
|
38
|
+
]);
|
|
39
|
+
const subscribe = useCallback((listener) => {
|
|
40
|
+
const entry = getSnapshot();
|
|
41
|
+
return entry && cache ? cache.subscribe(entry, listener) : noSubscription();
|
|
42
|
+
}, [cache, getSnapshot]);
|
|
43
|
+
const entry = useSyncExternalStore(subscribe, getSnapshot, noEntry);
|
|
44
|
+
return entry ? use(entry.promise) : void 0;
|
|
45
|
+
}
|
|
46
|
+
/** Suspends for bytes and throws initial failures to the nearest error boundary.
|
|
47
|
+
* Identical reads share an object URL within this view. Unused settled reads are
|
|
48
|
+
* released after five minutes; closing the view aborts reads and revokes all URLs.
|
|
49
|
+
*/
|
|
50
|
+
function useAttachmentUrl(value, options = {}) {
|
|
51
|
+
const ontology = useOntology();
|
|
52
|
+
const handle = useAttachmentValue(value);
|
|
53
|
+
return useAttachmentRead(options.enabled !== false && handle ? JSON.stringify(["url", handle]) : void 0, useCallback(async (signal) => URL.createObjectURL(await ontology.attachments.blob(handle, { signal })), [ontology, handle]), URL.revokeObjectURL);
|
|
54
|
+
}
|
|
55
|
+
/** Suspends until the selected metadata is available. Selection order does not
|
|
56
|
+
* create duplicate reads, and changing the source creates a separate request.
|
|
57
|
+
*/
|
|
58
|
+
function useAttachmentMetadata(value, options) {
|
|
59
|
+
const ontology = useOntology();
|
|
60
|
+
const handle = useAttachmentValue(value);
|
|
61
|
+
const fieldsKey = [...new Set(options.select)].sort().join(",");
|
|
62
|
+
const select = useMemo(() => fieldsKey.split(",").filter(Boolean), [fieldsKey]);
|
|
63
|
+
return useAttachmentRead(options.enabled !== false && handle ? JSON.stringify([
|
|
64
|
+
"metadata",
|
|
65
|
+
handle,
|
|
66
|
+
select
|
|
67
|
+
]) : void 0, useCallback((signal) => ontology.attachments.metadata(handle, {
|
|
68
|
+
select,
|
|
69
|
+
signal
|
|
70
|
+
}), [
|
|
71
|
+
ontology,
|
|
72
|
+
handle,
|
|
73
|
+
select
|
|
74
|
+
]));
|
|
75
|
+
}
|
|
76
|
+
//#endregion
|
|
77
|
+
export { useAttachmentMetadata, useAttachmentUrl };
|
|
78
|
+
|
|
79
|
+
//# sourceMappingURL=attachments.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attachments.js","names":[],"sources":["../src/attachments.ts"],"sourcesContent":["\"use client\";\nimport { use, useCallback, useContext, useMemo, useSyncExternalStore } from \"react\";\nimport { AttachmentResourcesContext } from \"./attachmentResources.js\";\nimport { useOntology } from \"./context.js\";\nimport type { Entry } from \"./attachmentPromiseCache.js\";\nimport type { AttachmentMetadata } from \"@party-stack/ontology\";\nimport type { attachment } from \"@party-stack/ontology/values\";\n\nconst noSubscription = () => () => {};\nconst noEntry = () => undefined;\n\nfunction useAttachmentValue(value: attachment | undefined) {\n const source = value?.source;\n const id = value?.id;\n const type = value?.type;\n const objectType = source?.objectType;\n const primaryKey = source?.primaryKey;\n const property = source?.property;\n return useMemo<attachment | undefined>(\n () =>\n id === undefined\n ? undefined\n : {\n id,\n ...(type === undefined ? {} : { type }),\n ...(objectType === undefined || primaryKey === undefined || property === undefined\n ? {}\n : { source: { objectType, primaryKey, property } }),\n },\n [id, type, objectType, primaryKey, property]\n );\n}\n\nfunction useAttachmentRead<A>(\n key: string | undefined,\n open: (signal: AbortSignal) => Promise<A>,\n close?: (value: A) => void\n): A | undefined {\n const cache = useContext(AttachmentResourcesContext);\n const getSnapshot = useCallback(\n () => (cache && key !== undefined ? cache.get(key, open, close) : undefined),\n [cache, key, open, close]\n );\n const subscribe = useCallback(\n (listener: () => void) => {\n const entry = getSnapshot();\n return entry && cache ? cache.subscribe(entry, listener) : noSubscription();\n },\n [cache, getSnapshot]\n );\n // A committed subscription leases the URL. Snapshot consistency also prevents\n // committing a URL whose inactive entry expired during concurrent rendering.\n const entry: Entry<A> | undefined = useSyncExternalStore(subscribe, getSnapshot, noEntry);\n return entry ? use(entry.promise) : undefined;\n}\n\n/** Suspends for bytes and throws initial failures to the nearest error boundary.\n * Identical reads share an object URL within this view. Unused settled reads are\n * released after five minutes; closing the view aborts reads and revokes all URLs.\n */\nexport function useAttachmentUrl(value: attachment | undefined, options: { enabled?: boolean } = {}) {\n const ontology = useOntology();\n const handle = useAttachmentValue(value);\n const key = options.enabled !== false && handle ? JSON.stringify([\"url\", handle]) : undefined;\n const open = useCallback(\n async (signal: AbortSignal) =>\n URL.createObjectURL(await ontology.attachments.blob(handle!, { signal })),\n [ontology, handle]\n );\n const url = useAttachmentRead(key, open, URL.revokeObjectURL);\n return url;\n}\n\n/** Suspends until the selected metadata is available. Selection order does not\n * create duplicate reads, and changing the source creates a separate request.\n */\nexport function useAttachmentMetadata<const Fields extends readonly (\"size\" | \"type\" | \"name\")[]>(\n value: attachment | undefined,\n options: { select: Fields; enabled?: boolean }\n) {\n const ontology = useOntology();\n const handle = useAttachmentValue(value);\n const fieldsKey = [...new Set(options.select)].sort().join(\",\");\n const select = useMemo(() => fieldsKey.split(\",\").filter(Boolean) as Fields[number][], [fieldsKey]);\n const key =\n options.enabled !== false && handle ? JSON.stringify([\"metadata\", handle, select]) : undefined;\n const open = useCallback(\n (signal: AbortSignal) => ontology.attachments.metadata(handle!, { select, signal }),\n [ontology, handle, select]\n );\n const data = useAttachmentRead(key, open) as Pick<AttachmentMetadata, Fields[number]> | undefined;\n return data;\n}\n"],"mappings":";;;;;AAQA,MAAM,6BAA6B,CAAC;AACpC,MAAM,gBAAgB,KAAA;AAEtB,SAAS,mBAAmB,OAA+B;CACvD,MAAM,SAAS,OAAO;CACtB,MAAM,KAAK,OAAO;CAClB,MAAM,OAAO,OAAO;CACpB,MAAM,aAAa,QAAQ;CAC3B,MAAM,aAAa,QAAQ;CAC3B,MAAM,WAAW,QAAQ;CACzB,OAAO,cAEC,OAAO,KAAA,IACD,KAAA,IACA;EACI;EACA,GAAI,SAAS,KAAA,IAAY,CAAC,IAAI,EAAE,KAAK;EACrC,GAAI,eAAe,KAAA,KAAa,eAAe,KAAA,KAAa,aAAa,KAAA,IACnE,CAAC,IACD,EAAE,QAAQ;GAAE;GAAY;GAAY;EAAS,EAAE;CACzD,GACV;EAAC;EAAI;EAAM;EAAY;EAAY;CAAQ,CAC/C;AACJ;AAEA,SAAS,kBACL,KACA,MACA,OACa;CACb,MAAM,QAAQ,WAAW,0BAA0B;CACnD,MAAM,cAAc,kBACT,SAAS,QAAQ,KAAA,IAAY,MAAM,IAAI,KAAK,MAAM,KAAK,IAAI,KAAA,GAClE;EAAC;EAAO;EAAK;EAAM;CAAK,CAC5B;CACA,MAAM,YAAY,aACb,aAAyB;EACtB,MAAM,QAAQ,YAAY;EAC1B,OAAO,SAAS,QAAQ,MAAM,UAAU,OAAO,QAAQ,IAAI,eAAe;CAC9E,GACA,CAAC,OAAO,WAAW,CACvB;CAGA,MAAM,QAA8B,qBAAqB,WAAW,aAAa,OAAO;CACxF,OAAO,QAAQ,IAAI,MAAM,OAAO,IAAI,KAAA;AACxC;;;;;AAMA,SAAgB,iBAAiB,OAA+B,UAAiC,CAAC,GAAG;CACjG,MAAM,WAAW,YAAY;CAC7B,MAAM,SAAS,mBAAmB,KAAK;CAQvC,OADY,kBANA,QAAQ,YAAY,SAAS,SAAS,KAAK,UAAU,CAAC,OAAO,MAAM,CAAC,IAAI,KAAA,GACvE,YACT,OAAO,WACH,IAAI,gBAAgB,MAAM,SAAS,YAAY,KAAK,QAAS,EAAE,OAAO,CAAC,CAAC,GAC5E,CAAC,UAAU,MAAM,CAEiB,GAAG,IAAI,eACpC;AACb;;;;AAKA,SAAgB,sBACZ,OACA,SACF;CACE,MAAM,WAAW,YAAY;CAC7B,MAAM,SAAS,mBAAmB,KAAK;CACvC,MAAM,YAAY,CAAC,GAAG,IAAI,IAAI,QAAQ,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,KAAK,GAAG;CAC9D,MAAM,SAAS,cAAc,UAAU,MAAM,GAAG,CAAC,CAAC,OAAO,OAAO,GAAuB,CAAC,SAAS,CAAC;CAQlG,OADa,kBALT,QAAQ,YAAY,SAAS,SAAS,KAAK,UAAU;EAAC;EAAY;EAAQ;CAAM,CAAC,IAAI,KAAA,GAC5E,aACR,WAAwB,SAAS,YAAY,SAAS,QAAS;EAAE;EAAQ;CAAO,CAAC,GAClF;EAAC;EAAU;EAAQ;CAAM,CAEU,CAC7B;AACd"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { OntologyViewOptions } from "./options.js";
|
|
2
|
+
import { ReactNode } from "react";
|
|
3
|
+
import { AuthenticationClient } from "@party-stack/ontology/authentication";
|
|
4
|
+
//#region src/authentication.d.ts
|
|
5
|
+
/** Own authentication commands and a reactive view of Connect's saved accounts. The
|
|
6
|
+
* surrounding environment owns the transport and browser authentication runtime.
|
|
7
|
+
*/
|
|
8
|
+
export declare function AuthenticationProvider({ ontology, children, ...props }: OntologyViewOptions & {
|
|
9
|
+
readonly ontology?: string;
|
|
10
|
+
readonly children?: ReactNode;
|
|
11
|
+
}): import("react").JSX.Element;
|
|
12
|
+
/** Authentication actions run through Connect directly, outside a replica outbox. */
|
|
13
|
+
export declare function useAuthentication(): AuthenticationClient;
|
|
14
|
+
//#endregion
|
|
15
|
+
//# sourceMappingURL=authentication.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"authentication.d.ts","names":[],"sources":["../src/authentication.tsx"],"mappings":";;;;;;;wBAgBgB,yBACZ,UACA,aACG,SACJ;WACU;WACA,WAAW;oBACvB,IAAA;;wBAuBe,qBAAqB"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useViewOptions } from "./options.js";
|
|
3
|
+
import { OwnedResource } from "./resource.js";
|
|
4
|
+
import { OntologyProvider, useOntologyEnvironment } from "./index.js";
|
|
5
|
+
import { createContext, useContext, useMemo } from "react";
|
|
6
|
+
import { createAuthenticationClient } from "@party-stack/ontology/authentication";
|
|
7
|
+
import { jsx } from "react/jsx-runtime";
|
|
8
|
+
//#region src/authentication.tsx
|
|
9
|
+
const AuthenticationContext = createContext(void 0);
|
|
10
|
+
/** Own authentication commands and a reactive view of Connect's saved accounts. The
|
|
11
|
+
* surrounding environment owns the transport and browser authentication runtime.
|
|
12
|
+
*/
|
|
13
|
+
function AuthenticationProvider({ ontology = "connect", children, ...props }) {
|
|
14
|
+
const environment = useOntologyEnvironment();
|
|
15
|
+
const { client } = environment;
|
|
16
|
+
const overrides = useViewOptions(props);
|
|
17
|
+
const runtime = overrides.runtime ?? environment.runtime;
|
|
18
|
+
const open = useMemo(() => () => createAuthenticationClient({
|
|
19
|
+
client,
|
|
20
|
+
ontology,
|
|
21
|
+
runtime
|
|
22
|
+
}), [
|
|
23
|
+
client,
|
|
24
|
+
ontology,
|
|
25
|
+
runtime
|
|
26
|
+
]);
|
|
27
|
+
return /* @__PURE__ */ jsx(OwnedResource, {
|
|
28
|
+
open,
|
|
29
|
+
children: (authentication) => /* @__PURE__ */ jsx(AuthenticationContext, {
|
|
30
|
+
value: authentication,
|
|
31
|
+
children: /* @__PURE__ */ jsx(OntologyProvider, {
|
|
32
|
+
ontology,
|
|
33
|
+
...overrides,
|
|
34
|
+
children
|
|
35
|
+
})
|
|
36
|
+
})
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
/** Authentication actions run through Connect directly, outside a replica outbox. */
|
|
40
|
+
function useAuthentication() {
|
|
41
|
+
const authentication = useContext(AuthenticationContext);
|
|
42
|
+
if (!authentication) throw new Error("useAuthentication requires an AuthenticationProvider.");
|
|
43
|
+
return authentication;
|
|
44
|
+
}
|
|
45
|
+
//#endregion
|
|
46
|
+
export { AuthenticationProvider, useAuthentication };
|
|
47
|
+
|
|
48
|
+
//# sourceMappingURL=authentication.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"authentication.js","names":[],"sources":["../src/authentication.tsx"],"sourcesContent":["\"use client\";\n\nimport { createAuthenticationClient } from \"@party-stack/ontology/authentication\";\nimport { createContext, useContext, useMemo } from \"react\";\nimport { useViewOptions } from \"./options.js\";\nimport { OwnedResource } from \"./resource.js\";\nimport { OntologyProvider, useOntologyEnvironment } from \"./index.js\";\nimport type { OntologyViewOptions } from \"./options.js\";\nimport type { AuthenticationClient } from \"@party-stack/ontology/authentication\";\nimport type { ReactNode } from \"react\";\n\nconst AuthenticationContext = createContext<AuthenticationClient | undefined>(undefined);\n\n/** Own authentication commands and a reactive view of Connect's saved accounts. The\n * surrounding environment owns the transport and browser authentication runtime.\n */\nexport function AuthenticationProvider({\n ontology = \"connect\",\n children,\n ...props\n}: OntologyViewOptions & {\n readonly ontology?: string;\n readonly children?: ReactNode;\n}) {\n const environment = useOntologyEnvironment();\n const { client } = environment;\n const overrides = useViewOptions(props);\n const runtime = overrides.runtime ?? environment.runtime;\n const open = useMemo(\n () => () => createAuthenticationClient({ client, ontology, runtime }),\n [client, ontology, runtime]\n );\n return (\n <OwnedResource open={open}>\n {(authentication) => (\n <AuthenticationContext value={authentication}>\n <OntologyProvider ontology={ontology} {...overrides}>\n {children}\n </OntologyProvider>\n </AuthenticationContext>\n )}\n </OwnedResource>\n );\n}\n\n/** Authentication actions run through Connect directly, outside a replica outbox. */\nexport function useAuthentication(): AuthenticationClient {\n const authentication = useContext(AuthenticationContext);\n if (!authentication) throw new Error(\"useAuthentication requires an AuthenticationProvider.\");\n return authentication;\n}\n"],"mappings":";;;;;;;;AAWA,MAAM,wBAAwB,cAAgD,KAAA,CAAS;;;;AAKvF,SAAgB,uBAAuB,EACnC,WAAW,WACX,UACA,GAAG,SAIJ;CACC,MAAM,cAAc,uBAAuB;CAC3C,MAAM,EAAE,WAAW;CACnB,MAAM,YAAY,eAAe,KAAK;CACtC,MAAM,UAAU,UAAU,WAAW,YAAY;CACjD,MAAM,OAAO,oBACG,2BAA2B;EAAE;EAAQ;EAAU;CAAQ,CAAC,GACpE;EAAC;EAAQ;EAAU;CAAO,CAC9B;CACA,OACI,oBAAC,eAAD;EAAqB;EACf,WAAA,mBACE,oBAAC,uBAAD;GAAuB,OAAO;GAC1B,UAAA,oBAAC,kBAAD;IAA4B;IAAU,GAAI;IACrC;GACa,CAAA;EACC,CAAA;CAEhB,CAAA;AAEvB;;AAGA,SAAgB,oBAA0C;CACtD,MAAM,iBAAiB,WAAW,qBAAqB;CACvD,IAAI,CAAC,gBAAgB,MAAM,IAAI,MAAM,uDAAuD;CAC5F,OAAO;AACX"}
|
package/lib/context.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { LiveOntology, OntologyDefinition } from "@party-stack/ontology";
|
|
2
|
+
//#region src/context.d.ts
|
|
3
|
+
export declare const OntologyContext: import("react").Context<LiveOntology<OntologyDefinition> | undefined>;
|
|
4
|
+
/** Optional access for commands such as actions, attachments and offline pins.
|
|
5
|
+
* Generated collection descriptors can be queried directly without this hook.
|
|
6
|
+
*/
|
|
7
|
+
export declare function useOntology<Definition extends OntologyDefinition = OntologyDefinition>(): LiveOntology<Definition>;
|
|
8
|
+
//#endregion
|
|
9
|
+
//# sourceMappingURL=context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","names":[],"sources":["../src/context.ts"],"mappings":";;qBAGa,iCAAe,QAAA,aAAA;;;;wBAIZ,YACZ,mBAAmB,qBAAqB,uBACvC,aAAa"}
|
package/lib/context.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { createContext, useContext } from "react";
|
|
3
|
+
//#region src/context.ts
|
|
4
|
+
const OntologyContext = createContext(void 0);
|
|
5
|
+
/** Optional access for commands such as actions, attachments and offline pins.
|
|
6
|
+
* Generated collection descriptors can be queried directly without this hook.
|
|
7
|
+
*/
|
|
8
|
+
function useOntology() {
|
|
9
|
+
const ontology = useContext(OntologyContext);
|
|
10
|
+
if (!ontology) throw new Error("useOntology requires an OntologyProvider.");
|
|
11
|
+
return ontology;
|
|
12
|
+
}
|
|
13
|
+
//#endregion
|
|
14
|
+
export { OntologyContext, useOntology };
|
|
15
|
+
|
|
16
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","names":[],"sources":["../src/context.ts"],"sourcesContent":["\"use client\";\nimport { createContext, useContext } from \"react\";\nimport type { LiveOntology, OntologyDefinition } from \"@party-stack/ontology\";\nexport const OntologyContext = createContext<LiveOntology | undefined>(undefined);\n/** Optional access for commands such as actions, attachments and offline pins.\n * Generated collection descriptors can be queried directly without this hook.\n */\nexport function useOntology<\n Definition extends OntologyDefinition = OntologyDefinition,\n>(): LiveOntology<Definition> {\n const ontology = useContext(OntologyContext);\n if (!ontology) throw new Error(\"useOntology requires an OntologyProvider.\");\n return ontology as unknown as LiveOntology<Definition>;\n}\n"],"mappings":";;;AAGA,MAAa,kBAAkB,cAAwC,KAAA,CAAS;;;;AAIhF,SAAgB,cAEc;CAC1B,MAAM,WAAW,WAAW,eAAe;CAC3C,IAAI,CAAC,UAAU,MAAM,IAAI,MAAM,2CAA2C;CAC1E,OAAO;AACX"}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { useAttachmentMetadata, useAttachmentUrl } from "./attachments.js";
|
|
2
|
+
import { OntologyViewOptions } from "./options.js";
|
|
3
|
+
import { useOntology } from "./context.js";
|
|
4
|
+
import { useOfflinePin } from "./pins.js";
|
|
5
|
+
import { ReactNode } from "react";
|
|
6
|
+
import { BackendError, CreateLiveOntologyOpts, LiveOntology, OntologyDefinition, OntologyIR, OntologyServerClient } from "@party-stack/ontology";
|
|
7
|
+
import { Effect, Scope } from "effect";
|
|
8
|
+
import { FragmentCollection, FragmentData, FragmentRef, ListFragmentData, ListFragmentRef, defineFragment, defineListFragment } from "@party-stack/db-fragments";
|
|
9
|
+
import { useFragment, useListFragment } from "@party-stack/db-fragments/react";
|
|
10
|
+
import { ontologyCollections } from "@party-stack/ontology/tanstack";
|
|
11
|
+
//#region src/index.d.ts
|
|
12
|
+
export type OntologyEnvironmentProviderProps = OntologyViewOptions & {
|
|
13
|
+
readonly client: OntologyServerClient | Effect.Effect<OntologyServerClient, BackendError, Scope.Scope>;
|
|
14
|
+
readonly children?: ReactNode;
|
|
15
|
+
};
|
|
16
|
+
type OntologyEnvironment = OntologyViewOptions & {
|
|
17
|
+
readonly client: OntologyServerClient;
|
|
18
|
+
};
|
|
19
|
+
/** Own one transport and supply default construction settings for independent local views. */
|
|
20
|
+
export declare function OntologyEnvironmentProvider({ client, children, ...props }: OntologyEnvironmentProviderProps): import("react").JSX.Element;
|
|
21
|
+
export declare function useOntologyEnvironment(): OntologyEnvironment;
|
|
22
|
+
type ConstructedOntologyOptions = CreateLiveOntologyOpts | (OntologyViewOptions & {
|
|
23
|
+
readonly ontology?: string;
|
|
24
|
+
readonly ir?: OntologyIR;
|
|
25
|
+
readonly client?: never;
|
|
26
|
+
readonly backend?: never;
|
|
27
|
+
});
|
|
28
|
+
export type OntologyProviderProps<Definition extends OntologyDefinition> = {
|
|
29
|
+
readonly children?: ReactNode;
|
|
30
|
+
} & ({
|
|
31
|
+
readonly ontology: LiveOntology<Definition>;
|
|
32
|
+
} | ConstructedOntologyOptions);
|
|
33
|
+
/** Existing views are borrowed. Construction props inherit environment defaults;
|
|
34
|
+
* an explicit backend/client overrides the environment's transport. Owned views
|
|
35
|
+
* close on selection change/unmount and report acquisition errors to React.
|
|
36
|
+
*/
|
|
37
|
+
export declare function OntologyProvider<Definition extends OntologyDefinition>(props: OntologyProviderProps<Definition>): import("react").JSX.Element;
|
|
38
|
+
//#endregion
|
|
39
|
+
export { type FragmentCollection, type FragmentData, type FragmentRef, type ListFragmentData, type ListFragmentRef, type OntologyViewOptions, defineFragment, defineListFragment, ontologyCollections, useAttachmentMetadata, useAttachmentUrl, useFragment, useListFragment, useOfflinePin, useOntology };
|
|
40
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.tsx"],"mappings":";;;;;;;;;;;YAyCY,mCAAmC;WAClC,QAAQ,uBAAuB,OAAO,OAAO,sBAAsB,cAAc,MAAM;WACvF,WAAW;;KAEnB,sBAAsB;WAAiC,QAAQ;;;wBAIpD,8BACZ,QACA,aACG,SACJ,mDAAgC,IAAA;wBA2DnB,0BAAsB;KAMjC,6BACC,0BACC;WACY;WACA,KAAK;WACL;WACA;;YAEP,sBAAsB,mBAAmB;WACxC,WAAW;;WACR,UAAU,aAAa;IAAgB;;;;;wBAMvC,iBAAiB,mBAAmB,oBAChD,OAAO,sBAAsB,8BAAW,IAAA"}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { AttachmentResourcesContext, makeAttachmentResources } from "./attachmentResources.js";
|
|
3
|
+
import { OntologyContext, useOntology } from "./context.js";
|
|
4
|
+
import { useAttachmentMetadata, useAttachmentUrl } from "./attachments.js";
|
|
5
|
+
import { useViewOptions } from "./options.js";
|
|
6
|
+
import { OwnedResource, OwnedResourceLease, OwnedValue } from "./resource.js";
|
|
7
|
+
import { useOfflinePin } from "./pins.js";
|
|
8
|
+
import { createContext, useContext, useMemo, useSyncExternalStore } from "react";
|
|
9
|
+
import { jsx } from "react/jsx-runtime";
|
|
10
|
+
import { createLiveOntology, createOntologyClient } from "@party-stack/ontology";
|
|
11
|
+
import { classifyBackendFailure } from "@party-stack/ontology-backend";
|
|
12
|
+
import { DbProvider } from "@tanstack/react-db";
|
|
13
|
+
import { Effect } from "effect";
|
|
14
|
+
import { defineFragment, defineListFragment } from "@party-stack/db-fragments";
|
|
15
|
+
import { useFragment, useListFragment } from "@party-stack/db-fragments/react";
|
|
16
|
+
import { ontologyCollections } from "@party-stack/ontology/tanstack";
|
|
17
|
+
//#region src/index.tsx
|
|
18
|
+
const EnvironmentContext = createContext(void 0);
|
|
19
|
+
/** Own one transport and supply default construction settings for independent local views. */
|
|
20
|
+
function OntologyEnvironmentProvider({ client, children, ...props }) {
|
|
21
|
+
const defaults = useViewOptions(props);
|
|
22
|
+
const open = useMemo(() => Effect.isEffect(client) ? () => createOntologyClient(client) : void 0, [client]);
|
|
23
|
+
return open ? /* @__PURE__ */ jsx(OwnedResourceLease, {
|
|
24
|
+
open,
|
|
25
|
+
children: (promise) => /* @__PURE__ */ jsx(OpeningEnvironmentValue, {
|
|
26
|
+
promise,
|
|
27
|
+
defaults,
|
|
28
|
+
children
|
|
29
|
+
})
|
|
30
|
+
}) : /* @__PURE__ */ jsx(EnvironmentValue, {
|
|
31
|
+
client,
|
|
32
|
+
defaults,
|
|
33
|
+
children
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
function OpeningEnvironmentValue({ promise, defaults, children }) {
|
|
37
|
+
const client = useMemo(() => {
|
|
38
|
+
const transport = Effect.tryPromise({
|
|
39
|
+
try: () => promise,
|
|
40
|
+
catch: (cause) => classifyBackendFailure("ontology.client", cause)
|
|
41
|
+
});
|
|
42
|
+
return { ontology: (name, options) => Effect.flatMap(transport, (client) => client.ontology(name, options)) };
|
|
43
|
+
}, [promise]);
|
|
44
|
+
return /* @__PURE__ */ jsx(EnvironmentValue, {
|
|
45
|
+
client,
|
|
46
|
+
defaults,
|
|
47
|
+
children
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
function EnvironmentValue({ client, defaults, children }) {
|
|
51
|
+
const value = useMemo(() => ({
|
|
52
|
+
...defaults,
|
|
53
|
+
client
|
|
54
|
+
}), [client, defaults]);
|
|
55
|
+
return /* @__PURE__ */ jsx(EnvironmentContext, {
|
|
56
|
+
value,
|
|
57
|
+
children
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
function useOntologyEnvironment() {
|
|
61
|
+
const environment = useContext(EnvironmentContext);
|
|
62
|
+
if (!environment) throw new Error("An OntologyEnvironmentProvider is required.");
|
|
63
|
+
return environment;
|
|
64
|
+
}
|
|
65
|
+
/** Existing views are borrowed. Construction props inherit environment defaults;
|
|
66
|
+
* an explicit backend/client overrides the environment's transport. Owned views
|
|
67
|
+
* close on selection change/unmount and report acquisition errors to React.
|
|
68
|
+
*/
|
|
69
|
+
function OntologyProvider(props) {
|
|
70
|
+
if (props.ontology && typeof props.ontology !== "string") return /* @__PURE__ */ jsx(BorrowedViewProvider, {
|
|
71
|
+
ontology: props.ontology,
|
|
72
|
+
children: props.children
|
|
73
|
+
});
|
|
74
|
+
return /* @__PURE__ */ jsx(ConstructedProvider, {
|
|
75
|
+
...props,
|
|
76
|
+
children: props.children
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
function ConstructedProvider({ children, ...props }) {
|
|
80
|
+
const environment = useContext(EnvironmentContext);
|
|
81
|
+
const defaults = useViewOptions(environment ?? {});
|
|
82
|
+
const overrides = useViewOptions(props);
|
|
83
|
+
const backend = props.backend;
|
|
84
|
+
const client = props.client ?? environment?.client;
|
|
85
|
+
const ir = props.ir;
|
|
86
|
+
const ontology = props.ontology;
|
|
87
|
+
const options = useMemo(() => {
|
|
88
|
+
const settings = {
|
|
89
|
+
...defaults,
|
|
90
|
+
...overrides
|
|
91
|
+
};
|
|
92
|
+
if (backend) return {
|
|
93
|
+
...settings,
|
|
94
|
+
ir,
|
|
95
|
+
backend
|
|
96
|
+
};
|
|
97
|
+
if (!client) throw new Error("Supply a backend, a client, or an OntologyEnvironmentProvider.");
|
|
98
|
+
return {
|
|
99
|
+
...settings,
|
|
100
|
+
ir,
|
|
101
|
+
client,
|
|
102
|
+
ontology
|
|
103
|
+
};
|
|
104
|
+
}, [
|
|
105
|
+
defaults,
|
|
106
|
+
overrides,
|
|
107
|
+
backend,
|
|
108
|
+
client,
|
|
109
|
+
ir,
|
|
110
|
+
ontology
|
|
111
|
+
]);
|
|
112
|
+
const open = useMemo(() => async () => {
|
|
113
|
+
const ontology = await createLiveOntology(options);
|
|
114
|
+
const resources = makeAttachmentResources();
|
|
115
|
+
return {
|
|
116
|
+
ontology,
|
|
117
|
+
resources,
|
|
118
|
+
cleanup: async () => {
|
|
119
|
+
await resources.cleanup();
|
|
120
|
+
await ontology.cleanup();
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
}, [options]);
|
|
124
|
+
return /* @__PURE__ */ jsx(OwnedResource, {
|
|
125
|
+
open,
|
|
126
|
+
children: ({ ontology, resources }) => /* @__PURE__ */ jsx(ViewProvider, {
|
|
127
|
+
ontology,
|
|
128
|
+
attachments: resources.cache,
|
|
129
|
+
children
|
|
130
|
+
})
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
const subscribeClient = () => () => {};
|
|
134
|
+
const clientSnapshot = () => true;
|
|
135
|
+
const serverSnapshot = () => false;
|
|
136
|
+
function BorrowedViewProvider({ ontology, children }) {
|
|
137
|
+
const client = useSyncExternalStore(subscribeClient, clientSnapshot, serverSnapshot);
|
|
138
|
+
const open = useMemo(() => () => makeAttachmentResources(), [ontology]);
|
|
139
|
+
if (!client) return /* @__PURE__ */ jsx(ViewProvider, {
|
|
140
|
+
ontology,
|
|
141
|
+
children
|
|
142
|
+
});
|
|
143
|
+
return /* @__PURE__ */ jsx(OwnedValue, {
|
|
144
|
+
open,
|
|
145
|
+
children: ({ cache }) => /* @__PURE__ */ jsx(ViewProvider, {
|
|
146
|
+
ontology,
|
|
147
|
+
attachments: cache,
|
|
148
|
+
children
|
|
149
|
+
})
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
function ViewProvider({ ontology, attachments, children }) {
|
|
153
|
+
return /* @__PURE__ */ jsx(OntologyContext, {
|
|
154
|
+
value: ontology,
|
|
155
|
+
children: /* @__PURE__ */ jsx(AttachmentResourcesContext, {
|
|
156
|
+
value: attachments,
|
|
157
|
+
children: /* @__PURE__ */ jsx(DbProvider, {
|
|
158
|
+
client: ontology.dbClient,
|
|
159
|
+
children
|
|
160
|
+
})
|
|
161
|
+
})
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
//#endregion
|
|
165
|
+
export { OntologyEnvironmentProvider, OntologyProvider, defineFragment, defineListFragment, ontologyCollections, useAttachmentMetadata, useAttachmentUrl, useFragment, useListFragment, useOfflinePin, useOntology, useOntologyEnvironment };
|
|
166
|
+
|
|
167
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/index.tsx"],"sourcesContent":["\"use client\";\nimport { OntologyContext } from \"./context.js\";\nexport { useOntology } from \"./context.js\";\n\nimport { createLiveOntology, createOntologyClient } from \"@party-stack/ontology\";\nimport { classifyBackendFailure } from \"@party-stack/ontology-backend\";\nimport { DbProvider } from \"@tanstack/react-db\";\nimport { Effect } from \"effect\";\nimport { createContext, useContext, useMemo, useSyncExternalStore } from \"react\";\nimport { AttachmentResourcesContext, makeAttachmentResources } from \"./attachmentResources.js\";\nimport { useViewOptions } from \"./options.js\";\nimport { OwnedResource, OwnedResourceLease, OwnedValue } from \"./resource.js\";\nimport type { AttachmentPromiseCache } from \"./attachmentPromiseCache.js\";\nimport type { OntologyViewOptions } from \"./options.js\";\nimport type {\n CreateLiveOntologyOpts,\n LiveOntology,\n OntologyDefinition,\n OntologyIR,\n OntologyServerClient,\n BackendError,\n} from \"@party-stack/ontology\";\nimport type { Scope } from \"effect\";\nimport type { ReactNode } from \"react\";\n\nexport { defineFragment, defineListFragment } from \"@party-stack/db-fragments\";\nexport { useFragment, useListFragment } from \"@party-stack/db-fragments/react\";\nexport type {\n FragmentData,\n FragmentRef,\n FragmentCollection,\n ListFragmentRef,\n ListFragmentData,\n} from \"@party-stack/db-fragments\";\n\nexport { useAttachmentUrl, useAttachmentMetadata } from \"./attachments.js\";\nexport { useOfflinePin } from \"./pins.js\";\n\nexport { ontologyCollections } from \"@party-stack/ontology/tanstack\";\nexport type { OntologyViewOptions } from \"./options.js\";\n\nexport type OntologyEnvironmentProviderProps = OntologyViewOptions & {\n readonly client: OntologyServerClient | Effect.Effect<OntologyServerClient, BackendError, Scope.Scope>;\n readonly children?: ReactNode;\n};\ntype OntologyEnvironment = OntologyViewOptions & { readonly client: OntologyServerClient };\nconst EnvironmentContext = createContext<OntologyEnvironment | undefined>(undefined);\n\n/** Own one transport and supply default construction settings for independent local views. */\nexport function OntologyEnvironmentProvider({\n client,\n children,\n ...props\n}: OntologyEnvironmentProviderProps) {\n const defaults = useViewOptions(props);\n const open = useMemo(\n () => (Effect.isEffect(client) ? () => createOntologyClient(client) : undefined),\n [client]\n );\n return open ? (\n <OwnedResourceLease open={open}>\n {(promise) => (\n <OpeningEnvironmentValue promise={promise} defaults={defaults}>\n {children}\n </OpeningEnvironmentValue>\n )}\n </OwnedResourceLease>\n ) : (\n <EnvironmentValue client={client as OntologyServerClient} defaults={defaults}>\n {children}\n </EnvironmentValue>\n );\n}\nfunction OpeningEnvironmentValue({\n promise,\n defaults,\n children,\n}: {\n readonly promise: ReturnType<typeof createOntologyClient>;\n readonly defaults: OntologyViewOptions;\n readonly children?: ReactNode;\n}) {\n const client = useMemo<OntologyServerClient>(() => {\n // The environment owns the transport. Child openings borrow the same\n // acquisition instead of waiting for a separate React reveal to mount.\n const transport = Effect.tryPromise({\n try: () => promise,\n catch: (cause) => classifyBackendFailure(\"ontology.client\", cause),\n });\n return {\n ontology: (name, options) =>\n Effect.flatMap(transport, (client) => client.ontology(name, options)),\n };\n }, [promise]);\n return (\n <EnvironmentValue client={client} defaults={defaults}>\n {children}\n </EnvironmentValue>\n );\n}\nfunction EnvironmentValue({\n client,\n defaults,\n children,\n}: {\n readonly client: OntologyServerClient;\n readonly defaults: OntologyViewOptions;\n readonly children?: ReactNode;\n}) {\n const value = useMemo(() => ({ ...defaults, client }), [client, defaults]);\n return <EnvironmentContext value={value}>{children}</EnvironmentContext>;\n}\nexport function useOntologyEnvironment() {\n const environment = useContext(EnvironmentContext);\n if (!environment) throw new Error(\"An OntologyEnvironmentProvider is required.\");\n return environment;\n}\n\ntype ConstructedOntologyOptions =\n | CreateLiveOntologyOpts\n | (OntologyViewOptions & {\n readonly ontology?: string;\n readonly ir?: OntologyIR;\n readonly client?: never;\n readonly backend?: never;\n });\nexport type OntologyProviderProps<Definition extends OntologyDefinition> = {\n readonly children?: ReactNode;\n} & ({ readonly ontology: LiveOntology<Definition> } | ConstructedOntologyOptions);\n\n/** Existing views are borrowed. Construction props inherit environment defaults;\n * an explicit backend/client overrides the environment's transport. Owned views\n * close on selection change/unmount and report acquisition errors to React.\n */\nexport function OntologyProvider<Definition extends OntologyDefinition>(\n props: OntologyProviderProps<Definition>\n) {\n if (props.ontology && typeof props.ontology !== \"string\")\n return <BorrowedViewProvider ontology={props.ontology}>{props.children}</BorrowedViewProvider>;\n return (\n <ConstructedProvider<Definition> {...(props as ConstructedOntologyOptions)}>\n {props.children}\n </ConstructedProvider>\n );\n}\nfunction ConstructedProvider<Definition extends OntologyDefinition>({\n children,\n ...props\n}: ConstructedOntologyOptions & { readonly children?: ReactNode }) {\n const environment = useContext(EnvironmentContext);\n const defaults = useViewOptions(environment ?? {});\n const overrides = useViewOptions(props);\n const backend = props.backend;\n const client = props.client ?? environment?.client;\n const ir = props.ir;\n const ontology = props.ontology;\n const options = useMemo<CreateLiveOntologyOpts>(() => {\n const settings = { ...defaults, ...overrides };\n if (backend) return { ...settings, ir: ir!, backend };\n if (!client) throw new Error(\"Supply a backend, a client, or an OntologyEnvironmentProvider.\");\n return { ...settings, ir, client, ontology };\n }, [defaults, overrides, backend, client, ir, ontology]);\n const open = useMemo(\n () => async () => {\n const ontology = await createLiveOntology<Definition>(options);\n const resources = makeAttachmentResources();\n return {\n ontology,\n resources,\n cleanup: async () => {\n await resources.cleanup();\n await ontology.cleanup();\n },\n };\n },\n [options]\n );\n return (\n <OwnedResource open={open}>\n {({ ontology, resources }) => (\n <ViewProvider ontology={ontology} attachments={resources.cache}>\n {children}\n </ViewProvider>\n )}\n </OwnedResource>\n );\n}\n\nconst subscribeClient = () => () => {};\nconst clientSnapshot = () => true;\nconst serverSnapshot = () => false;\n\nfunction BorrowedViewProvider<Definition extends OntologyDefinition>({\n ontology,\n children,\n}: {\n readonly ontology: LiveOntology<Definition>;\n readonly children?: ReactNode;\n}) {\n const client = useSyncExternalStore(subscribeClient, clientSnapshot, serverSnapshot);\n const open = useMemo(() => () => makeAttachmentResources(), [ontology]);\n // Borrowed views still render during SSR. On the client an independent owner\n // commits before attachment readers can suspend, without owning the ontology.\n if (!client) return <ViewProvider ontology={ontology}>{children}</ViewProvider>;\n return (\n <OwnedValue open={open}>\n {({ cache }) => (\n <ViewProvider ontology={ontology} attachments={cache}>\n {children}\n </ViewProvider>\n )}\n </OwnedValue>\n );\n}\n\nfunction ViewProvider<Definition extends OntologyDefinition>({\n ontology,\n attachments,\n children,\n}: {\n readonly ontology: LiveOntology<Definition>;\n readonly attachments?: AttachmentPromiseCache;\n readonly children?: ReactNode;\n}) {\n return (\n <OntologyContext value={ontology as unknown as LiveOntology}>\n <AttachmentResourcesContext value={attachments}>\n <DbProvider client={ontology.dbClient}>{children}</DbProvider>\n </AttachmentResourcesContext>\n </OntologyContext>\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AA8CA,MAAM,qBAAqB,cAA+C,KAAA,CAAS;;AAGnF,SAAgB,4BAA4B,EACxC,QACA,UACA,GAAG,SAC8B;CACjC,MAAM,WAAW,eAAe,KAAK;CACrC,MAAM,OAAO,cACF,OAAO,SAAS,MAAM,UAAU,qBAAqB,MAAM,IAAI,KAAA,GACtE,CAAC,MAAM,CACX;CACA,OAAO,OACH,oBAAC,oBAAD;EAA0B;EACpB,WAAA,YACE,oBAAC,yBAAD;GAAkC;GAAmB;GAChD;EACoB,CAAA;CAEb,CAAA,IAEpB,oBAAC,kBAAD;EAA0B;EAA0C;EAC/D;CACa,CAAA;AAE1B;AACA,SAAS,wBAAwB,EAC7B,SACA,UACA,YAKD;CACC,MAAM,SAAS,cAAoC;EAG/C,MAAM,YAAY,OAAO,WAAW;GAChC,WAAW;GACX,QAAQ,UAAU,uBAAuB,mBAAmB,KAAK;EACrE,CAAC;EACD,OAAO,EACH,WAAW,MAAM,YACb,OAAO,QAAQ,YAAY,WAAW,OAAO,SAAS,MAAM,OAAO,CAAC,EAC5E;CACJ,GAAG,CAAC,OAAO,CAAC;CACZ,OACI,oBAAC,kBAAD;EAA0B;EAAkB;EACvC;CACa,CAAA;AAE1B;AACA,SAAS,iBAAiB,EACtB,QACA,UACA,YAKD;CACC,MAAM,QAAQ,eAAe;EAAE,GAAG;EAAU;CAAO,IAAI,CAAC,QAAQ,QAAQ,CAAC;CACzE,OAAO,oBAAC,oBAAD;EAA2B;EAAQ;CAA6B,CAAA;AAC3E;AACA,SAAgB,yBAAyB;CACrC,MAAM,cAAc,WAAW,kBAAkB;CACjD,IAAI,CAAC,aAAa,MAAM,IAAI,MAAM,6CAA6C;CAC/E,OAAO;AACX;;;;;AAkBA,SAAgB,iBACZ,OACF;CACE,IAAI,MAAM,YAAY,OAAO,MAAM,aAAa,UAC5C,OAAO,oBAAC,sBAAD;EAAsB,UAAU,MAAM;EAAW,UAAA,MAAM;CAA+B,CAAA;CACjG,OACI,oBAAC,qBAAD;EAAiC,GAAK;EACjC,UAAA,MAAM;CACU,CAAA;AAE7B;AACA,SAAS,oBAA2D,EAChE,UACA,GAAG,SAC4D;CAC/D,MAAM,cAAc,WAAW,kBAAkB;CACjD,MAAM,WAAW,eAAe,eAAe,CAAC,CAAC;CACjD,MAAM,YAAY,eAAe,KAAK;CACtC,MAAM,UAAU,MAAM;CACtB,MAAM,SAAS,MAAM,UAAU,aAAa;CAC5C,MAAM,KAAK,MAAM;CACjB,MAAM,WAAW,MAAM;CACvB,MAAM,UAAU,cAAsC;EAClD,MAAM,WAAW;GAAE,GAAG;GAAU,GAAG;EAAU;EAC7C,IAAI,SAAS,OAAO;GAAE,GAAG;GAAc;GAAK;EAAQ;EACpD,IAAI,CAAC,QAAQ,MAAM,IAAI,MAAM,gEAAgE;EAC7F,OAAO;GAAE,GAAG;GAAU;GAAI;GAAQ;EAAS;CAC/C,GAAG;EAAC;EAAU;EAAW;EAAS;EAAQ;EAAI;CAAQ,CAAC;CACvD,MAAM,OAAO,cACH,YAAY;EACd,MAAM,WAAW,MAAM,mBAA+B,OAAO;EAC7D,MAAM,YAAY,wBAAwB;EAC1C,OAAO;GACH;GACA;GACA,SAAS,YAAY;IACjB,MAAM,UAAU,QAAQ;IACxB,MAAM,SAAS,QAAQ;GAC3B;EACJ;CACJ,GACA,CAAC,OAAO,CACZ;CACA,OACI,oBAAC,eAAD;EAAqB;EACf,WAAA,EAAE,UAAU,gBACV,oBAAC,cAAD;GAAwB;GAAU,aAAa,UAAU;GACpD;EACS,CAAA;CAEP,CAAA;AAEvB;AAEA,MAAM,8BAA8B,CAAC;AACrC,MAAM,uBAAuB;AAC7B,MAAM,uBAAuB;AAE7B,SAAS,qBAA4D,EACjE,UACA,YAID;CACC,MAAM,SAAS,qBAAqB,iBAAiB,gBAAgB,cAAc;CACnF,MAAM,OAAO,oBAAoB,wBAAwB,GAAG,CAAC,QAAQ,CAAC;CAGtE,IAAI,CAAC,QAAQ,OAAO,oBAAC,cAAD;EAAwB;EAAW;CAAuB,CAAA;CAC9E,OACI,oBAAC,YAAD;EAAkB;EACZ,WAAA,EAAE,YACA,oBAAC,cAAD;GAAwB;GAAU,aAAa;GAC1C;EACS,CAAA;CAEV,CAAA;AAEpB;AAEA,SAAS,aAAoD,EACzD,UACA,aACA,YAKD;CACC,OACI,oBAAC,iBAAD;EAAiB,OAAO;EACpB,UAAA,oBAAC,4BAAD;GAA4B,OAAO;GAC/B,UAAA,oBAAC,YAAD;IAAY,QAAQ,SAAS;IAAW;GAAqB,CAAA;EACrC,CAAA;CACf,CAAA;AAEzB"}
|
package/lib/options.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { CreateLiveOntologyOpts } from "@party-stack/ontology";
|
|
2
|
+
//#region src/options.d.ts
|
|
3
|
+
export type OntologyViewOptions = Omit<CreateLiveOntologyOpts, "ir" | "backend" | "client" | "ontology">;
|
|
4
|
+
/** Primitive JSX props can change independently without reopening the view on
|
|
5
|
+
* every parent render. Object-valued construction inputs retain normal identity semantics.
|
|
6
|
+
*/
|
|
7
|
+
export declare function useViewOptions({ id, live, runtime, writes, demand, cache, traceRecording, context, authentication, label, parent }: OntologyViewOptions): OntologyViewOptions;
|
|
8
|
+
//#endregion
|
|
9
|
+
//# sourceMappingURL=options.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"options.d.ts","names":[],"sources":["../src/options.ts"],"mappings":";;YAGY,sBAAsB,KAAK;;;;wBAKvB,iBACZ,IACA,MACA,SACA,QACA,QACA,OACA,gBACA,SACA,gBACA,OACA,UACD,sBAAmB"}
|
package/lib/options.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { useMemo } from "react";
|
|
2
|
+
//#region src/options.ts
|
|
3
|
+
/** Primitive JSX props can change independently without reopening the view on
|
|
4
|
+
* every parent render. Object-valued construction inputs retain normal identity semantics.
|
|
5
|
+
*/
|
|
6
|
+
function useViewOptions({ id, live, runtime, writes, demand, cache, traceRecording, context, authentication, label, parent }) {
|
|
7
|
+
return useMemo(() => {
|
|
8
|
+
return Object.fromEntries(Object.entries({
|
|
9
|
+
id,
|
|
10
|
+
live,
|
|
11
|
+
runtime,
|
|
12
|
+
writes,
|
|
13
|
+
demand,
|
|
14
|
+
cache,
|
|
15
|
+
traceRecording,
|
|
16
|
+
context,
|
|
17
|
+
authentication,
|
|
18
|
+
label,
|
|
19
|
+
parent
|
|
20
|
+
}).filter(([, value]) => value !== void 0));
|
|
21
|
+
}, [
|
|
22
|
+
id,
|
|
23
|
+
live,
|
|
24
|
+
runtime,
|
|
25
|
+
writes,
|
|
26
|
+
demand,
|
|
27
|
+
cache,
|
|
28
|
+
traceRecording,
|
|
29
|
+
context,
|
|
30
|
+
authentication,
|
|
31
|
+
label,
|
|
32
|
+
parent
|
|
33
|
+
]);
|
|
34
|
+
}
|
|
35
|
+
//#endregion
|
|
36
|
+
export { useViewOptions };
|
|
37
|
+
|
|
38
|
+
//# sourceMappingURL=options.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"options.js","names":[],"sources":["../src/options.ts"],"sourcesContent":["import { useMemo } from \"react\";\nimport type { CreateLiveOntologyOpts } from \"@party-stack/ontology\";\n\nexport type OntologyViewOptions = Omit<CreateLiveOntologyOpts, \"ir\" | \"backend\" | \"client\" | \"ontology\">;\n\n/** Primitive JSX props can change independently without reopening the view on\n * every parent render. Object-valued construction inputs retain normal identity semantics.\n */\nexport function useViewOptions({\n id,\n live,\n runtime,\n writes,\n demand,\n cache,\n traceRecording,\n context,\n authentication,\n label,\n parent,\n}: OntologyViewOptions) {\n return useMemo(() => {\n const values = {\n id,\n live,\n runtime,\n writes,\n demand,\n cache,\n traceRecording,\n context,\n authentication,\n label,\n parent,\n } satisfies Record<keyof OntologyViewOptions, unknown>;\n return Object.fromEntries(\n Object.entries(values).filter(([, value]) => value !== undefined)\n ) as OntologyViewOptions;\n }, [id, live, runtime, writes, demand, cache, traceRecording, context, authentication, label, parent]);\n}\n"],"mappings":";;;;;AAQA,SAAgB,eAAe,EAC3B,IACA,MACA,SACA,QACA,QACA,OACA,gBACA,SACA,gBACA,OACA,UACoB;CACpB,OAAO,cAAc;EAcjB,OAAO,OAAO,YACV,OAAO,QAAQ;GAbf;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;EAGoB,CAAC,CAAC,CAAC,QAAQ,GAAG,WAAW,UAAU,KAAA,CAAS,CACpE;CACJ,GAAG;EAAC;EAAI;EAAM;EAAS;EAAQ;EAAQ;EAAO;EAAgB;EAAS;EAAgB;EAAO;CAAM,CAAC;AACzG"}
|
package/lib/pins.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { PinOptions, PinStatus, QueryDocument } from "@party-stack/ontology";
|
|
2
|
+
//#region src/pins.d.ts
|
|
3
|
+
/** Observe the owning replica's persisted offline graph; a tab targets its parent. */
|
|
4
|
+
export declare function useOfflinePin(): {
|
|
5
|
+
status: PinStatus | undefined;
|
|
6
|
+
error: unknown;
|
|
7
|
+
isLoading: boolean;
|
|
8
|
+
set: (query: QueryDocument, options?: PinOptions) => Promise<void>;
|
|
9
|
+
clear: () => Promise<void>;
|
|
10
|
+
};
|
|
11
|
+
//#endregion
|
|
12
|
+
//# sourceMappingURL=pins.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pins.d.ts","names":[],"sources":["../src/pins.ts"],"mappings":";;;wBAMgB;;;;EAkBoB,MAAA,OAAA,eAAa,UAAY,eAAU"}
|
package/lib/pins.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useOntology } from "./context.js";
|
|
3
|
+
import { useCallback, useEffect, useState } from "react";
|
|
4
|
+
//#region src/pins.ts
|
|
5
|
+
/** Observe the owning replica's persisted offline graph; a tab targets its parent. */
|
|
6
|
+
function useOfflinePin() {
|
|
7
|
+
const { pins } = useOntology();
|
|
8
|
+
const [state, setState] = useState();
|
|
9
|
+
useEffect(() => {
|
|
10
|
+
let active = true;
|
|
11
|
+
const stop = pins.subscribe((status) => {
|
|
12
|
+
if (active) setState({
|
|
13
|
+
pins,
|
|
14
|
+
status
|
|
15
|
+
});
|
|
16
|
+
}, (error) => {
|
|
17
|
+
if (active) setState({
|
|
18
|
+
pins,
|
|
19
|
+
error
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
return () => {
|
|
23
|
+
active = false;
|
|
24
|
+
stop();
|
|
25
|
+
};
|
|
26
|
+
}, [pins]);
|
|
27
|
+
const set = useCallback((query, options) => pins.set(query, options), [pins]);
|
|
28
|
+
const clear = useCallback(() => pins.clear(), [pins]);
|
|
29
|
+
return {
|
|
30
|
+
status: state?.pins === pins ? state.status : void 0,
|
|
31
|
+
error: state?.pins === pins ? state.error : void 0,
|
|
32
|
+
isLoading: state?.pins !== pins,
|
|
33
|
+
set,
|
|
34
|
+
clear
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
//#endregion
|
|
38
|
+
export { useOfflinePin };
|
|
39
|
+
|
|
40
|
+
//# sourceMappingURL=pins.js.map
|
package/lib/pins.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pins.js","names":[],"sources":["../src/pins.ts"],"sourcesContent":["\"use client\";\nimport { useCallback, useEffect, useState } from \"react\";\nimport { useOntology } from \"./context.js\";\nimport type { PinStatus, PinOptions, QueryDocument } from \"@party-stack/ontology\";\n\n/** Observe the owning replica's persisted offline graph; a tab targets its parent. */\nexport function useOfflinePin() {\n const { pins } = useOntology();\n const [state, setState] = useState<{ pins: typeof pins; status?: PinStatus; error?: unknown }>();\n useEffect(() => {\n let active = true;\n const stop = pins.subscribe(\n (status) => {\n if (active) setState({ pins, status });\n },\n (error) => {\n if (active) setState({ pins, error });\n }\n );\n return () => {\n active = false;\n stop();\n };\n }, [pins]);\n const set = useCallback((query: QueryDocument, options?: PinOptions) => pins.set(query, options), [pins]);\n const clear = useCallback(() => pins.clear(), [pins]);\n return {\n status: state?.pins === pins ? state.status : undefined,\n error: state?.pins === pins ? state.error : undefined,\n isLoading: state?.pins !== pins,\n set,\n clear,\n };\n}\n"],"mappings":";;;;;AAMA,SAAgB,gBAAgB;CAC5B,MAAM,EAAE,SAAS,YAAY;CAC7B,MAAM,CAAC,OAAO,YAAY,SAAqE;CAC/F,gBAAgB;EACZ,IAAI,SAAS;EACb,MAAM,OAAO,KAAK,WACb,WAAW;GACR,IAAI,QAAQ,SAAS;IAAE;IAAM;GAAO,CAAC;EACzC,IACC,UAAU;GACP,IAAI,QAAQ,SAAS;IAAE;IAAM;GAAM,CAAC;EACxC,CACJ;EACA,aAAa;GACT,SAAS;GACT,KAAK;EACT;CACJ,GAAG,CAAC,IAAI,CAAC;CACT,MAAM,MAAM,aAAa,OAAsB,YAAyB,KAAK,IAAI,OAAO,OAAO,GAAG,CAAC,IAAI,CAAC;CACxG,MAAM,QAAQ,kBAAkB,KAAK,MAAM,GAAG,CAAC,IAAI,CAAC;CACpD,OAAO;EACH,QAAQ,OAAO,SAAS,OAAO,MAAM,SAAS,KAAA;EAC9C,OAAO,OAAO,SAAS,OAAO,MAAM,QAAQ,KAAA;EAC5C,WAAW,OAAO,SAAS;EAC3B;EACA;CACJ;AACJ"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { ReactNode } from "react";
|
|
2
|
+
//#region src/resource.d.ts
|
|
3
|
+
/** A synchronous value still needs a committed owner before descendants can
|
|
4
|
+
* start reads. Publishing it directly avoids introducing a Suspense Promise.
|
|
5
|
+
* Abandoned renders allocate nothing; StrictMode's immediate effect replay
|
|
6
|
+
* reuses the value, while a real unmount disposes it in the next microtask.
|
|
7
|
+
*/
|
|
8
|
+
export declare function OwnedValue<Resource extends {
|
|
9
|
+
cleanup(): void | Promise<void>;
|
|
10
|
+
}>({ open, children }: {
|
|
11
|
+
readonly open: () => Resource;
|
|
12
|
+
readonly children: (resource: Resource) => ReactNode;
|
|
13
|
+
}): ReactNode;
|
|
14
|
+
/** The first empty commit installs the owner before reading its Promise can
|
|
15
|
+
* suspend. A component-local lease survives StrictMode's immediate effect replay;
|
|
16
|
+
* a real unmount closes it in the next microtask, including late acquisitions.
|
|
17
|
+
* No render starts IO and no global resource cache retains abandoned Scopes.
|
|
18
|
+
*/
|
|
19
|
+
export declare function OwnedResource<Resource extends {
|
|
20
|
+
cleanup(): Promise<void>;
|
|
21
|
+
}>({ open, children }: {
|
|
22
|
+
readonly open: () => Promise<Resource>;
|
|
23
|
+
readonly children: (resource: Resource) => ReactNode;
|
|
24
|
+
}): import("react").JSX.Element;
|
|
25
|
+
/** Publish the owned acquisition before it resolves so dependent openings can
|
|
26
|
+
* compose their Promises without serial Suspense reveals. The lease, including
|
|
27
|
+
* late completion cleanup, still belongs to this committed component.
|
|
28
|
+
*/
|
|
29
|
+
export declare function OwnedResourceLease<Resource extends {
|
|
30
|
+
cleanup(): Promise<void>;
|
|
31
|
+
}>({ open, children }: {
|
|
32
|
+
readonly open: () => Promise<Resource>;
|
|
33
|
+
readonly children: (promise: Promise<Resource>) => ReactNode;
|
|
34
|
+
}): ReactNode;
|
|
35
|
+
//#endregion
|
|
36
|
+
//# sourceMappingURL=resource.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resource.d.ts","names":[],"sources":["../src/resource.tsx"],"mappings":";;;;;;;wBAQgB,WAAW;EAAmB,kBAAkB;KAC5D,MACA;WAES,YAAY;WACZ,WAAW,UAAU,aAAa;IAC9C;;;;;;wBAoCe,cAAc;EAAmB,WAAW;KACxD,MACA;WAES,YAAY,QAAQ;WACpB,WAAW,UAAU,aAAa;oBAC9C,IAAA;;;;;wBAYe,mBAAmB;EAAmB,WAAW;KAC7D,MACA;WAES,YAAY,QAAQ;WACpB,WAAW,SAAS,QAAQ,cAAc;IACtD"}
|
package/lib/resource.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { use, useEffect, useRef, useState } from "react";
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
//#region src/resource.tsx
|
|
4
|
+
/** A synchronous value still needs a committed owner before descendants can
|
|
5
|
+
* start reads. Publishing it directly avoids introducing a Suspense Promise.
|
|
6
|
+
* Abandoned renders allocate nothing; StrictMode's immediate effect replay
|
|
7
|
+
* reuses the value, while a real unmount disposes it in the next microtask.
|
|
8
|
+
*/
|
|
9
|
+
function OwnedValue({ open, children }) {
|
|
10
|
+
const owner = useRef(void 0);
|
|
11
|
+
const [state, setState] = useState();
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
if (!owner.current || owner.current.open !== open || owner.current.disposed) owner.current = {
|
|
14
|
+
open,
|
|
15
|
+
value: open(),
|
|
16
|
+
active: true,
|
|
17
|
+
disposed: false
|
|
18
|
+
};
|
|
19
|
+
const lease = owner.current;
|
|
20
|
+
lease.active = true;
|
|
21
|
+
setState(lease);
|
|
22
|
+
return () => {
|
|
23
|
+
lease.active = false;
|
|
24
|
+
queueMicrotask(() => {
|
|
25
|
+
if (lease.active || lease.disposed) return;
|
|
26
|
+
lease.disposed = true;
|
|
27
|
+
if (owner.current === lease) owner.current = void 0;
|
|
28
|
+
Promise.resolve().then(() => lease.value.cleanup()).catch((error) => console.error("Failed to close owned value.", error));
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
}, [open]);
|
|
32
|
+
if (!state || state.open !== open || state.disposed) return null;
|
|
33
|
+
return children(state.value);
|
|
34
|
+
}
|
|
35
|
+
/** The first empty commit installs the owner before reading its Promise can
|
|
36
|
+
* suspend. A component-local lease survives StrictMode's immediate effect replay;
|
|
37
|
+
* a real unmount closes it in the next microtask, including late acquisitions.
|
|
38
|
+
* No render starts IO and no global resource cache retains abandoned Scopes.
|
|
39
|
+
*/
|
|
40
|
+
function OwnedResource({ open, children }) {
|
|
41
|
+
return /* @__PURE__ */ jsx(OwnedResourceLease, {
|
|
42
|
+
open,
|
|
43
|
+
children: (promise) => /* @__PURE__ */ jsx(ResolvedProvider, {
|
|
44
|
+
promise,
|
|
45
|
+
children
|
|
46
|
+
})
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/** Publish the owned acquisition before it resolves so dependent openings can
|
|
50
|
+
* compose their Promises without serial Suspense reveals. The lease, including
|
|
51
|
+
* late completion cleanup, still belongs to this committed component.
|
|
52
|
+
*/
|
|
53
|
+
function OwnedResourceLease({ open, children }) {
|
|
54
|
+
const owner = useRef(void 0);
|
|
55
|
+
const [state, setState] = useState();
|
|
56
|
+
const [failure, setFailure] = useState();
|
|
57
|
+
useEffect(() => {
|
|
58
|
+
const close = (ontology) => {
|
|
59
|
+
ontology.cleanup().catch((error) => console.error("Failed to close ontology resource.", error));
|
|
60
|
+
};
|
|
61
|
+
if (!owner.current || owner.current.open !== open || owner.current.disposed) {
|
|
62
|
+
const lease = {
|
|
63
|
+
open,
|
|
64
|
+
active: true,
|
|
65
|
+
disposed: false,
|
|
66
|
+
promise: Promise.resolve().then(open).then((ontology) => {
|
|
67
|
+
if (lease.disposed) close(ontology);
|
|
68
|
+
else lease.acquired = ontology;
|
|
69
|
+
return ontology;
|
|
70
|
+
})
|
|
71
|
+
};
|
|
72
|
+
lease.promise.catch((cause) => {
|
|
73
|
+
if (lease.active && !lease.disposed) setFailure({
|
|
74
|
+
lease,
|
|
75
|
+
cause
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
owner.current = lease;
|
|
79
|
+
}
|
|
80
|
+
const lease = owner.current;
|
|
81
|
+
lease.active = true;
|
|
82
|
+
queueMicrotask(() => {
|
|
83
|
+
if (lease.active && !lease.disposed) setState(lease);
|
|
84
|
+
});
|
|
85
|
+
return () => {
|
|
86
|
+
lease.active = false;
|
|
87
|
+
queueMicrotask(() => {
|
|
88
|
+
if (lease.active || lease.disposed) return;
|
|
89
|
+
lease.disposed = true;
|
|
90
|
+
if (lease.acquired) close(lease.acquired);
|
|
91
|
+
if (owner.current === lease) owner.current = void 0;
|
|
92
|
+
});
|
|
93
|
+
};
|
|
94
|
+
}, [open]);
|
|
95
|
+
if (!state || state.open !== open) return null;
|
|
96
|
+
if (failure?.lease === state) throw failure.cause;
|
|
97
|
+
return children(state.promise);
|
|
98
|
+
}
|
|
99
|
+
function ResolvedProvider({ promise, children }) {
|
|
100
|
+
return children(use(promise));
|
|
101
|
+
}
|
|
102
|
+
//#endregion
|
|
103
|
+
export { OwnedResource, OwnedResourceLease, OwnedValue };
|
|
104
|
+
|
|
105
|
+
//# sourceMappingURL=resource.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resource.js","names":[],"sources":["../src/resource.tsx"],"sourcesContent":["import { use, useEffect, useRef, useState } from \"react\";\nimport type { ReactNode } from \"react\";\n\n/** A synchronous value still needs a committed owner before descendants can\n * start reads. Publishing it directly avoids introducing a Suspense Promise.\n * Abandoned renders allocate nothing; StrictMode's immediate effect replay\n * reuses the value, while a real unmount disposes it in the next microtask.\n */\nexport function OwnedValue<Resource extends { cleanup(): void | Promise<void> }>({\n open,\n children,\n}: {\n readonly open: () => Resource;\n readonly children: (resource: Resource) => ReactNode;\n}) {\n type Lease = {\n readonly open: typeof open;\n readonly value: Resource;\n active: boolean;\n disposed: boolean;\n };\n const owner = useRef<Lease | undefined>(undefined);\n const [state, setState] = useState<Lease>();\n useEffect(() => {\n if (!owner.current || owner.current.open !== open || owner.current.disposed)\n owner.current = { open, value: open(), active: true, disposed: false };\n const lease = owner.current;\n lease.active = true;\n setState(lease);\n return () => {\n lease.active = false;\n queueMicrotask(() => {\n if (lease.active || lease.disposed) return;\n lease.disposed = true;\n if (owner.current === lease) owner.current = undefined;\n void Promise.resolve()\n .then(() => lease.value.cleanup())\n .catch((error: unknown) => console.error(\"Failed to close owned value.\", error));\n });\n };\n }, [open]);\n if (!state || state.open !== open || state.disposed) return null;\n return children(state.value);\n}\n\n/** The first empty commit installs the owner before reading its Promise can\n * suspend. A component-local lease survives StrictMode's immediate effect replay;\n * a real unmount closes it in the next microtask, including late acquisitions.\n * No render starts IO and no global resource cache retains abandoned Scopes.\n */\nexport function OwnedResource<Resource extends { cleanup(): Promise<void> }>({\n open,\n children,\n}: {\n readonly open: () => Promise<Resource>;\n readonly children: (resource: Resource) => ReactNode;\n}) {\n return (\n <OwnedResourceLease open={open}>\n {(promise) => <ResolvedProvider promise={promise}>{children}</ResolvedProvider>}\n </OwnedResourceLease>\n );\n}\n\n/** Publish the owned acquisition before it resolves so dependent openings can\n * compose their Promises without serial Suspense reveals. The lease, including\n * late completion cleanup, still belongs to this committed component.\n */\nexport function OwnedResourceLease<Resource extends { cleanup(): Promise<void> }>({\n open,\n children,\n}: {\n readonly open: () => Promise<Resource>;\n readonly children: (promise: Promise<Resource>) => ReactNode;\n}) {\n type Lease = {\n readonly open: typeof open;\n readonly promise: ReturnType<typeof open>;\n active: boolean;\n disposed: boolean;\n acquired?: Resource;\n };\n const owner = useRef<Lease | undefined>(undefined);\n const [state, setState] = useState<Lease>();\n const [failure, setFailure] = useState<{ lease: Lease; cause: unknown }>();\n useEffect(() => {\n const close = (ontology: Resource) => {\n void ontology\n .cleanup()\n .catch((error: unknown) => console.error(\"Failed to close ontology resource.\", error));\n };\n if (!owner.current || owner.current.open !== open || owner.current.disposed) {\n const lease: Lease = {\n open,\n active: true,\n disposed: false,\n promise: Promise.resolve()\n .then(open)\n .then((ontology) => {\n if (lease.disposed) close(ontology);\n else lease.acquired = ontology;\n return ontology;\n }),\n };\n // The owner reports failures even when no child consumes its lease.\n // Late failures after disposal remain observed without updating React.\n void lease.promise.catch((cause: unknown) => {\n if (lease.active && !lease.disposed) setFailure({ lease, cause });\n });\n owner.current = lease;\n }\n const lease = owner.current;\n lease.active = true;\n queueMicrotask(() => {\n if (lease.active && !lease.disposed) setState(lease);\n });\n return () => {\n lease.active = false;\n queueMicrotask(() => {\n if (lease.active || lease.disposed) return;\n lease.disposed = true;\n if (lease.acquired) close(lease.acquired);\n if (owner.current === lease) owner.current = undefined;\n });\n };\n }, [open]);\n if (!state || state.open !== open) return null;\n if (failure?.lease === state) throw failure.cause;\n return children(state.promise);\n}\nfunction ResolvedProvider<Resource>({\n promise,\n children,\n}: {\n readonly promise: Promise<Resource>;\n readonly children: (resource: Resource) => ReactNode;\n}) {\n return children(use(promise));\n}\n"],"mappings":";;;;;;;;AAQA,SAAgB,WAAiE,EAC7E,MACA,YAID;CAOC,MAAM,QAAQ,OAA0B,KAAA,CAAS;CACjD,MAAM,CAAC,OAAO,YAAY,SAAgB;CAC1C,gBAAgB;EACZ,IAAI,CAAC,MAAM,WAAW,MAAM,QAAQ,SAAS,QAAQ,MAAM,QAAQ,UAC/D,MAAM,UAAU;GAAE;GAAM,OAAO,KAAK;GAAG,QAAQ;GAAM,UAAU;EAAM;EACzE,MAAM,QAAQ,MAAM;EACpB,MAAM,SAAS;EACf,SAAS,KAAK;EACd,aAAa;GACT,MAAM,SAAS;GACf,qBAAqB;IACjB,IAAI,MAAM,UAAU,MAAM,UAAU;IACpC,MAAM,WAAW;IACjB,IAAI,MAAM,YAAY,OAAO,MAAM,UAAU,KAAA;IAC7C,QAAa,QAAQ,CAAC,CACjB,WAAW,MAAM,MAAM,QAAQ,CAAC,CAAC,CACjC,OAAO,UAAmB,QAAQ,MAAM,gCAAgC,KAAK,CAAC;GACvF,CAAC;EACL;CACJ,GAAG,CAAC,IAAI,CAAC;CACT,IAAI,CAAC,SAAS,MAAM,SAAS,QAAQ,MAAM,UAAU,OAAO;CAC5D,OAAO,SAAS,MAAM,KAAK;AAC/B;;;;;;AAOA,SAAgB,cAA6D,EACzE,MACA,YAID;CACC,OACI,oBAAC,oBAAD;EAA0B;EACpB,WAAA,YAAY,oBAAC,kBAAD;GAA2B;GAAU;EAA2B,CAAA;CAC9D,CAAA;AAE5B;;;;;AAMA,SAAgB,mBAAkE,EAC9E,MACA,YAID;CAQC,MAAM,QAAQ,OAA0B,KAAA,CAAS;CACjD,MAAM,CAAC,OAAO,YAAY,SAAgB;CAC1C,MAAM,CAAC,SAAS,cAAc,SAA2C;CACzE,gBAAgB;EACZ,MAAM,SAAS,aAAuB;GAClC,SACK,QAAQ,CAAC,CACT,OAAO,UAAmB,QAAQ,MAAM,sCAAsC,KAAK,CAAC;EAC7F;EACA,IAAI,CAAC,MAAM,WAAW,MAAM,QAAQ,SAAS,QAAQ,MAAM,QAAQ,UAAU;GACzE,MAAM,QAAe;IACjB;IACA,QAAQ;IACR,UAAU;IACV,SAAS,QAAQ,QAAQ,CAAC,CACrB,KAAK,IAAI,CAAC,CACV,MAAM,aAAa;KAChB,IAAI,MAAM,UAAU,MAAM,QAAQ;UAC7B,MAAM,WAAW;KACtB,OAAO;IACX,CAAC;GACT;GAGA,MAAW,QAAQ,OAAO,UAAmB;IACzC,IAAI,MAAM,UAAU,CAAC,MAAM,UAAU,WAAW;KAAE;KAAO;IAAM,CAAC;GACpE,CAAC;GACD,MAAM,UAAU;EACpB;EACA,MAAM,QAAQ,MAAM;EACpB,MAAM,SAAS;EACf,qBAAqB;GACjB,IAAI,MAAM,UAAU,CAAC,MAAM,UAAU,SAAS,KAAK;EACvD,CAAC;EACD,aAAa;GACT,MAAM,SAAS;GACf,qBAAqB;IACjB,IAAI,MAAM,UAAU,MAAM,UAAU;IACpC,MAAM,WAAW;IACjB,IAAI,MAAM,UAAU,MAAM,MAAM,QAAQ;IACxC,IAAI,MAAM,YAAY,OAAO,MAAM,UAAU,KAAA;GACjD,CAAC;EACL;CACJ,GAAG,CAAC,IAAI,CAAC;CACT,IAAI,CAAC,SAAS,MAAM,SAAS,MAAM,OAAO;CAC1C,IAAI,SAAS,UAAU,OAAO,MAAM,QAAQ;CAC5C,OAAO,SAAS,MAAM,OAAO;AACjC;AACA,SAAS,iBAA2B,EAChC,SACA,YAID;CACC,OAAO,SAAS,IAAI,OAAO,CAAC;AAChC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@party-stack/ontology-react",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"files": [
|
|
5
|
+
"lib"
|
|
6
|
+
],
|
|
7
|
+
"type": "module",
|
|
8
|
+
"exports": {
|
|
9
|
+
"./authentication": {
|
|
10
|
+
"types": "./lib/authentication.d.ts",
|
|
11
|
+
"default": "./lib/authentication.js"
|
|
12
|
+
},
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./lib/index.d.ts",
|
|
15
|
+
"default": "./lib/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"effect": "^4.0.5",
|
|
20
|
+
"@party-stack/db-fragments": "0.2.0",
|
|
21
|
+
"@party-stack/ontology": "0.21.0",
|
|
22
|
+
"@party-stack/ontology-backend": "0.2.0"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@babel/core": "^7.29.7",
|
|
26
|
+
"@rolldown/plugin-babel": "^0.2.4",
|
|
27
|
+
"@tanstack/db": "1.0.0-rc.1.4",
|
|
28
|
+
"@tanstack/react-db": "1.0.0-rc.1.4",
|
|
29
|
+
"@types/node": "^24.10.1",
|
|
30
|
+
"@types/react": "^19.3.0",
|
|
31
|
+
"@types/react-dom": "^19.3.0",
|
|
32
|
+
"@vitejs/plugin-react": "^6.1.2",
|
|
33
|
+
"babel-plugin-react-compiler": "^1.0.0",
|
|
34
|
+
"playwright": "^1.61.1",
|
|
35
|
+
"react": "^19.3.0",
|
|
36
|
+
"react-dom": "^19.3.0",
|
|
37
|
+
"typescript": "~7.0.2",
|
|
38
|
+
"vite": "npm:@voidzero-dev/vite-plus-core@1.2.0",
|
|
39
|
+
"vite-plus": "1.2.0",
|
|
40
|
+
"@party-stack/build-config": "0.2.0"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"@tanstack/db": "1.0.0-rc.1.4",
|
|
44
|
+
"@tanstack/react-db": "1.0.0-rc.1.4",
|
|
45
|
+
"react": "^19.2.0"
|
|
46
|
+
}
|
|
6
47
|
}
|