Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP - Exploring using search from Rails #18

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
"watch": "concurrently --kill-others 'npm run server' 'npm run check-types -- --watch --preserveWatchOutput' 'sleep 3; npm run dev'"
},
"dependencies": {
"@rocicorp/rails": "file:../rocicorp-rails-0.10.0.tgz",
"classnames": "^2.3.1",
"navigo": "^8.11.1",
"qs": "^6.11.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"replicache": "^14.2.2",
"replicache-react": "5.0.1",
"shared": "^0.1.0",
"todomvc-app-css": "^2.4.2"
Expand Down
39 changes: 20 additions & 19 deletions client/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import {Dialog} from '@headlessui/react';
import {nanoid} from 'nanoid';
import Navigo from 'navigo';
import {useEffect, useState} from 'react';
import {ReadTransaction, Replicache} from 'replicache';
import {useSubscribe} from 'replicache-react';
import {TodoUpdate, todosByList} from 'shared';
import {getList, listLists} from 'shared/src/list';
import {Replicache} from 'replicache';
import {List, Todo, TodoUpdate} from 'shared';
import Header from './components/header';
import MainSection from './components/main-section';
import {Share} from './components/share';
import {M} from './mutators';
import {useQuery, useTable} from './use-query';

// This is the top-level component for our app.
const App = ({
Expand Down Expand Up @@ -46,21 +45,23 @@ const App = ({
// Listen for pokes related to the docs this user has access to.
useEventSourcePoke(`/api/replicache/poke?channel=user/${userID}`, rep);

const lists = useSubscribe(rep, listLists, {default: []});
lists.sort((a, b) => a.name.localeCompare(b.name));
const listTable = useTable<List>(rep, 'list');
const todoTable = useTable<Todo>(rep, 'todo');

const selectedList = useSubscribe(
rep,
(tx: ReadTransaction) => getList(tx, listID),
{dependencies: [listID]},
);
const allListsQuery = listTable.select('id', 'name').asc('name');
const lists = useQuery(allListsQuery);

// Subscribe to all todos and sort them.
const todos = useSubscribe(rep, async tx => todosByList(tx, listID), {
default: [],
dependencies: [listID],
});
todos.sort((a, b) => a.sort - b.sort);
const selectedListsQuery = allListsQuery.where('id', '=', listID).limit(1);
const selectedLists = useQuery(selectedListsQuery, [listID]);
const selectedList = selectedLists[0];

const todosQuery = todoTable
.select('id', 'listID', 'text', 'completed', 'sort')
.where('listID', '=', listID)
.desc('sort');
const todos = useQuery(todosQuery, [listID]);

const todosCount = useQuery(todoTable.where('listID', '=', listID).count());
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing [listID] deps


// Define event handlers and connect them to Replicache mutators. Each
// of these mutators runs immediately (optimistically) locally, then runs
Expand Down Expand Up @@ -110,6 +111,7 @@ const App = ({

return (
<div id="layout">
<div>listID: {listID}</div>
<div id="nav">
{lists.map(list => {
const path = `/list/${list.id}`;
Expand All @@ -120,7 +122,6 @@ const App = ({
onClick={e => {
router.navigate(path);
e.preventDefault();
return false;
}}
>
{list.name}
Expand All @@ -130,7 +131,7 @@ const App = ({
</div>
<div className="todoapp">
<Header
listName={selectedList?.name}
listName={selectedList?.name + ' (' + todosCount + ' items)'}
userID={userID}
onNewItem={handleNewItem}
onNewList={handleNewList}
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/main-section.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const MainSection = ({
onDeleteTodos,
onCompleteTodos,
}: {
todos: Todo[];
todos: readonly Todo[];
onUpdateTodo: (update: TodoUpdate) => void;
onDeleteTodos: (ids: string[]) => void;
onCompleteTodos: (completed: boolean, ids: string[]) => void;
Expand Down
24 changes: 12 additions & 12 deletions client/src/components/share.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import {useSubscribe} from 'replicache-react';
import {M} from '../mutators';
import {Replicache} from 'replicache';
import {listShares} from 'shared';
import {FormEvent} from 'react';
import {Dialog} from '@headlessui/react';
import {nanoid} from 'nanoid';
import {FormEvent} from 'react';
import {Replicache} from 'replicache';
import {Share as ShareType} from 'shared';
import {M} from '../mutators';
import {useQuery, useTable} from '../use-query.js';

export function Share({rep, listID}: {rep: Replicache<M>; listID: string}) {
const guests = useSubscribe(
rep,
async tx => {
const allShares = await listShares(tx);
return allShares.filter(a => a.listID === listID);
},
{default: []},
const shareTable = useTable<ShareType>(rep, 'share');
const guests = useQuery(
shareTable
.select('id', 'listID', 'userID')
.asc('userID')
.where('listID', '=', listID),
[listID],
);

const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
Expand Down
26 changes: 26 additions & 0 deletions client/src/use-query.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {Entity, generateZQL} from '@rocicorp/rails';
import {EntityQuery} from '@rocicorp/rails/out/zql/query/entity-query.js';
import {EntitySchema} from '@rocicorp/rails/out/zql/schema/entity-schema.js';
import {useEffect, useMemo, useState} from 'react';

export function useQuery<S extends EntitySchema, R>(
query: EntityQuery<S, R>,
dependencies: unknown[] = [],
): R {
const view = useMemo(() => query.prepare().materialize(), dependencies);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you'll need a way to destroy the statement.

const [value, setValue] = useState<R>(view.value as R);
useEffect(() => {
Copy link

@tantaman tantaman Mar 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting state via useEffect causes UI flashing / tearing.

useState calls inside useEffect will not run in the same render pass as the render caused by a change in dependencies.

In other words, the component will render and fully paint to the browser once with the updated dependencies and then again with the effects (setValue) of useEffect.

You'd need to emulate useEffect with useState to prevent flashing.

const [prevDependencies, setPrevDependencies] = useState(dependencies);
if (!shallowEqual(dependencies, prevDependencies)) {
  setPrevDependencies(prevDependencies);
  const newView = viewFn();
  setValue(newView.value);
  setView(newView);
  disposer();
  setDisposer(newView.on(setValue));
}

This will ensure that when dependencies change, the new view is created and value set on the component before it renders to the screen.

some related discussion: https://twitter.com/tantaman/status/1732140032729985512

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have certainly opened up a can of worms!

The code that you have here and the code on twitter seems incomplete. We need to have a cleanup phase too to remove the listener.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See also useLayoutEffect ?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code that you have here and the code on twitter seems incomplete. We need to have a cleanup phase too to remove the listener.

setDisposer would handle that, although given the disposer isn't used to render anything it should be a ref rather than state.

This should be more complete:

// track previous deps
const [prevDependencies, setPrevDependencies] = useState();
// track our view / query results
const [view, setView] = useState();
// track our subscription to the view
const [disposer, setDisposer] = useState();

// prevDeps === undefined -> first render
// prevDeps != deps, re-render
if (prevDependencies === undefined || !shallowEqual(dependencies, prevDependencies)) {
  setPrevDependencies(prevDependencies);
  // new deps? compute view / query
  const newView = viewFn();
  // destroy old view
  view?.destroy();
  // destroy old subscription
  disposer?.();

  setValue(newView.value);
  setView(newView);
  setDisposer(newView.on(setValue));
}

// also need to try cleaning up on un-mount of the component
useEffect(() => {
  disposer?.();
  view?.destroy();
}, []);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I will try again to see if I can get something along these lines working.

For replicache-react, useSubscribe was simpler because useSubscribe is always async and we do not have the result immediately.

setValue(view.value);
return view.on(setValue);
}, dependencies);
return value;
}

type ReplicacheLike = Parameters<typeof generateZQL>[0];

export function useTable<E extends Entity>(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few things:

  • Is this a hook? use is typically reserved for hooks.
  • I don't think we'll call them tables, probably collections.
  • What about query<T>(rep, name)
  • Also I think it is fine to wrap this all up into a useQuery react hook and not expose useTable/query at all. I bet it will feel more natural to just repeat shared sections of the query rather than share them. They're so succint already.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useTable does feel redundant.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially had a useMemo in there... that is why it was structured like a hook.

Copy link
Author

@arv arv Mar 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I think it is fine to wrap this all up into a useQuery react hook and not expose useTable/query at all. I bet it will feel more natural to just repeat shared sections of the query rather than share them. They're so succint already.

The query starts with an object... If we want to collapse this into one hook it needs to be something along the lines of:

useQuery<List[]>(rep, 'list', q => q.select('id', 'name').where(...), deps);

This looks fine to me :-)

But we also need to get parse in there...

and when there is join involved we need to register all the key namespaces.

useQuery(rep, {
  list: listSchema.parse,
  todo: todoSchema.parse,
}, q => ..., deps)

rep: ReplicacheLike,
tableName: string,
) {
return generateZQL<E>(rep, tableName);
}
51 changes: 30 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"shared"
],
"dependencies": {
"@rocicorp/rails": "0.8.1",
"@rocicorp/rails": "file:./rocicorp-rails-0.10.0.tgz",
"replicache": "^14.2.2"
}
}
Binary file added rocicorp-rails-0.10.0.tgz
Binary file not shown.