@yorkie-js/react 0.6.2 → 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.
@@ -7559,6 +7559,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
7559
7559
  Code2["ErrPermissionDenied"] = "ErrPermissionDenied";
7560
7560
  Code2["ErrUnauthenticated"] = "ErrUnauthenticated";
7561
7561
  Code2["ErrTooManySubscribers"] = "ErrTooManySubscribers";
7562
+ Code2["ErrTooManyAttachments"] = "ErrTooManyAttachments";
7562
7563
  return Code2;
7563
7564
  })(Code || {});
7564
7565
  class YorkieError extends Error {
@@ -20176,7 +20177,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
20176
20177
  };
20177
20178
  }
20178
20179
  const name = "@yorkie-js/sdk";
20179
- const version = "0.6.2";
20180
+ const version = "0.6.5";
20180
20181
  const pkg = {
20181
20182
  name,
20182
20183
  version
@@ -20965,6 +20966,9 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
20965
20966
  logger.error(`[WD] c:"${this.getKey()}" err :`, err.rawMessage);
20966
20967
  return true;
20967
20968
  }
20969
+ if (errorCodeOf(err) === Code.ErrTooManyAttachments) {
20970
+ return false;
20971
+ }
20968
20972
  if (errorCodeOf(err) === Code.ErrClientNotActivated || errorCodeOf(err) === Code.ErrClientNotFound) {
20969
20973
  this.deactivateInternal();
20970
20974
  }
@@ -21086,15 +21090,23 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
21086
21090
  const [client, setClient] = react.useState(void 0);
21087
21091
  const [loading, setLoading] = react.useState(true);
21088
21092
  const [error, setError] = react.useState(void 0);
21093
+ const clientRef = react.useRef(void 0);
21089
21094
  react.useEffect(() => {
21090
21095
  setLoading(true);
21091
21096
  setError(void 0);
21092
21097
  async function activateClient() {
21098
+ var _a2;
21093
21099
  try {
21100
+ if ((_a2 = clientRef.current) == null ? void 0 : _a2.isActive()) {
21101
+ setClient(clientRef.current);
21102
+ setLoading(false);
21103
+ return;
21104
+ }
21094
21105
  const newClient = new Client(rpcAddr, {
21095
21106
  apiKey
21096
21107
  });
21097
21108
  await newClient.activate();
21109
+ clientRef.current = newClient;
21098
21110
  setClient(newClient);
21099
21111
  } catch (e) {
21100
21112
  setError(
@@ -21106,8 +21118,10 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
21106
21118
  }
21107
21119
  activateClient();
21108
21120
  return () => {
21109
- if (client && client.isActive()) {
21110
- 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;
21111
21125
  }
21112
21126
  };
21113
21127
  }, [apiKey, rpcAddr]);
@@ -21137,6 +21151,9 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
21137
21151
  const [error, setError] = react.useState(void 0);
21138
21152
  const [root, setRoot] = react.useState(initialRoot);
21139
21153
  const [presences, setPresences] = react.useState([]);
21154
+ const [connection, setConnection] = react.useState(
21155
+ StreamConnectionStatus.Disconnected
21156
+ );
21140
21157
  react.useEffect(() => {
21141
21158
  if (clientError) {
21142
21159
  setLoading(false);
@@ -21150,20 +21167,31 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
21150
21167
  setLoading(true);
21151
21168
  setError(void 0);
21152
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
+ );
21153
21186
  async function attachDocument() {
21154
21187
  try {
21155
21188
  await (client == null ? void 0 : client.attach(newDoc, {
21156
21189
  initialRoot,
21157
21190
  initialPresence
21158
21191
  }));
21159
- newDoc.subscribe(() => {
21160
- setRoot(newDoc.getRoot());
21161
- });
21162
- newDoc.subscribe("presence", () => {
21163
- setPresences(newDoc.getPresences());
21164
- });
21165
21192
  setDoc(newDoc);
21166
21193
  setRoot(newDoc.getRoot());
21194
+ setPresences(newDoc.getPresences());
21167
21195
  } catch (err) {
21168
21196
  setError(
21169
21197
  err instanceof Error ? err : new Error("Failed to attach document")
@@ -21177,6 +21205,9 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
21177
21205
  if (client && client.hasDocument(docKey)) {
21178
21206
  client.detach(newDoc);
21179
21207
  }
21208
+ for (const unsub of unsubs) {
21209
+ unsub();
21210
+ }
21180
21211
  };
21181
21212
  }, [client, clientLoading, clientError, docKey]);
21182
21213
  const update = react.useCallback(
@@ -21197,7 +21228,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
21197
21228
  return /* @__PURE__ */ jsxRuntime.jsx(
21198
21229
  DocumentContext.Provider,
21199
21230
  {
21200
- value: { root, presences, update, loading, error },
21231
+ value: { root, presences, connection, update, loading, error },
21201
21232
  children
21202
21233
  }
21203
21234
  );
@@ -21210,6 +21241,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
21210
21241
  return {
21211
21242
  root: context.root,
21212
21243
  presences: context.presences,
21244
+ connection: context.connection,
21213
21245
  update: context.update,
21214
21246
  loading: context.loading,
21215
21247
  error: context.error
@@ -21229,8 +21261,16 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
21229
21261
  }
21230
21262
  return context.presences;
21231
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
+ };
21232
21271
  exports2.DocumentProvider = DocumentProvider;
21233
21272
  exports2.YorkieProvider = YorkieProvider;
21273
+ exports2.useConnection = useConnection;
21234
21274
  exports2.useDocument = useDocument;
21235
21275
  exports2.usePresences = usePresences;
21236
21276
  exports2.useRoot = useRoot;