Compare commits

...

14 Commits

Author SHA1 Message Date
Anselm
b29ac306ea Release 2024-06-01 14:00:50 +02:00
Anselm
e8e883f4d6 Ability to add seed accounts to DemoAuth 2024-06-01 14:00:26 +02:00
Anselm
4fe14f03b4 Formatting fixes 2024-05-31 09:13:25 +02:00
Anselm
90e2a661e4 Very last fix 2024-05-31 09:00:56 +02:00
Anselm
6ed53ecb79 Last fixes 2024-05-31 08:59:41 +02:00
Anselm
c18775766c More fixes 2024-05-31 08:56:11 +02:00
Anselm
4bb3a6209a Doc fixes 2024-05-31 08:44:45 +02:00
Anselm
0f44a547a4 Lots more guide progress 2024-05-28 17:10:59 +01:00
Anselm
1e2f6d8f14 Release 2024-05-26 20:44:05 +01:00
Anselm Eickhoff
7e5b176930 Merge pull request #199 from tobiaslins/implement-loading-ui
Implement basic `loading` component that is rendered when migrations …
2024-05-26 20:40:00 +01:00
Anselm
b420eab503 Fix type for Provider 2024-05-26 20:37:55 +01:00
Tobias Lins
b370e2e14e Merge branch 'main' into implement-loading-ui 2024-05-26 20:48:38 +02:00
Tobias Lins
1fabee297d Implement basic loading component that is rendered when migrations are running or jazz not ready yet 2024-05-26 20:46:52 +02:00
Anselm
484dc460c5 Lots of doc progress 2024-05-26 17:39:16 +01:00
33 changed files with 923 additions and 261 deletions

View File

@@ -1,5 +1,19 @@
# jazz-example-chat
## 0.0.52
### Patch Changes
- Updated dependencies
- jazz-react@0.7.5
## 0.0.51
### Patch Changes
- Updated dependencies
- jazz-react@0.7.4
## 0.0.50
### Patch Changes

View File

@@ -1,7 +1,7 @@
{
"name": "jazz-example-chat",
"private": true,
"version": "0.0.50",
"version": "0.0.52",
"type": "module",
"scripts": {
"dev": "vite",

View File

@@ -1,5 +1,20 @@
# jazz-example-pets
## 0.0.70
### Patch Changes
- Updated dependencies
- jazz-react@0.7.5
- jazz-browser-media-images@0.7.5
## 0.0.69
### Patch Changes
- Updated dependencies
- jazz-react@0.7.4
## 0.0.68
### Patch Changes

View File

@@ -1,7 +1,7 @@
{
"name": "jazz-example-pets",
"private": true,
"version": "0.0.68",
"version": "0.0.70",
"type": "module",
"scripts": {
"dev": "vite",

View File

@@ -42,7 +42,7 @@ ReactDOM.createRoot(document.getElementById("root")!).render(
<ThemeProvider>
<TitleAndLogo name={appName} />
<div className="flex flex-col h-full items-center justify-start gap-10 pt-10 pb-10 px-5">
<Jazz.Provider>
<Jazz.Provider loading={<div>Loading</div>}>
<App />
</Jazz.Provider>
</div>

View File

@@ -1,5 +1,19 @@
# jazz-example-todo
## 0.0.69
### Patch Changes
- Updated dependencies
- jazz-react@0.7.5
## 0.0.68
### Patch Changes
- Updated dependencies
- jazz-react@0.7.4
## 0.0.67
### Patch Changes

View File

@@ -1,7 +1,7 @@
{
"name": "jazz-example-todo",
"private": true,
"version": "0.0.67",
"version": "0.0.69",
"type": "module",
"scripts": {
"dev": "vite",

View File

@@ -1,17 +1,18 @@
import { Slogan } from "@/components/forMdx";
import { JazzLogo } from "@/components/logos";
<h1 id="guide">Learn some Jazz.</h1>
<Slogan>Build an issue tracking app with distributed state.</Slogan>
<h1 id="guide">Learn some <JazzLogo className="h-[1.3em] relative -top-0.5 inline-block -ml-[0.1em] -mr-[0.1em]"/></h1>
<Slogan>Build an issue tracker with distributed state.</Slogan>
Our issues app will be quite simple, but it will have team collaboration. <span className="text-nowrap">**Let's call it... &ldquo;Circular.&rdquo;**</span>
We'll build everything **step-by-step,** in typical, immediately usable stages. We'll explore many important things Jazz does &mdash; so **follow along** or **just pick things out.**
<h2 id="setup">Project Setup</h2>
<h2 id="guide-setup">Project Setup</h2>
1. Create a project from a generic Vite starter template:
1. Create a project called "circular" from a generic Vite starter template:
{/* prettier-ignore */}
```bash
npx degit gardencmp/vite-ts-react-tailwind circular
cd circular
@@ -29,33 +30,36 @@ We'll build everything **step-by-step,** in typical, immediately usable stages.
<small>(in a new terminal window):</small>
{/* prettier-ignore */}
```bash
cd circular
npm install jazz-tools jazz-react
```
3. Set up a Jazz context, by modifying `src/main.tsx`:
```tsx subtle=1,2,3,4,13,15,16,17,19
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App.tsx";
import "./index.css";
import { JazzReact } from "jazz-react";
3. Modify `src/main.tsx` to set up a Jazz context:
{/* prettier-ignore */}
```tsx
import React from "react"; // old
import ReactDOM from "react-dom/client"; // old
import App from "./App.tsx"; // old
import "./index.css"; // old
import { createJazzReactContext, DemoAuth } from "jazz-react";
// old
const Jazz = createJazzReactContext({
auth: DemoAuth({ appName: "Circular" }),
peer: "wss://mesh.jazz.tools/?key=you@example.com", // <- put your email here to receive a proper API key for later
peer: "wss://mesh.jazz.tools/?key=you@example.com", // <- put your email here to get a proper API key later
});
export const { useAccount, useCoState } = Jazz;
ReactDOM.createRoot(document.getElementById("root")!).render(
// old
ReactDOM.createRoot(document.getElementById("root")!).render( // old
<Jazz.Provider>
<React.StrictMode>
<App />
</React.StrictMode>
<React.StrictMode> // old
{" "}// old
<App /> // old
</React.StrictMode>{" "}// old
</Jazz.Provider>,
);
); // old
```
This sets Jazz up, extracts app-specific hooks for later, and wraps our app in the provider.
@@ -75,7 +79,7 @@ We can
- **edit** CoValues, from anywhere, by mutating them like local state
- **subscribe to edits** in CoValues, whether they're local or remote
<h3 id="first-schema">Declaring our own CoValues</h3>
<h3 id="declaring-covalues">Declaring our own CoValues</h3>
To make our own CoValues, we first need to declare a schema for them. Think of a schema as a combination of TypeScript types and runtime type information.
@@ -96,16 +100,17 @@ export class Issue extends CoMap {
TODO: explain what's happening
<h3>Reading from CoValues</h3>
<h3 id="reading-covalues">Reading from CoValues</h3>
CoValues are designed to be read like simple local JSON state. Let's see how we can read from an Issue by building a component to render one.
Create a new file `src/components/Issue.tsx` and add the following:
{/* prettier-ignore */}
```tsx
import { Issue } from "../schema";
export function IssueComponent({ issue }, { issue: Issue }) {
export function IssueComponent({ issue }: { issue: Issue }) {
return (
<div className="grid grid-cols-6 text-sm border-r border-b [&>*]:p-2 [&>*]:border-l [&>*]:border-t">
<h2>{issue.title}</h2>
@@ -119,42 +124,44 @@ export function IssueComponent({ issue }, { issue: Issue }) {
Simple enough!
<h3>Creating CoValues</h3>
<h3 id="creating-covalues">Creating CoValues</h3>
To actually see an Issue, we have to create one. This is where things start to get interesting...
Let's modify `src/App.tsx` to prepare for creating an Issue and then rendering it:
```tsx subtle=5,13,14,15
import { useState, useCallback } from "react";
{/* prettier-ignore */}
```tsx
import { useState } from "react";
import { Issue } from "./schema";
import { IssueComponent } from "./components/Issue";
function App() {
import { IssueComponent } from "./components/Issue.tsx";
// old
function App() {// old
const [issue, setIssue] = useState<Issue>();
// old
if (issue) {
return <IssueComponent issue={issue} />;
} else {
return <button>Create Issue</button>;
}
}
export default App;
} // old
// old
export default App; // old
```
Now, finally, let's implement creating an issue:
```tsx subtle=1,2,3,5,6,8,23,24,25,27,28,29,30
import { useState } from "react";
import { Issue } from "./schema";
import { IssueComponent } from "./components/Issue";
{/* prettier-ignore */}
```tsx
import { useState } from "react"; // old
import { Issue } from "./schema"; // old
import { IssueComponent } from "./components/Issue.tsx"; // old
import { useAccount } from "./main";
function App() {
// old
function App() {// old
const { me } = useAccount();
const [issue, setIssue] = useState<Issue>();
const [issue, setIssue] = useState<Issue>(); // old
// old
const createIssue = () => {
const newIssue = Issue.create(
{
@@ -167,20 +174,20 @@ function App() {
);
setIssue(newIssue);
};
if (issue) {
return <IssueComponent issue={issue} />;
} else {
return <button onClick={createIssue}>Create Issue</button>;
}
}
export default App;
// old
if (issue) {// old
return <IssueComponent issue={issue} />; // old
} else { // old
return <button onClick={createIssue}>Create Issue</button>; // old
} // old
} // old
// old
export default App; // old
```
Now you should be able to create a new issue by clicking the button and then see it rendered.
🏁 Now you should be able to create a new issue by clicking the button and then see it rendered!
<div className="text-xs uppercase text-stone-400 dark:text-stone-600 tracking-wider">
<div className="text-xs uppercase text-stone-400 dark:text-stone-600 tracking-wider -mb-3">
Preview
</div>
<div className="p-3 md:-mx-3 rounded border border-stone-100 dark:border-stone-900 bg-white dark:bg-black not-prose">
@@ -202,7 +209,7 @@ We'll already notice one interesting thing here:
We'll make use of both of these facts in a bit, but for now let's start with local editing and subscribing.
<h3>Editing CoValues and subscribing to edits</h3>
<h3 id="editing-and-subscription">Editing CoValues and subscribing to edits</h3>
Since we're the owner of the CoValue, we should be able to edit it, right?
@@ -216,49 +223,52 @@ This is exactly what the `useCoState` hook is for!
Let's modify `src/App.tsx`:
```tsx subtle=1,2,3,4,5,6,7,12,13,14,15,16,17,18,19,20,21,23,25,26,27,28,29,30,32
import { useState } from "react";
import { Issue } from "./schema";
import { IssueComponent } from "./components/Issue";
import { useAccount } from "./main";
function App() {
const { me } = useAccount();
{/* prettier-ignore */}
```tsx
import { useState } from "react"; // old
import { Issue } from "./schema"; // old
import { IssueComponent } from "./components/Issue.tsx"; // old
import { useAccount, useCoState } from "./main";
import { ID } from "jazz-tools"
// old
function App() { // old
const { me } = useAccount(); // old
const [issueID, setIssueID] = useState<ID<Issue>>();
// old
const issue = useCoState(Issue, issueID);
const createIssue = () => {
const newIssue = Issue.create(
{
title: "Buy terrarium",
description: "Make sure it's big enough for 10 snails.",
estimate: 5,
status: "backlog",
},
{ owner: me },
);
// old
const createIssue = () => {// old
const newIssue = Issue.create(// old
{ // old
title: "Buy terrarium", // old
description: "Make sure it's big enough for 10 snails.", // old
estimate: 5, // old
status: "backlog", // old
}, // old
{ owner: me }, // old
); // old
setIssueID(newIssue.id);
};
if (issue) {
return <IssueComponent issue={issue} />;
} else {
return <button onClick={createIssue}>Create Issue</button>;
}
}
export default App;
}; // old
// old
if (issue) { // old
return <IssueComponent issue={issue} />; // old
} else { // old
return <button onClick={createIssue}>Create Issue</button>; // old
} // old
} // old
// old
export default App; // old
```
And now for the exciting part! Let's make `src/components/Issue.tsx` an editing component.
```tsx subtle=1,3,4,5,28,29,30,31
import { Issue } from "../schema";
export function IssueComponent({ issue }, { issue: Issue }) {
return (
<div className="grid grid-cols-6 text-sm border-r border-b [&>*]:p-2 [&>*]:border-l [&>*]:border-t">
{/* prettier-ignore */}
```tsx
import { Issue } from "../schema"; // old
// old
export function IssueComponent({ issue }: { issue: Issue }) { // old
return ( // old
<div className="grid grid-cols-6 text-sm border-r border-b [&>*]:p-2 [&>*]:border-l [&>*]:border-t"> // old
<input type="text"
value={issue.title}
onChange={(event) => { issue.title = event.target.value }}/>
@@ -279,14 +289,14 @@ export function IssueComponent({ issue }, { issue: Issue }) {
>
<option value="backlog">Backlog</option>
<option value="in progress">In Progress</option>
<option value="done">Done</options>
<option value="done">Done</option>
</select>
</div>
);
}
</div> // old
); // old
} // old
```
<div className="text-xs uppercase text-stone-400 dark:text-stone-600 tracking-wider">
<div className="text-xs uppercase text-stone-400 dark:text-stone-600 tracking-wider -mb-3">
Preview
</div>
<div className="p-3 md:-mx-3 rounded border border-stone-100 dark:border-stone-900 bg-white dark:bg-black not-prose">
@@ -309,9 +319,15 @@ export function IssueComponent({ issue }, { issue: Issue }) {
</div>
</div>
🏁 Now you should be able to edit the issue after creating it!
You'll immediately notice that we're doing something non-idiomatic for React: we mutate the issue directly, by assigning to its properties.
This works because CoValues intercept these edits, update their local view accordingly (React doesn't really care after rendering) and then notify subscribers of the change, who will receive a fresh, updated view of the CoValue.
This works because CoValues
- intercept these edits
- update their local view accordingly (React doesn't really care after rendering)
- notify subscribers of the change (who will receive a fresh, updated view of the CoValue)
<aside className="text-sm border border-stone-300 dark:border-stone-700 rounded px-4 my-4 max-w-3xl [&_pre]:mx-0">
<h4 className="not-prose text-base py-2 mb-3 -mx-4 px-4 border-b border-stone-300 dark:border-stone-700">💡 A Quick Overview of Subscribing to CoValues</h4>
@@ -321,13 +337,13 @@ This works because CoValues intercept these edits, update their local view accor
1. Directly on an instance:
```ts
const unsub = issue.subscribe((updatedIssue) => console.log(updatedIssue));
const unsub = issue.subscribe([], (updatedIssue) => console.log(updatedIssue));
```
2. If you only have an ID (this will load the issue if needed):
```ts
const unsub = Issue.subscribe(issueID, { as: me }, (updatedIssue) => {
const unsub = Issue.subscribe(issueID, me, [], (updatedIssue) => {
console.log(updatedIssue);
});
```
@@ -344,7 +360,7 @@ This works because CoValues intercept these edits, update their local view accor
const { me } = useAccount();
const [value, setValue] = useState<V>();
useEffect(() => Schema.subscribe(id, { as: me }, setValue), [id]);
useEffect(() => Schema.subscribe(id, me, [], setValue), [id]);
return value;
}
@@ -354,62 +370,290 @@ This works because CoValues intercept these edits, update their local view accor
We have one subscriber on our Issue, with `useCoState` in `src/App.tsx`, which will cause the `App` component and its children **to** re-render whenever the Issue changes.
<h3>Automatic local & cloud persistence</h3>
<h3 id="persistence">Automatic local & cloud persistence</h3>
So far our Issue CoValues just looked like ephemeral local state. We'll now start exploring the first main feature that makes CoValues special: **automatic persistence.**
Actually, all the Issue CoValues we've created so far **have already been automatically persisted** to the cloud and locally - but we loose track of their ID after a reload.
So let's store the ID in URL state and make sure our useState is in sync with that.
So let's store the ID in window URL state and make sure our useState is in sync with that.
```tsx subtle=1,2,3,4,5,6,7,12,13,14,15,16,17,18,19,20,21,22,23,24,26,27,28,29,30,31,32,33,34,35
import { useState } from "react";
import { Issue } from "./schema";
import { IssueComponent } from "./components/Issue";
import { useAccount } from "./main";
function App() {
const { me } = useAccount();
const [issueID, setIssueID] = useState<ID<Issue>>(
window.location.search?.replace("?issue=", "") || undefined
{/* prettier-ignore */}
```tsx
import { useState } from "react"; // old
import { Issue } from "./schema"; // old
import { IssueComponent } from "./components/Issue.tsx"; // old
import { useAccount, useCoState } from "./main"; // old
import { ID } from "jazz-tools" // old
// old
function App() { // old
const { me } = useAccount(); // old
const [issueID, setIssueID] = useState<ID<Issue> | undefined>(
(window.location.search?.replace("?issue=", "") || undefined) as ID<Issue> | undefined,
);
const issue = useCoState(Issue, issueID);
const createIssue = () => {
const newIssue = Issue.create(
{
title: "Buy terrarium",
description: "Make sure it's big enough for 10 snails.",
estimate: 5,
status: "backlog",
},
{ owner: me },
);
setIssueID(newIssue.id);
// old
const issue = useCoState(Issue, issueID); // old
// old
const createIssue = () => {// old
const newIssue = Issue.create(// old
{ // old
title: "Buy terrarium", // old
description: "Make sure it's big enough for 10 snails.", // old
estimate: 5, // old
status: "backlog", // old
}, // old
{ owner: me }, // old
); // old
setIssueID(newIssue.id); // old
window.history.pushState({}, "", `?issue=${newIssue.id}`);
};
if (issue) {
return <IssueComponent issue={issue} />;
} else {
return <button onClick={createIssue}>Create Issue</button>;
}
}
export default App;
}; // old
// old
if (issue) { // old
return <IssueComponent issue={issue} />; // old
} else { // old
return <button onClick={createIssue}>Create Issue</button>; // old
} // old
} // old
// old
export default App; // old
```
Now you should be able to create an issue, reload the page, and still see the same issue.
🏁 Now you should be able to create an issue, edit it, reload the page, and still see the same issue.
<h3 id="remote-sync">Remote sync</h3>
<h3>Remote sync</h3>
To see that sync is also already working, try the following:
But even better, you should be able to:
- copy the URL to a new tab in the same browser window and see the same issue
- edit the issue and see the changes reflected in the other tab!
- copy the URL to a new tab and see the same issue
- edit the issue and see the changes reflected in the other tab
This works because we load the issue as the same account that created it and owns it (remember setting `{ owner: me }`?).
We'll learn more about access control in "Groups & Permissions", but for now let's build a super simple way of sharing an Issue by just making it publicly readable & writable.
All we have to do is create a new group to own each new issue and add "everyone" as a "writer":
{/* prettier-ignore */}
```tsx
import { useState } from "react"; // old
import { Issue } from "./schema"; // old
import { IssueComponent } from "./components/Issue.tsx"; // old
import { useAccount, useCoState } from "./main"; // old
import { ID, Group } from "jazz-tools"
// old
function App() { // old
const { me } = useAccount(); // old
const [issueID, setIssueID] = useState<ID<Issue> | undefined>(// old
(window.location.search?.replace("?issue=", "") || undefined) as ID<Issue> | undefined,// old
); // old
// old
const issue = useCoState(Issue, issueID); // old
// old
const createIssue = () => { // old
const group = Group.create({ owner: me });
group.addMember("everyone", "writer");
// old
const newIssue = Issue.create( // old
{ // old
title: "Buy terrarium", // old
description: "Make sure it's big enough for 10 snails.", // old
estimate: 5, // old
status: "backlog", // old
}, // old
{ owner: group },
); // old
setIssueID(newIssue.id); // old
window.history.pushState({}, "", `?issue=${newIssue.id}`); // old
}; // old
// old
if (issue) { // old
return <IssueComponent issue={issue} />; // old
} else { // old
return <button onClick={createIssue}>Create Issue</button>; // old
} // old
} // old
// old
export default App; // old
```
🏁 Now you should be able to open the Issue (with its unique URL) on another device or browser, or send it to a friend and you should be able to **edit it together in realtime!**
This concludes our intro to the essence of CoValues. Hopefully you're starting to have a feeling for how CoValues behave and how they're magically available everywhere.
<h2 id="refs-and-on-demand-subscribe">Refs & Auto-Subscribe</h2>
Now let's have a look at how to compose CoValues into more complex structures and build a whole app around them.
Let's extend our two data model to include "Projects" which have a list of tasks and some properties of their own.
Using plain objects, you would probably type a Project like this:
```ts
type Project = {
name: string;
issues: Issue[];
};
```
In order to create this more complex structure in a fully collaborative way, we're going to need _references_ that allow us to nest or link CoValues.
Add the following to `src/schema.ts`:
```ts
import { CoMap, CoList, co } from "jazz-tools";
// old
export class Issue extends CoMap { // old
title = co.string; // old
description = co.string; // old
estimate = co.number; // old
status? = co.literal("backlog", "in progress", "done"); // old
} // old
// old
export class ListOfIssues extends CoList.Of(co.ref(Issue)) {}
export class Project extends CoMap {
name = co.string;
issues = co.ref(ListOfIssues);
}
```
Now let's change things up a bit in terms of components as well.
First, we'll change `App.tsx` to create and render `Project`s instead of `Issue`s. (We'll move the `useCoState` into the `ProjectComponent` we'll create in a second).
{/* prettier-ignore */}
```tsx
import { useState } from "react"; // old
import { Project, ListOfIssues } from "./schema";
import { ProjectComponent } from "./components/Project.tsx";
import { useAccount } from "./main";
import { ID, Group } from "jazz-tools"
// old
function App() { // old
const { me } = useAccount(); // old
const [projectID, setProjectID] = useState<ID<Project> | undefined>(
(window.location.search?.replace("?project=", "") || undefined) as ID<Project> | undefined,// old
);
// old
const issue = useCoState(Issue, issueID); // *bin*
// old
const createProject = () => {
const group = Group.create({ owner: me });
group.addMember("everyone", "writer");
const newProject = Project.create(
{
name: "New Project",
issues: ListOfIssues.create([], { owner: group })
},
{ owner: group },
);
setProjectID(newProject.id);
window.history.pushState({}, "", `?project=${newProject.id}`);
};
// old
if (projectID) {
return <ProjectComponent projectID={projectID} />;
} else {
return <button onClick={createProject}>Create Project</button>;
}
} // old
// old
export default App; // old
```
Now we'll actually create the `ProjectComponent` that renders a `Project` and its `Issue`s.
Create a new file `src/components/Project.tsx` and add the following:
{/* prettier-ignore */}
```tsx
import { ID } from "jazz-tools";
import { Project, Issue } from "../schema";
import { IssueComponent } from "./Issue.tsx";
import { useCoState } from "../main";
export function ProjectComponent({ projectID }: { projectID: ID<Project> }) {
const project = useCoState(Project, projectID);
const createAndAddIssue = () => {
project?.issues?.push(Issue.create({
title: "",
description: "",
estimate: 0,
status: "backlog",
}, { owner: project._owner }));
};
return project ? (
<div>
<h1>{project.name}</h1>
<div className="border-r border-b">
{project.issues?.map((issue) => (
issue && <IssueComponent key={issue.id} issue={issue} />
))}
<button onClick={createAndAddIssue}>Create Issue</button>
</div>
</div>
) : (
<div>Loading project...</div>
);
}
```
🏁 Now you should be able to create a project, add issues to it, share it, and edit it collaboratively!
Two things to note here:
- We create a new Issue like before, and then push it into the `issues` list of the Project. By setting the `owner` to the Project's owner, we ensure that the Issue has the same access rights as the project itself.
- We only need to use `useCoState` on the Project, and the nested `ListOfIssues` and each `Issue` will be **automatically loaded and subscribed to when we access them.**
- However, because either the `Project`, `ListOfIssues`, or each `Issue` might not be loaded yet, we have to check for them being defined.
The load-and-subscribe-on-access is a convenient way to have your rendering drive data loading (including in nested components!) and lets you quickly chuck UIs together without worrying too much about the shape of all data you'll need.
But you can also take more precise control over loading by defining a minimum-depth to load in `useCoState`:
{/* prettier-ignore */}
```tsx
import { ID } from "jazz-tools";// old
import { Project, Issue } from "../schema"; // old
import { IssueComponent } from "./Issue.tsx"; // old
import { useCoState } from "../main"; // old
// old
export function ProjectComponent({ projectID }: { projectID: ID<Project> }) {// old
const project = useCoState(Project, projectID, { issues: [{}] });
const createAndAddIssue = () => {// old
project?.issues.push(Issue.create({
title: "",// old
description: "",// old
estimate: 0,// old
status: "backlog",// old
}, { owner: project._owner }));// old
};// old
// old
return project ? (// old
<div>// old
<h1>{project.name}</h1>// old
<div className="border-r border-b">// old
{project.issues.map((issue) => (
<IssueComponent key={issue.id} issue={issue} />
))}// old
<button onClick={createAndAddIssue}>Create Issue</button>// old
</div>// old
</div>// old
) : (// old
<div>Loading project...</div>// old
);// old
}// old
```
The loading-depth spec `{ issues: [{}] }` means "in `Project`, load `issues` and load each item in `issues` shallowly". (Since an `Issue` doesn't have any further references, "shallowly" actually means all its properties will be available).
- Now, we can get rid of a lot of coniditional accesses because we know that once `project` is loaded, `project.issues` and each `Issue` in it will be loaded as well.
- This also results in only one rerender and visual update when everything is loaded, which is faster (especially for long lists) and gives you more control over the loading UX.
TODO: explainer about not loaded vs not set/defined and `_refs` basics
<div className="text-amber-500 mt-52">
🚧 OH NO - This is as far as we've written the Guide. 🚧
@@ -417,10 +661,10 @@ But even better, you should be able to:
{" -> "}
<a href="https://github.com/gardencmp/jazz/issues/186">Complain on GitHub</a>
<h2 id="refs-and-on-demand-subscribe">Refs & Auto-Subscribe</h2>
<h2 id="groups-and-permissions">Groups & Permissions</h2>
<h2 id="accounts-and-migrations">Accounts & Migrations</h2>
<h2 id="auth-accounts-and-migrations">Auth, Accounts & Migrations</h2>
<h2 id="backend-workers">Backend Workers</h2>
<h2 id="edits-and-time-travel">Edit Metadata & Time Travel</h2>
<h2 id="backend-workers">Backend Workers</h2>

View File

@@ -1,6 +1,7 @@
import { DocNav } from "@/components/docs/nav";
import { PackageDocs } from "@/components/docs/packageDocs";
import Guide from "./guide.mdx";
import { Prose } from "@/components/forMdx";
export const metadata = {
title: "jazz - Docs",
@@ -10,44 +11,50 @@ export const metadata = {
export default function Page() {
return (
<>
<div className="hidden md:block bg-stone-100 dark:bg-stone-900 p-4 rounded-xl sticky overflow-y-scroll overscroll-contain w-[16rem] h-[calc(100dvh-8rem)] -mb-[calc(100dvh-8rem)] top-[6rem] mr-10 prose-sm prose-ul:pl-1 prose-ul:ml-1 prose-li:my-2 prose-li:leading-tight prose-ul:list-['-']">
<div className="hidden md:block bg-stone-100 dark:bg-stone-900 text-stone-700 dark:text-stone-300 p-4 rounded-xl sticky overflow-y-scroll overscroll-contain w-[16rem] h-[calc(100dvh-8rem)] -mb-[calc(100dvh-8rem)] top-[6rem] mr-10 prose-sm prose-ul:pl-1 prose-ul:ml-1 prose-li:my-2 prose-li:leading-tight prose-ul:list-['-']">
<DocNav />
</div>
<div className="md:ml-[20rem]">
<Guide />
<div className="md:ml-[20rem] text-base">
<Prose className="prose">
<Guide />
<h1 id="faq">FAQ</h1>
<p>
<span className="text-amber-500">
🚧 OH NO - We don&apos;t have any FAQ yet. 🚧
</span>{" "}
{"->"}{" "}
<a href="https://github.com/gardencmp/jazz/issues/187">
Complain on GitHub
</a>
</p>
<div className="xl:-mr-[calc(50vw-40rem)]">
<h1>API Reference</h1>
<h1 id="faq">FAQ</h1>
<p>
<span className="text-amber-500">
🚧 OH NO - These docs are still highly
work-in-progress. 🚧
🚧 OH NO - We don&apos;t have any FAQ yet. 🚧
</span>{" "}
{"->"}{" "}
<a href="https://github.com/gardencmp/jazz/issues/188">
<a href="https://github.com/gardencmp/jazz/issues/187">
Complain on GitHub
</a>
</p>
</Prose>
<div className="xl:-mr-[calc(50vw-40rem)]">
<Prose>
<h1>API Reference</h1>
<p>
<span className="text-amber-500">
🚧 OH NO - These docs are still highly
work-in-progress. 🚧
</span>{" "}
{"->"}{" "}
<a href="https://github.com/gardencmp/jazz/issues/188">
Complain on GitHub
</a>
</p>
</Prose>
<div className="text-stone-800 dark:text-stone-200">
<PackageDocs package="jazz-tools" />
<PackageDocs package="jazz-react" />
<PackageDocs package="jazz-browser" />
<PackageDocs package="jazz-browser-media-images" />
<PackageDocs package="jazz-nodejs" />
</div>
</div>
</div>
</>

View File

@@ -394,12 +394,12 @@ body {
--shiki-color-text: #606060;
--shiki-color-background: transparent;
--shiki-token-constant: #00a5a5;
--shiki-token-string: #1aa245;
--shiki-token-string: #4e3a2c;
--shiki-token-comment: #aaa;
--shiki-token-keyword: #7b8bff;
--shiki-token-parameter: #ff9800;
--shiki-token-function: #445dd7;
--shiki-token-string-expression: #1aa245;
--shiki-token-string-expression: #38a35f;
--shiki-token-punctuation: #969696;
--shiki-token-link: #1aa245;
}
@@ -407,7 +407,7 @@ body {
.dark body {
--shiki-color-text: #d1d1d1;
--shiki-token-constant: #2dc9c9;
--shiki-token-string: #ffab70;
--shiki-token-string: #feb179;
--shiki-token-comment: #6b737c;
--shiki-token-keyword: #7b8bff;
--shiki-token-parameter: #ff9800;

View File

@@ -101,22 +101,7 @@ export default function RootLayout({
docNav={<DocNav />}
/>
<main className="flex min-h-screen flex-col p-8 max-w-[80rem] w-full [&_*]:scroll-mt-[6rem]">
<article
className={[
"prose lg:prose-lg max-w-none prose-stone dark:prose-invert",
"prose-headings:font-display",
"prose-h1:text-5xl lg:prose-h1:text-6xl prose-h1:font-medium prose-h1:tracking-tighter",
"prose-h2:text-2xl lg:prose-h2:text-3xl prose-h2:font-medium prose-h2:tracking-tight",
"prose-p:max-w-3xl prose-p:leading-snug",
"prose-strong:font-medium",
"prose-code:font-normal prose-code:leading-tight prose-code:before:content-none prose-code:after:content-none prose-code:bg-stone-100 prose-code:dark:bg-stone-900 prose-code:p-1 prose-code:-my-1 prose-code:rounded",
"prose-pre:max-w-3xl prose-pre:text-[0.8em] prose-pre:leading-[1.3] prose-pre:-mt-4 prose-pre:my-4 prose-pre:px-3 prose-pre:py-2 md:prose-pre:-mx-3 prose-pre:bg-stone-100 dark:prose-pre:bg-stone-900",
"prose-inner-code:font-normal prose-inner-code:text-[1em]",
].join(" ")}
>
{children}
</article>
{children}
</main>
<footer className="flex z-10 mt-10 min-h-[15rem] -mb-20 bg-stone-100 dark:bg-stone-900 text-stone-600 dark:text-stone-400 w-full justify-center">
<div className="p-8 max-w-[80rem] w-full grid grid-cols-3 md:grid-cols-4 lg:grid-cols-7 gap-8 max-sm:mb-12">

View File

@@ -4,6 +4,7 @@ import {
GridCard,
GridItem,
ComingSoonBadge,
Prose,
} from "@/components/forMdx";
export const metadata = {
@@ -13,6 +14,8 @@ export const metadata = {
<div className="md:pt-20" />
<Prose>
# Sync & Storage Mesh
<Slogan>The first Collaboration Delivery Network.</Slogan>
@@ -251,3 +254,5 @@ Costs:
</div>
</GridCard>
</Grid>
</Prose>

View File

@@ -7,6 +7,7 @@ import {
MultiplayerIcon,
ResponsiveIframe,
ComingSoonBadge,
Prose
} from "@/components/forMdx";
import { JazzLogo, LocalFirstConfLogo } from "@/components/logos";
import {
@@ -25,11 +26,14 @@ import Link from "next/link";
<div className="md:pt-20" />
<Prose>
<a href="https://app.localfirstconf.com/schedule/conference/every-app-secretly-wants-to-be-local-first" className="-mt-8 md:-mt-20 float-right top-[5rem] right-4 border border-stone-700 dark:border-stone-300 rounded flex gap-3 items-center px-4 py-2 mb-4 rotate-2 md:rotate-6 no-underline hover:scale-105 transition-transform">
<div className="text-sm font-bold uppercase">See you in Berlin<br/>May 30-31!</div>
<LocalFirstConfLogo className="w-24"/>
</a>
# Instant sync.
<Slogan>A new way to build apps with distributed state.</Slogan>
@@ -276,3 +280,5 @@ Jazz Mesh is currently free &mdash; and it's set up as the default sync & storag
- <Link href="https://discord.gg/utDMjHYg42" target="_blank">
Join our Discord
</Link>
</Prose>

View File

@@ -6,7 +6,7 @@ import { PackageIcon } from "lucide-react";
export function DocNav() {
return (
<>
<p className="mt-0 not-prose font-medium">
<p className="mt-0 font-medium">
<DocNavLink href="#guide">Guide</DocNavLink>
</p>
@@ -20,38 +20,38 @@ export function DocNav() {
</DocNavLink>
<ul>
<li>
<DocNavLink href="#intro-to-covalues">
<DocNavLink href="#declaring-covalues">
Declaration
</DocNavLink>
</li>
<li>
<DocNavLink href="#intro-to-covalues">
<DocNavLink href="#reading-covalues">
Reading
</DocNavLink>
</li>
<li>
<DocNavLink href="#intro-to-covalues">
<DocNavLink href="#creating-covalues">
Creation
</DocNavLink>
</li>
<li>
<DocNavLink href="#intro-to-covalues">
<DocNavLink href="#editing-and-subscription">
Editing & Subscription
</DocNavLink>
</li>
<li>
<DocNavLink href="#intro-to-covalues">
<DocNavLink href="#persistence">
Persistence
</DocNavLink>
</li>
<li>
<DocNavLink href="#intro-to-covalues">
<DocNavLink href="#remote-sync">
Remote Sync
</DocNavLink>
</li>
<li>
<DocNavLink href="#intro-to-covalues">
Public Sharing
<DocNavLink href="#simple-public-sharing">
Simple Public Sharing
</DocNavLink>
</li>
</ul>
@@ -67,8 +67,13 @@ export function DocNav() {
</DocNavLink>
</li>
<li>
<DocNavLink href="#accounts-and-migrations">
Accounts & Migrations
<DocNavLink href="#auth-accounts-and-migrations">
Auth, Accounts & Migrations
</DocNavLink>
</li>
<li>
<DocNavLink href="#edits-and-time-travel">
Edit Metadata & Time Travel
</DocNavLink>
</li>
<li>
@@ -100,7 +105,7 @@ export async function NavPackage({
return (
<>
<h2 className="text-sm not-prose mt-4 flex gap-1 items-center -mx-4 px-4 pt-4 border-t border-stone-200 dark:border-stone-800 ">
<h2 className="text-sm mt-4 flex gap-1 items-center -mx-4 px-4 pt-4 border-t border-stone-200 dark:border-stone-800 ">
<code className="font-bold">{packageName}</code>{" "}
<PackageIcon size={15} strokeWidth={1.5} />
</h2>
@@ -123,7 +128,7 @@ export async function NavPackage({
<>
<a
key={child.id}
className="inline-block not-prose px-1 m-0.5 bg-stone-200 dark:bg-stone-800 rounded opacity-70 hover:opacity-100 cursor-pointer"
className="text-sm inline-block px-2 m-0.5 text-stone-800 dark:text-stone-200 bg-stone-200 dark:bg-stone-800 rounded opacity-70 hover:opacity-100 cursor-pointer"
href={`#${packageName}/${child.name}`}
>
<code>{child.name}</code>
@@ -150,7 +155,7 @@ export function DocNavLink({
return (
<a
href={href}
className="not-prose hover:text-black dark:hover:text-white"
className="hover:text-black dark:hover:text-white py-1 hover:transition-colors"
>
{children}
</a>

View File

@@ -213,7 +213,10 @@ function RenderClassOrInterface({
function renderSummary(commentSummary: CommentDisplayPart[] | undefined) {
return commentSummary?.map((part, idx) =>
part.kind === "text" ? (
<span key={idx}>{part.text}</span>
<span key={idx}>{part.text.split("\n").map((line, i, lines) => <>
{line}
{i !== lines.length - 1 && <br />}
</>)}</span>
) : part.kind === "inline-tag" ? (
<code key={idx}>
{part.tag} {part.text}

View File

@@ -1,3 +1,24 @@
export function Prose(props: { children: ReactNode, className?: string }) {
return (
<div
className={[
"max-w-none prose-stone dark:prose-invert",
"prose-headings:font-display",
"prose-h1:text-5xl lg:prose-h1:text-6xl prose-h1:font-medium prose-h1:tracking-tighter",
"prose-h2:text-2xl lg:prose-h2:text-3xl prose-h2:font-medium prose-h2:tracking-tight",
"prose-p:max-w-3xl prose-p:leading-snug",
"prose-strong:font-medium",
"prose-code:font-normal prose-code:leading-tight prose-code:before:content-none prose-code:after:content-none prose-code:bg-stone-100 prose-code:dark:bg-stone-900 prose-code:p-1 prose-code:rounded",
"prose-pre:text-black dark:prose-pre:text-white prose-pre:max-w-3xl prose-pre:text-[0.8em] prose-pre:leading-[1.3] prose-pre:-mt-2 prose-pre:my-4 prose-pre:px-10 prose-pre:py-2 prose-pre:-mx-10 prose-pre:bg-transparent",
"[&_pre_.line]:relative [&_pre_.line]:min-h-[1.3em] [&_pre_.lineNo]:text-[0.75em] [&_pre_.lineNo]:text-stone-300 [&_pre_.lineNo]:dark:text-stone-700 [&_pre_.lineNo]:absolute [&_pre_.lineNo]:text-right [&_pre_.lineNo]:w-8 [&_pre_.lineNo]:-left-10 [&_pre_.lineNo]:top-[0.3em] [&_pre_.lineNo]:select-none",
props.className || "prose lg:prose-lg"
].join(" ")}
>
{props.children}
</div>
);
}
export function Slogan(props: { children: ReactNode; small?: boolean }) {
return (
<div

View File

@@ -41,21 +41,31 @@ function highlightPlugin() {
"css-variables",
);
// match a meta tag like `subtle=0,1,2,3` and parse out the line numbers
const subtleTag = node.meta && node.meta.match(/subtle=\S+/);
const subtle =
subtleTag && subtleTag[0].split("=")[1].split(",").map(Number);
let lineNo = -1;
node.type = "html";
node.value = `<pre><code class="not-prose">${lines
.map((line, lineI) =>
line
.map(
(token) =>
`<span style="color: ${token.color};${subtle?.includes(lineI + 1) ? "opacity: 0.3;" : ""}">${escape(token.content)}</span>`,
)
.join(""),
)
.map((line) => {
const isSubduedLine = line.some((token) =>
token.content.includes("// old"),
);
const isBinnedLine = line.some((token) =>
token.content.includes("// *bin*"),
);
if (!isBinnedLine) {
lineNo++;
}
return (
`<span class="line" style="${isBinnedLine ? "opacity: 0.3; text-decoration: line-through; user-select: none" : ""}"><div class="lineNo" style="${isSubduedLine ? "opacity: 0.3;" : ""}${isBinnedLine ? "color: red;" : ""}">${node.lang === "bash" ? ">" : isBinnedLine ? "✕" : (lineNo + 1)}</div>` +
line
.map(
(token) =>
`<span style="color: ${isBinnedLine ? "red" : token.color};${isSubduedLine ? "opacity: 0.3;" : ""}">${escape(token.content.replace("// old", "").replace("// *bin*", ""))}</span>`,
)
.join("") +
"</span>"
);
})
.join("\n")}</code></pre>`;
node.children = [];
return SKIP;

View File

@@ -95,9 +95,6 @@ const config: Config = {
plugins: [
tailwindCSSAnimate,
typography(),
typography({
className: "prose-inner",
}),
],
};
export default config;

View File

@@ -1,5 +1,12 @@
# jazz-browser-media-images
## 0.7.5
### Patch Changes
- Updated dependencies
- jazz-browser@0.7.5
## 0.7.3
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "jazz-browser-media-images",
"version": "0.7.3",
"version": "0.7.5",
"type": "module",
"main": "dist/index.js",
"types": "src/index.ts",

View File

@@ -1,5 +1,11 @@
# jazz-browser
## 0.7.5
### Patch Changes
- Ability to add seed accounts to DemoAuth
## 0.7.3
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "jazz-browser",
"version": "0.7.3",
"version": "0.7.5",
"type": "module",
"main": "dist/index.js",
"types": "src/index.ts",

View File

@@ -15,7 +15,27 @@ export class BrowserDemoAuth<Acc extends Account> implements AuthProvider<Acc> {
public accountSchema: CoValueClass<Acc> & typeof Account,
public driver: BrowserDemoAuth.Driver,
public appName: string,
) {}
seedAccounts?: {
[name: string]: {
accountID: ID<Account>;
accountSecret: AgentSecret;
};
},
) {
for (const [name, credentials] of Object.entries(seedAccounts || {})) {
const storageData = JSON.stringify(
credentials satisfies StorageData,
);
if (!(localStorage["demo-auth-existing-users"]?.split(",") as string[] | undefined)?.includes(name)) {
localStorage["demo-auth-existing-users"] = localStorage[
"demo-auth-existing-users"
]
? localStorage["demo-auth-existing-users"] + "," + name
: name;
}
localStorage["demo-auth-existing-users-" + name] = storageData;
}
}
async createOrLoadAccount(
getSessionFor: SessionProvider,

View File

@@ -1,5 +1,19 @@
# jazz-react
## 0.7.5
### Patch Changes
- Ability to add seed accounts to DemoAuth
- Updated dependencies
- jazz-browser@0.7.5
## 0.7.4
### Patch Changes
- Expose auth loading state in a simple way
## 0.7.3
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "jazz-react",
"version": "0.7.3",
"version": "0.7.5",
"type": "module",
"main": "dist/index.js",
"types": "src/index.ts",

View File

@@ -1,7 +1,8 @@
import { ReactNode, useMemo, useState } from "react";
import { ReactNode, useEffect, useMemo, useState } from "react";
import { BrowserDemoAuth } from "jazz-browser";
import { Account, CoValueClass } from "jazz-tools";
import { Account, CoValueClass, ID } from "jazz-tools";
import { ReactAuthHook } from "./auth.js";
import { AgentSecret } from "cojson";
/** @category Auth Providers */
export function DemoAuth<Acc extends Account = Account>({
@@ -9,13 +10,15 @@ export function DemoAuth<Acc extends Account = Account>({
appName,
appHostname,
Component = DemoAuth.BasicUI,
seedAccounts
}: {
accountSchema?: CoValueClass<Acc> & typeof Account;
appName: string;
appHostname?: string;
Component?: DemoAuth.Component;
seedAccounts?: {[name: string]: {accountID: ID<Account>, accountSecret: AgentSecret}}
}): ReactAuthHook<Acc> {
return function useLocalAuth() {
return function useLocalAuth(setJazzAuthState) {
const [authState, setAuthState] = useState<
| { state: "loading" }
| {
@@ -29,6 +32,10 @@ export function DemoAuth<Acc extends Account = Account>({
const [logOutCounter, setLogOutCounter] = useState(0);
useEffect(() => {
setJazzAuthState(authState.state);
}, [authState]);
const auth = useMemo(() => {
return new BrowserDemoAuth<Acc>(
accountSchema,
@@ -53,8 +60,9 @@ export function DemoAuth<Acc extends Account = Account>({
},
},
appName,
seedAccounts
);
}, [appName, appHostname, logOutCounter]);
}, [appName, appHostname, logOutCounter, seedAccounts]);
const AuthUI =
authState.state === "ready"

View File

@@ -1,4 +1,4 @@
import { useMemo, useState, ReactNode } from "react";
import { useMemo, useState, ReactNode, useEffect } from "react";
import { BrowserPasskeyAuth } from "jazz-browser";
import { Account, CoValueClass } from "jazz-tools";
import { ReactAuthHook } from "./auth.js";
@@ -15,7 +15,7 @@ export function PasskeyAuth<Acc extends Account>({
appHostname?: string;
Component?: PasskeyAuth.Component;
}): ReactAuthHook<Acc> {
return function useLocalAuth() {
return function useLocalAuth(setJazzAuthState) {
const [authState, setAuthState] = useState<
| { state: "loading" }
| {
@@ -26,6 +26,10 @@ export function PasskeyAuth<Acc extends Account>({
| { state: "signedIn"; logOut: () => void }
>({ state: "loading" });
useEffect(() => {
setJazzAuthState(authState.state);
}, [authState]);
const [logOutCounter, setLogOutCounter] = useState(0);
const auth = useMemo(() => {

View File

@@ -1,4 +1,4 @@
import { useMemo, useState, ReactNode } from "react";
import { useMemo, useState, ReactNode, useEffect } from "react";
import { BrowserPassphraseAuth } from "jazz-browser";
import { generateMnemonic } from "@scure/bip39";
import { cojsonInternals } from "cojson";
@@ -19,7 +19,7 @@ export function PassphraseAuth<Acc extends Account>({
wordlist: string[];
Component?: PassphraseAuth.Component;
}): ReactAuthHook<Acc> {
return function useLocalAuth() {
return function useLocalAuth(setJazzAuthState) {
const [authState, setAuthState] = useState<
| { state: "loading" }
| {
@@ -32,6 +32,10 @@ export function PassphraseAuth<Acc extends Account>({
const [logOutCounter, setLogOutCounter] = useState(0);
useEffect(() => {
setJazzAuthState(authState.state);
}, [authState]);
const auth = useMemo(() => {
return new BrowserPassphraseAuth<Acc>(
accountSchema,

View File

@@ -2,8 +2,12 @@ import React from "react";
import { AuthProvider } from "jazz-browser";
import { Account } from "jazz-tools";
export type AuthState = "loading" | "ready" | "signedIn";
/** @category Auth Providers */
export type ReactAuthHook<Acc extends Account> = () => {
export type ReactAuthHook<Acc extends Account> = (
setJazzAuthState: (state: AuthState) => void,
) => {
auth: AuthProvider<Acc>;
AuthUI: React.ReactNode;
logOut?: () => void;

View File

@@ -4,6 +4,7 @@ import {
createJazzBrowserContext,
} from "jazz-browser";
import {
Account,
CoValue,
@@ -13,7 +14,7 @@ import {
ID,
subscribeToCoValue,
} from "jazz-tools";
import { ReactAuthHook } from "./auth/auth.js";
import { AuthState, ReactAuthHook } from "./auth/auth.js";
/** @category Context & Hooks */
export function createJazzReactContext<Acc extends Account>({
@@ -33,10 +34,16 @@ export function createJazzReactContext<Acc extends Account>({
| undefined
>(undefined);
function Provider({ children }: { children: React.ReactNode }) {
function Provider({
children,
loading,
}: {
children: React.ReactNode;
loading?: React.ReactNode;
}) {
const [me, setMe] = useState<Acc | undefined>();
const { auth, AuthUI, logOut } = authHook();
const [authState, setAuthState] = useState<AuthState>("loading");
const { auth, AuthUI, logOut } = authHook(setAuthState);
useEffect(() => {
let done: (() => void) | undefined = undefined;
@@ -74,7 +81,8 @@ export function createJazzReactContext<Acc extends Account>({
return (
<>
{me && logOut ? (
{authState === "loading" ? loading : null}
{authState === "signedIn" && me && logOut ? (
<JazzContext.Provider
value={{
me,
@@ -83,9 +91,8 @@ export function createJazzReactContext<Acc extends Account>({
>
{children}
</JazzContext.Provider>
) : (
AuthUI
)}
) : null}
{authState === "ready" && AuthUI}
</>
);
}
@@ -181,6 +188,7 @@ export interface JazzReactContext<Acc extends Account> {
/** @category Provider Component */
Provider: React.FC<{
children: React.ReactNode;
loading?: React.ReactNode;
}>;
/** @category Hooks */

View File

@@ -35,9 +35,40 @@ import {
import { encodeSync, decodeSync } from "@effect/schema/Schema";
import { Effect, Stream } from "effect";
/** @category CoValues */
/**
* CoLists are collaborative versions of plain arrays.
*
* * @categoryDescription Content
* You can access items on a `CoList` as if they were normal items on a plain array, using `[]` notation, etc.
*
* Since `CoList` is a subclass of `Array`, you can use all the normal array methods like `push`, `pop`, `splice`, etc.
*
* ```ts
* colorList[0];
* colorList[3] = "yellow";
* colorList.push("Kawazaki Green");
* colorList.splice(1, 1);
* ```
*
* @category CoValues
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export class CoList<Item = any> extends Array<Item> implements CoValue {
/**
* Declare a `CoList` by subclassing `CoList.Of(...)` and passing the item schema using `co`.
*
* @example
* ```ts
* class ColorList extends CoList.Of(
* co.string
* ) {}
* class AnimalList extends CoList.Of(
* co.ref(Animal)
* ) {}
* ```
*
* @category Declaration
*/
static Of<Item>(item: Item): typeof CoList<Item> {
// TODO: cache superclass for item class
return class CoListOf extends CoList<Item> {
@@ -45,36 +76,62 @@ export class CoList<Item = any> extends Array<Item> implements CoValue {
};
}
/** @deprecated Use UPPERCASE `CoList.Of` instead! */
/**
* @ignore
* @deprecated Use UPPERCASE `CoList.Of` instead! */
static of(..._args: never): never {
throw new Error("Can't use Array.of with CoLists");
}
/**
* The ID of this `CoList`
* @category Content */
id!: ID<this>;
/** @category Type Helpers */
_type!: "CoList";
static {
this.prototype._type = "CoList";
}
/** @category Internals */
_raw!: RawCoList;
/** @category Internals */
_instanceID!: string;
/** @internal This is only a marker type and doesn't exist at runtime */
[ItemsSym]!: Item;
/** @internal */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
static _schema: any;
/** @internal */
get _schema(): {
[ItemsSym]: SchemaFor<Item>;
} {
return (this.constructor as typeof CoList)._schema;
}
/** @category Collaboration */
get _owner(): Account | Group {
return this._raw.group instanceof RawAccount
? Account.fromRaw(this._raw.group)
: Group.fromRaw(this._raw.group);
}
/** @category Content */
/**
* If a `CoList`'s items are a `co.ref(...)`, you can use `coList._refs[i]` to access
* the `Ref` instead of the potentially loaded/null value.
*
* This allows you to always get the ID or load the value manually.
*
* @example
* ```ts
* animals._refs[0].id; // => ID<Animal>
* animals._refs[0].value;
* // => Animal | null
* const animal = await animals._refs[0].load();
* ```
*
* @category Content
**/
get _refs(): {
[idx: number]: Exclude<Item, null> extends CoValue
? Ref<UnCo<Exclude<Item, null>>>
@@ -152,6 +209,27 @@ export class CoList<Item = any> extends Array<Item> implements CoValue {
return new Proxy(this, CoListProxyHandler as ProxyHandler<this>);
}
/**
* Create a new CoList with the given initial values and owner.
*
* The owner (a Group or Account) determines access rights to the CoMap.
*
* The CoList will immediately be persisted and synced to connected peers.
*
* @example
* ```ts
* const colours = ColorList.create(
* ["red", "green", "blue"],
* { owner: me }
* );
* const animals = AnimalList.create(
* [cat, dog, fish],
* { owner: me }
* );
* ```
*
* @category Creation
**/
static create<L extends CoList>(
this: CoValueClass<L>,
items: UnCo<L[number]>[],
@@ -160,9 +238,6 @@ export class CoList<Item = any> extends Array<Item> implements CoValue {
return new this({ init: items, owner: options.owner });
}
push(...items: Item[]): number;
/** @private For exact type compatibility with Array superclass */
push(...items: Item[]): number;
push(...items: Item[]): number {
for (const item of toRawItems(
items as Item[],
@@ -174,9 +249,6 @@ export class CoList<Item = any> extends Array<Item> implements CoValue {
return this._raw.entries().length;
}
unshift(...items: Item[]): number;
/** @private For exact type compatibility with Array superclass */
unshift(...items: Item[]): number;
unshift(...items: Item[]): number {
for (const item of toRawItems(
items as Item[],
@@ -247,6 +319,7 @@ export class CoList<Item = any> extends Array<Item> implements CoValue {
return this.toJSON();
}
/** @category Internals */
static fromRaw<V extends CoList>(
this: CoValueClass<V> & typeof CoList,
raw: RawCoList,
@@ -254,6 +327,7 @@ export class CoList<Item = any> extends Array<Item> implements CoValue {
return new this({ fromRaw: raw });
}
/** @internal */
static schema<V extends CoList>(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this: { new (...args: any): V } & typeof CoList,
@@ -263,7 +337,28 @@ export class CoList<Item = any> extends Array<Item> implements CoValue {
Object.assign(this._schema, def);
}
/** @category Subscription & Loading */
/**
* Load a `CoList` with a given ID, as a given account.
*
* `depth` specifies if item CoValue references should be loaded as well before resolving.
* The `DeeplyLoaded` return type guarantees that corresponding referenced CoValues are loaded to the specified depth.
*
* You can pass `[]` or for shallowly loading only this CoList, or `[itemDepth]` for recursively loading referenced CoValues.
*
* Check out the `load` methods on `CoMap`/`CoList`/`CoStream`/`Group`/`Account` to see which depth structures are valid to nest.
*
* @example
* ```ts
* const animalsWithVets =
* await ListOfAnimals.load(
* "co_zdsMhHtfG6VNKt7RqPUPvUtN2Ax",
* me,
* [{ vet: {} }]
* );
* ```
*
* @category Subscription & Loading
*/
static load<L extends CoList, Depth>(
this: CoValueClass<L>,
id: ID<L>,
@@ -273,7 +368,13 @@ export class CoList<Item = any> extends Array<Item> implements CoValue {
return loadCoValue(this, id, as, depth);
}
/** @category Subscription & Loading */
/**
* Effectful version of `CoList.load()`.
*
* Needs to be run inside an `AccountCtx` context.
*
* @category Subscription & Loading
*/
static loadEf<L extends CoList, Depth>(
this: CoValueClass<L>,
id: ID<L>,
@@ -282,7 +383,34 @@ export class CoList<Item = any> extends Array<Item> implements CoValue {
return loadCoValueEf<L, Depth>(this, id, depth);
}
/** @category Subscription & Loading */
/**
* Load and subscribe to a `CoList` with a given ID, as a given account.
*
* Automatically also subscribes to updates to all referenced/nested CoValues as soon as they are accessed in the listener.
*
* `depth` specifies if item CoValue references should be loaded as well before calling `listener` for the first time.
* The `DeeplyLoaded` return type guarantees that corresponding referenced CoValues are loaded to the specified depth.
*
* You can pass `[]` or for shallowly loading only this CoList, or `[itemDepth]` for recursively loading referenced CoValues.
*
* Check out the `load` methods on `CoMap`/`CoList`/`CoStream`/`Group`/`Account` to see which depth structures are valid to nest.
*
* Returns an unsubscribe function that you should call when you no longer need updates.
*
* Also see the `useCoState` hook to reactively subscribe to a CoValue in a React component.
*
* @example
* ```ts
* const unsub = ListOfAnimals.subscribe(
* "co_zdsMhHtfG6VNKt7RqPUPvUtN2Ax",
* me,
* { vet: {} },
* (animalsWithVets) => console.log(animalsWithVets)
* );
* ```
*
* @category Subscription & Loading
*/
static subscribe<L extends CoList, Depth>(
this: CoValueClass<L>,
id: ID<L>,
@@ -293,7 +421,13 @@ export class CoList<Item = any> extends Array<Item> implements CoValue {
return subscribeToCoValue<L, Depth>(this, id, as, depth, listener);
}
/** @category Subscription & Loading */
/**
* Effectful version of `CoList.subscribe()` that returns a stream of updates.
*
* Needs to be run inside an `AccountCtx` context.
*
* @category Subscription & Loading
*/
static subscribeEf<L extends CoList, Depth>(
this: CoValueClass<L>,
id: ID<L>,
@@ -302,7 +436,13 @@ export class CoList<Item = any> extends Array<Item> implements CoValue {
return subscribeToCoValueEf<L, Depth>(this, id, depth);
}
/** @category Subscription & Loading */
/**
* Given an already loaded `CoList`, ensure that items are loaded to the specified depth.
*
* Works like `CoList.load()`, but you don't need to pass the ID or the account to load as again.
*
* @category Subscription & Loading
*/
ensureLoaded<L extends CoList, Depth>(
this: L,
depth: Depth & DepthsIn<L>,
@@ -310,7 +450,15 @@ export class CoList<Item = any> extends Array<Item> implements CoValue {
return ensureCoValueLoaded(this, depth);
}
/** @category Subscription & Loading */
/**
* Given an already loaded `CoList`, subscribe to updates to the `CoList` and ensure that items are loaded to the specified depth.
*
* Works like `CoList.subscribe()`, but you don't need to pass the ID or the account to load as again.
*
* Returns an unsubscribe function that you should call when you no longer need updates.
*
* @category Subscription & Loading
**/
subscribe<L extends CoList, Depth>(
this: L,
depth: Depth & DepthsIn<L>,

View File

@@ -53,6 +53,8 @@ type InitValuesFor<C extends CoMap> = {
* @categoryDescription Declaration
* Declare your own CoMap schemas by subclassing `CoMap` and assigning field schemas with `co`.
*
* Optional `co.ref(...)` fields must be marked with `{ optional: true }`.
*
* ```ts
* import { co, CoMap } from "jazz-tools";
*
@@ -60,6 +62,7 @@ type InitValuesFor<C extends CoMap> = {
* name = co.string;
* age = co.number;
* pet = co.ref(Animal);
* car = co.ref(Car, { optional: true });
* }
* ```
*
@@ -103,17 +106,19 @@ export class CoMap extends CoValueBase implements CoValue {
/**
* If property `prop` is a `co.ref(...)`, you can use `coMaps._refs.prop` to access
* the `Ref` instead of the potentially loaded/null value.
*
* This allows you to always get the ID or load the value manually.
*
* @example
* ```ts
* person._refs.pet.id; // => ID<Animal>
* person._refs.pet.value;
* // => Animal | undefined
* // => Animal | null
* const pet = await person._refs.pet.load();
* ```
*
* @category Content */
* @category Content
**/
get _refs(): {
[Key in CoKeys<this>]: IfCo<this[Key], RefIfCoValue<this[Key]>>;
} {
@@ -227,7 +232,24 @@ export class CoMap extends CoValueBase implements CoValue {
return new Proxy(this, CoMapProxyHandler as ProxyHandler<this>);
}
/** @category Creation */
/**
* Create a new CoMap with the given initial values and owner.
*
* The owner (a Group or Account) determines access rights to the CoMap.
*
* The CoMap will immediately be persisted and synced to connected peers.
*
* @example
* ```ts
* const person = Person.create({
* name: "Alice",
* age: 42,
* pet: cat,
* }, { owner: friendGroup });
* ```
*
* @category Creation
**/
static create<M extends CoMap>(
this: CoValueClass<M>,
init: Simplify<CoMapInit<M>>,
@@ -301,7 +323,24 @@ export class CoMap extends CoValueBase implements CoValue {
return rawOwner.createMap(rawInit);
}
/** @category Declaration */
/**
* Declare a Record-like CoMap schema, by extending `CoMap.Record(...)` and passing the value schema using `co`. Keys are always `string`.
*
* @example
* ```ts
* import { co, CoMap } from "jazz-tools";
*
* class ColorToFruitMap extends CoMap.Record(
* co.ref(Fruit)
* ) {}
*
* // assume we have map: ColorToFruitMap
* // and strawberry: Fruit
* map["red"] = strawberry;
* ```
*
* @category Declaration
*/
static Record<Value>(value: IfCo<Value, Value>) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
class RecordLikeCoMap extends CoMap {
@@ -313,7 +352,27 @@ export class CoMap extends CoValueBase implements CoValue {
return RecordLikeCoMap;
}
/** @category Subscription & Loading */
/**
* Load a `CoMap` with a given ID, as a given account.
*
* `depth` specifies which (if any) fields that reference other CoValues to load as well before resolving.
* The `DeeplyLoaded` return type guarantees that corresponding referenced CoValues are loaded to the specified depth.
*
* You can pass `[]` or `{}` for shallowly loading only this CoMap, or `{ fieldA: depthA, fieldB: depthB }` for recursively loading referenced CoValues.
*
* Check out the `load` methods on `CoMap`/`CoList`/`CoStream`/`Group`/`Account` to see which depth structures are valid to nest.
*
* @example
* ```ts
* const person = await Person.load(
* "co_zdsMhHtfG6VNKt7RqPUPvUtN2Ax",
* me,
* { pet: {} }
* );
* ```
*
* @category Subscription & Loading
*/
static load<M extends CoMap, Depth>(
this: CoValueClass<M>,
id: ID<M>,
@@ -323,7 +382,13 @@ export class CoMap extends CoValueBase implements CoValue {
return loadCoValue(this, id, as, depth);
}
/** @category Subscription & Loading */
/**
* Effectful version of `CoMap.load()`.
*
* Needs to be run inside an `AccountCtx` context.
*
* @category Subscription & Loading
*/
static loadEf<M extends CoMap, Depth>(
this: CoValueClass<M>,
id: ID<M>,
@@ -332,7 +397,34 @@ export class CoMap extends CoValueBase implements CoValue {
return loadCoValueEf<M, Depth>(this, id, depth);
}
/** @category Subscription & Loading */
/**
* Load and subscribe to a `CoMap` with a given ID, as a given account.
*
* Automatically also subscribes to updates to all referenced/nested CoValues as soon as they are accessed in the listener.
*
* `depth` specifies which (if any) fields that reference other CoValues to load as well before calling `listener` for the first time.
* The `DeeplyLoaded` return type guarantees that corresponding referenced CoValues are loaded to the specified depth.
*
* You can pass `[]` or `{}` for shallowly loading only this CoMap, or `{ fieldA: depthA, fieldB: depthB }` for recursively loading referenced CoValues.
*
* Check out the `load` methods on `CoMap`/`CoList`/`CoStream`/`Group`/`Account` to see which depth structures are valid to nest.
*
* Returns an unsubscribe function that you should call when you no longer need updates.
*
* Also see the `useCoState` hook to reactively subscribe to a CoValue in a React component.
*
* @example
* ```ts
* const unsub = Person.subscribe(
* "co_zdsMhHtfG6VNKt7RqPUPvUtN2Ax",
* me,
* { pet: {} },
* (person) => console.log(person)
* );
* ```
*
* @category Subscription & Loading
*/
static subscribe<M extends CoMap, Depth>(
this: CoValueClass<M>,
id: ID<M>,
@@ -343,7 +435,13 @@ export class CoMap extends CoValueBase implements CoValue {
return subscribeToCoValue<M, Depth>(this, id, as, depth, listener);
}
/** @category Subscription & Loading */
/**
* Effectful version of `CoMap.subscribe()` that returns a stream of updates.
*
* Needs to be run inside an `AccountCtx` context.
*
* @category Subscription & Loading
*/
static subscribeEf<M extends CoMap, Depth>(
this: CoValueClass<M>,
id: ID<M>,
@@ -352,7 +450,13 @@ export class CoMap extends CoValueBase implements CoValue {
return subscribeToCoValueEf<M, Depth>(this, id, depth);
}
/** @category Subscription & Loading */
/**
* Given an already loaded `CoMap`, ensure that the specified fields are loaded to the specified depth.
*
* Works like `CoMap.load()`, but you don't need to pass the ID or the account to load as again.
*
* @category Subscription & Loading
*/
ensureLoaded<M extends CoMap, Depth>(
this: M,
depth: Depth & DepthsIn<M>,
@@ -360,7 +464,15 @@ export class CoMap extends CoValueBase implements CoValue {
return ensureCoValueLoaded(this, depth);
}
/** @category Subscription & Loading */
/**
* Given an already loaded `CoMap`, subscribe to updates to the `CoMap` and ensure that the specified fields are loaded to the specified depth.
*
* Works like `CoMap.subscribe()`, but you don't need to pass the ID or the account to load as again.
*
* Returns an unsubscribe function that you should call when you no longer need updates.
*
* @category Subscription & Loading
**/
subscribe<M extends CoMap, Depth>(
this: M,
depth: Depth & DepthsIn<M>,

View File

@@ -68,6 +68,7 @@ export class CoValueBase implements CoValue {
id!: ID<this>;
_type!: string;
_raw!: RawCoValue;
/** @category Internals */
_instanceID!: string;
get _owner(): Account | Group {