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
4 changes: 2 additions & 2 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
@@ -1,6 +1,6 @@
{
"name": "replicache-react",
"version": "3.1.0",
"version": "4.0.0",
"description": "Miscellaneous utilities for using Replicache with React",
"homepage": "https://replicache.dev",
"repository": "github:rocicorp/replicache-react",
Expand Down
10 changes: 5 additions & 5 deletions src/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ test('Batching of subscriptions', async () => {
const dataA = useSubscribe(
rep,
// TODO: Use type param to get when new Replicache is released.
async tx => ((await tx.get('a')) as string | undefined) ?? null,
async tx => (await tx.get('a')) as string | undefined,
null,
);
renderLog.push('render A', dataA);
Expand All @@ -86,7 +86,7 @@ test('Batching of subscriptions', async () => {
function B({rep, dataA}: {rep: MyRep; dataA: string | null}) {
const dataB = useSubscribe(
rep,
async tx => ((await tx.get('b')) as string | undefined) ?? null,
async tx => (await tx.get('b')) as string | undefined,
null,
);
renderLog.push('render B', dataA, dataB);
Expand Down Expand Up @@ -129,7 +129,7 @@ test('returning undefined', async () => {
},
def,
);
return <div>{subResult === undefined ? 'undefined' : 'defined'}</div>;
return <div>{subResult}</div>;
}

const div = document.createElement('div');
Expand All @@ -141,10 +141,10 @@ test('returning undefined', async () => {
});

render(<A key="c" rep={rep} def="default" />, div);
expect(div.textContent).to.equal('defined');
expect(div.textContent).to.equal('default');
await promise;
await sleep(1);
expect(div.textContent).to.equal('undefined');
expect(div.textContent).to.equal('default');

await rep.close();
});
Expand Down
29 changes: 19 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ function doCallback() {
});
}

export function useSubscribe<Tx, D, R extends D>(
r: Subscribable<Tx, D> | null | undefined,
query: (tx: Tx) => Promise<R>,
def: R,
export type RemoveUndefined<T> = T extends undefined ? never : T;

export function useSubscribe<Tx, Data, QueryRet extends Data, Default>(
r: Subscribable<Tx, Data> | null | undefined,
query: (tx: Tx) => Promise<QueryRet>,
def: Default,
deps: Array<unknown> = [],
): R {
const [snapshot, setSnapshot] = useState<R>(def);
) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please write out the return type here. The inferred type is pretty cryptic.

export type RemoveUndefined<T> = T extends undefined ? never : T;
...
): Default | RemoveUndefined<QueryRet> {

const [snapshot, setSnapshot] = useState<QueryRet | undefined>(undefined);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I find it easier to reason about this by setting the state to def here.

But both should behave the same so your call here.

useEffect(() => {
if (!r) {
return;
Expand All @@ -41,7 +43,7 @@ export function useSubscribe<Tx, D, R extends D>(
onData: data => {
// This is safe because we know that subscribe in fact can only return
// `R` (the return type of query or def).
callbacks.push(() => setSnapshot(data as R));
callbacks.push(() => setSnapshot(data as QueryRet));
if (!hasPendingCallback) {
void Promise.resolve().then(doCallback);
hasPendingCallback = true;
Expand All @@ -51,8 +53,15 @@ export function useSubscribe<Tx, D, R extends D>(

return () => {
unsubscribe();
setSnapshot(def);
setSnapshot(undefined);
};
}, [r, ...deps]);
return snapshot;
}, [r, def, ...deps]);
if (snapshot === undefined) {
return def;
}
// This RemoveUndefined is just here to make the return type easier to read.
// It should be exactly equivalent to what the type would be without this.
// For some reason declaring the return type to be
// RemoveUndefined<QueryRet> | Default doesn't typecheck.
return snapshot as RemoveUndefined<QueryRet>;
}