@eleven-am/golem-react 0.2.0 → 0.2.1

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/README.md CHANGED
@@ -10,7 +10,7 @@ Built on `@eleven-am/golem-client` and `@tanstack/react-query`.
10
10
  npm install @eleven-am/golem-react @eleven-am/golem-client @tanstack/react-query react gql.tada graphql graphql-ws
11
11
  ```
12
12
 
13
- Peer dependencies: `@eleven-am/golem-client` ^0.1.0, `@tanstack/react-query` ^5, `react` >=18, `gql.tada` ^1.8. The client in turn needs `graphql` ^16 and `graphql-ws` ^6 (and `@tanstack/query-core` ^5, which `@tanstack/react-query` brings).
13
+ Peer dependencies: `@eleven-am/golem-client` ^0.2.1, `@tanstack/react-query` ^5, `react` >=18, `gql.tada` ^1.8. The client in turn needs `graphql` ^16 and `graphql-ws` ^6 (and `@tanstack/query-core` ^5, which `@tanstack/react-query` brings).
14
14
 
15
15
  ## Setup
16
16
 
@@ -84,6 +84,10 @@ const status = useGolemStatus(POSTS)
84
84
  return <small>{status?.state}</small>
85
85
  ```
86
86
 
87
+ ### useGolemConnection
88
+
89
+ The connection state of the whole client, as one of the `transport.status` values: `idle` before any live query has opened a connection, then `connecting`, `open`, `reconnecting` or one of the fatal states. It is global, unlike `useGolemStatus`.
90
+
87
91
  ### useGolemMutation
88
92
 
89
93
  `useMutation` over `client.mutate`. A committed mutation invalidates every cached query over the models it touches, so the live event or the refetch brings it up to date. The mutation result is never written into the cache. A document holds exactly one operation.
@@ -195,17 +199,11 @@ The client adds `_version` and `__typename` to every selection of a model with e
195
199
  - `golem.transport.status`: `idle`, `connecting`, `open`, `reconnecting`, `unauthenticated`, `forbidden`, `invalid`, `protocol-mismatch`, `contract-mismatch` or `deadline-required`.
196
200
  - `golem.transport.onStatus(listener)`: calls you on every change and returns an unsubscribe function.
197
201
 
198
- Wrap them in `useSyncExternalStore`:
202
+ `useGolemConnection()` wraps both and returns the transport status. It re-renders on every change.
199
203
 
200
204
  ```tsx
201
- import { useSyncExternalStore } from 'react'
202
-
203
- function useConnection() {
204
- return useSyncExternalStore(
205
- (notify) => golem.transport.onStatus(notify),
206
- () => golem.transport.status,
207
- )
208
- }
205
+ const connection = useGolemConnection()
206
+ return connection === 'open' ? null : <small>{connection}</small>
209
207
  ```
210
208
 
211
209
  #### Why there is a manifest
package/dist/status.d.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  import { type QueryKey } from '@tanstack/react-query';
2
- import type { GolemClient, LiveStatus } from '@eleven-am/golem-client';
2
+ import type { GolemClient, LiveStatus, SessionStatus } from '@eleven-am/golem-client';
3
3
  import type { GolemDocument } from './types.js';
4
4
  export declare function useLiveStatus(client: GolemClient, queryKey: QueryKey): LiveStatus | undefined;
5
5
  export interface GolemStatusOptions {
6
6
  readonly infinite?: boolean;
7
7
  }
8
8
  export declare function useGolemStatus(document: GolemDocument, variables?: Readonly<Record<string, unknown>>, options?: GolemStatusOptions): LiveStatus | undefined;
9
+ export declare function useGolemConnection(): SessionStatus;
package/dist/status.js CHANGED
@@ -16,3 +16,9 @@ export function useGolemStatus(document, variables = {}, options = {}) {
16
16
  const queryKey = useMemo(() => (options.infinite === true ? client.infiniteQueryOptions(document, variables) : client.queryOptions(document, variables)).queryKey, [client, document, variablesHash, options.infinite]);
17
17
  return useLiveStatus(client, queryKey);
18
18
  }
19
+ export function useGolemConnection() {
20
+ const client = useGolemClient();
21
+ const subscribe = useCallback((listener) => client.transport.onStatus(listener), [client]);
22
+ const snapshot = useCallback(() => client.transport.status, [client]);
23
+ return useSyncExternalStore(subscribe, snapshot, snapshot);
24
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eleven-am/golem-react",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "React bindings for the golem live GraphQL client",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -20,7 +20,7 @@
20
20
  "test": "tsc --noEmit -p tsconfig.spec.json && vitest run"
21
21
  },
22
22
  "peerDependencies": {
23
- "@eleven-am/golem-client": "^0.2.0",
23
+ "@eleven-am/golem-client": "^0.2.1",
24
24
  "@tanstack/react-query": "^5.0.0",
25
25
  "gql.tada": "^1.8.0",
26
26
  "react": ">=18"