Skip to content
Merged
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
6 changes: 6 additions & 0 deletions .changeset/metal-cows-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@will-be-done/hyperdb": minor
"@will-be-done/hyperdb-devtool": minor
---

improve hook
5 changes: 5 additions & 0 deletions .changeset/sync-dispatch-hook.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@will-be-done/hyperdb": minor
---

Rename the React synchronous dispatch hook from `useDispatch` to `useSyncDispatch`.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,14 @@ export async function createAppDB() {

If your whole app state can be loaded into memory at startup, you may not need
`HybridDB`. A plain `new SubscribableDB(new DB(new BptreeInmemDriver()))` keeps
reads and writes synchronous, so you can use `useSyncSelector`, `useDispatch`,
reads and writes synchronous, so you can use `useSyncSelector`, `useSyncDispatch`,
`selectSync`, and `syncDispatch` without promise or cache-miss tradeoffs.
For one-off reads that should reuse the selector cache, use `selectCachedSync`,
`selectCachedAsync`, or `selectCachedMaybeAsync`.
`selectCachedAsync`, or `selectCachedMaybeAsync`. For subscriptions outside
React, use `createSelectorStoreSync` / `createCachedSelectorStoreSync` when
selector results are available synchronously. Use `createAsyncSelectorStore` /
`createCachedSelectorStoreAsync` when a `HybridDB`/async driver run may need to
resolve a promise and expose query state.

`SubscribableDB` also exposes lifecycle hooks: mutation hooks such as
`afterInsert`, `afterUpsert`, `afterDelete`, and `afterChange`, plus `afterScan`
Expand Down Expand Up @@ -243,7 +247,9 @@ export function App({ db }: { db: SubscribableDB }) {
`useAsyncSelector` attempts the selector once for its initial snapshot. If the
run finishes synchronously, that value is visible immediately; if it returns a
promise, the hook shows `defaultValue`, `initialData`, or `placeholderData` until
that same promise resolves.
that same promise resolves. The hook uses `createCachedSelectorStoreAsync`
internally, so the same async subscription behavior is available without React.
`defaultValue` may be a value or a zero-argument function that returns the value.

## Entry points

Expand Down
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ DB:
7. In-mem tx for fast action
8. k-way indexxedb OR merging
9. Multibe column support in db index
10. Rename useSelect => useSyncSelect, useDispatch => useSyncDispatch, select -> selectSync, runSelector -> runSelectorSync, initCachedSelector -> initCachedSelectorSync, initSelector -> initSelectorSync
10. Rename useSelect => useSyncSelect, select -> selectSync, runSelector -> runSelectorSync, initCachedSelector -> initCachedSelectorSync, initSelector -> initSelectorSync
11. Maybe rename upsert -> put
12. Move async storage to selector.ts from react hook
11 changes: 6 additions & 5 deletions packages/hyperdb-doc/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import starlight from "@astrojs/starlight";
// https://astro.build/config
export default defineConfig({
site: "https://hyperdb.will-be-done.app",
redirects: {
"/database/selectors-reactivity": "/database/reading-data",
"/database/writing-data": "/database/actions",
},
integrations: [
starlight({
title: "HyperDB",
Expand Down Expand Up @@ -39,12 +43,9 @@ export default defineConfig({
{ label: "Schemas", slug: "database/schemas" },
{ label: "Indexes", slug: "database/indexes" },
{ label: "Data Types", slug: "database/data-types" },
{ label: "Selectors", slug: "database/selectors" },
{ label: "Reading Data", slug: "database/reading-data" },
{
label: "Selectors & Reactivity",
slug: "database/selectors-reactivity",
},
{ label: "Writing Data", slug: "database/writing-data" },
{ label: "Actions", slug: "database/actions" },
],
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
title: Writing Data
title: Actions
description: Change data with actions and the insert, upsert, and deleteRows mutations, dispatched in transactions.
sidebar:
order: 5
order: 6
---

You change data through actions, generator functions that may both read and
Expand Down Expand Up @@ -143,8 +143,8 @@ await asyncDispatch(
);
```

Inside React use [`useDispatch` / `useAsyncDispatch`](/integrations/react/), which
bind the dispatcher to the database from context.
Inside React use [`useSyncDispatch` / `useAsyncDispatch`](/integrations/react/),
which bind the dispatcher to the database from context.

With `HybridDB`, use the async dispatch path. Writes update the in-memory cache
first so subscribers and React can respond immediately, then flush to the
Expand Down
Loading
Loading