nucleus-core-ts 0.9.170 → 0.9.171

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,
@@ -244,17 +245,29 @@ export function useChat(props) {
244
245
  useEffect(()=>{
245
246
  loadConversations();
246
247
  }, []);
248
+ // The pubsub store PREPENDS events (newest at index 0) and caps the buffer,
249
+ // so index/count tracking is unsafe. Track the last-seen event id instead:
250
+ // collect everything newer than it (newest-first), then replay oldest-first.
251
+ // The initial mount skips the existing backlog — history loads via REST.
247
252
  useEffect(()=>{
248
253
  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
- }
254
+ if (!eventsInitializedRef.current) {
255
+ eventsInitializedRef.current = true;
256
+ lastSeenEventIdRef.current = events[0]?.id ?? null;
257
+ return;
258
+ }
259
+ const fresh = [];
260
+ for (const event of events){
261
+ if (event.id === lastSeenEventIdRef.current) break;
262
+ fresh.push(event);
263
+ }
264
+ lastSeenEventIdRef.current = events[0]?.id ?? lastSeenEventIdRef.current;
265
+ for(let i = fresh.length - 1; i >= 0; i--){
266
+ const event = fresh[i];
267
+ if (event.topic === topic) handleRealtime(event.data);
254
268
  }
255
- processedRef.current = events.length;
256
269
  }, [
257
- pubsub.events.length
270
+ pubsub.events[0]?.id
258
271
  ]);
259
272
  return {
260
273
  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.171",
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",