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/package.json b/package.json index bb1ce8f..a161593 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..3a73888 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, + subscribeable: Subscribeable | null | undefined, query: (tx: ReadTransaction) => Promise, def: R, // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -29,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) { @@ -42,6 +44,6 @@ export function useSubscribe( } }, }); - }, [rep, ...deps]); + }, [subscribeable, ...deps]); return snapshot; }