nucleus-core-ts 0.9.170 → 0.9.172

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.
@@ -19,7 +19,8 @@ function previewFor(message) {
19
19
  export function useChat(props) {
20
20
  const { currentUserId, actions, topic = 'chat', wsUrl, wsPath } = props;
21
21
  const store = useChatStore();
22
- const processedRef = useRef(0);
22
+ const lastSeenEventIdRef = useRef(null);
23
+ const eventsInitializedRef = useRef(false);
23
24
  const typingSentAtRef = useRef(0);
24
25
  const pubsub = usePubSub({
25
26
  userId: currentUserId,
@@ -53,7 +54,18 @@ export function useChat(props) {
53
54
  payload: {
54
55
  id: conversationId
55
56
  },
56
- onAfterHandle: ()=>{},
57
+ onAfterHandle: ()=>{
58
+ // The server excludes the reader from the `conversation.read` broadcast,
59
+ // so any external unread indicator (e.g. a header bell) won't otherwise
60
+ // know to refresh. Notify it once the read has been persisted.
61
+ if (typeof window !== 'undefined') {
62
+ window.dispatchEvent(new CustomEvent('nucleus:chat-read', {
63
+ detail: {
64
+ conversationId
65
+ }
66
+ }));
67
+ }
68
+ },
57
69
  onErrorHandle: ()=>{}
58
70
  });
59
71
  });
@@ -244,17 +256,29 @@ export function useChat(props) {
244
256
  useEffect(()=>{
245
257
  loadConversations();
246
258
  }, []);
259
+ // The pubsub store PREPENDS events (newest at index 0) and caps the buffer,
260
+ // so index/count tracking is unsafe. Track the last-seen event id instead:
261
+ // collect everything newer than it (newest-first), then replay oldest-first.
262
+ // The initial mount skips the existing backlog — history loads via REST.
247
263
  useEffect(()=>{
248
264
  const events = pubsub.events;
249
- for(let i = processedRef.current; i < events.length; i++){
250
- const event = events[i];
251
- if (event && event.topic === topic) {
252
- handleRealtime(event.data);
253
- }
265
+ if (!eventsInitializedRef.current) {
266
+ eventsInitializedRef.current = true;
267
+ lastSeenEventIdRef.current = events[0]?.id ?? null;
268
+ return;
269
+ }
270
+ const fresh = [];
271
+ for (const event of events){
272
+ if (event.id === lastSeenEventIdRef.current) break;
273
+ fresh.push(event);
274
+ }
275
+ lastSeenEventIdRef.current = events[0]?.id ?? lastSeenEventIdRef.current;
276
+ for(let i = fresh.length - 1; i >= 0; i--){
277
+ const event = fresh[i];
278
+ if (event.topic === topic) handleRealtime(event.data);
254
279
  }
255
- processedRef.current = events.length;
256
280
  }, [
257
- pubsub.events.length
281
+ pubsub.events[0]?.id
258
282
  ]);
259
283
  return {
260
284
  store,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nucleus-core-ts",
3
- "version": "0.9.170",
3
+ "version": "0.9.172",
4
4
  "description": "Production-ready, enterprise-grade TypeScript framework for building multi-tenant APIs",
5
5
  "author": "Hidayet Can Özcan <hidayetcan@gmail.com>",
6
6
  "license": "SEE LICENSE IN LICENSE",