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

feat: SolidJS port #16

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
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
52 changes: 40 additions & 12 deletions client/src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import {A, useNavigate, useParams} from '@solidjs/router';
import {nanoid} from 'nanoid';
import {Replicache} from 'replicache';
import {List, Todo, TodoUpdate, getList, listLists, todosByList} from 'shared';
import {
MutatorDefs,
ReadTransaction,
ReadonlyJSONValue,
Replicache,
} from 'replicache';
import {List, TodoUpdate, getList, listLists, todosByList} from 'shared';
import {
Component,
For,
JSX,
createEffect,
createSignal,
on,
onCleanup,
} from 'solid-js';
import {createStore, reconcile} from 'solid-js/store';
import Header from './components/header.jsx';
import MainSection from './components/main-section.jsx';
import Share from './components/share.jsx';
Expand Down Expand Up @@ -60,16 +67,37 @@ const App = (props: {
}, undefined);

// Subscribe to all todos and sort them.
const todos = createEffectAccessor<Todo[]>(set => {
const {rep} = props;
const {listID} = params;
onCleanup(
rep.subscribe(async tx => {
const todos = await todosByList(tx, listID);
return todos.sort((a, b) => a.sort - b.sort);
}, set),
const todos = subscribeStore(
Copy link
Author

Choose a reason for hiding this comment

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

This is getting a bit more ergonomic.

() => [props.rep, params.listID],
async (tx, listID) => {
const todos = await todosByList(tx, listID);
return todos.sort((a, b) => a.sort - b.sort);
},
[],
);

function subscribeStore<
T extends object,
MD extends MutatorDefs,
Deps extends [...args: ReadonlyJSONValue[]],
>(
deps: () => [Replicache<MD>, ...Deps],
query: (tx: ReadTransaction, ...args: Deps) => Promise<T>,
def: T,
) {
const [state, setState] = createStore<T>(def);
createEffect(
on(deps, ([rep, ...deps]) =>
onCleanup(
rep.subscribe(
tx => query(tx, ...deps),
data => setState(reconcile(data, {merge: true})),
),
),
),
);
}, []);
return state;
}

// Define event handlers and connect them to Replicache mutators. Each
// of these mutators runs immediately (optimistically) locally, then runs
Expand Down Expand Up @@ -139,7 +167,7 @@ const App = (props: {
/>
{selectedList() ? (
<MainSection
todos={todos()}
todos={todos}
onUpdateTodo={handleUpdateTodo}
onDeleteTodos={handleDeleteTodos}
onCompleteTodos={handleCompleteTodos}
Expand Down