@yorkie-js/react 0.6.7-rc → 0.6.7

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.
@@ -64,10 +64,6 @@ export declare const useRoot: () => {
64
64
  /**
65
65
  * `useYorkieDoc` is a custom hook that initializes a Yorkie Client and a
66
66
  * document in a single hook.
67
- *
68
- * @param apiKey
69
- * @param docKey
70
- * @returns
71
67
  */
72
68
  export declare function useYorkieDoc<R, P extends Indexable = Indexable>(apiKey: string, docKey: string, opts?: Omit<ClientOptions, 'apiKey'> & {
73
69
  initialRoot?: R;
@@ -4,6 +4,12 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
4
4
  var _a, _b, _c, _d, _e, _f, _g, _h, _i;
5
5
  import { jsx } from "react/jsx-runtime";
6
6
  import { createContext, useMemo, useState, useEffect, useContext, useCallback } from "react";
7
+ const name$1 = "@yorkie-js/react";
8
+ const version$1 = "0.6.7";
9
+ const pkg$1 = {
10
+ name: name$1,
11
+ version: version$1
12
+ };
7
13
  var Code$1;
8
14
  (function(Code2) {
9
15
  Code2[Code2["Canceled"] = 1] = "Canceled";
@@ -20234,14 +20240,18 @@ function createAuthInterceptor(apiKey, token) {
20234
20240
  };
20235
20241
  }
20236
20242
  const name = "@yorkie-js/sdk";
20237
- const version = "0.6.6";
20243
+ const version = "0.6.7";
20238
20244
  const pkg = {
20239
20245
  name,
20240
20246
  version
20241
20247
  };
20242
- function createMetricInterceptor() {
20248
+ function createMetricInterceptor(userAgent) {
20243
20249
  return (next) => async (req) => {
20244
- req.header.set("x-yorkie-user-agent", pkg.name + "/" + pkg.version);
20250
+ if (userAgent) {
20251
+ req.header.set("x-yorkie-user-agent", userAgent);
20252
+ } else {
20253
+ req.header.set("x-yorkie-user-agent", pkg.name + "/" + pkg.version);
20254
+ }
20245
20255
  return await next(req);
20246
20256
  };
20247
20257
  }
@@ -20323,7 +20333,10 @@ class Client {
20323
20333
  YorkieService,
20324
20334
  createGrpcWebTransport({
20325
20335
  baseUrl: rpcAddr,
20326
- interceptors: [authInterceptor, createMetricInterceptor()],
20336
+ interceptors: [
20337
+ authInterceptor,
20338
+ createMetricInterceptor(opts == null ? void 0 : opts.userAgent)
20339
+ ],
20327
20340
  fetch: (input, init) => {
20328
20341
  const newInit = {
20329
20342
  ...init,
@@ -20349,22 +20362,28 @@ class Client {
20349
20362
  this.setAuthToken(token);
20350
20363
  }
20351
20364
  return this.enqueueTask(async () => {
20352
- return this.rpcClient.activateClient(
20353
- {
20354
- clientKey: this.key,
20355
- metadata: this.metadata
20356
- },
20357
- { headers: { "x-shard-key": this.apiKey } }
20358
- ).then((res) => {
20365
+ try {
20366
+ const res = await this.rpcClient.activateClient(
20367
+ {
20368
+ clientKey: this.key,
20369
+ metadata: this.metadata
20370
+ },
20371
+ { headers: { "x-shard-key": this.apiKey } }
20372
+ );
20359
20373
  this.id = res.clientId;
20360
20374
  this.status = "activated";
20361
20375
  this.runSyncLoop();
20362
20376
  logger.info(`[AC] c:"${this.getKey()}" activated, id:"${this.id}"`);
20363
- }).catch(async (err) => {
20377
+ if (typeof window !== "undefined") {
20378
+ window.addEventListener("beforeunload", async () => {
20379
+ await this.deactivate({ keepalive: true });
20380
+ });
20381
+ }
20382
+ } catch (err) {
20364
20383
  logger.error(`[AC] c:"${this.getKey()}" err :`, err);
20365
20384
  await this.handleConnectError(err);
20366
20385
  throw err;
20367
- });
20386
+ }
20368
20387
  });
20369
20388
  }
20370
20389
  /**
@@ -20413,7 +20432,7 @@ class Client {
20413
20432
  * `attach` attaches the given document to this client. It tells the server that
20414
20433
  * this client will synchronize the given document.
20415
20434
  */
20416
- attach(doc, options = {}) {
20435
+ attach(doc, opts = {}) {
20417
20436
  if (!this.isActive()) {
20418
20437
  throw new YorkieError(
20419
20438
  Code.ErrClientNotActivated,
@@ -20427,32 +20446,30 @@ class Client {
20427
20446
  );
20428
20447
  }
20429
20448
  doc.setActor(this.id);
20430
- doc.update((_, p) => p.set(options.initialPresence || {}));
20431
- const unsubscribeBroacastEvent = doc.subscribe(
20432
- "local-broadcast",
20433
- async (event) => {
20434
- var _a2;
20435
- const { topic, payload } = event.value;
20436
- const errorFn = (_a2 = event.options) == null ? void 0 : _a2.error;
20437
- const options2 = event.options;
20438
- try {
20439
- await this.broadcast(doc.getKey(), topic, payload, options2);
20440
- } catch (error) {
20441
- if (error instanceof Error) {
20442
- errorFn == null ? void 0 : errorFn(error);
20443
- }
20449
+ doc.update((_, p) => p.set(opts.initialPresence || {}));
20450
+ const unsub = doc.subscribe("local-broadcast", async (event) => {
20451
+ var _a2;
20452
+ const { topic, payload } = event.value;
20453
+ const errorFn = (_a2 = event.options) == null ? void 0 : _a2.error;
20454
+ const options = event.options;
20455
+ try {
20456
+ await this.broadcast(doc.getKey(), topic, payload, options);
20457
+ } catch (error) {
20458
+ if (error instanceof Error) {
20459
+ errorFn == null ? void 0 : errorFn(error);
20444
20460
  }
20445
20461
  }
20446
- );
20447
- const syncMode = options.syncMode ?? "realtime";
20462
+ });
20463
+ const syncMode = opts.syncMode ?? "realtime";
20448
20464
  return this.enqueueTask(async () => {
20449
- return this.rpcClient.attachDocument(
20450
- {
20451
- clientId: this.id,
20452
- changePack: converter.toChangePack(doc.createChangePack())
20453
- },
20454
- { headers: { "x-shard-key": `${this.apiKey}/${doc.getKey()}` } }
20455
- ).then(async (res) => {
20465
+ try {
20466
+ const res = await this.rpcClient.attachDocument(
20467
+ {
20468
+ clientId: this.id,
20469
+ changePack: converter.toChangePack(doc.createChangePack())
20470
+ },
20471
+ { headers: { "x-shard-key": `${this.apiKey}/${doc.getKey()}` } }
20472
+ );
20456
20473
  const pack = converter.fromChangePack(res.changePack);
20457
20474
  doc.applyChangePack(pack);
20458
20475
  if (doc.getStatus() === DocStatus.Removed) {
@@ -20466,7 +20483,7 @@ class Client {
20466
20483
  doc,
20467
20484
  res.documentId,
20468
20485
  syncMode,
20469
- unsubscribeBroacastEvent
20486
+ unsub
20470
20487
  )
20471
20488
  );
20472
20489
  if (syncMode !== "manual") {
@@ -20474,8 +20491,8 @@ class Client {
20474
20491
  }
20475
20492
  logger.info(`[AD] c:"${this.getKey()}" attaches d:"${doc.getKey()}"`);
20476
20493
  const crdtObject = doc.getRootObject();
20477
- if (options.initialRoot) {
20478
- const initialRoot = options.initialRoot;
20494
+ if (opts.initialRoot) {
20495
+ const initialRoot = opts.initialRoot;
20479
20496
  doc.update((root) => {
20480
20497
  for (const [k, v] of Object.entries(initialRoot)) {
20481
20498
  if (!crdtObject.has(k)) {
@@ -20486,11 +20503,11 @@ class Client {
20486
20503
  });
20487
20504
  }
20488
20505
  return doc;
20489
- }).catch(async (err) => {
20506
+ } catch (err) {
20490
20507
  logger.error(`[AD] c:"${this.getKey()}" err :`, err);
20491
20508
  await this.handleConnectError(err);
20492
20509
  throw err;
20493
- });
20510
+ }
20494
20511
  });
20495
20512
  }
20496
20513
  /**
@@ -20501,7 +20518,7 @@ class Client {
20501
20518
  * the changes should be applied to other replicas before GC time. For this,
20502
20519
  * if the document is no longer used by this client, it should be detached.
20503
20520
  */
20504
- detach(doc, options = { keepalive: false }) {
20521
+ detach(doc, opts = { keepalive: false }) {
20505
20522
  if (!this.isActive()) {
20506
20523
  throw new YorkieError(
20507
20524
  Code.ErrClientNotActivated,
@@ -20517,15 +20534,16 @@ class Client {
20517
20534
  }
20518
20535
  doc.update((_, p) => p.clear());
20519
20536
  const task = async () => {
20520
- return await this.rpcClient.detachDocument(
20521
- {
20522
- clientId: this.id,
20523
- documentId: attachment.docID,
20524
- changePack: converter.toChangePack(doc.createChangePack()),
20525
- removeIfNotAttached: options.removeIfNotAttached ?? false
20526
- },
20527
- { headers: { "x-shard-key": `${this.apiKey}/${doc.getKey()}` } }
20528
- ).then((res) => {
20537
+ try {
20538
+ const res = await this.rpcClient.detachDocument(
20539
+ {
20540
+ clientId: this.id,
20541
+ documentId: attachment.docID,
20542
+ changePack: converter.toChangePack(doc.createChangePack()),
20543
+ removeIfNotAttached: opts.removeIfNotAttached ?? false
20544
+ },
20545
+ { headers: { "x-shard-key": `${this.apiKey}/${doc.getKey()}` } }
20546
+ );
20529
20547
  const pack = converter.fromChangePack(res.changePack);
20530
20548
  doc.applyChangePack(pack);
20531
20549
  if (doc.getStatus() !== DocStatus.Removed) {
@@ -20534,13 +20552,13 @@ class Client {
20534
20552
  this.detachInternal(doc.getKey());
20535
20553
  logger.info(`[DD] c:"${this.getKey()}" detaches d:"${doc.getKey()}"`);
20536
20554
  return doc;
20537
- }).catch(async (err) => {
20555
+ } catch (err) {
20538
20556
  logger.error(`[DD] c:"${this.getKey()}" err :`, err);
20539
20557
  await this.handleConnectError(err);
20540
20558
  throw err;
20541
- });
20559
+ }
20542
20560
  };
20543
- if (options.keepalive) {
20561
+ if (opts.keepalive) {
20544
20562
  this.keepalive = true;
20545
20563
  const resp = task();
20546
20564
  this.keepalive = false;
@@ -20649,23 +20667,24 @@ class Client {
20649
20667
  const pbChangePack = converter.toChangePack(doc.createChangePack());
20650
20668
  pbChangePack.isRemoved = true;
20651
20669
  return this.enqueueTask(async () => {
20652
- return this.rpcClient.removeDocument(
20653
- {
20654
- clientId: this.id,
20655
- documentId: attachment.docID,
20656
- changePack: pbChangePack
20657
- },
20658
- { headers: { "x-shard-key": `${this.apiKey}/${doc.getKey()}` } }
20659
- ).then((res) => {
20670
+ try {
20671
+ const res = await this.rpcClient.removeDocument(
20672
+ {
20673
+ clientId: this.id,
20674
+ documentId: attachment.docID,
20675
+ changePack: pbChangePack
20676
+ },
20677
+ { headers: { "x-shard-key": `${this.apiKey}/${doc.getKey()}` } }
20678
+ );
20660
20679
  const pack = converter.fromChangePack(res.changePack);
20661
20680
  doc.applyChangePack(pack);
20662
20681
  this.detachInternal(doc.getKey());
20663
20682
  logger.info(`[RD] c:"${this.getKey()}" removes d:"${doc.getKey()}"`);
20664
- }).catch(async (err) => {
20683
+ } catch (err) {
20665
20684
  logger.error(`[RD] c:"${this.getKey()}" err :`, err);
20666
20685
  await this.handleConnectError(err);
20667
20686
  throw err;
20668
- });
20687
+ }
20669
20688
  });
20670
20689
  }
20671
20690
  /**
@@ -20733,19 +20752,20 @@ class Client {
20733
20752
  };
20734
20753
  const doLoop = async () => {
20735
20754
  return this.enqueueTask(async () => {
20736
- return this.rpcClient.broadcast(
20737
- {
20738
- clientId: this.id,
20739
- documentId: attachment.docID,
20740
- topic,
20741
- payload: new TextEncoder().encode(JSON.stringify(payload))
20742
- },
20743
- { headers: { "x-shard-key": `${this.apiKey}/${docKey}` } }
20744
- ).then(() => {
20755
+ try {
20756
+ await this.rpcClient.broadcast(
20757
+ {
20758
+ clientId: this.id,
20759
+ documentId: attachment.docID,
20760
+ topic,
20761
+ payload: new TextEncoder().encode(JSON.stringify(payload))
20762
+ },
20763
+ { headers: { "x-shard-key": `${this.apiKey}/${docKey}` } }
20764
+ );
20745
20765
  logger.info(
20746
20766
  `[BC] c:"${this.getKey()}" broadcasts d:"${docKey}" t:"${topic}"`
20747
20767
  );
20748
- }).catch(async (err) => {
20768
+ } catch (err) {
20749
20769
  logger.error(`[BC] c:"${this.getKey()}" err:`, err);
20750
20770
  if (await this.handleConnectError(err)) {
20751
20771
  if (err instanceof ConnectError && errorCodeOf(err) === Code.ErrUnauthenticated) {
@@ -20774,7 +20794,7 @@ class Client {
20774
20794
  } else {
20775
20795
  throw err;
20776
20796
  }
20777
- });
20797
+ }
20778
20798
  });
20779
20799
  };
20780
20800
  return doLoop();
@@ -20959,19 +20979,20 @@ class Client {
20959
20979
  attachment.unsubscribeBroadcastEvent();
20960
20980
  this.attachmentMap.delete(docKey);
20961
20981
  }
20962
- syncInternal(attachment, syncMode) {
20982
+ async syncInternal(attachment, syncMode) {
20963
20983
  const { doc, docID } = attachment;
20964
20984
  const reqPack = doc.createChangePack();
20965
- return this.rpcClient.pushPullChanges(
20966
- {
20967
- clientId: this.id,
20968
- documentId: docID,
20969
- changePack: converter.toChangePack(reqPack),
20970
- pushOnly: syncMode === "realtime-pushonly"
20971
- /* RealtimePushOnly */
20972
- },
20973
- { headers: { "x-shard-key": `${this.apiKey}/${doc.getKey()}` } }
20974
- ).then((res) => {
20985
+ try {
20986
+ const res = await this.rpcClient.pushPullChanges(
20987
+ {
20988
+ clientId: this.id,
20989
+ documentId: docID,
20990
+ changePack: converter.toChangePack(reqPack),
20991
+ pushOnly: syncMode === "realtime-pushonly"
20992
+ /* RealtimePushOnly */
20993
+ },
20994
+ { headers: { "x-shard-key": `${this.apiKey}/${doc.getKey()}` } }
20995
+ );
20975
20996
  const respPack = converter.fromChangePack(res.changePack);
20976
20997
  if (respPack.hasChanges() && (attachment.syncMode === "realtime-pushonly" || attachment.syncMode === "realtime-syncoff")) {
20977
20998
  return doc;
@@ -20992,7 +21013,7 @@ class Client {
20992
21013
  `[PP] c:"${this.getKey()}" sync d:"${docKey}", push:${reqPack.getChangeSize()} pull:${remoteSize} cp:${respPack.getCheckpoint().toTestString()}`
20993
21014
  );
20994
21015
  return doc;
20995
- }).catch(async (err) => {
21016
+ } catch (err) {
20996
21017
  doc.publish([
20997
21018
  {
20998
21019
  type: DocEventType.SyncStatusChanged,
@@ -21001,7 +21022,7 @@ class Client {
21001
21022
  ]);
21002
21023
  logger.error(`[PP] c:"${this.getKey()}" err :`, err);
21003
21024
  throw err;
21004
- });
21025
+ }
21005
21026
  }
21006
21027
  /**
21007
21028
  * `handleConnectError` handles the given error. If the given error can be
@@ -21124,16 +21145,15 @@ function useYorkieClient(opts) {
21124
21145
  }
21125
21146
  const YorkieProvider = ({
21126
21147
  children,
21127
- ...rest
21148
+ ...opts
21128
21149
  }) => {
21129
- const opts = useMemo(
21130
- () => ({
21131
- apiKey: rest.apiKey,
21132
- rpcAddr: rest.rpcAddr
21133
- }),
21134
- [rest.apiKey, rest.rpcAddr]
21135
- );
21136
- const { client, loading, error } = useYorkieClient(opts);
21150
+ const clientOpts = useMemo(() => {
21151
+ return {
21152
+ userAgent: pkg$1.name + "/" + pkg$1.version,
21153
+ ...opts
21154
+ };
21155
+ }, [opts.apiKey, opts.rpcAddr]);
21156
+ const { client, loading, error } = useYorkieClient(clientOpts);
21137
21157
  return /* @__PURE__ */ jsx(YorkieContext.Provider, { value: { client, loading, error }, children });
21138
21158
  };
21139
21159
  const useYorkie = () => {
@@ -21300,9 +21320,10 @@ function useYorkieDoc(apiKey, docKey, opts) {
21300
21320
  const clientOpts = useMemo(() => {
21301
21321
  return {
21302
21322
  apiKey,
21323
+ userAgent: pkg$1.name + "/" + pkg$1.version,
21303
21324
  ...opts
21304
21325
  };
21305
- }, [apiKey, docKey]);
21326
+ }, [apiKey]);
21306
21327
  const {
21307
21328
  client,
21308
21329
  loading: clientLoading,