@dotwire/react 0.2.0 → 0.3.0
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 +6 -2
- package/dist/index.cjs +6 -21
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +6 -21
- package/dist/index.js.map +1 -1
- package/package.json +7 -4
package/README.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
For coding agents adding dotwire to an application, see the [SDK integration guide](https://github.com/pasztor-mark/dotwire/blob/main/docs/SDK_AGENT_GUIDE.md).
|
|
4
4
|
|
|
5
|
+
Full documentation: [https://dotwire.pasztormark.dev/docs](https://dotwire.pasztormark.dev/docs)
|
|
6
|
+
|
|
5
7
|
React hooks and a provider for [`@dotwire/client`](https://www.npmjs.com/package/@dotwire/client), the TypeScript/JavaScript SDK
|
|
6
8
|
for **dotwire**, the high-throughput, self-hosted real-time messaging infrastructure.
|
|
7
9
|
|
|
@@ -96,12 +98,14 @@ function Room({ roomId }: { roomId: string }) {
|
|
|
96
98
|
- **`useConnectionState()`** → the client's live `ConnectionState`
|
|
97
99
|
(`'disconnected' | 'connecting' | 'connected' | 'reconnecting' | 'disconnecting'`).
|
|
98
100
|
- **`useRoom(roomId, { history = 50 })`** → `{ messages, status, error, hasMore, loadOlder, send }`.
|
|
101
|
+
`messages` grows for as long as the component stays mounted (older pages are merged in by `loadOlder`); remount or key the component per conversation for very long-lived rooms. Render `content` as text, never as HTML.
|
|
99
102
|
Subscribes for the lifetime of the component, loads the initial history page, and applies
|
|
100
103
|
live messages, retractions, and revocation. `messages` is always sorted by `seq` and deduped.
|
|
101
104
|
`status` starts `'connecting'`, becomes `'ready'` once the initial subscribe resolves,
|
|
102
|
-
`'revoked'` on membership revocation, or `'error'` if subscribe
|
|
105
|
+
`'revoked'` on membership revocation, or `'error'` if subscribe or `loadOlder` throws. `loadOlder()`
|
|
103
106
|
pages backwards with `beforeSeq` and prepends the results; `hasMore` reflects whether an
|
|
104
|
-
older page might still exist. `send(content)` wraps `sendMessage
|
|
107
|
+
older page might still exist. `send(content, { idempotencyKey }?)` wraps `sendMessage`: a failed
|
|
108
|
+
send rejects its promise (for example `PresendRejected`) and leaves `status` and `error` unchanged.
|
|
105
109
|
- **`useTyping(roomId)`** → `{ typingUsers, notifyTyping, stopTyping }`. A user drops out of
|
|
106
110
|
`typingUsers` either on an explicit "stopped typing" notification or after ~3.5s of silence
|
|
107
111
|
(time-based expiry, checked every second).
|
package/dist/index.cjs
CHANGED
|
@@ -116,22 +116,12 @@ function typingReducer(state, action) {
|
|
|
116
116
|
if (action.isTyping) {
|
|
117
117
|
return { ...state, [action.userId]: { updatedAt: action.now } };
|
|
118
118
|
}
|
|
119
|
-
if (!(action.userId
|
|
120
|
-
|
|
121
|
-
delete next[action.userId];
|
|
122
|
-
return next;
|
|
119
|
+
if (!Object.hasOwn(state, action.userId)) return state;
|
|
120
|
+
return Object.fromEntries(Object.entries(state).filter(([userId]) => userId !== action.userId));
|
|
123
121
|
}
|
|
124
122
|
case "expire": {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
for (const [userId, entry] of Object.entries(state)) {
|
|
128
|
-
if (action.now - entry.updatedAt < action.ttlMs) {
|
|
129
|
-
next[userId] = entry;
|
|
130
|
-
} else {
|
|
131
|
-
changed = true;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
return changed ? next : state;
|
|
123
|
+
const kept = Object.entries(state).filter(([, entry]) => action.now - entry.updatedAt < action.ttlMs);
|
|
124
|
+
return kept.length === Object.keys(state).length ? state : Object.fromEntries(kept);
|
|
135
125
|
}
|
|
136
126
|
default:
|
|
137
127
|
return state;
|
|
@@ -237,16 +227,11 @@ function useRoom(roomId, options = {}) {
|
|
|
237
227
|
}
|
|
238
228
|
}, [client, roomId, history]);
|
|
239
229
|
const send = (0, import_react2.useCallback)(
|
|
240
|
-
async (content) => {
|
|
230
|
+
async (content, options2) => {
|
|
241
231
|
if (!client) {
|
|
242
232
|
throw new Error("Dotwire client is not ready yet.");
|
|
243
233
|
}
|
|
244
|
-
|
|
245
|
-
return await client.sendMessage(roomId, content);
|
|
246
|
-
} catch (error) {
|
|
247
|
-
dispatch({ type: "error", error });
|
|
248
|
-
throw error;
|
|
249
|
-
}
|
|
234
|
+
return client.sendMessage(roomId, content, void 0, options2);
|
|
250
235
|
},
|
|
251
236
|
[client, roomId]
|
|
252
237
|
);
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/provider.tsx","../src/hooks.ts","../src/reducers.ts","../src/subscription.ts"],"sourcesContent":["export { DotwireProvider, DotwireContext, type DotwireProviderProps } from './provider.js';\nexport {\n useDotwireClient,\n useConnectionState,\n useRoom,\n useTyping,\n usePresence,\n type UseRoomOptions,\n type UseRoomResult,\n type UseTypingResult,\n type UsePresenceResult\n} from './hooks.js';\nexport * from './reducers.js';\n\n// Re-exported for convenience so consumers of `@dotwire/react` do not also need a direct\n// dependency on `@dotwire/client` just to type `options`/`client` props or hook results.\nexport type {\n ConnectionState,\n DotwireClient,\n DotwireClientOptions,\n HistoryPage,\n MembershipRevoked,\n MessageRetracted,\n PresenceDelta,\n RoomMessage,\n SendAck,\n TypingNotification\n} from '@dotwire/client';\n","import { createContext, useEffect, useState, type ReactNode } from 'react';\nimport { DotwireClient, type DotwireClientOptions } from '@dotwire/client';\n\nexport const DotwireContext = createContext<DotwireClient | null>(null);\n\nexport interface DotwireProviderProps {\n children?: ReactNode;\n /**\n * Options to construct a `DotwireClient` internally. Ignored if `client` is also given.\n * Construct this once (e.g. with `useMemo`/`useState`, or a module-level constant) — a new\n * object identity on every render tears down and rebuilds the client.\n */\n options?: DotwireClientOptions;\n /**\n * A pre-built `DotwireClient` instance. When given, the provider never constructs or\n * disposes a client itself — the caller owns that instance's lifecycle.\n */\n client?: DotwireClient;\n}\n\n/**\n * Provides a `DotwireClient` to `useDotwireClient`/`useConnectionState`/`useRoom`/`useTyping`/\n * `usePresence` (spec §7).\n *\n * - Construction happens in an effect, never during render, so this component is safe to\n * render on the server (`react-dom/server`) — `useDotwireClient()` simply returns `null`\n * until the effect runs on the client. See `test/ssr.test.tsx`.\n * - In React 18 StrictMode, effects run an extra setup+cleanup pass in development; the\n * cleanup below disposes whatever instance that pass constructed before the next one runs,\n * so double-invocation is safe.\n * - A client built from `options` is disposed on unmount (or when `options` changes). A\n * client passed via the `client` prop is never disposed here — the caller owns it.\n */\nexport function DotwireProvider(props: DotwireProviderProps): ReactNode {\n const { children, options, client: providedClient } = props;\n const [managedClient, setManagedClient] = useState<DotwireClient | null>(null);\n\n useEffect(() => {\n if (providedClient || !options) {\n return undefined;\n }\n\n const instance = new DotwireClient(options);\n setManagedClient(instance);\n\n return () => {\n instance.dispose();\n setManagedClient((current) => (current === instance ? null : current));\n };\n // `options` is compared by reference deliberately: a new object rebuilds the client.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [providedClient, options]);\n\n const client = providedClient ?? managedClient;\n\n return <DotwireContext.Provider value={client}>{children}</DotwireContext.Provider>;\n}\n","import { useCallback, useContext, useEffect, useReducer, useRef, useState } from 'react';\nimport type { ConnectionState, DotwireClient, RoomMessage, SendAck } from '@dotwire/client';\nimport { DotwireContext } from './provider.js';\nimport {\n initialPresenceState,\n initialRoomMessagesState,\n initialTypingState,\n presenceReducer,\n roomMessagesReducer,\n typingReducer,\n typingUsersOf,\n type RoomMessagesState,\n type RoomStatus\n} from './reducers.js';\nimport { releaseRoom, retainRoom } from './subscription.js';\n\n/** The `DotwireClient` from the nearest `DotwireProvider`, or `null` before it has mounted. */\nexport function useDotwireClient(): DotwireClient | null {\n return useContext(DotwireContext);\n}\n\n/** The client's live `ConnectionState`, re-rendering on every `onStateChange` event. */\nexport function useConnectionState(): ConnectionState {\n const client = useDotwireClient();\n const [state, setState] = useState<ConnectionState>(() => client?.getConnectionState() ?? 'disconnected');\n\n useEffect(() => {\n if (!client) {\n setState('disconnected');\n return undefined;\n }\n setState(client.getConnectionState());\n return client.onStateChange(setState);\n }, [client]);\n\n return state;\n}\n\nexport interface UseRoomOptions {\n /** How many of the latest messages to load on subscribe. Default 50. */\n history?: number;\n}\n\nexport interface UseRoomResult {\n messages: RoomMessage[];\n status: RoomStatus;\n error: unknown;\n hasMore: boolean;\n loadOlder: () => Promise<void>;\n send: (content: string) => Promise<SendAck>;\n}\n\n/**\n * Subscribes to `roomId` for as long as the component is mounted: loads the initial history\n * page, applies live messages/retractions/revocation, and exposes `loadOlder`/`send` (spec\n * §7). All state transitions are delegated to `roomMessagesReducer` (`src/reducers.ts`).\n */\nexport function useRoom(roomId: string, options: UseRoomOptions = {}): UseRoomResult {\n const history = options.history ?? 50;\n const client = useDotwireClient();\n const [state, dispatch] = useReducer(roomMessagesReducer, initialRoomMessagesState);\n const stateRef = useRef<RoomMessagesState>(state);\n stateRef.current = state;\n const scopeRef = useRef<object | null>(null);\n\n useEffect(() => {\n if (!client) return undefined;\n\n const scope = {};\n scopeRef.current = scope;\n dispatch({ type: 'reset' });\n const room = client.room(roomId);\n retainRoom(client, roomId);\n let cancelled = false;\n\n const unsubscribers = [\n room.onMessage((message) => dispatch({ type: 'message', message })),\n room.onMessageRetracted(({ seq }) => dispatch({ type: 'retracted', seq })),\n room.onMembershipRevoked(() => dispatch({ type: 'revoked' }))\n ];\n\n room\n .subscribe({ history })\n .then((page) => {\n if (cancelled) return;\n dispatch({ type: 'subscribed', page: page ?? { messages: [], hasMore: false } });\n })\n .catch((error: unknown) => {\n if (!cancelled) dispatch({ type: 'error', error });\n });\n\n return () => {\n cancelled = true;\n if (scopeRef.current === scope) scopeRef.current = null;\n for (const unsub of unsubscribers) unsub();\n room.release();\n // Only the last interested hook for this (client, roomId) actually leaves the hub\n // group — see `src/subscription.ts`. We deliberately do NOT call `room.dispose()`\n // here: it would unconditionally unsubscribe, which is exactly what the refcount\n // guards against when `useTyping`/`usePresence` share the same room.\n if (releaseRoom(client, roomId)) {\n client.unsubscribe(roomId).catch(() => {});\n }\n };\n }, [client, roomId, history]);\n\n const loadOlder = useCallback(async () => {\n if (!client) return;\n const oldest = stateRef.current.messages[0];\n if (!oldest) return;\n const scope = scopeRef.current;\n try {\n const page = await client.getHistory(roomId, { beforeSeq: oldest.seq, limit: history });\n if (scopeRef.current === scope) dispatch({ type: 'olderLoaded', page });\n } catch (error) {\n if (scopeRef.current === scope) dispatch({ type: 'error', error });\n }\n }, [client, roomId, history]);\n\n const send = useCallback(\n async (content: string): Promise<SendAck> => {\n if (!client) {\n throw new Error('Dotwire client is not ready yet.');\n }\n try {\n return await client.sendMessage(roomId, content);\n } catch (error) {\n dispatch({ type: 'error', error });\n throw error;\n }\n },\n [client, roomId]\n );\n\n return {\n messages: state.messages,\n status: state.status,\n error: state.error,\n hasMore: state.hasMore,\n loadOlder,\n send\n };\n}\n\nexport interface UseTypingResult {\n /** User ids currently considered typing (expired entries are removed automatically). */\n typingUsers: string[];\n notifyTyping: () => Promise<void>;\n stopTyping: () => Promise<void>;\n}\n\n/** How long a typing notification stays valid absent a follow-up (spec §7: \"with expiry\"). */\nconst TYPING_EXPIRY_MS = 3500;\nconst TYPING_EXPIRY_CHECK_INTERVAL_MS = 1000;\n\n/**\n * Tracks who is typing in `roomId`. Expiry is time-based (a user who goes silent without an\n * explicit stop notification drops out after `TYPING_EXPIRY_MS`) and driven by `typingReducer`,\n * which takes an explicit `now` so it is unit-testable without timers.\n */\nexport function useTyping(roomId: string): UseTypingResult {\n const client = useDotwireClient();\n const [state, dispatch] = useReducer(typingReducer, initialTypingState);\n const roomHandleRef = useRef<ReturnType<DotwireClient['room']> | null>(null);\n\n useEffect(() => {\n if (!client) return undefined;\n\n const room = client.room(roomId);\n roomHandleRef.current = room;\n retainRoom(client, roomId);\n // Typing/presence delivery requires hub group membership, same as message delivery.\n room.subscribe().catch(() => {\n // Best-effort: if this never resolves, no typing events will arrive for this room;\n // the caller can observe that via useConnectionState/useRoom's own error state.\n });\n\n const unsub = room.onTyping((notification) => {\n dispatch({ type: 'notification', userId: notification.userId, isTyping: notification.isTyping, now: Date.now() });\n });\n\n const interval = setInterval(() => {\n dispatch({ type: 'expire', now: Date.now(), ttlMs: TYPING_EXPIRY_MS });\n }, TYPING_EXPIRY_CHECK_INTERVAL_MS);\n\n return () => {\n unsub();\n clearInterval(interval);\n roomHandleRef.current = null;\n room.release();\n if (releaseRoom(client, roomId)) {\n client.unsubscribe(roomId).catch(() => {});\n }\n };\n }, [client, roomId]);\n\n const notifyTyping = useCallback(async () => {\n await roomHandleRef.current?.notifyTyping();\n }, []);\n\n const stopTyping = useCallback(async () => {\n await roomHandleRef.current?.stopTyping();\n }, []);\n\n return { typingUsers: typingUsersOf(state), notifyTyping, stopTyping };\n}\n\nexport interface UsePresenceResult {\n /** User ids present, accumulated from deltas since this hook mounted. */\n present: string[];\n}\n\n/**\n * Accumulates presence deltas for `roomId` since mount (spec §7). There is no presence\n * snapshot endpoint on the server, so a component that mounts this hook after other members\n * already joined will not see them in `present` until the next `PresenceUpdated` delta for\n * this room (e.g. the next member to join or leave).\n */\nexport function usePresence(roomId: string): UsePresenceResult {\n const client = useDotwireClient();\n const [state, dispatch] = useReducer(presenceReducer, initialPresenceState);\n\n useEffect(() => {\n if (!client) return undefined;\n\n const room = client.room(roomId);\n retainRoom(client, roomId);\n room.subscribe().catch(() => {\n // Best-effort, same rationale as useTyping.\n });\n\n const unsub = room.onPresence((delta) => {\n dispatch({ type: 'delta', joined: delta.joined, left: delta.left });\n });\n\n return () => {\n unsub();\n room.release();\n if (releaseRoom(client, roomId)) {\n client.unsubscribe(roomId).catch(() => {});\n }\n };\n }, [client, roomId]);\n\n return { present: state };\n}\n","/**\n * Pure state-transition functions backing `useRoom`, `useTyping`, and `usePresence`\n * (spec §7). None of these import React; each is unit-tested directly with Node's\n * test runner in `test/reducers.test.ts`. The hooks in `src/hooks.ts` are thin\n * `useReducer` wrappers around these.\n */\nimport type { HistoryPage, RoomMessage } from '@dotwire/client';\n\n// ---------------------------------------------------------------------------\n// Room messages\n// ---------------------------------------------------------------------------\n\nexport type RoomStatus = 'connecting' | 'ready' | 'revoked' | 'error';\n\nexport interface RoomMessagesState {\n messages: RoomMessage[];\n status: RoomStatus;\n error: unknown;\n hasMore: boolean;\n}\n\nexport type RoomMessagesAction =\n | { type: 'reset' }\n | { type: 'subscribed'; page: HistoryPage }\n | { type: 'message'; message: RoomMessage }\n | { type: 'retracted'; seq: number }\n | { type: 'revoked' }\n | { type: 'error'; error: unknown }\n | { type: 'olderLoaded'; page: HistoryPage };\n\nexport const initialRoomMessagesState: RoomMessagesState = {\n messages: [],\n status: 'connecting',\n error: null,\n hasMore: false\n};\n\n/**\n * Merges `incoming` into `existing`, deduping by `seq` (last write for a given seq\n * wins) and returning the result sorted ascending by `seq` (spec §6, §7: \"consumers\n * order by seq themselves\").\n */\nfunction mergeMessages(existing: RoomMessage[], incoming: RoomMessage[]): RoomMessage[] {\n if (incoming.length === 0) return existing;\n const last = existing[existing.length - 1];\n if (incoming.length === 1 && (last === undefined || incoming[0].seq > last.seq)) return [...existing, incoming[0]];\n const bySeq = new Map<number, RoomMessage>();\n for (const message of existing) bySeq.set(message.seq, message);\n for (const message of incoming) bySeq.set(message.seq, message);\n return Array.from(bySeq.values()).sort((a, b) => a.seq - b.seq);\n}\n\nexport function roomMessagesReducer(\n state: RoomMessagesState,\n action: RoomMessagesAction\n): RoomMessagesState {\n switch (action.type) {\n case 'reset':\n return initialRoomMessagesState;\n case 'subscribed':\n return {\n messages: mergeMessages(state.messages, action.page.messages),\n status: 'ready',\n error: null,\n hasMore: action.page.hasMore\n };\n case 'message':\n return { ...state, messages: mergeMessages(state.messages, [action.message]) };\n case 'retracted':\n return { ...state, messages: state.messages.filter((m) => m.seq !== action.seq) };\n case 'revoked':\n return { ...state, status: 'revoked' };\n case 'error':\n return { ...state, status: 'error', error: action.error };\n case 'olderLoaded':\n return {\n ...state,\n messages: mergeMessages(state.messages, action.page.messages),\n hasMore: action.page.hasMore\n };\n default:\n return state;\n }\n}\n\n// ---------------------------------------------------------------------------\n// Typing\n// ---------------------------------------------------------------------------\n\nexport interface TypingEntry {\n updatedAt: number;\n}\n\n/** Keyed by userId; a key is present only while that user is considered typing. */\nexport type TypingState = Record<string, TypingEntry>;\n\nexport type TypingAction =\n | { type: 'notification'; userId: string; isTyping: boolean; now: number }\n | { type: 'expire'; now: number; ttlMs: number };\n\nexport const initialTypingState: TypingState = {};\n\nexport function typingReducer(state: TypingState, action: TypingAction): TypingState {\n switch (action.type) {\n case 'notification': {\n if (action.isTyping) {\n return { ...state, [action.userId]: { updatedAt: action.now } };\n }\n if (!(action.userId in state)) return state;\n const next = { ...state };\n delete next[action.userId];\n return next;\n }\n case 'expire': {\n let changed = false;\n const next: TypingState = {};\n for (const [userId, entry] of Object.entries(state)) {\n if (action.now - entry.updatedAt < action.ttlMs) {\n next[userId] = entry;\n } else {\n changed = true;\n }\n }\n return changed ? next : state;\n }\n default:\n return state;\n }\n}\n\nexport function typingUsersOf(state: TypingState): string[] {\n return Object.keys(state);\n}\n\n// ---------------------------------------------------------------------------\n// Presence\n// ---------------------------------------------------------------------------\n\n/** Sorted, deduped user ids accumulated from deltas since mount (spec §7: no snapshot endpoint). */\nexport type PresenceState = string[];\n\nexport type PresenceAction = { type: 'delta'; joined: string[]; left: string[] };\n\nexport const initialPresenceState: PresenceState = [];\n\nexport function presenceReducer(state: PresenceState, action: PresenceAction): PresenceState {\n const set = new Set(state);\n for (const id of action.left) set.delete(id);\n for (const id of action.joined) set.add(id);\n return Array.from(set).sort();\n}\n","import type { DotwireClient } from '@dotwire/client';\n\n/**\n * `DotwireClient.subscribe`/`unsubscribe` (and `DotwireRoomHandle.subscribe`/`unsubscribe`)\n * operate on a single, client-wide \"am I in this room's hub group\" flag per `roomId` — it is\n * not refcounted. If two hooks (e.g. `useRoom` and `useTyping`) are both mounted against the\n * same room, calling `subscribe` from each is harmless (redundant hub invokes), but the FIRST\n * hook to unmount would call `unsubscribe` and silently drop the OTHER hook's live delivery.\n *\n * This module refcounts unsubscribe intent per `(client, roomId)` pair so the underlying hub\n * group is only actually left once every hook interested in that room has released it. It does\n * not wrap `subscribe` itself, since redundant `Subscribe` hub invokes are cheap and safe.\n */\nconst refCounts = new WeakMap<DotwireClient, Map<string, number>>();\n\nexport function retainRoom(client: DotwireClient, roomId: string): void {\n let rooms = refCounts.get(client);\n if (!rooms) {\n rooms = new Map();\n refCounts.set(client, rooms);\n }\n rooms.set(roomId, (rooms.get(roomId) ?? 0) + 1);\n}\n\n/**\n * Releases one hook's interest in `roomId`. Returns `true` when this was the last interested\n * hook, meaning the caller should now call `client.unsubscribe(roomId)`.\n */\nexport function releaseRoom(client: DotwireClient, roomId: string): boolean {\n const rooms = refCounts.get(client);\n if (!rooms) return true;\n const next = (rooms.get(roomId) ?? 1) - 1;\n if (next <= 0) {\n rooms.delete(roomId);\n return true;\n }\n rooms.set(roomId, next);\n return false;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAAmE;AACnE,oBAAyD;AAsDhD;AApDF,IAAM,qBAAiB,4BAAoC,IAAI;AA8B/D,SAAS,gBAAgB,OAAwC;AACtE,QAAM,EAAE,UAAU,SAAS,QAAQ,eAAe,IAAI;AACtD,QAAM,CAAC,eAAe,gBAAgB,QAAI,uBAA+B,IAAI;AAE7E,8BAAU,MAAM;AACd,QAAI,kBAAkB,CAAC,SAAS;AAC9B,aAAO;AAAA,IACT;AAEA,UAAM,WAAW,IAAI,4BAAc,OAAO;AAC1C,qBAAiB,QAAQ;AAEzB,WAAO,MAAM;AACX,eAAS,QAAQ;AACjB,uBAAiB,CAAC,YAAa,YAAY,WAAW,OAAO,OAAQ;AAAA,IACvE;AAAA,EAGF,GAAG,CAAC,gBAA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AAAA,MAC3C;AAAA,IACF;AAAA,EACF,GAAG,CAAC,QAAQ,MAAM,CAAC;AAEnB,SAAO,EAAE,SAAS,MAAM;AAC1B;","names":["import_react"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/provider.tsx","../src/hooks.ts","../src/reducers.ts","../src/subscription.ts"],"sourcesContent":["export { DotwireProvider, DotwireContext, type DotwireProviderProps } from './provider.js';\nexport {\n useDotwireClient,\n useConnectionState,\n useRoom,\n useTyping,\n usePresence,\n type UseRoomOptions,\n type UseRoomResult,\n type UseTypingResult,\n type UsePresenceResult\n} from './hooks.js';\nexport * from './reducers.js';\n\n// Re-exported for convenience so consumers of `@dotwire/react` do not also need a direct\n// dependency on `@dotwire/client` just to type `options`/`client` props or hook results.\nexport type {\n ConnectionState,\n DotwireClient,\n DotwireClientOptions,\n HistoryPage,\n MembershipRevoked,\n MessageRetracted,\n PresenceDelta,\n RoomMessage,\n SendAck,\n SendMessageOptions,\n TypingNotification\n} from '@dotwire/client';\n","import { createContext, useEffect, useState, type ReactNode } from 'react';\nimport { DotwireClient, type DotwireClientOptions } from '@dotwire/client';\n\nexport const DotwireContext = createContext<DotwireClient | null>(null);\n\nexport interface DotwireProviderProps {\n children?: ReactNode;\n /**\n * Options to construct a `DotwireClient` internally. Ignored if `client` is also given.\n * Construct this once (e.g. with `useMemo`/`useState`, or a module-level constant) — a new\n * object identity on every render tears down and rebuilds the client.\n */\n options?: DotwireClientOptions;\n /**\n * A pre-built `DotwireClient` instance. When given, the provider never constructs or\n * disposes a client itself — the caller owns that instance's lifecycle.\n */\n client?: DotwireClient;\n}\n\n/**\n * Provides a `DotwireClient` to `useDotwireClient`/`useConnectionState`/`useRoom`/`useTyping`/\n * `usePresence` (spec §7).\n *\n * - Construction happens in an effect, never during render, so this component is safe to\n * render on the server (`react-dom/server`) — `useDotwireClient()` simply returns `null`\n * until the effect runs on the client. See `test/ssr.test.tsx`.\n * - In React 18 StrictMode, effects run an extra setup+cleanup pass in development; the\n * cleanup below disposes whatever instance that pass constructed before the next one runs,\n * so double-invocation is safe.\n * - A client built from `options` is disposed on unmount (or when `options` changes). A\n * client passed via the `client` prop is never disposed here — the caller owns it.\n */\nexport function DotwireProvider(props: DotwireProviderProps): ReactNode {\n const { children, options, client: providedClient } = props;\n const [managedClient, setManagedClient] = useState<DotwireClient | null>(null);\n\n useEffect(() => {\n if (providedClient || !options) {\n return undefined;\n }\n\n const instance = new DotwireClient(options);\n setManagedClient(instance);\n\n return () => {\n instance.dispose();\n setManagedClient((current) => (current === instance ? null : current));\n };\n // `options` is compared by reference deliberately: a new object rebuilds the client.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [providedClient, options]);\n\n const client = providedClient ?? managedClient;\n\n return <DotwireContext.Provider value={client}>{children}</DotwireContext.Provider>;\n}\n","import { useCallback, useContext, useEffect, useReducer, useRef, useState } from 'react';\nimport type { ConnectionState, DotwireClient, RoomMessage, SendAck, SendMessageOptions } from '@dotwire/client';\nimport { DotwireContext } from './provider.js';\nimport {\n initialPresenceState,\n initialRoomMessagesState,\n initialTypingState,\n presenceReducer,\n roomMessagesReducer,\n typingReducer,\n typingUsersOf,\n type RoomMessagesState,\n type RoomStatus\n} from './reducers.js';\nimport { releaseRoom, retainRoom } from './subscription.js';\n\n/** The `DotwireClient` from the nearest `DotwireProvider`, or `null` before it has mounted. */\nexport function useDotwireClient(): DotwireClient | null {\n return useContext(DotwireContext);\n}\n\n/** The client's live `ConnectionState`, re-rendering on every `onStateChange` event. */\nexport function useConnectionState(): ConnectionState {\n const client = useDotwireClient();\n const [state, setState] = useState<ConnectionState>(() => client?.getConnectionState() ?? 'disconnected');\n\n useEffect(() => {\n if (!client) {\n setState('disconnected');\n return undefined;\n }\n setState(client.getConnectionState());\n return client.onStateChange(setState);\n }, [client]);\n\n return state;\n}\n\nexport interface UseRoomOptions {\n /** How many of the latest messages to load on subscribe. Default 50. */\n history?: number;\n}\n\nexport interface UseRoomResult {\n messages: RoomMessage[];\n status: RoomStatus;\n error: unknown;\n hasMore: boolean;\n loadOlder: () => Promise<void>;\n send: (content: string, options?: SendMessageOptions) => Promise<SendAck>;\n}\n\n/**\n * Subscribes to `roomId` for as long as the component is mounted: loads the initial history\n * page, applies live messages/retractions/revocation, and exposes `loadOlder`/`send` (spec\n * §7). All state transitions are delegated to `roomMessagesReducer` (`src/reducers.ts`).\n */\nexport function useRoom(roomId: string, options: UseRoomOptions = {}): UseRoomResult {\n const history = options.history ?? 50;\n const client = useDotwireClient();\n const [state, dispatch] = useReducer(roomMessagesReducer, initialRoomMessagesState);\n const stateRef = useRef<RoomMessagesState>(state);\n stateRef.current = state;\n const scopeRef = useRef<object | null>(null);\n\n useEffect(() => {\n if (!client) return undefined;\n\n const scope = {};\n scopeRef.current = scope;\n dispatch({ type: 'reset' });\n const room = client.room(roomId);\n retainRoom(client, roomId);\n let cancelled = false;\n\n const unsubscribers = [\n room.onMessage((message) => dispatch({ type: 'message', message })),\n room.onMessageRetracted(({ seq }) => dispatch({ type: 'retracted', seq })),\n room.onMembershipRevoked(() => dispatch({ type: 'revoked' }))\n ];\n\n room\n .subscribe({ history })\n .then((page) => {\n if (cancelled) return;\n dispatch({ type: 'subscribed', page: page ?? { messages: [], hasMore: false } });\n })\n .catch((error: unknown) => {\n if (!cancelled) dispatch({ type: 'error', error });\n });\n\n return () => {\n cancelled = true;\n if (scopeRef.current === scope) scopeRef.current = null;\n for (const unsub of unsubscribers) unsub();\n room.release();\n // Only the last interested hook for this (client, roomId) actually leaves the hub\n // group — see `src/subscription.ts`. We deliberately do NOT call `room.dispose()`\n // here: it would unconditionally unsubscribe, which is exactly what the refcount\n // guards against when `useTyping`/`usePresence` share the same room.\n if (releaseRoom(client, roomId)) {\n client.unsubscribe(roomId).catch(() => {});\n }\n };\n }, [client, roomId, history]);\n\n const loadOlder = useCallback(async () => {\n if (!client) return;\n const oldest = stateRef.current.messages[0];\n if (!oldest) return;\n const scope = scopeRef.current;\n try {\n const page = await client.getHistory(roomId, { beforeSeq: oldest.seq, limit: history });\n if (scopeRef.current === scope) dispatch({ type: 'olderLoaded', page });\n } catch (error) {\n if (scopeRef.current === scope) dispatch({ type: 'error', error });\n }\n }, [client, roomId, history]);\n\n const send = useCallback(\n async (content: string, options?: SendMessageOptions): Promise<SendAck> => {\n if (!client) {\n throw new Error('Dotwire client is not ready yet.');\n }\n // A failed send is reported to the caller only: the subscription itself is still healthy.\n return client.sendMessage(roomId, content, undefined, options);\n },\n [client, roomId]\n );\n\n return {\n messages: state.messages,\n status: state.status,\n error: state.error,\n hasMore: state.hasMore,\n loadOlder,\n send\n };\n}\n\nexport interface UseTypingResult {\n /** User ids currently considered typing (expired entries are removed automatically). */\n typingUsers: string[];\n notifyTyping: () => Promise<void>;\n stopTyping: () => Promise<void>;\n}\n\n/** How long a typing notification stays valid absent a follow-up (spec §7: \"with expiry\"). */\nconst TYPING_EXPIRY_MS = 3500;\nconst TYPING_EXPIRY_CHECK_INTERVAL_MS = 1000;\n\n/**\n * Tracks who is typing in `roomId`. Expiry is time-based (a user who goes silent without an\n * explicit stop notification drops out after `TYPING_EXPIRY_MS`) and driven by `typingReducer`,\n * which takes an explicit `now` so it is unit-testable without timers.\n */\nexport function useTyping(roomId: string): UseTypingResult {\n const client = useDotwireClient();\n const [state, dispatch] = useReducer(typingReducer, initialTypingState);\n const roomHandleRef = useRef<ReturnType<DotwireClient['room']> | null>(null);\n\n useEffect(() => {\n if (!client) return undefined;\n\n const room = client.room(roomId);\n roomHandleRef.current = room;\n retainRoom(client, roomId);\n // Typing/presence delivery requires hub group membership, same as message delivery.\n room.subscribe().catch(() => {\n // Best-effort: if this never resolves, no typing events will arrive for this room;\n // the caller can observe that via useConnectionState/useRoom's own error state.\n });\n\n const unsub = room.onTyping((notification) => {\n dispatch({ type: 'notification', userId: notification.userId, isTyping: notification.isTyping, now: Date.now() });\n });\n\n const interval = setInterval(() => {\n dispatch({ type: 'expire', now: Date.now(), ttlMs: TYPING_EXPIRY_MS });\n }, TYPING_EXPIRY_CHECK_INTERVAL_MS);\n\n return () => {\n unsub();\n clearInterval(interval);\n roomHandleRef.current = null;\n room.release();\n if (releaseRoom(client, roomId)) {\n client.unsubscribe(roomId).catch(() => {});\n }\n };\n }, [client, roomId]);\n\n const notifyTyping = useCallback(async () => {\n await roomHandleRef.current?.notifyTyping();\n }, []);\n\n const stopTyping = useCallback(async () => {\n await roomHandleRef.current?.stopTyping();\n }, []);\n\n return { typingUsers: typingUsersOf(state), notifyTyping, stopTyping };\n}\n\nexport interface UsePresenceResult {\n /** User ids present, accumulated from deltas since this hook mounted. */\n present: string[];\n}\n\n/**\n * Accumulates presence deltas for `roomId` since mount (spec §7). There is no presence\n * snapshot endpoint on the server, so a component that mounts this hook after other members\n * already joined will not see them in `present` until the next `PresenceUpdated` delta for\n * this room (e.g. the next member to join or leave).\n */\nexport function usePresence(roomId: string): UsePresenceResult {\n const client = useDotwireClient();\n const [state, dispatch] = useReducer(presenceReducer, initialPresenceState);\n\n useEffect(() => {\n if (!client) return undefined;\n\n const room = client.room(roomId);\n retainRoom(client, roomId);\n room.subscribe().catch(() => {\n // Best-effort, same rationale as useTyping.\n });\n\n const unsub = room.onPresence((delta) => {\n dispatch({ type: 'delta', joined: delta.joined, left: delta.left });\n });\n\n return () => {\n unsub();\n room.release();\n if (releaseRoom(client, roomId)) {\n client.unsubscribe(roomId).catch(() => {});\n }\n };\n }, [client, roomId]);\n\n return { present: state };\n}\n","/**\n * Pure state-transition functions backing `useRoom`, `useTyping`, and `usePresence`\n * (spec §7). None of these import React; each is unit-tested directly with Node's\n * test runner in `test/reducers.test.ts`. The hooks in `src/hooks.ts` are thin\n * `useReducer` wrappers around these.\n */\nimport type { HistoryPage, RoomMessage } from '@dotwire/client';\n\n// ---------------------------------------------------------------------------\n// Room messages\n// ---------------------------------------------------------------------------\n\nexport type RoomStatus = 'connecting' | 'ready' | 'revoked' | 'error';\n\nexport interface RoomMessagesState {\n messages: RoomMessage[];\n status: RoomStatus;\n error: unknown;\n hasMore: boolean;\n}\n\nexport type RoomMessagesAction =\n | { type: 'reset' }\n | { type: 'subscribed'; page: HistoryPage }\n | { type: 'message'; message: RoomMessage }\n | { type: 'retracted'; seq: number }\n | { type: 'revoked' }\n | { type: 'error'; error: unknown }\n | { type: 'olderLoaded'; page: HistoryPage };\n\nexport const initialRoomMessagesState: RoomMessagesState = {\n messages: [],\n status: 'connecting',\n error: null,\n hasMore: false\n};\n\n/**\n * Merges `incoming` into `existing`, deduping by `seq` (last write for a given seq\n * wins) and returning the result sorted ascending by `seq` (spec §6, §7: \"consumers\n * order by seq themselves\").\n */\nfunction mergeMessages(existing: RoomMessage[], incoming: RoomMessage[]): RoomMessage[] {\n if (incoming.length === 0) return existing;\n const last = existing[existing.length - 1];\n if (incoming.length === 1 && (last === undefined || incoming[0].seq > last.seq)) return [...existing, incoming[0]];\n const bySeq = new Map<number, RoomMessage>();\n for (const message of existing) bySeq.set(message.seq, message);\n for (const message of incoming) bySeq.set(message.seq, message);\n return Array.from(bySeq.values()).sort((a, b) => a.seq - b.seq);\n}\n\nexport function roomMessagesReducer(\n state: RoomMessagesState,\n action: RoomMessagesAction\n): RoomMessagesState {\n switch (action.type) {\n case 'reset':\n return initialRoomMessagesState;\n case 'subscribed':\n return {\n messages: mergeMessages(state.messages, action.page.messages),\n status: 'ready',\n error: null,\n hasMore: action.page.hasMore\n };\n case 'message':\n return { ...state, messages: mergeMessages(state.messages, [action.message]) };\n case 'retracted':\n return { ...state, messages: state.messages.filter((m) => m.seq !== action.seq) };\n case 'revoked':\n return { ...state, status: 'revoked' };\n case 'error':\n return { ...state, status: 'error', error: action.error };\n case 'olderLoaded':\n return {\n ...state,\n messages: mergeMessages(state.messages, action.page.messages),\n hasMore: action.page.hasMore\n };\n default:\n return state;\n }\n}\n\n// ---------------------------------------------------------------------------\n// Typing\n// ---------------------------------------------------------------------------\n\nexport interface TypingEntry {\n updatedAt: number;\n}\n\n/** Keyed by userId; a key is present only while that user is considered typing. */\nexport type TypingState = Record<string, TypingEntry>;\n\nexport type TypingAction =\n | { type: 'notification'; userId: string; isTyping: boolean; now: number }\n | { type: 'expire'; now: number; ttlMs: number };\n\nexport const initialTypingState: TypingState = {};\n\nexport function typingReducer(state: TypingState, action: TypingAction): TypingState {\n switch (action.type) {\n case 'notification': {\n if (action.isTyping) {\n return { ...state, [action.userId]: { updatedAt: action.now } };\n }\n if (!Object.hasOwn(state, action.userId)) return state;\n return Object.fromEntries(Object.entries(state).filter(([userId]) => userId !== action.userId));\n }\n case 'expire': {\n const kept = Object.entries(state).filter(([, entry]) => action.now - entry.updatedAt < action.ttlMs);\n return kept.length === Object.keys(state).length ? state : Object.fromEntries(kept);\n }\n default:\n return state;\n }\n}\n\nexport function typingUsersOf(state: TypingState): string[] {\n return Object.keys(state);\n}\n\n// ---------------------------------------------------------------------------\n// Presence\n// ---------------------------------------------------------------------------\n\n/** Sorted, deduped user ids accumulated from deltas since mount (spec §7: no snapshot endpoint). */\nexport type PresenceState = string[];\n\nexport type PresenceAction = { type: 'delta'; joined: string[]; left: string[] };\n\nexport const initialPresenceState: PresenceState = [];\n\nexport function presenceReducer(state: PresenceState, action: PresenceAction): PresenceState {\n const set = new Set(state);\n for (const id of action.left) set.delete(id);\n for (const id of action.joined) set.add(id);\n return Array.from(set).sort();\n}\n","import type { DotwireClient } from '@dotwire/client';\n\n/**\n * `DotwireClient.subscribe`/`unsubscribe` (and `DotwireRoomHandle.subscribe`/`unsubscribe`)\n * operate on a single, client-wide \"am I in this room's hub group\" flag per `roomId` — it is\n * not refcounted. If two hooks (e.g. `useRoom` and `useTyping`) are both mounted against the\n * same room, calling `subscribe` from each is harmless (redundant hub invokes), but the FIRST\n * hook to unmount would call `unsubscribe` and silently drop the OTHER hook's live delivery.\n *\n * This module refcounts unsubscribe intent per `(client, roomId)` pair so the underlying hub\n * group is only actually left once every hook interested in that room has released it. It does\n * not wrap `subscribe` itself, since redundant `Subscribe` hub invokes are cheap and safe.\n */\nconst refCounts = new WeakMap<DotwireClient, Map<string, number>>();\n\nexport function retainRoom(client: DotwireClient, roomId: string): void {\n let rooms = refCounts.get(client);\n if (!rooms) {\n rooms = new Map();\n refCounts.set(client, rooms);\n }\n rooms.set(roomId, (rooms.get(roomId) ?? 0) + 1);\n}\n\n/**\n * Releases one hook's interest in `roomId`. Returns `true` when this was the last interested\n * hook, meaning the caller should now call `client.unsubscribe(roomId)`.\n */\nexport function releaseRoom(client: DotwireClient, roomId: string): boolean {\n const rooms = refCounts.get(client);\n if (!rooms) return true;\n const next = (rooms.get(roomId) ?? 1) - 1;\n if (next <= 0) {\n rooms.delete(roomId);\n return true;\n }\n rooms.set(roomId, next);\n return false;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAAmE;AACnE,oBAAyD;AAsDhD;AApDF,IAAM,qBAAiB,4BAAoC,IAAI;AA8B/D,SAAS,gBAAgB,OAAwC;AACtE,QAAM,EAAE,UAAU,SAAS,QAAQ,eAAe,IAAI;AACtD,QAAM,CAAC,eAAe,gBAAgB,QAAI,uBAA+B,IAAI;AAE7E,8BAAU,MAAM;AACd,QAAI,kBAAkB,CAAC,SAAS;AAC9B,aAAO;AAAA,IACT;AAEA,UAAM,WAAW,IAAI,4BAAc,OAAO;AAC1C,qBAAiB,QAAQ;AAEzB,WAAO,MAAM;AACX,eAAS,QAAQ;AACjB,uBAAiB,CAAC,YAAa,YAAY,WAAW,OAAO,OAAQ;AAAA,IACvE;AAAA,EAGF,GAAG,CAAC,gBAAgB,OAAO,CAAC;AAE5B,QAAM,SAAS,kBAAkB;AAEjC,SAAO,4CAAC,eAAe,UAAf,EAAwB,OAAO,QAAS,UAAS;AAC3D;;;ACxDA,IAAAA,gBAAiF;;;AC8B1E,IAAM,2BAA8C;AAAA,EACzD,UAAU,CAAC;AAAA,EACX,QAAQ;AAA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YAAY,MAAM,EAAE,MAAM,MAAM;AAAA,QAAC,CAAC;AAAA,MAC3C;AAAA,IACF;AAAA,EACF,GAAG,CAAC,QAAQ,MAAM,CAAC;AAEnB,QAAM,mBAAe,2BAAY,YAAY;AAC3C,UAAM,cAAc,SAAS,aAAa;AAAA,EAC5C,GAAG,CAAC,CAAC;AAEL,QAAM,iBAAa,2BAAY,YAAY;AACzC,UAAM,cAAc,SAAS,WAAW;AAAA,EAC1C,GAAG,CAAC,CAAC;AAEL,SAAO,EAAE,aAAa,cAAc,KAAK,GAAG,cAAc,WAAW;AACvE;AAaO,SAAS,YAAY,QAAmC;AAC7D,QAAM,SAAS,iBAAiB;AAChC,QAAM,CAAC,OAAO,QAAQ,QAAI,0BAAW,iBAAiB,oBAAoB;AAE1E,+BAAU,MAAM;AACd,QAAI,CAAC,OAAQ,QAAO;AAEpB,UAAM,OAAO,OAAO,KAAK,MAAM;AAC/B,eAAW,QAAQ,MAAM;AACzB,SAAK,UAAU,EAAE,MAAM,MAAM;AAAA,IAE7B,CAAC;AAED,UAAM,QAAQ,KAAK,WAAW,CAAC,UAAU;AACvC,eAAS,EAAE,MAAM,SAAS,QAAQ,MAAM,QAAQ,MAAM,MAAM,KAAK,CAAC;AAAA,IACpE,CAAC;AAED,WAAO,MAAM;AACX,YAAM;AACN,WAAK,QAAQ;AACb,UAAI,YAAY,QAAQ,MAAM,GAAG;AAC/B,eAAO,YAAY,MAAM,EAAE,MAAM,MAAM;AAAA,QAAC,CAAC;AAAA,MAC3C;AAAA,IACF;AAAA,EACF,GAAG,CAAC,QAAQ,MAAM,CAAC;AAEnB,SAAO,EAAE,SAAS,MAAM;AAC1B;","names":["import_react","options"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as react from 'react';
|
|
2
2
|
import { ReactNode } from 'react';
|
|
3
|
-
import { DotwireClient, DotwireClientOptions, HistoryPage, RoomMessage, SendAck, ConnectionState } from '@dotwire/client';
|
|
4
|
-
export { ConnectionState, DotwireClient, DotwireClientOptions, HistoryPage, MembershipRevoked, MessageRetracted, PresenceDelta, RoomMessage, SendAck, TypingNotification } from '@dotwire/client';
|
|
3
|
+
import { DotwireClient, DotwireClientOptions, HistoryPage, RoomMessage, SendMessageOptions, SendAck, ConnectionState } from '@dotwire/client';
|
|
4
|
+
export { ConnectionState, DotwireClient, DotwireClientOptions, HistoryPage, MembershipRevoked, MessageRetracted, PresenceDelta, RoomMessage, SendAck, SendMessageOptions, TypingNotification } from '@dotwire/client';
|
|
5
5
|
|
|
6
6
|
declare const DotwireContext: react.Context<DotwireClient | null>;
|
|
7
7
|
interface DotwireProviderProps {
|
|
@@ -111,7 +111,7 @@ interface UseRoomResult {
|
|
|
111
111
|
error: unknown;
|
|
112
112
|
hasMore: boolean;
|
|
113
113
|
loadOlder: () => Promise<void>;
|
|
114
|
-
send: (content: string) => Promise<SendAck>;
|
|
114
|
+
send: (content: string, options?: SendMessageOptions) => Promise<SendAck>;
|
|
115
115
|
}
|
|
116
116
|
/**
|
|
117
117
|
* Subscribes to `roomId` for as long as the component is mounted: loads the initial history
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as react from 'react';
|
|
2
2
|
import { ReactNode } from 'react';
|
|
3
|
-
import { DotwireClient, DotwireClientOptions, HistoryPage, RoomMessage, SendAck, ConnectionState } from '@dotwire/client';
|
|
4
|
-
export { ConnectionState, DotwireClient, DotwireClientOptions, HistoryPage, MembershipRevoked, MessageRetracted, PresenceDelta, RoomMessage, SendAck, TypingNotification } from '@dotwire/client';
|
|
3
|
+
import { DotwireClient, DotwireClientOptions, HistoryPage, RoomMessage, SendMessageOptions, SendAck, ConnectionState } from '@dotwire/client';
|
|
4
|
+
export { ConnectionState, DotwireClient, DotwireClientOptions, HistoryPage, MembershipRevoked, MessageRetracted, PresenceDelta, RoomMessage, SendAck, SendMessageOptions, TypingNotification } from '@dotwire/client';
|
|
5
5
|
|
|
6
6
|
declare const DotwireContext: react.Context<DotwireClient | null>;
|
|
7
7
|
interface DotwireProviderProps {
|
|
@@ -111,7 +111,7 @@ interface UseRoomResult {
|
|
|
111
111
|
error: unknown;
|
|
112
112
|
hasMore: boolean;
|
|
113
113
|
loadOlder: () => Promise<void>;
|
|
114
|
-
send: (content: string) => Promise<SendAck>;
|
|
114
|
+
send: (content: string, options?: SendMessageOptions) => Promise<SendAck>;
|
|
115
115
|
}
|
|
116
116
|
/**
|
|
117
117
|
* Subscribes to `roomId` for as long as the component is mounted: loads the initial history
|
package/dist/index.js
CHANGED
|
@@ -78,22 +78,12 @@ function typingReducer(state, action) {
|
|
|
78
78
|
if (action.isTyping) {
|
|
79
79
|
return { ...state, [action.userId]: { updatedAt: action.now } };
|
|
80
80
|
}
|
|
81
|
-
if (!(action.userId
|
|
82
|
-
|
|
83
|
-
delete next[action.userId];
|
|
84
|
-
return next;
|
|
81
|
+
if (!Object.hasOwn(state, action.userId)) return state;
|
|
82
|
+
return Object.fromEntries(Object.entries(state).filter(([userId]) => userId !== action.userId));
|
|
85
83
|
}
|
|
86
84
|
case "expire": {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
for (const [userId, entry] of Object.entries(state)) {
|
|
90
|
-
if (action.now - entry.updatedAt < action.ttlMs) {
|
|
91
|
-
next[userId] = entry;
|
|
92
|
-
} else {
|
|
93
|
-
changed = true;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
return changed ? next : state;
|
|
85
|
+
const kept = Object.entries(state).filter(([, entry]) => action.now - entry.updatedAt < action.ttlMs);
|
|
86
|
+
return kept.length === Object.keys(state).length ? state : Object.fromEntries(kept);
|
|
97
87
|
}
|
|
98
88
|
default:
|
|
99
89
|
return state;
|
|
@@ -199,16 +189,11 @@ function useRoom(roomId, options = {}) {
|
|
|
199
189
|
}
|
|
200
190
|
}, [client, roomId, history]);
|
|
201
191
|
const send = useCallback(
|
|
202
|
-
async (content) => {
|
|
192
|
+
async (content, options2) => {
|
|
203
193
|
if (!client) {
|
|
204
194
|
throw new Error("Dotwire client is not ready yet.");
|
|
205
195
|
}
|
|
206
|
-
|
|
207
|
-
return await client.sendMessage(roomId, content);
|
|
208
|
-
} catch (error) {
|
|
209
|
-
dispatch({ type: "error", error });
|
|
210
|
-
throw error;
|
|
211
|
-
}
|
|
196
|
+
return client.sendMessage(roomId, content, void 0, options2);
|
|
212
197
|
},
|
|
213
198
|
[client, roomId]
|
|
214
199
|
);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/provider.tsx","../src/hooks.ts","../src/reducers.ts","../src/subscription.ts"],"sourcesContent":["import { createContext, useEffect, useState, type ReactNode } from 'react';\nimport { DotwireClient, type DotwireClientOptions } from '@dotwire/client';\n\nexport const DotwireContext = createContext<DotwireClient | null>(null);\n\nexport interface DotwireProviderProps {\n children?: ReactNode;\n /**\n * Options to construct a `DotwireClient` internally. Ignored if `client` is also given.\n * Construct this once (e.g. with `useMemo`/`useState`, or a module-level constant) — a new\n * object identity on every render tears down and rebuilds the client.\n */\n options?: DotwireClientOptions;\n /**\n * A pre-built `DotwireClient` instance. When given, the provider never constructs or\n * disposes a client itself — the caller owns that instance's lifecycle.\n */\n client?: DotwireClient;\n}\n\n/**\n * Provides a `DotwireClient` to `useDotwireClient`/`useConnectionState`/`useRoom`/`useTyping`/\n * `usePresence` (spec §7).\n *\n * - Construction happens in an effect, never during render, so this component is safe to\n * render on the server (`react-dom/server`) — `useDotwireClient()` simply returns `null`\n * until the effect runs on the client. See `test/ssr.test.tsx`.\n * - In React 18 StrictMode, effects run an extra setup+cleanup pass in development; the\n * cleanup below disposes whatever instance that pass constructed before the next one runs,\n * so double-invocation is safe.\n * - A client built from `options` is disposed on unmount (or when `options` changes). A\n * client passed via the `client` prop is never disposed here — the caller owns it.\n */\nexport function DotwireProvider(props: DotwireProviderProps): ReactNode {\n const { children, options, client: providedClient } = props;\n const [managedClient, setManagedClient] = useState<DotwireClient | null>(null);\n\n useEffect(() => {\n if (providedClient || !options) {\n return undefined;\n }\n\n const instance = new DotwireClient(options);\n setManagedClient(instance);\n\n return () => {\n instance.dispose();\n setManagedClient((current) => (current === instance ? null : current));\n };\n // `options` is compared by reference deliberately: a new object rebuilds the client.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [providedClient, options]);\n\n const client = providedClient ?? managedClient;\n\n return <DotwireContext.Provider value={client}>{children}</DotwireContext.Provider>;\n}\n","import { useCallback, useContext, useEffect, useReducer, useRef, useState } from 'react';\nimport type { ConnectionState, DotwireClient, RoomMessage, SendAck } from '@dotwire/client';\nimport { DotwireContext } from './provider.js';\nimport {\n initialPresenceState,\n initialRoomMessagesState,\n initialTypingState,\n presenceReducer,\n roomMessagesReducer,\n typingReducer,\n typingUsersOf,\n type RoomMessagesState,\n type RoomStatus\n} from './reducers.js';\nimport { releaseRoom, retainRoom } from './subscription.js';\n\n/** The `DotwireClient` from the nearest `DotwireProvider`, or `null` before it has mounted. */\nexport function useDotwireClient(): DotwireClient | null {\n return useContext(DotwireContext);\n}\n\n/** The client's live `ConnectionState`, re-rendering on every `onStateChange` event. */\nexport function useConnectionState(): ConnectionState {\n const client = useDotwireClient();\n const [state, setState] = useState<ConnectionState>(() => client?.getConnectionState() ?? 'disconnected');\n\n useEffect(() => {\n if (!client) {\n setState('disconnected');\n return undefined;\n }\n setState(client.getConnectionState());\n return client.onStateChange(setState);\n }, [client]);\n\n return state;\n}\n\nexport interface UseRoomOptions {\n /** How many of the latest messages to load on subscribe. Default 50. */\n history?: number;\n}\n\nexport interface UseRoomResult {\n messages: RoomMessage[];\n status: RoomStatus;\n error: unknown;\n hasMore: boolean;\n loadOlder: () => Promise<void>;\n send: (content: string) => Promise<SendAck>;\n}\n\n/**\n * Subscribes to `roomId` for as long as the component is mounted: loads the initial history\n * page, applies live messages/retractions/revocation, and exposes `loadOlder`/`send` (spec\n * §7). All state transitions are delegated to `roomMessagesReducer` (`src/reducers.ts`).\n */\nexport function useRoom(roomId: string, options: UseRoomOptions = {}): UseRoomResult {\n const history = options.history ?? 50;\n const client = useDotwireClient();\n const [state, dispatch] = useReducer(roomMessagesReducer, initialRoomMessagesState);\n const stateRef = useRef<RoomMessagesState>(state);\n stateRef.current = state;\n const scopeRef = useRef<object | null>(null);\n\n useEffect(() => {\n if (!client) return undefined;\n\n const scope = {};\n scopeRef.current = scope;\n dispatch({ type: 'reset' });\n const room = client.room(roomId);\n retainRoom(client, roomId);\n let cancelled = false;\n\n const unsubscribers = [\n room.onMessage((message) => dispatch({ type: 'message', message })),\n room.onMessageRetracted(({ seq }) => dispatch({ type: 'retracted', seq })),\n room.onMembershipRevoked(() => dispatch({ type: 'revoked' }))\n ];\n\n room\n .subscribe({ history })\n .then((page) => {\n if (cancelled) return;\n dispatch({ type: 'subscribed', page: page ?? { messages: [], hasMore: false } });\n })\n .catch((error: unknown) => {\n if (!cancelled) dispatch({ type: 'error', error });\n });\n\n return () => {\n cancelled = true;\n if (scopeRef.current === scope) scopeRef.current = null;\n for (const unsub of unsubscribers) unsub();\n room.release();\n // Only the last interested hook for this (client, roomId) actually leaves the hub\n // group — see `src/subscription.ts`. We deliberately do NOT call `room.dispose()`\n // here: it would unconditionally unsubscribe, which is exactly what the refcount\n // guards against when `useTyping`/`usePresence` share the same room.\n if (releaseRoom(client, roomId)) {\n client.unsubscribe(roomId).catch(() => {});\n }\n };\n }, [client, roomId, history]);\n\n const loadOlder = useCallback(async () => {\n if (!client) return;\n const oldest = stateRef.current.messages[0];\n if (!oldest) return;\n const scope = scopeRef.current;\n try {\n const page = await client.getHistory(roomId, { beforeSeq: oldest.seq, limit: history });\n if (scopeRef.current === scope) dispatch({ type: 'olderLoaded', page });\n } catch (error) {\n if (scopeRef.current === scope) dispatch({ type: 'error', error });\n }\n }, [client, roomId, history]);\n\n const send = useCallback(\n async (content: string): Promise<SendAck> => {\n if (!client) {\n throw new Error('Dotwire client is not ready yet.');\n }\n try {\n return await client.sendMessage(roomId, content);\n } catch (error) {\n dispatch({ type: 'error', error });\n throw error;\n }\n },\n [client, roomId]\n );\n\n return {\n messages: state.messages,\n status: state.status,\n error: state.error,\n hasMore: state.hasMore,\n loadOlder,\n send\n };\n}\n\nexport interface UseTypingResult {\n /** User ids currently considered typing (expired entries are removed automatically). */\n typingUsers: string[];\n notifyTyping: () => Promise<void>;\n stopTyping: () => Promise<void>;\n}\n\n/** How long a typing notification stays valid absent a follow-up (spec §7: \"with expiry\"). */\nconst TYPING_EXPIRY_MS = 3500;\nconst TYPING_EXPIRY_CHECK_INTERVAL_MS = 1000;\n\n/**\n * Tracks who is typing in `roomId`. Expiry is time-based (a user who goes silent without an\n * explicit stop notification drops out after `TYPING_EXPIRY_MS`) and driven by `typingReducer`,\n * which takes an explicit `now` so it is unit-testable without timers.\n */\nexport function useTyping(roomId: string): UseTypingResult {\n const client = useDotwireClient();\n const [state, dispatch] = useReducer(typingReducer, initialTypingState);\n const roomHandleRef = useRef<ReturnType<DotwireClient['room']> | null>(null);\n\n useEffect(() => {\n if (!client) return undefined;\n\n const room = client.room(roomId);\n roomHandleRef.current = room;\n retainRoom(client, roomId);\n // Typing/presence delivery requires hub group membership, same as message delivery.\n room.subscribe().catch(() => {\n // Best-effort: if this never resolves, no typing events will arrive for this room;\n // the caller can observe that via useConnectionState/useRoom's own error state.\n });\n\n const unsub = room.onTyping((notification) => {\n dispatch({ type: 'notification', userId: notification.userId, isTyping: notification.isTyping, now: Date.now() });\n });\n\n const interval = setInterval(() => {\n dispatch({ type: 'expire', now: Date.now(), ttlMs: TYPING_EXPIRY_MS });\n }, TYPING_EXPIRY_CHECK_INTERVAL_MS);\n\n return () => {\n unsub();\n clearInterval(interval);\n roomHandleRef.current = null;\n room.release();\n if (releaseRoom(client, roomId)) {\n client.unsubscribe(roomId).catch(() => {});\n }\n };\n }, [client, roomId]);\n\n const notifyTyping = useCallback(async () => {\n await roomHandleRef.current?.notifyTyping();\n }, []);\n\n const stopTyping = useCallback(async () => {\n await roomHandleRef.current?.stopTyping();\n }, []);\n\n return { typingUsers: typingUsersOf(state), notifyTyping, stopTyping };\n}\n\nexport interface UsePresenceResult {\n /** User ids present, accumulated from deltas since this hook mounted. */\n present: string[];\n}\n\n/**\n * Accumulates presence deltas for `roomId` since mount (spec §7). There is no presence\n * snapshot endpoint on the server, so a component that mounts this hook after other members\n * already joined will not see them in `present` until the next `PresenceUpdated` delta for\n * this room (e.g. the next member to join or leave).\n */\nexport function usePresence(roomId: string): UsePresenceResult {\n const client = useDotwireClient();\n const [state, dispatch] = useReducer(presenceReducer, initialPresenceState);\n\n useEffect(() => {\n if (!client) return undefined;\n\n const room = client.room(roomId);\n retainRoom(client, roomId);\n room.subscribe().catch(() => {\n // Best-effort, same rationale as useTyping.\n });\n\n const unsub = room.onPresence((delta) => {\n dispatch({ type: 'delta', joined: delta.joined, left: delta.left });\n });\n\n return () => {\n unsub();\n room.release();\n if (releaseRoom(client, roomId)) {\n client.unsubscribe(roomId).catch(() => {});\n }\n };\n }, [client, roomId]);\n\n return { present: state };\n}\n","/**\n * Pure state-transition functions backing `useRoom`, `useTyping`, and `usePresence`\n * (spec §7). None of these import React; each is unit-tested directly with Node's\n * test runner in `test/reducers.test.ts`. The hooks in `src/hooks.ts` are thin\n * `useReducer` wrappers around these.\n */\nimport type { HistoryPage, RoomMessage } from '@dotwire/client';\n\n// ---------------------------------------------------------------------------\n// Room messages\n// ---------------------------------------------------------------------------\n\nexport type RoomStatus = 'connecting' | 'ready' | 'revoked' | 'error';\n\nexport interface RoomMessagesState {\n messages: RoomMessage[];\n status: RoomStatus;\n error: unknown;\n hasMore: boolean;\n}\n\nexport type RoomMessagesAction =\n | { type: 'reset' }\n | { type: 'subscribed'; page: HistoryPage }\n | { type: 'message'; message: RoomMessage }\n | { type: 'retracted'; seq: number }\n | { type: 'revoked' }\n | { type: 'error'; error: unknown }\n | { type: 'olderLoaded'; page: HistoryPage };\n\nexport const initialRoomMessagesState: RoomMessagesState = {\n messages: [],\n status: 'connecting',\n error: null,\n hasMore: false\n};\n\n/**\n * Merges `incoming` into `existing`, deduping by `seq` (last write for a given seq\n * wins) and returning the result sorted ascending by `seq` (spec §6, §7: \"consumers\n * order by seq themselves\").\n */\nfunction mergeMessages(existing: RoomMessage[], incoming: RoomMessage[]): RoomMessage[] {\n if (incoming.length === 0) return existing;\n const last = existing[existing.length - 1];\n if (incoming.length === 1 && (last === undefined || incoming[0].seq > last.seq)) return [...existing, incoming[0]];\n const bySeq = new Map<number, RoomMessage>();\n for (const message of existing) bySeq.set(message.seq, message);\n for (const message of incoming) bySeq.set(message.seq, message);\n return Array.from(bySeq.values()).sort((a, b) => a.seq - b.seq);\n}\n\nexport function roomMessagesReducer(\n state: RoomMessagesState,\n action: RoomMessagesAction\n): RoomMessagesState {\n switch (action.type) {\n case 'reset':\n return initialRoomMessagesState;\n case 'subscribed':\n return {\n messages: mergeMessages(state.messages, action.page.messages),\n status: 'ready',\n error: null,\n hasMore: action.page.hasMore\n };\n case 'message':\n return { ...state, messages: mergeMessages(state.messages, [action.message]) };\n case 'retracted':\n return { ...state, messages: state.messages.filter((m) => m.seq !== action.seq) };\n case 'revoked':\n return { ...state, status: 'revoked' };\n case 'error':\n return { ...state, status: 'error', error: action.error };\n case 'olderLoaded':\n return {\n ...state,\n messages: mergeMessages(state.messages, action.page.messages),\n hasMore: action.page.hasMore\n };\n default:\n return state;\n }\n}\n\n// ---------------------------------------------------------------------------\n// Typing\n// ---------------------------------------------------------------------------\n\nexport interface TypingEntry {\n updatedAt: number;\n}\n\n/** Keyed by userId; a key is present only while that user is considered typing. */\nexport type TypingState = Record<string, TypingEntry>;\n\nexport type TypingAction =\n | { type: 'notification'; userId: string; isTyping: boolean; now: number }\n | { type: 'expire'; now: number; ttlMs: number };\n\nexport const initialTypingState: TypingState = {};\n\nexport function typingReducer(state: TypingState, action: TypingAction): TypingState {\n switch (action.type) {\n case 'notification': {\n if (action.isTyping) {\n return { ...state, [action.userId]: { updatedAt: action.now } };\n }\n if (!(action.userId in state)) return state;\n const next = { ...state };\n delete next[action.userId];\n return next;\n }\n case 'expire': {\n let changed = false;\n const next: TypingState = {};\n for (const [userId, entry] of Object.entries(state)) {\n if (action.now - entry.updatedAt < action.ttlMs) {\n next[userId] = entry;\n } else {\n changed = true;\n }\n }\n return changed ? next : state;\n }\n default:\n return state;\n }\n}\n\nexport function typingUsersOf(state: TypingState): string[] {\n return Object.keys(state);\n}\n\n// ---------------------------------------------------------------------------\n// Presence\n// ---------------------------------------------------------------------------\n\n/** Sorted, deduped user ids accumulated from deltas since mount (spec §7: no snapshot endpoint). */\nexport type PresenceState = string[];\n\nexport type PresenceAction = { type: 'delta'; joined: string[]; left: string[] };\n\nexport const initialPresenceState: PresenceState = [];\n\nexport function presenceReducer(state: PresenceState, action: PresenceAction): PresenceState {\n const set = new Set(state);\n for (const id of action.left) set.delete(id);\n for (const id of action.joined) set.add(id);\n return Array.from(set).sort();\n}\n","import type { DotwireClient } from '@dotwire/client';\n\n/**\n * `DotwireClient.subscribe`/`unsubscribe` (and `DotwireRoomHandle.subscribe`/`unsubscribe`)\n * operate on a single, client-wide \"am I in this room's hub group\" flag per `roomId` — it is\n * not refcounted. If two hooks (e.g. `useRoom` and `useTyping`) are both mounted against the\n * same room, calling `subscribe` from each is harmless (redundant hub invokes), but the FIRST\n * hook to unmount would call `unsubscribe` and silently drop the OTHER hook's live delivery.\n *\n * This module refcounts unsubscribe intent per `(client, roomId)` pair so the underlying hub\n * group is only actually left once every hook interested in that room has released it. It does\n * not wrap `subscribe` itself, since redundant `Subscribe` hub invokes are cheap and safe.\n */\nconst refCounts = new WeakMap<DotwireClient, Map<string, number>>();\n\nexport function retainRoom(client: DotwireClient, roomId: string): void {\n let rooms = refCounts.get(client);\n if (!rooms) {\n rooms = new Map();\n refCounts.set(client, rooms);\n }\n rooms.set(roomId, (rooms.get(roomId) ?? 0) + 1);\n}\n\n/**\n * Releases one hook's interest in `roomId`. Returns `true` when this was the last interested\n * hook, meaning the caller should now call `client.unsubscribe(roomId)`.\n */\nexport function releaseRoom(client: DotwireClient, roomId: string): boolean {\n const rooms = refCounts.get(client);\n if (!rooms) return true;\n const next = (rooms.get(roomId) ?? 1) - 1;\n if (next <= 0) {\n rooms.delete(roomId);\n return true;\n }\n rooms.set(roomId, next);\n return false;\n}\n"],"mappings":";;;AAAA,SAAS,eAAe,WAAW,gBAAgC;AACnE,SAAS,qBAAgD;AAsDhD;AApDF,IAAM,iBAAiB,cAAoC,IAAI;AA8B/D,SAAS,gBAAgB,OAAwC;AACtE,QAAM,EAAE,UAAU,SAAS,QAAQ,eAAe,IAAI;AACtD,QAAM,CAAC,eAAe,gBAAgB,IAAI,SAA+B,IAAI;AAE7E,YAAU,MAAM;AACd,QAAI,kBAAkB,CAAC,SAAS;AAC9B,aAAO;AAAA,IACT;AAEA,UAAM,WAAW,IAAI,cAAc,OAAO;AAC1C,qBAAiB,QAAQ;AAEzB,WAAO,MAAM;AACX,eAAS,QAAQ;AACjB,uBAAiB,CAAC,YAAa,YAAY,WAAW,OAAO,OAAQ;AAAA,IACvE;AAAA,EAGF,GAAG,CAAC,gBAAgB,OAAO,CAAC;AAE5B,QAAM,SAAS,kBAAkB;AAEjC,SAAO,oBAAC,eAAe,UAAf,EAAwB,OAAO,QAAS,UAAS;AAC3D;;;ACxDA,SAAS,aAAa,YAAY,aAAAA,YAAW,YAAY,QAAQ,YAAAC,iBAAgB;;;AC8B1E,IAAM,2BAA8C;AAAA,EACzD,UAAU,CAAC;AAAA,EACX,QAAQ;AAAA,EACR,OAAO;AAAA,EACP,SAAS;AACX;AAOA,SAAS,cAAc,UAAyB,UAAwC;AACtF,MAAI,SAAS,WAAW,EAAG,QAAO;AAClC,QAAM,OAAO,SAAS,SAAS,SAAS,CAAC;AACzC,MAAI,SAAS,WAAW,MAAM,SAAS,UAAa,SAAS,CAAC,EAAE,MAAM,KAAK,KAAM,QAAO,CAAC,GAAG,UAAU,SAAS,CAAC,CAAC;AACjH,QAAM,QAAQ,oBAAI,IAAyB;AA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AG,CAAC,CAAC;AAEL,QAAM,aAAa,YAAY,YAAY;AACzC,UAAM,cAAc,SAAS,WAAW;AAAA,EAC1C,GAAG,CAAC,CAAC;AAEL,SAAO,EAAE,aAAa,cAAc,KAAK,GAAG,cAAc,WAAW;AACvE;AAaO,SAAS,YAAY,QAAmC;AAC7D,QAAM,SAAS,iBAAiB;AAChC,QAAM,CAAC,OAAO,QAAQ,IAAI,WAAW,iBAAiB,oBAAoB;AAE1E,EAAAA,WAAU,MAAM;AACd,QAAI,CAAC,OAAQ,QAAO;AAEpB,UAAM,OAAO,OAAO,KAAK,MAAM;AAC/B,eAAW,QAAQ,MAAM;AACzB,SAAK,UAAU,EAAE,MAAM,MAAM;AAAA,IAE7B,CAAC;AAED,UAAM,QAAQ,KAAK,WAAW,CAAC,UAAU;AACvC,eAAS,EAAE,MAAM,SAAS,QAAQ,MAAM,QAAQ,MAAM,MAAM,KAAK,CAAC;AAAA,IACpE,CAAC;AAED,WAAO,MAAM;AACX,YAAM;AACN,WAAK,QAAQ;AACb,UAAI,YAAY,QAAQ,MAAM,GAAG;AAC/B,eAAO,YAAY,MAAM,EAAE,MAAM,MAAM;AAAA,QAAC,CAAC;AAAA,MAC3C;AAAA,IACF;AAAA,EACF,GAAG,CAAC,QAAQ,MAAM,CAAC;AAEnB,SAAO,EAAE,SAAS,MAAM;AAC1B;","names":["useEffect","useState","useState","useEffect"]}
|
|
1
|
+
{"version":3,"sources":["../src/provider.tsx","../src/hooks.ts","../src/reducers.ts","../src/subscription.ts"],"sourcesContent":["import { createContext, useEffect, useState, type ReactNode } from 'react';\nimport { DotwireClient, type DotwireClientOptions } from '@dotwire/client';\n\nexport const DotwireContext = createContext<DotwireClient | null>(null);\n\nexport interface DotwireProviderProps {\n children?: ReactNode;\n /**\n * Options to construct a `DotwireClient` internally. Ignored if `client` is also given.\n * Construct this once (e.g. with `useMemo`/`useState`, or a module-level constant) — a new\n * object identity on every render tears down and rebuilds the client.\n */\n options?: DotwireClientOptions;\n /**\n * A pre-built `DotwireClient` instance. When given, the provider never constructs or\n * disposes a client itself — the caller owns that instance's lifecycle.\n */\n client?: DotwireClient;\n}\n\n/**\n * Provides a `DotwireClient` to `useDotwireClient`/`useConnectionState`/`useRoom`/`useTyping`/\n * `usePresence` (spec §7).\n *\n * - Construction happens in an effect, never during render, so this component is safe to\n * render on the server (`react-dom/server`) — `useDotwireClient()` simply returns `null`\n * until the effect runs on the client. See `test/ssr.test.tsx`.\n * - In React 18 StrictMode, effects run an extra setup+cleanup pass in development; the\n * cleanup below disposes whatever instance that pass constructed before the next one runs,\n * so double-invocation is safe.\n * - A client built from `options` is disposed on unmount (or when `options` changes). A\n * client passed via the `client` prop is never disposed here — the caller owns it.\n */\nexport function DotwireProvider(props: DotwireProviderProps): ReactNode {\n const { children, options, client: providedClient } = props;\n const [managedClient, setManagedClient] = useState<DotwireClient | null>(null);\n\n useEffect(() => {\n if (providedClient || !options) {\n return undefined;\n }\n\n const instance = new DotwireClient(options);\n setManagedClient(instance);\n\n return () => {\n instance.dispose();\n setManagedClient((current) => (current === instance ? null : current));\n };\n // `options` is compared by reference deliberately: a new object rebuilds the client.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [providedClient, options]);\n\n const client = providedClient ?? managedClient;\n\n return <DotwireContext.Provider value={client}>{children}</DotwireContext.Provider>;\n}\n","import { useCallback, useContext, useEffect, useReducer, useRef, useState } from 'react';\nimport type { ConnectionState, DotwireClient, RoomMessage, SendAck, SendMessageOptions } from '@dotwire/client';\nimport { DotwireContext } from './provider.js';\nimport {\n initialPresenceState,\n initialRoomMessagesState,\n initialTypingState,\n presenceReducer,\n roomMessagesReducer,\n typingReducer,\n typingUsersOf,\n type RoomMessagesState,\n type RoomStatus\n} from './reducers.js';\nimport { releaseRoom, retainRoom } from './subscription.js';\n\n/** The `DotwireClient` from the nearest `DotwireProvider`, or `null` before it has mounted. */\nexport function useDotwireClient(): DotwireClient | null {\n return useContext(DotwireContext);\n}\n\n/** The client's live `ConnectionState`, re-rendering on every `onStateChange` event. */\nexport function useConnectionState(): ConnectionState {\n const client = useDotwireClient();\n const [state, setState] = useState<ConnectionState>(() => client?.getConnectionState() ?? 'disconnected');\n\n useEffect(() => {\n if (!client) {\n setState('disconnected');\n return undefined;\n }\n setState(client.getConnectionState());\n return client.onStateChange(setState);\n }, [client]);\n\n return state;\n}\n\nexport interface UseRoomOptions {\n /** How many of the latest messages to load on subscribe. Default 50. */\n history?: number;\n}\n\nexport interface UseRoomResult {\n messages: RoomMessage[];\n status: RoomStatus;\n error: unknown;\n hasMore: boolean;\n loadOlder: () => Promise<void>;\n send: (content: string, options?: SendMessageOptions) => Promise<SendAck>;\n}\n\n/**\n * Subscribes to `roomId` for as long as the component is mounted: loads the initial history\n * page, applies live messages/retractions/revocation, and exposes `loadOlder`/`send` (spec\n * §7). All state transitions are delegated to `roomMessagesReducer` (`src/reducers.ts`).\n */\nexport function useRoom(roomId: string, options: UseRoomOptions = {}): UseRoomResult {\n const history = options.history ?? 50;\n const client = useDotwireClient();\n const [state, dispatch] = useReducer(roomMessagesReducer, initialRoomMessagesState);\n const stateRef = useRef<RoomMessagesState>(state);\n stateRef.current = state;\n const scopeRef = useRef<object | null>(null);\n\n useEffect(() => {\n if (!client) return undefined;\n\n const scope = {};\n scopeRef.current = scope;\n dispatch({ type: 'reset' });\n const room = client.room(roomId);\n retainRoom(client, roomId);\n let cancelled = false;\n\n const unsubscribers = [\n room.onMessage((message) => dispatch({ type: 'message', message })),\n room.onMessageRetracted(({ seq }) => dispatch({ type: 'retracted', seq })),\n room.onMembershipRevoked(() => dispatch({ type: 'revoked' }))\n ];\n\n room\n .subscribe({ history })\n .then((page) => {\n if (cancelled) return;\n dispatch({ type: 'subscribed', page: page ?? { messages: [], hasMore: false } });\n })\n .catch((error: unknown) => {\n if (!cancelled) dispatch({ type: 'error', error });\n });\n\n return () => {\n cancelled = true;\n if (scopeRef.current === scope) scopeRef.current = null;\n for (const unsub of unsubscribers) unsub();\n room.release();\n // Only the last interested hook for this (client, roomId) actually leaves the hub\n // group — see `src/subscription.ts`. We deliberately do NOT call `room.dispose()`\n // here: it would unconditionally unsubscribe, which is exactly what the refcount\n // guards against when `useTyping`/`usePresence` share the same room.\n if (releaseRoom(client, roomId)) {\n client.unsubscribe(roomId).catch(() => {});\n }\n };\n }, [client, roomId, history]);\n\n const loadOlder = useCallback(async () => {\n if (!client) return;\n const oldest = stateRef.current.messages[0];\n if (!oldest) return;\n const scope = scopeRef.current;\n try {\n const page = await client.getHistory(roomId, { beforeSeq: oldest.seq, limit: history });\n if (scopeRef.current === scope) dispatch({ type: 'olderLoaded', page });\n } catch (error) {\n if (scopeRef.current === scope) dispatch({ type: 'error', error });\n }\n }, [client, roomId, history]);\n\n const send = useCallback(\n async (content: string, options?: SendMessageOptions): Promise<SendAck> => {\n if (!client) {\n throw new Error('Dotwire client is not ready yet.');\n }\n // A failed send is reported to the caller only: the subscription itself is still healthy.\n return client.sendMessage(roomId, content, undefined, options);\n },\n [client, roomId]\n );\n\n return {\n messages: state.messages,\n status: state.status,\n error: state.error,\n hasMore: state.hasMore,\n loadOlder,\n send\n };\n}\n\nexport interface UseTypingResult {\n /** User ids currently considered typing (expired entries are removed automatically). */\n typingUsers: string[];\n notifyTyping: () => Promise<void>;\n stopTyping: () => Promise<void>;\n}\n\n/** How long a typing notification stays valid absent a follow-up (spec §7: \"with expiry\"). */\nconst TYPING_EXPIRY_MS = 3500;\nconst TYPING_EXPIRY_CHECK_INTERVAL_MS = 1000;\n\n/**\n * Tracks who is typing in `roomId`. Expiry is time-based (a user who goes silent without an\n * explicit stop notification drops out after `TYPING_EXPIRY_MS`) and driven by `typingReducer`,\n * which takes an explicit `now` so it is unit-testable without timers.\n */\nexport function useTyping(roomId: string): UseTypingResult {\n const client = useDotwireClient();\n const [state, dispatch] = useReducer(typingReducer, initialTypingState);\n const roomHandleRef = useRef<ReturnType<DotwireClient['room']> | null>(null);\n\n useEffect(() => {\n if (!client) return undefined;\n\n const room = client.room(roomId);\n roomHandleRef.current = room;\n retainRoom(client, roomId);\n // Typing/presence delivery requires hub group membership, same as message delivery.\n room.subscribe().catch(() => {\n // Best-effort: if this never resolves, no typing events will arrive for this room;\n // the caller can observe that via useConnectionState/useRoom's own error state.\n });\n\n const unsub = room.onTyping((notification) => {\n dispatch({ type: 'notification', userId: notification.userId, isTyping: notification.isTyping, now: Date.now() });\n });\n\n const interval = setInterval(() => {\n dispatch({ type: 'expire', now: Date.now(), ttlMs: TYPING_EXPIRY_MS });\n }, TYPING_EXPIRY_CHECK_INTERVAL_MS);\n\n return () => {\n unsub();\n clearInterval(interval);\n roomHandleRef.current = null;\n room.release();\n if (releaseRoom(client, roomId)) {\n client.unsubscribe(roomId).catch(() => {});\n }\n };\n }, [client, roomId]);\n\n const notifyTyping = useCallback(async () => {\n await roomHandleRef.current?.notifyTyping();\n }, []);\n\n const stopTyping = useCallback(async () => {\n await roomHandleRef.current?.stopTyping();\n }, []);\n\n return { typingUsers: typingUsersOf(state), notifyTyping, stopTyping };\n}\n\nexport interface UsePresenceResult {\n /** User ids present, accumulated from deltas since this hook mounted. */\n present: string[];\n}\n\n/**\n * Accumulates presence deltas for `roomId` since mount (spec §7). There is no presence\n * snapshot endpoint on the server, so a component that mounts this hook after other members\n * already joined will not see them in `present` until the next `PresenceUpdated` delta for\n * this room (e.g. the next member to join or leave).\n */\nexport function usePresence(roomId: string): UsePresenceResult {\n const client = useDotwireClient();\n const [state, dispatch] = useReducer(presenceReducer, initialPresenceState);\n\n useEffect(() => {\n if (!client) return undefined;\n\n const room = client.room(roomId);\n retainRoom(client, roomId);\n room.subscribe().catch(() => {\n // Best-effort, same rationale as useTyping.\n });\n\n const unsub = room.onPresence((delta) => {\n dispatch({ type: 'delta', joined: delta.joined, left: delta.left });\n });\n\n return () => {\n unsub();\n room.release();\n if (releaseRoom(client, roomId)) {\n client.unsubscribe(roomId).catch(() => {});\n }\n };\n }, [client, roomId]);\n\n return { present: state };\n}\n","/**\n * Pure state-transition functions backing `useRoom`, `useTyping`, and `usePresence`\n * (spec §7). None of these import React; each is unit-tested directly with Node's\n * test runner in `test/reducers.test.ts`. The hooks in `src/hooks.ts` are thin\n * `useReducer` wrappers around these.\n */\nimport type { HistoryPage, RoomMessage } from '@dotwire/client';\n\n// ---------------------------------------------------------------------------\n// Room messages\n// ---------------------------------------------------------------------------\n\nexport type RoomStatus = 'connecting' | 'ready' | 'revoked' | 'error';\n\nexport interface RoomMessagesState {\n messages: RoomMessage[];\n status: RoomStatus;\n error: unknown;\n hasMore: boolean;\n}\n\nexport type RoomMessagesAction =\n | { type: 'reset' }\n | { type: 'subscribed'; page: HistoryPage }\n | { type: 'message'; message: RoomMessage }\n | { type: 'retracted'; seq: number }\n | { type: 'revoked' }\n | { type: 'error'; error: unknown }\n | { type: 'olderLoaded'; page: HistoryPage };\n\nexport const initialRoomMessagesState: RoomMessagesState = {\n messages: [],\n status: 'connecting',\n error: null,\n hasMore: false\n};\n\n/**\n * Merges `incoming` into `existing`, deduping by `seq` (last write for a given seq\n * wins) and returning the result sorted ascending by `seq` (spec §6, §7: \"consumers\n * order by seq themselves\").\n */\nfunction mergeMessages(existing: RoomMessage[], incoming: RoomMessage[]): RoomMessage[] {\n if (incoming.length === 0) return existing;\n const last = existing[existing.length - 1];\n if (incoming.length === 1 && (last === undefined || incoming[0].seq > last.seq)) return [...existing, incoming[0]];\n const bySeq = new Map<number, RoomMessage>();\n for (const message of existing) bySeq.set(message.seq, message);\n for (const message of incoming) bySeq.set(message.seq, message);\n return Array.from(bySeq.values()).sort((a, b) => a.seq - b.seq);\n}\n\nexport function roomMessagesReducer(\n state: RoomMessagesState,\n action: RoomMessagesAction\n): RoomMessagesState {\n switch (action.type) {\n case 'reset':\n return initialRoomMessagesState;\n case 'subscribed':\n return {\n messages: mergeMessages(state.messages, action.page.messages),\n status: 'ready',\n error: null,\n hasMore: action.page.hasMore\n };\n case 'message':\n return { ...state, messages: mergeMessages(state.messages, [action.message]) };\n case 'retracted':\n return { ...state, messages: state.messages.filter((m) => m.seq !== action.seq) };\n case 'revoked':\n return { ...state, status: 'revoked' };\n case 'error':\n return { ...state, status: 'error', error: action.error };\n case 'olderLoaded':\n return {\n ...state,\n messages: mergeMessages(state.messages, action.page.messages),\n hasMore: action.page.hasMore\n };\n default:\n return state;\n }\n}\n\n// ---------------------------------------------------------------------------\n// Typing\n// ---------------------------------------------------------------------------\n\nexport interface TypingEntry {\n updatedAt: number;\n}\n\n/** Keyed by userId; a key is present only while that user is considered typing. */\nexport type TypingState = Record<string, TypingEntry>;\n\nexport type TypingAction =\n | { type: 'notification'; userId: string; isTyping: boolean; now: number }\n | { type: 'expire'; now: number; ttlMs: number };\n\nexport const initialTypingState: TypingState = {};\n\nexport function typingReducer(state: TypingState, action: TypingAction): TypingState {\n switch (action.type) {\n case 'notification': {\n if (action.isTyping) {\n return { ...state, [action.userId]: { updatedAt: action.now } };\n }\n if (!Object.hasOwn(state, action.userId)) return state;\n return Object.fromEntries(Object.entries(state).filter(([userId]) => userId !== action.userId));\n }\n case 'expire': {\n const kept = Object.entries(state).filter(([, entry]) => action.now - entry.updatedAt < action.ttlMs);\n return kept.length === Object.keys(state).length ? state : Object.fromEntries(kept);\n }\n default:\n return state;\n }\n}\n\nexport function typingUsersOf(state: TypingState): string[] {\n return Object.keys(state);\n}\n\n// ---------------------------------------------------------------------------\n// Presence\n// ---------------------------------------------------------------------------\n\n/** Sorted, deduped user ids accumulated from deltas since mount (spec §7: no snapshot endpoint). */\nexport type PresenceState = string[];\n\nexport type PresenceAction = { type: 'delta'; joined: string[]; left: string[] };\n\nexport const initialPresenceState: PresenceState = [];\n\nexport function presenceReducer(state: PresenceState, action: PresenceAction): PresenceState {\n const set = new Set(state);\n for (const id of action.left) set.delete(id);\n for (const id of action.joined) set.add(id);\n return Array.from(set).sort();\n}\n","import type { DotwireClient } from '@dotwire/client';\n\n/**\n * `DotwireClient.subscribe`/`unsubscribe` (and `DotwireRoomHandle.subscribe`/`unsubscribe`)\n * operate on a single, client-wide \"am I in this room's hub group\" flag per `roomId` — it is\n * not refcounted. If two hooks (e.g. `useRoom` and `useTyping`) are both mounted against the\n * same room, calling `subscribe` from each is harmless (redundant hub invokes), but the FIRST\n * hook to unmount would call `unsubscribe` and silently drop the OTHER hook's live delivery.\n *\n * This module refcounts unsubscribe intent per `(client, roomId)` pair so the underlying hub\n * group is only actually left once every hook interested in that room has released it. It does\n * not wrap `subscribe` itself, since redundant `Subscribe` hub invokes are cheap and safe.\n */\nconst refCounts = new WeakMap<DotwireClient, Map<string, number>>();\n\nexport function retainRoom(client: DotwireClient, roomId: string): void {\n let rooms = refCounts.get(client);\n if (!rooms) {\n rooms = new Map();\n refCounts.set(client, rooms);\n }\n rooms.set(roomId, (rooms.get(roomId) ?? 0) + 1);\n}\n\n/**\n * Releases one hook's interest in `roomId`. Returns `true` when this was the last interested\n * hook, meaning the caller should now call `client.unsubscribe(roomId)`.\n */\nexport function releaseRoom(client: DotwireClient, roomId: string): boolean {\n const rooms = refCounts.get(client);\n if (!rooms) return true;\n const next = (rooms.get(roomId) ?? 1) - 1;\n if (next <= 0) {\n rooms.delete(roomId);\n return true;\n }\n rooms.set(roomId, next);\n return false;\n}\n"],"mappings":";;;AAAA,SAAS,eAAe,WAAW,gBAAgC;AACnE,SAAS,qBAAgD;AAsDhD;AApDF,IAAM,iBAAiB,cAAoC,IAAI;AA8B/D,SAAS,gBAAgB,OAAwC;AACtE,QAAM,EAAE,UAAU,SAAS,QAAQ,eAAe,IAAI;AACtD,QAAM,CAAC,eAAe,gBAAgB,IAAI,SAA+B,IAAI;AAE7E,YAAU,MAAM;AACd,QAAI,kBAAkB,CAAC,SAAS;AAC9B,aAAO;AAAA,IACT;AAEA,UAAM,WAAW,IAAI,cAAc,OAAO;AAC1C,qBAAiB,QAAQ;AAEzB,WAAO,MAAM;AACX,eAAS,QAAQ;AACjB,uBAAiB,CAAC,YAAa,YAAY,WAAW,OAAO,OAAQ;AAAA,IACvE;AAAA,EAGF,GAAG,CA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,CAAC,QAAQ,MAAM,CAAC;AAEnB,SAAO,EAAE,SAAS,MAAM;AAC1B;","names":["useEffect","useState","useState","useEffect","options"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dotwire/react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "React hooks and provider for @dotwire/client: DotwireProvider, useRoom, useTyping, usePresence, useConnectionState.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -17,10 +17,13 @@
|
|
|
17
17
|
"require": "./dist/index.cjs"
|
|
18
18
|
}
|
|
19
19
|
},
|
|
20
|
-
"files": [
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"README.md"
|
|
23
|
+
],
|
|
21
24
|
"sideEffects": false,
|
|
22
25
|
"engines": {
|
|
23
|
-
"node": ">=
|
|
26
|
+
"node": ">=22"
|
|
24
27
|
},
|
|
25
28
|
"scripts": {
|
|
26
29
|
"build": "tsup",
|
|
@@ -39,7 +42,7 @@
|
|
|
39
42
|
"type": "module",
|
|
40
43
|
"peerDependencies": {
|
|
41
44
|
"react": ">=18",
|
|
42
|
-
"@dotwire/client": "^0.
|
|
45
|
+
"@dotwire/client": "^0.3.0"
|
|
43
46
|
},
|
|
44
47
|
"devDependencies": {
|
|
45
48
|
"@dotwire/client": "*",
|