@ponder/react 0.9.0-next.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Cantrip, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,14 @@
1
+ import { Client } from '@ponder/client';
2
+ import { UseQueryOptions, UseQueryResult } from '@tanstack/react-query';
3
+ import React from 'react';
4
+
5
+ declare function usePonderQuery<result>(params: {
6
+ queryFn: (db: Client["db"]) => Promise<result>;
7
+ } & Omit<UseQueryOptions<result>, "queryFn" | "queryKey">): UseQueryResult<result>;
8
+
9
+ type PonderProviderProps = {
10
+ client: Client;
11
+ };
12
+ declare function PonderProvider(params: React.PropsWithChildren<PonderProviderProps>): React.JSX.Element;
13
+
14
+ export { PonderProvider, usePonderQuery };
package/dist/index.js ADDED
@@ -0,0 +1,45 @@
1
+ // src/usePonderQuery.ts
2
+ import { status } from "@ponder/client";
3
+ import { compileQuery } from "@ponder/client";
4
+ import {
5
+ useQuery,
6
+ useQueryClient
7
+ } from "@tanstack/react-query";
8
+ import { useContext, useEffect, useMemo } from "react";
9
+
10
+ // src/context.tsx
11
+ import React, { createContext } from "react";
12
+ var PonderContext = createContext(void 0);
13
+ function PonderProvider(params) {
14
+ const { children, client } = params;
15
+ return /* @__PURE__ */ React.createElement(PonderContext.Provider, { value: client }, children);
16
+ }
17
+
18
+ // src/usePonderQuery.ts
19
+ function usePonderQuery(params) {
20
+ const queryClient = useQueryClient();
21
+ const client = useContext(PonderContext);
22
+ if (client === void 0) {
23
+ throw new Error("PonderProvider not found");
24
+ }
25
+ const queryPromise = params.queryFn(client.db);
26
+ const query = compileQuery(queryPromise);
27
+ const queryKey = useMemo(() => [query.sql, ...query.params], [query]);
28
+ useEffect(() => {
29
+ const { unsubscribe } = client.live(
30
+ (db) => db.select().from(status),
31
+ () => queryClient.invalidateQueries({ queryKey })
32
+ );
33
+ return unsubscribe;
34
+ }, [queryClient, client, queryKey]);
35
+ return useQuery({
36
+ ...params,
37
+ queryKey,
38
+ queryFn: () => queryPromise
39
+ });
40
+ }
41
+ export {
42
+ PonderProvider,
43
+ usePonderQuery
44
+ };
45
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/usePonderQuery.ts","../src/context.tsx"],"sourcesContent":["\"use client\";\n\nimport { type Client, status } from \"@ponder/client\";\nimport { compileQuery } from \"@ponder/client\";\nimport {\n type UseQueryOptions,\n type UseQueryResult,\n useQuery,\n useQueryClient,\n} from \"@tanstack/react-query\";\nimport { useContext, useEffect, useMemo } from \"react\";\nimport { PonderContext } from \"./context.js\";\n\nexport function usePonderQuery<result>(\n params: {\n queryFn: (db: Client[\"db\"]) => Promise<result>;\n } & Omit<UseQueryOptions<result>, \"queryFn\" | \"queryKey\">,\n): UseQueryResult<result> {\n const queryClient = useQueryClient();\n\n const client = useContext(PonderContext);\n if (client === undefined) {\n throw new Error(\"PonderProvider not found\");\n }\n\n const queryPromise = params.queryFn(client.db);\n // @ts-ignore\n const query = compileQuery(queryPromise);\n const queryKey = useMemo(() => [query.sql, ...query.params], [query]);\n\n useEffect(() => {\n const { unsubscribe } = client.live(\n (db) => db.select().from(status),\n () => queryClient.invalidateQueries({ queryKey }),\n );\n return unsubscribe;\n }, [queryClient, client, queryKey]);\n\n return useQuery({\n ...params,\n queryKey,\n queryFn: () => queryPromise,\n });\n}\n","\"use client\";\n\nimport type { Client } from \"@ponder/client\";\n// biome-ignore lint/style/useImportType: \"React\" is needed\nimport React, { createContext } from \"react\";\n\nexport const PonderContext = createContext<Client | undefined>(undefined);\n\ntype PonderProviderProps = {\n client: Client;\n};\n\nexport function PonderProvider(\n params: React.PropsWithChildren<PonderProviderProps>,\n) {\n const { children, client } = params;\n\n return (\n <PonderContext.Provider value={client}>{children}</PonderContext.Provider>\n );\n}\n"],"mappings":";AAEA,SAAsB,cAAc;AACpC,SAAS,oBAAoB;AAC7B;AAAA,EAGE;AAAA,EACA;AAAA,OACK;AACP,SAAS,YAAY,WAAW,eAAe;;;ACN/C,OAAO,SAAS,qBAAqB;AAE9B,IAAM,gBAAgB,cAAkC,MAAS;AAMjE,SAAS,eACd,QACA;AACA,QAAM,EAAE,UAAU,OAAO,IAAI;AAE7B,SACE,oCAAC,cAAc,UAAd,EAAuB,OAAO,UAAS,QAAS;AAErD;;;ADPO,SAAS,eACd,QAGwB;AACxB,QAAM,cAAc,eAAe;AAEnC,QAAM,SAAS,WAAW,aAAa;AACvC,MAAI,WAAW,QAAW;AACxB,UAAM,IAAI,MAAM,0BAA0B;AAAA,EAC5C;AAEA,QAAM,eAAe,OAAO,QAAQ,OAAO,EAAE;AAE7C,QAAM,QAAQ,aAAa,YAAY;AACvC,QAAM,WAAW,QAAQ,MAAM,CAAC,MAAM,KAAK,GAAG,MAAM,MAAM,GAAG,CAAC,KAAK,CAAC;AAEpE,YAAU,MAAM;AACd,UAAM,EAAE,YAAY,IAAI,OAAO;AAAA,MAC7B,CAAC,OAAO,GAAG,OAAO,EAAE,KAAK,MAAM;AAAA,MAC/B,MAAM,YAAY,kBAAkB,EAAE,SAAS,CAAC;AAAA,IAClD;AACA,WAAO;AAAA,EACT,GAAG,CAAC,aAAa,QAAQ,QAAQ,CAAC;AAElC,SAAO,SAAS;AAAA,IACd,GAAG;AAAA,IACH;AAAA,IACA,SAAS,MAAM;AAAA,EACjB,CAAC;AACH;","names":[]}
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@ponder/react",
3
+ "version": "0.9.0-next.4",
4
+ "description": "React hooks for Ponder",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "sideEffects": false,
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/ponder-sh/ponder",
11
+ "directory": "packages/react"
12
+ },
13
+ "files": [
14
+ "dist",
15
+ "src/**/*.ts",
16
+ "!src/**/*.test.ts",
17
+ "!src/**/*.test-d.ts",
18
+ "!src/_test/**/*"
19
+ ],
20
+ "module": "./dist/index.js",
21
+ "types": "./dist/index.d.ts",
22
+ "typings": "./dist/index.d.ts",
23
+ "exports": {
24
+ ".": {
25
+ "types": "./dist/index.d.ts",
26
+ "import": "./dist/index.js"
27
+ }
28
+ },
29
+ "peerDependencies": {
30
+ "@ponder/client": ">=0.9.0",
31
+ "@tanstack/react-query": ">=5.0.0",
32
+ "react": ">=18",
33
+ "typescript": ">=5.0.4"
34
+ },
35
+ "peerDependenciesMeta": {
36
+ "typescript": {
37
+ "optional": true
38
+ }
39
+ },
40
+ "devDependencies": {
41
+ "@tanstack/react-query": "^5.12.2",
42
+ "@types/react": "^18.2.0",
43
+ "react": "^18.2.0",
44
+ "tsup": "^8.0.1"
45
+ },
46
+ "imports": {
47
+ "@ponder/client": "../client/index.ts"
48
+ },
49
+ "scripts": {
50
+ "build": "tsup",
51
+ "test": "vitest",
52
+ "test:typecheck": "vitest --typecheck.only",
53
+ "typecheck": "tsc --noEmit"
54
+ }
55
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { usePonderQuery } from "./usePonderQuery.js";
2
+ export { PonderProvider } from "./context.js";
@@ -0,0 +1,44 @@
1
+ "use client";
2
+
3
+ import { type Client, status } from "@ponder/client";
4
+ import { compileQuery } from "@ponder/client";
5
+ import {
6
+ type UseQueryOptions,
7
+ type UseQueryResult,
8
+ useQuery,
9
+ useQueryClient,
10
+ } from "@tanstack/react-query";
11
+ import { useContext, useEffect, useMemo } from "react";
12
+ import { PonderContext } from "./context.js";
13
+
14
+ export function usePonderQuery<result>(
15
+ params: {
16
+ queryFn: (db: Client["db"]) => Promise<result>;
17
+ } & Omit<UseQueryOptions<result>, "queryFn" | "queryKey">,
18
+ ): UseQueryResult<result> {
19
+ const queryClient = useQueryClient();
20
+
21
+ const client = useContext(PonderContext);
22
+ if (client === undefined) {
23
+ throw new Error("PonderProvider not found");
24
+ }
25
+
26
+ const queryPromise = params.queryFn(client.db);
27
+ // @ts-ignore
28
+ const query = compileQuery(queryPromise);
29
+ const queryKey = useMemo(() => [query.sql, ...query.params], [query]);
30
+
31
+ useEffect(() => {
32
+ const { unsubscribe } = client.live(
33
+ (db) => db.select().from(status),
34
+ () => queryClient.invalidateQueries({ queryKey }),
35
+ );
36
+ return unsubscribe;
37
+ }, [queryClient, client, queryKey]);
38
+
39
+ return useQuery({
40
+ ...params,
41
+ queryKey,
42
+ queryFn: () => queryPromise,
43
+ });
44
+ }