@meshagent/meshagent-react 0.5.2 → 0.5.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## [0.5.4]
2
+ - Stability
3
+
4
+ ## [0.5.3]
5
+ - Stability
6
+
1
7
  ## [0.5.2]
2
8
  - Stability
3
9
 
@@ -25,6 +25,7 @@ export interface UseMessageChatResult {
25
25
  selectAttachments: (files: File[]) => void;
26
26
  attachments: FileUpload[];
27
27
  setAttachments: (attachments: FileUpload[]) => void;
28
+ schemaFileExists: boolean;
28
29
  }
29
30
  export declare function fileToAsyncIterable(file: File): AsyncIterable<Uint8Array>;
30
31
  export declare function useChat({ room, path, participants, participantNames, initialMessage, includeLocalParticipant }: UseMessageChatProps): UseMessageChatResult;
package/dist/cjs/chat.js CHANGED
@@ -123,7 +123,7 @@ function fileToAsyncIterable(file) {
123
123
  return (hasNativeStream ? nativeStream : sliceStream)();
124
124
  }
125
125
  function useChat({ room, path, participants, participantNames, initialMessage, includeLocalParticipant }) {
126
- const { document } = (0, document_connection_scope_1.useDocumentConnection)({ room, path });
126
+ const { document, schemaFileExists } = (0, document_connection_scope_1.useDocumentConnection)({ room, path });
127
127
  const [messages, setMessages] = (0, react_1.useState)(() => document ? mapMessages(document) : []);
128
128
  const [attachments, setAttachments] = (0, react_1.useState)([]);
129
129
  (0, document_connection_scope_1.useDocumentChanged)({
@@ -176,5 +176,6 @@ function useChat({ room, path, participants, participantNames, initialMessage, i
176
176
  selectAttachments,
177
177
  attachments,
178
178
  setAttachments,
179
+ schemaFileExists,
179
180
  };
180
181
  }
@@ -7,6 +7,7 @@ export interface UseDocumentConnectionResult {
7
7
  document: MeshDocument | null;
8
8
  error: unknown;
9
9
  loading: boolean;
10
+ schemaFileExists: boolean;
10
11
  }
11
12
  /**
12
13
  * Connects to a Mesh document inside an existing RoomClient and keeps it in sync.
@@ -13,23 +13,37 @@ const react_1 = require("react");
13
13
  * @param path Path to the document inside the room.
14
14
  */
15
15
  function useDocumentConnection({ room, path }) {
16
+ const [schemaFileExists, setSchemaFileExists] = (0, react_1.useState)(null);
16
17
  const [document, setDocument] = (0, react_1.useState)(null);
17
18
  const [error, setError] = (0, react_1.useState)(null);
19
+ const openedRef = (0, react_1.useRef)(false);
18
20
  const retryCountRef = (0, react_1.useRef)(0);
19
21
  const timeoutRef = (0, react_1.useRef)(null);
22
+ const pathExtension = path.split('.').pop()?.toLowerCase();
23
+ const schemaFile = `.schemas/${pathExtension}.json`;
20
24
  (0, react_1.useEffect)(() => {
21
25
  let cancelled = false;
22
26
  const openDocument = async () => {
23
27
  try {
28
+ const schemaExists = await room.storage.exists(schemaFile);
29
+ if (schemaExists) {
30
+ setSchemaFileExists(true);
31
+ }
32
+ else {
33
+ setSchemaFileExists(false);
34
+ return;
35
+ }
24
36
  const doc = await room.sync.open(path);
25
37
  if (cancelled)
26
38
  return;
39
+ openedRef.current = true;
27
40
  // sleep for 100 ms to ensure the document is ready
28
41
  await new Promise(resolve => setTimeout(resolve, 100));
29
42
  setDocument(doc);
30
43
  setError(null);
31
44
  }
32
45
  catch (err) {
46
+ console.error('Failed to open document:', err);
33
47
  if (cancelled)
34
48
  return;
35
49
  setError(err);
@@ -46,15 +60,19 @@ function useDocumentConnection({ room, path }) {
46
60
  clearTimeout(timeoutRef.current);
47
61
  timeoutRef.current = null;
48
62
  }
49
- room.sync.close(path);
63
+ if (openedRef.current) {
64
+ room.sync.close(path);
65
+ }
50
66
  setDocument(null);
51
67
  retryCountRef.current = 0;
68
+ openedRef.current = false;
52
69
  };
53
70
  }, [path]);
54
71
  return {
55
72
  document,
56
73
  error,
57
74
  loading: document === null && error == null,
75
+ schemaFileExists: schemaFileExists !== null ? schemaFileExists : true,
58
76
  };
59
77
  }
60
78
  function useDocumentChanged({ document, onChanged }) {
@@ -25,6 +25,7 @@ export interface UseMessageChatResult {
25
25
  selectAttachments: (files: File[]) => void;
26
26
  attachments: FileUpload[];
27
27
  setAttachments: (attachments: FileUpload[]) => void;
28
+ schemaFileExists: boolean;
28
29
  }
29
30
  export declare function fileToAsyncIterable(file: File): AsyncIterable<Uint8Array>;
30
31
  export declare function useChat({ room, path, participants, participantNames, initialMessage, includeLocalParticipant }: UseMessageChatProps): UseMessageChatResult;
package/dist/esm/chat.js CHANGED
@@ -117,7 +117,7 @@ export function fileToAsyncIterable(file) {
117
117
  return (hasNativeStream ? nativeStream : sliceStream)();
118
118
  }
119
119
  export function useChat({ room, path, participants, participantNames, initialMessage, includeLocalParticipant }) {
120
- const { document } = useDocumentConnection({ room, path });
120
+ const { document, schemaFileExists } = useDocumentConnection({ room, path });
121
121
  const [messages, setMessages] = useState(() => document ? mapMessages(document) : []);
122
122
  const [attachments, setAttachments] = useState([]);
123
123
  useDocumentChanged({
@@ -170,5 +170,6 @@ export function useChat({ room, path, participants, participantNames, initialMes
170
170
  selectAttachments,
171
171
  attachments,
172
172
  setAttachments,
173
+ schemaFileExists,
173
174
  };
174
175
  }
@@ -7,6 +7,7 @@ export interface UseDocumentConnectionResult {
7
7
  document: MeshDocument | null;
8
8
  error: unknown;
9
9
  loading: boolean;
10
+ schemaFileExists: boolean;
10
11
  }
11
12
  /**
12
13
  * Connects to a Mesh document inside an existing RoomClient and keeps it in sync.
@@ -9,23 +9,37 @@ import { useEffect, useRef, useState } from 'react';
9
9
  * @param path Path to the document inside the room.
10
10
  */
11
11
  export function useDocumentConnection({ room, path }) {
12
+ const [schemaFileExists, setSchemaFileExists] = useState(null);
12
13
  const [document, setDocument] = useState(null);
13
14
  const [error, setError] = useState(null);
15
+ const openedRef = useRef(false);
14
16
  const retryCountRef = useRef(0);
15
17
  const timeoutRef = useRef(null);
18
+ const pathExtension = path.split('.').pop()?.toLowerCase();
19
+ const schemaFile = `.schemas/${pathExtension}.json`;
16
20
  useEffect(() => {
17
21
  let cancelled = false;
18
22
  const openDocument = async () => {
19
23
  try {
24
+ const schemaExists = await room.storage.exists(schemaFile);
25
+ if (schemaExists) {
26
+ setSchemaFileExists(true);
27
+ }
28
+ else {
29
+ setSchemaFileExists(false);
30
+ return;
31
+ }
20
32
  const doc = await room.sync.open(path);
21
33
  if (cancelled)
22
34
  return;
35
+ openedRef.current = true;
23
36
  // sleep for 100 ms to ensure the document is ready
24
37
  await new Promise(resolve => setTimeout(resolve, 100));
25
38
  setDocument(doc);
26
39
  setError(null);
27
40
  }
28
41
  catch (err) {
42
+ console.error('Failed to open document:', err);
29
43
  if (cancelled)
30
44
  return;
31
45
  setError(err);
@@ -42,15 +56,19 @@ export function useDocumentConnection({ room, path }) {
42
56
  clearTimeout(timeoutRef.current);
43
57
  timeoutRef.current = null;
44
58
  }
45
- room.sync.close(path);
59
+ if (openedRef.current) {
60
+ room.sync.close(path);
61
+ }
46
62
  setDocument(null);
47
63
  retryCountRef.current = 0;
64
+ openedRef.current = false;
48
65
  };
49
66
  }, [path]);
50
67
  return {
51
68
  document,
52
69
  error,
53
70
  loading: document === null && error == null,
71
+ schemaFileExists: schemaFileExists !== null ? schemaFileExists : true,
54
72
  };
55
73
  }
56
74
  export function useDocumentChanged({ document, onChanged }) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meshagent/meshagent-react",
3
- "version": "0.5.2",
3
+ "version": "0.5.4",
4
4
  "description": "Meshagent React Client",
5
5
  "homepage": "https://github.com/meshagent/meshagent-react",
6
6
  "scripts": {
@@ -32,7 +32,7 @@
32
32
  },
33
33
  "dependencies": {
34
34
  "react": "^19.1.0",
35
- "@meshagent/meshagent": "^0.5.2",
35
+ "@meshagent/meshagent": "^0.5.4",
36
36
  "react-dom": "^19.1.0",
37
37
  "typescript": "^5.8.3",
38
38
  "uuid": "^11.1.0"