@yorkie-js/react 0.6.3 → 0.6.5

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.
@@ -20177,7 +20177,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
20177
20177
  };
20178
20178
  }
20179
20179
  const name = "@yorkie-js/sdk";
20180
- const version = "0.6.3";
20180
+ const version = "0.6.5";
20181
20181
  const pkg = {
20182
20182
  name,
20183
20183
  version
@@ -21090,15 +21090,23 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
21090
21090
  const [client, setClient] = react.useState(void 0);
21091
21091
  const [loading, setLoading] = react.useState(true);
21092
21092
  const [error, setError] = react.useState(void 0);
21093
+ const clientRef = react.useRef(void 0);
21093
21094
  react.useEffect(() => {
21094
21095
  setLoading(true);
21095
21096
  setError(void 0);
21096
21097
  async function activateClient() {
21098
+ var _a2;
21097
21099
  try {
21100
+ if ((_a2 = clientRef.current) == null ? void 0 : _a2.isActive()) {
21101
+ setClient(clientRef.current);
21102
+ setLoading(false);
21103
+ return;
21104
+ }
21098
21105
  const newClient = new Client(rpcAddr, {
21099
21106
  apiKey
21100
21107
  });
21101
21108
  await newClient.activate();
21109
+ clientRef.current = newClient;
21102
21110
  setClient(newClient);
21103
21111
  } catch (e) {
21104
21112
  setError(
@@ -21110,8 +21118,10 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
21110
21118
  }
21111
21119
  activateClient();
21112
21120
  return () => {
21113
- if (client && client.isActive()) {
21114
- client.deactivate({ keepalive: true });
21121
+ var _a2;
21122
+ if ((_a2 = clientRef.current) == null ? void 0 : _a2.isActive()) {
21123
+ clientRef.current.deactivate({ keepalive: true });
21124
+ clientRef.current = void 0;
21115
21125
  }
21116
21126
  };
21117
21127
  }, [apiKey, rpcAddr]);
@@ -21141,6 +21151,9 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
21141
21151
  const [error, setError] = react.useState(void 0);
21142
21152
  const [root, setRoot] = react.useState(initialRoot);
21143
21153
  const [presences, setPresences] = react.useState([]);
21154
+ const [connection, setConnection] = react.useState(
21155
+ StreamConnectionStatus.Disconnected
21156
+ );
21144
21157
  react.useEffect(() => {
21145
21158
  if (clientError) {
21146
21159
  setLoading(false);
@@ -21154,20 +21167,31 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
21154
21167
  setLoading(true);
21155
21168
  setError(void 0);
21156
21169
  const newDoc = new Document(docKey);
21170
+ const unsubs = [];
21171
+ unsubs.push(
21172
+ newDoc.subscribe(() => {
21173
+ setRoot(newDoc.getRoot());
21174
+ })
21175
+ );
21176
+ unsubs.push(
21177
+ newDoc.subscribe("presence", () => {
21178
+ setPresences(newDoc.getPresences());
21179
+ })
21180
+ );
21181
+ unsubs.push(
21182
+ newDoc.subscribe("connection", (event) => {
21183
+ setConnection(event.value);
21184
+ })
21185
+ );
21157
21186
  async function attachDocument() {
21158
21187
  try {
21159
21188
  await (client == null ? void 0 : client.attach(newDoc, {
21160
21189
  initialRoot,
21161
21190
  initialPresence
21162
21191
  }));
21163
- newDoc.subscribe(() => {
21164
- setRoot(newDoc.getRoot());
21165
- });
21166
- newDoc.subscribe("presence", () => {
21167
- setPresences(newDoc.getPresences());
21168
- });
21169
21192
  setDoc(newDoc);
21170
21193
  setRoot(newDoc.getRoot());
21194
+ setPresences(newDoc.getPresences());
21171
21195
  } catch (err) {
21172
21196
  setError(
21173
21197
  err instanceof Error ? err : new Error("Failed to attach document")
@@ -21181,6 +21205,9 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
21181
21205
  if (client && client.hasDocument(docKey)) {
21182
21206
  client.detach(newDoc);
21183
21207
  }
21208
+ for (const unsub of unsubs) {
21209
+ unsub();
21210
+ }
21184
21211
  };
21185
21212
  }, [client, clientLoading, clientError, docKey]);
21186
21213
  const update = react.useCallback(
@@ -21201,7 +21228,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
21201
21228
  return /* @__PURE__ */ jsxRuntime.jsx(
21202
21229
  DocumentContext.Provider,
21203
21230
  {
21204
- value: { root, presences, update, loading, error },
21231
+ value: { root, presences, connection, update, loading, error },
21205
21232
  children
21206
21233
  }
21207
21234
  );
@@ -21214,6 +21241,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
21214
21241
  return {
21215
21242
  root: context.root,
21216
21243
  presences: context.presences,
21244
+ connection: context.connection,
21217
21245
  update: context.update,
21218
21246
  loading: context.loading,
21219
21247
  error: context.error
@@ -21233,8 +21261,16 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
21233
21261
  }
21234
21262
  return context.presences;
21235
21263
  };
21264
+ const useConnection = () => {
21265
+ const context = react.useContext(DocumentContext);
21266
+ if (!context) {
21267
+ throw new Error("useConnection must be used within a DocumentProvider");
21268
+ }
21269
+ return context.connection;
21270
+ };
21236
21271
  exports2.DocumentProvider = DocumentProvider;
21237
21272
  exports2.YorkieProvider = YorkieProvider;
21273
+ exports2.useConnection = useConnection;
21238
21274
  exports2.useDocument = useDocument;
21239
21275
  exports2.usePresences = usePresences;
21240
21276
  exports2.useRoot = useRoot;