From 7752a923c1681ae886d06eb8d76fa9c8cfbf7b55 Mon Sep 17 00:00:00 2001 From: Gregory Baker Date: Thu, 7 Apr 2022 10:08:27 -0700 Subject: [PATCH 1/4] feat: Update useSubscribe to accept Replicache or Pick so it can accepts ReflectClient --- package.json | 5 +++++ src/index.ts | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index bb1ce8f..1dd7788 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,11 @@ "react-dom": ">=16.0 <18.0", "replicache": ">=4.0.1 <10.0 || >7.0.0-beta <7.0.0 || >8.0.0-beta <8.0.0 || >9.0.0-beta <9.0.0" }, + "peerDependenciesMeta": { + "replicache": { + "optional": true + } + } "devDependencies": { "@esm-bundle/chai": "^4.3.4-fix.0", "@snowpack/web-test-runner-plugin": "^0.2.2", diff --git a/src/index.ts b/src/index.ts index eb65ce3..3ca1f11 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,8 +20,10 @@ function doCallback() { }); } +export type Subscribeable = Pick; + export function useSubscribe( - rep: Replicache | null | undefined, + rep: Replicache | null | undefined | Subscribeable, query: (tx: ReadTransaction) => Promise, def: R, // eslint-disable-next-line @typescript-eslint/no-explicit-any From a2e41b3574c93a7512d7e8b8dc6172abf4c13289 Mon Sep 17 00:00:00 2001 From: Gregory Baker Date: Thu, 7 Apr 2022 10:12:24 -0700 Subject: [PATCH 2/4] fixup --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1dd7788..a161593 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "replicache": { "optional": true } - } + }, "devDependencies": { "@esm-bundle/chai": "^4.3.4-fix.0", "@snowpack/web-test-runner-plugin": "^0.2.2", From 5b99a05643c295aefe5b25e26a84a1e88833e5dd Mon Sep 17 00:00:00 2001 From: Gregory Baker Date: Thu, 7 Apr 2022 10:17:33 -0700 Subject: [PATCH 3/4] fixup --- package-lock.json | 5 +++++ src/index.ts | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index bdc0fad..aefc957 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27,6 +27,11 @@ "react": ">=16.0 <18.0", "react-dom": ">=16.0 <18.0", "replicache": ">=4.0.1 <10.0 || >7.0.0-beta <7.0.0 || >8.0.0-beta <8.0.0 || >9.0.0-beta <9.0.0" + }, + "peerDependenciesMeta": { + "replicache": { + "optional": true + } } }, "node_modules/@babel/code-frame": { diff --git a/src/index.ts b/src/index.ts index 3ca1f11..50c0935 100644 --- a/src/index.ts +++ b/src/index.ts @@ -23,7 +23,7 @@ function doCallback() { export type Subscribeable = Pick; export function useSubscribe( - rep: Replicache | null | undefined | Subscribeable, + rep: Replicache | Subscribeable | null | undefined, query: (tx: ReadTransaction) => Promise, def: R, // eslint-disable-next-line @typescript-eslint/no-explicit-any From 3e56914266d051aff1152c161ad4bad66bf976a5 Mon Sep 17 00:00:00 2001 From: Gregory Baker Date: Thu, 7 Apr 2022 10:30:28 -0700 Subject: [PATCH 4/4] cleaner --- src/index.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 50c0935..3a73888 100644 --- a/src/index.ts +++ b/src/index.ts @@ -23,7 +23,7 @@ function doCallback() { export type Subscribeable = Pick; export function useSubscribe( - rep: Replicache | Subscribeable | null | undefined, + subscribeable: Subscribeable | null | undefined, query: (tx: ReadTransaction) => Promise, def: R, // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -31,11 +31,11 @@ export function useSubscribe( ): R { const [snapshot, setSnapshot] = useState(def); useEffect(() => { - if (!rep) { + if (!subscribeable) { return; } - return rep.subscribe(query, { + return subscribeable.subscribe(query, { onData: (data: R) => { callbacks.push(() => setSnapshot(data)); if (!hasPendingCallback) { @@ -44,6 +44,6 @@ export function useSubscribe( } }, }); - }, [rep, ...deps]); + }, [subscribeable, ...deps]); return snapshot; }