@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.
@@ -1,8 +1,10 @@
1
+ import { default as default_2 } from 'react';
1
2
  import { Indexable } from '@yorkie-js/sdk';
2
3
  import { JSONArray } from '@yorkie-js/sdk';
3
4
  import { JSONObject } from '@yorkie-js/sdk';
4
5
  import { JSX } from 'react/jsx-dev-runtime';
5
6
  import { PropsWithChildren } from 'react';
7
+ import { StreamConnectionStatus } from '@yorkie-js/sdk';
6
8
 
7
9
  /**
8
10
  * `DocumentProvider` is a component that provides a document to its children.
@@ -13,13 +15,18 @@ export declare const DocumentProvider: <R, P extends Indexable = Indexable>({ do
13
15
  docKey: string;
14
16
  initialRoot?: R | undefined;
15
17
  initialPresence?: P | undefined;
16
- children?: React.ReactNode;
18
+ children?: default_2.ReactNode;
17
19
  }) => JSX.Element;
18
20
 
19
21
  export { JSONArray }
20
22
 
21
23
  export { JSONObject }
22
24
 
25
+ /**
26
+ * `useConnection` is a custom hook that returns the connection status of the document.
27
+ */
28
+ export declare const useConnection: () => StreamConnectionStatus;
29
+
23
30
  /**
24
31
  * `useDocument` is a custom hook that returns the root object and update function of the document.
25
32
  * This hook must be used within a `DocumentProvider`.
@@ -30,6 +37,7 @@ export declare const useDocument: <R, P extends Indexable = Indexable>() => {
30
37
  clientID: string;
31
38
  presence: P;
32
39
  }[];
40
+ connection: StreamConnectionStatus;
33
41
  update: (callback: (root: R, presence: P) => void) => void;
34
42
  loading: boolean;
35
43
  error: Error | undefined;
@@ -2,7 +2,7 @@ var __defProp = Object.defineProperty;
2
2
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
3
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
4
  var _a, _b, _c, _d, _e, _f, _g, _h, _i;
5
- import { useState, useEffect, useCallback, createContext, useContext } from "react";
5
+ import { useState, useEffect, useCallback, createContext, useRef, useContext } from "react";
6
6
  import { jsx } from "react/jsx-runtime";
7
7
  var Code$1;
8
8
  (function(Code2) {
@@ -20175,7 +20175,7 @@ function createAuthInterceptor(apiKey, token) {
20175
20175
  };
20176
20176
  }
20177
20177
  const name = "@yorkie-js/sdk";
20178
- const version = "0.6.3";
20178
+ const version = "0.6.5";
20179
20179
  const pkg = {
20180
20180
  name,
20181
20181
  version
@@ -21088,15 +21088,23 @@ const YorkieProvider = ({ apiKey, rpcAddr = "https://api.yorkie.dev", children }
21088
21088
  const [client, setClient] = useState(void 0);
21089
21089
  const [loading, setLoading] = useState(true);
21090
21090
  const [error, setError] = useState(void 0);
21091
+ const clientRef = useRef(void 0);
21091
21092
  useEffect(() => {
21092
21093
  setLoading(true);
21093
21094
  setError(void 0);
21094
21095
  async function activateClient() {
21096
+ var _a2;
21095
21097
  try {
21098
+ if ((_a2 = clientRef.current) == null ? void 0 : _a2.isActive()) {
21099
+ setClient(clientRef.current);
21100
+ setLoading(false);
21101
+ return;
21102
+ }
21096
21103
  const newClient = new Client(rpcAddr, {
21097
21104
  apiKey
21098
21105
  });
21099
21106
  await newClient.activate();
21107
+ clientRef.current = newClient;
21100
21108
  setClient(newClient);
21101
21109
  } catch (e) {
21102
21110
  setError(
@@ -21108,8 +21116,10 @@ const YorkieProvider = ({ apiKey, rpcAddr = "https://api.yorkie.dev", children }
21108
21116
  }
21109
21117
  activateClient();
21110
21118
  return () => {
21111
- if (client && client.isActive()) {
21112
- client.deactivate({ keepalive: true });
21119
+ var _a2;
21120
+ if ((_a2 = clientRef.current) == null ? void 0 : _a2.isActive()) {
21121
+ clientRef.current.deactivate({ keepalive: true });
21122
+ clientRef.current = void 0;
21113
21123
  }
21114
21124
  };
21115
21125
  }, [apiKey, rpcAddr]);
@@ -21139,6 +21149,9 @@ const DocumentProvider = ({
21139
21149
  const [error, setError] = useState(void 0);
21140
21150
  const [root, setRoot] = useState(initialRoot);
21141
21151
  const [presences, setPresences] = useState([]);
21152
+ const [connection, setConnection] = useState(
21153
+ StreamConnectionStatus.Disconnected
21154
+ );
21142
21155
  useEffect(() => {
21143
21156
  if (clientError) {
21144
21157
  setLoading(false);
@@ -21152,20 +21165,31 @@ const DocumentProvider = ({
21152
21165
  setLoading(true);
21153
21166
  setError(void 0);
21154
21167
  const newDoc = new Document(docKey);
21168
+ const unsubs = [];
21169
+ unsubs.push(
21170
+ newDoc.subscribe(() => {
21171
+ setRoot(newDoc.getRoot());
21172
+ })
21173
+ );
21174
+ unsubs.push(
21175
+ newDoc.subscribe("presence", () => {
21176
+ setPresences(newDoc.getPresences());
21177
+ })
21178
+ );
21179
+ unsubs.push(
21180
+ newDoc.subscribe("connection", (event) => {
21181
+ setConnection(event.value);
21182
+ })
21183
+ );
21155
21184
  async function attachDocument() {
21156
21185
  try {
21157
21186
  await (client == null ? void 0 : client.attach(newDoc, {
21158
21187
  initialRoot,
21159
21188
  initialPresence
21160
21189
  }));
21161
- newDoc.subscribe(() => {
21162
- setRoot(newDoc.getRoot());
21163
- });
21164
- newDoc.subscribe("presence", () => {
21165
- setPresences(newDoc.getPresences());
21166
- });
21167
21190
  setDoc(newDoc);
21168
21191
  setRoot(newDoc.getRoot());
21192
+ setPresences(newDoc.getPresences());
21169
21193
  } catch (err) {
21170
21194
  setError(
21171
21195
  err instanceof Error ? err : new Error("Failed to attach document")
@@ -21179,6 +21203,9 @@ const DocumentProvider = ({
21179
21203
  if (client && client.hasDocument(docKey)) {
21180
21204
  client.detach(newDoc);
21181
21205
  }
21206
+ for (const unsub of unsubs) {
21207
+ unsub();
21208
+ }
21182
21209
  };
21183
21210
  }, [client, clientLoading, clientError, docKey]);
21184
21211
  const update = useCallback(
@@ -21199,7 +21226,7 @@ const DocumentProvider = ({
21199
21226
  return /* @__PURE__ */ jsx(
21200
21227
  DocumentContext.Provider,
21201
21228
  {
21202
- value: { root, presences, update, loading, error },
21229
+ value: { root, presences, connection, update, loading, error },
21203
21230
  children
21204
21231
  }
21205
21232
  );
@@ -21212,6 +21239,7 @@ const useDocument = () => {
21212
21239
  return {
21213
21240
  root: context.root,
21214
21241
  presences: context.presences,
21242
+ connection: context.connection,
21215
21243
  update: context.update,
21216
21244
  loading: context.loading,
21217
21245
  error: context.error
@@ -21231,9 +21259,17 @@ const usePresences = () => {
21231
21259
  }
21232
21260
  return context.presences;
21233
21261
  };
21262
+ const useConnection = () => {
21263
+ const context = useContext(DocumentContext);
21264
+ if (!context) {
21265
+ throw new Error("useConnection must be used within a DocumentProvider");
21266
+ }
21267
+ return context.connection;
21268
+ };
21234
21269
  export {
21235
21270
  DocumentProvider,
21236
21271
  YorkieProvider,
21272
+ useConnection,
21237
21273
  useDocument,
21238
21274
  usePresences,
21239
21275
  useRoot,