Skip to content

Commit 9634400

Browse files
committed
feat: Return default value whenever query function returns undefined.
1 parent 528efb5 commit 9634400

4 files changed

Lines changed: 18 additions & 15 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "replicache-react",
3-
"version": "3.1.0",
3+
"version": "4.0.0",
44
"description": "Miscellaneous utilities for using Replicache with React",
55
"homepage": "https://replicache.dev",
66
"repository": "github:rocicorp/replicache-react",

src/index.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ test('returning undefined', async () => {
129129
},
130130
def,
131131
);
132-
return <div>{subResult === undefined ? 'undefined' : 'defined'}</div>;
132+
return <div>{subResult}</div>;
133133
}
134134

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

143143
render(<A key="c" rep={rep} def="default" />, div);
144-
expect(div.textContent).to.equal('defined');
144+
expect(div.textContent).to.equal('default');
145145
await promise;
146146
await sleep(1);
147-
expect(div.textContent).to.equal('undefined');
147+
expect(div.textContent).to.equal('default');
148148

149149
await rep.close();
150150
});

src/index.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ function doCallback() {
2525
});
2626
}
2727

28-
export function useSubscribe<Tx, D, R extends D>(
29-
r: Subscribable<Tx, D> | null | undefined,
30-
query: (tx: Tx) => Promise<R>,
31-
def: R,
28+
export function useSubscribe<Tx, Data, QueryRet extends Data, Default>(
29+
r: Subscribable<Tx, Data> | null | undefined,
30+
query: (tx: Tx) => Promise<QueryRet>,
31+
def: Default,
3232
deps: Array<unknown> = [],
33-
): R {
34-
const [snapshot, setSnapshot] = useState<R>(def);
33+
) {
34+
const [snapshot, setSnapshot] = useState<QueryRet | undefined>(undefined);
3535
useEffect(() => {
3636
if (!r) {
3737
return;
@@ -41,7 +41,7 @@ export function useSubscribe<Tx, D, R extends D>(
4141
onData: data => {
4242
// This is safe because we know that subscribe in fact can only return
4343
// `R` (the return type of query or def).
44-
callbacks.push(() => setSnapshot(data as R));
44+
callbacks.push(() => setSnapshot(data as QueryRet));
4545
if (!hasPendingCallback) {
4646
void Promise.resolve().then(doCallback);
4747
hasPendingCallback = true;
@@ -51,8 +51,11 @@ export function useSubscribe<Tx, D, R extends D>(
5151

5252
return () => {
5353
unsubscribe();
54-
setSnapshot(def);
54+
setSnapshot(undefined);
5555
};
56-
}, [r, ...deps]);
56+
}, [r, def, ...deps]);
57+
if (snapshot === undefined) {
58+
return def;
59+
}
5760
return snapshot;
5861
}

0 commit comments

Comments
 (0)