@yorkie-js/sdk 0.6.6 → 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.
package/dist/counter.html CHANGED
@@ -37,7 +37,9 @@
37
37
  async function main() {
38
38
  try {
39
39
  // 01. create client with RPCAddr then activate it.
40
- const client = new yorkie.Client('http://localhost:8080');
40
+ const client = new yorkie.Client({
41
+ rpcAddr: 'http://localhost:8080',
42
+ });
41
43
  await client.activate();
42
44
 
43
45
  // 02. create a document then attach it into the client.
package/dist/drawing.html CHANGED
@@ -38,7 +38,8 @@
38
38
  async function main() {
39
39
  try {
40
40
  // 01. create client with RPCAddr then activate it.
41
- const client = new yorkie.Client('http://localhost:8080', {
41
+ const client = new yorkie.Client({
42
+ rpcAddr: 'http://localhost:8080',
42
43
  syncLoopDuration: 0,
43
44
  reconnectStreamDelay: 1000,
44
45
  });
package/dist/index.html CHANGED
@@ -320,7 +320,9 @@
320
320
  devtool.setCodeMirror(codemirror);
321
321
 
322
322
  // 02-1. create client with RPCAddr.
323
- const client = new yorkie.Client('http://localhost:8080');
323
+ const client = new yorkie.Client({
324
+ rpcAddr: 'http://localhost:8080',
325
+ });
324
326
  // 02-2. activate client
325
327
  await client.activate();
326
328
 
package/dist/multi.html CHANGED
@@ -79,7 +79,9 @@
79
79
  async function main() {
80
80
  try {
81
81
  // 01-1. create client with RPCAddr.
82
- const client = new yorkie.Client('http://localhost:8080');
82
+ const client = new yorkie.Client({
83
+ rpcAddr: 'http://localhost:8080',
84
+ });
83
85
  // 01-2. activate client
84
86
  await client.activate();
85
87
 
@@ -252,7 +252,7 @@
252
252
  * main is the entry point of the example.
253
253
  */
254
254
  async function main() {
255
- const client = new yorkie.Client('http://localhost:8080');
255
+ const client = new yorkie.Client({ rpcAddr: 'http://localhost:8080' });
256
256
  await client.activate();
257
257
 
258
258
  // 01. Build yorkie.Text from ProseMirror doc.
@@ -142,7 +142,9 @@
142
142
  try {
143
143
  async function initializeRealtimeEditor(clientElem) {
144
144
  // 01. create client with RPCAddr(envoy) then activate it.
145
- const client = new yorkie.Client('http://localhost:8080');
145
+ const client = new yorkie.Client({
146
+ rpcAddr: 'http://localhost:8080',
147
+ });
146
148
  await client.activate();
147
149
  const clientID = client.getID().slice(-2);
148
150
  clientElem.querySelector('.client-id').textContent = clientID;
@@ -484,9 +486,6 @@
484
486
 
485
487
  syncText(doc, quill);
486
488
  updateAllCursors();
487
- window.addEventListener('beforeunload', async () => {
488
- await client.deactivate({ keepalive: true });
489
- });
490
489
 
491
490
  return { client, doc, quill };
492
491
  }
package/dist/quill.html CHANGED
@@ -64,7 +64,9 @@
64
64
  async function main() {
65
65
  try {
66
66
  // 01. create client with RPCAddr then activate it.
67
- const client = new yorkie.Client('http://localhost:8080');
67
+ const client = new yorkie.Client({
68
+ rpcAddr: 'http://localhost:8080',
69
+ });
68
70
  await client.activate();
69
71
 
70
72
  // 02. create a document then attach it into the client.
@@ -254,7 +254,9 @@
254
254
  async function main() {
255
255
  try {
256
256
  // 01. create client with RPCAddr then activate it.
257
- const client = new yorkie.Client('http://localhost:8080');
257
+ const client = new yorkie.Client({
258
+ rpcAddr: 'http://localhost:8080',
259
+ });
258
260
  await client.activate();
259
261
  const myClientID = client.getID();
260
262
 
@@ -53,6 +53,26 @@ export declare type ArrayOperationInfo = AddOpInfo | RemoveOpInfo | MoveOpInfo;
53
53
  */
54
54
  declare type ArrayOperationInfo_2 = AddOpInfo_2 | RemoveOpInfo_2 | MoveOpInfo_2;
55
55
 
56
+ /**
57
+ * `AttachOptions` are user-settable options used when attaching documents.
58
+ */
59
+ declare interface AttachOptions<R, P> {
60
+ /**
61
+ * `initialRoot` is the initial root of the document. It is used to
62
+ * initialize the document. It is used when the fields are not set in the
63
+ * document.
64
+ */
65
+ initialRoot?: R;
66
+ /**
67
+ * `initialPresence` is the initial presence of the client.
68
+ */
69
+ initialPresence?: P;
70
+ /**
71
+ * `syncMode` defines the synchronization mode of the document.
72
+ */
73
+ syncMode?: SyncMode;
74
+ }
75
+
56
76
  declare interface AuthErrorEvent extends BaseDocEvent {
57
77
  type: DocEventType_2.AuthError;
58
78
  value: {
@@ -710,7 +730,7 @@ export declare class Client {
710
730
  * @param rpcAddr - the address of the RPC server.
711
731
  * @param opts - the options of the client.
712
732
  */
713
- constructor(rpcAddr: string, opts?: ClientOptions);
733
+ constructor(opts?: ClientOptions);
714
734
  /**
715
735
  * `activate` activates this client. That is, it registers itself to the server
716
736
  * and receives a unique ID from the server. The given ID is used to
@@ -738,11 +758,7 @@ export declare class Client {
738
758
  * `attach` attaches the given document to this client. It tells the server that
739
759
  * this client will synchronize the given document.
740
760
  */
741
- attach<T, P extends Indexable>(doc: Document_2<T, P>, options?: {
742
- initialRoot?: T;
743
- initialPresence?: P;
744
- syncMode?: SyncMode;
745
- }): Promise<Document_2<T, P>>;
761
+ attach<R, P extends Indexable>(doc: Document_2<R, P>, opts?: AttachOptions<R, P>): Promise<Document_2<R, P>>;
746
762
  /**
747
763
  * `detach` detaches the given document from this client. It tells the
748
764
  * server that this client will no longer synchronize the given document.
@@ -751,24 +767,24 @@ export declare class Client {
751
767
  * the changes should be applied to other replicas before GC time. For this,
752
768
  * if the document is no longer used by this client, it should be detached.
753
769
  */
754
- detach<T, P extends Indexable>(doc: Document_2<T, P>, options?: {
770
+ detach<R, P extends Indexable>(doc: Document_2<R, P>, opts?: {
755
771
  removeIfNotAttached?: boolean;
756
772
  keepalive?: boolean;
757
- }): Promise<Document_2<T, P>>;
773
+ }): Promise<Document_2<R, P>>;
758
774
  /**
759
775
  * `changeRealtimeSync` changes the synchronization mode of the given document.
760
776
  */
761
- changeSyncMode<T, P extends Indexable>(doc: Document_2<T, P>, syncMode: SyncMode): Promise<Document_2<T, P>>;
777
+ changeSyncMode<R, P extends Indexable>(doc: Document_2<R, P>, syncMode: SyncMode): Promise<Document_2<R, P>>;
762
778
  /**
763
779
  * `sync` pushes local changes of the attached documents to the server and
764
780
  * receives changes of the remote replica from the server then apply them to
765
781
  * local documents.
766
782
  */
767
- sync<T, P extends Indexable>(doc?: Document_2<T, P>): Promise<Array<Document_2<T, P>>>;
783
+ sync<R, P extends Indexable>(doc?: Document_2<R, P>): Promise<Array<Document_2<R, P>>>;
768
784
  /**
769
785
  * `remove` removes the given document.
770
786
  */
771
- remove<T, P extends Indexable>(doc: Document_2<T, P>): Promise<void>;
787
+ remove<R, P extends Indexable>(doc: Document_2<R, P>): Promise<void>;
772
788
  /**
773
789
  * `getID` returns a ActorID of client.
774
790
  */
@@ -851,6 +867,11 @@ export declare enum ClientCondition {
851
867
  * @public
852
868
  */
853
869
  export declare interface ClientOptions {
870
+ /**
871
+ * `rpcAddr` is the address of the RPC server. It is used to connect to
872
+ * the server.
873
+ */
874
+ rpcAddr?: string;
854
875
  /**
855
876
  * `key` is the client key. It is used to identify the client.
856
877
  * If not set, a random key is generated.
@@ -890,6 +911,11 @@ export declare interface ClientOptions {
890
911
  * default value is `1000`(ms).
891
912
  */
892
913
  reconnectStreamDelay?: number;
914
+ /**
915
+ * `userAgent` is the user agent of the client. It is used to identify the
916
+ * client.
917
+ */
918
+ userAgent?: string;
893
919
  }
894
920
 
895
921
  /**
@@ -2203,7 +2229,7 @@ declare enum DocSyncStatus_2 {
2203
2229
  *
2204
2230
  * @public
2205
2231
  */
2206
- declare class Document_2<T, P extends Indexable = Indexable> {
2232
+ declare class Document_2<R, P extends Indexable = Indexable> {
2207
2233
  private key;
2208
2234
  private status;
2209
2235
  private opts;
@@ -2244,7 +2270,7 @@ declare class Document_2<T, P extends Indexable = Indexable> {
2244
2270
  /**
2245
2271
  * `update` executes the given updater to update this document.
2246
2272
  */
2247
- update(updater: (root: JSONObject<T>, presence: Presence<P>) => void, message?: string): void;
2273
+ update(updater: (root: JSONObject<R>, presence: Presence<P>) => void, message?: string): void;
2248
2274
  /**
2249
2275
  * `subscribe` registers a callback to subscribe to events on the document.
2250
2276
  * The callback will be called when the document is changed.
@@ -2286,7 +2312,7 @@ declare class Document_2<T, P extends Indexable = Indexable> {
2286
2312
  * `subscribe` registers a callback to subscribe to events on the document.
2287
2313
  * The callback will be called when the targetPath or any of its nested values change.
2288
2314
  */
2289
- subscribe<TPath extends PathOf<T>, TOperationInfo extends OperationInfoOf<T, TPath>>(targetPath: TPath, next: NextFn<LocalChangeEvent<TOperationInfo, P> | RemoteChangeEvent<TOperationInfo, P>>, error?: ErrorFn, complete?: CompleteFn): Unsubscribe;
2315
+ subscribe<TPath extends PathOf<R>, TOperationInfo extends OperationInfoOf<R, TPath>>(targetPath: TPath, next: NextFn<LocalChangeEvent<TOperationInfo, P> | RemoteChangeEvent<TOperationInfo, P>>, error?: ErrorFn, complete?: CompleteFn): Unsubscribe;
2290
2316
  /**
2291
2317
  * `subscribe` registers a callback to subscribe to events on the document.
2292
2318
  * The callback will be called when the broadcast event is received from the remote client.
@@ -2345,7 +2371,7 @@ declare class Document_2<T, P extends Indexable = Indexable> {
2345
2371
  /**
2346
2372
  * `getRoot` returns a new proxy of cloned root.
2347
2373
  */
2348
- getRoot(): JSONObject<T>;
2374
+ getRoot(): JSONObject<R>;
2349
2375
  /* Excluded from this release type: garbageCollect */
2350
2376
  /* Excluded from this release type: getRootObject */
2351
2377
  /* Excluded from this release type: getGarbageLen */
@@ -3672,9 +3672,6 @@ function createClient(service, transport) {
3672
3672
  }
3673
3673
  });
3674
3674
  }
3675
- function createPromiseClient(service, transport) {
3676
- return createClient(service, transport);
3677
- }
3678
3675
  function createUnaryFn(transport, service, method) {
3679
3676
  return async function(input, options) {
3680
3677
  var _a2, _b2;
@@ -20215,14 +20212,18 @@ function createAuthInterceptor(apiKey, token) {
20215
20212
  };
20216
20213
  }
20217
20214
  const name = "@yorkie-js/sdk";
20218
- const version = "0.6.6";
20215
+ const version = "0.6.7";
20219
20216
  const pkg = {
20220
20217
  name,
20221
20218
  version
20222
20219
  };
20223
- function createMetricInterceptor() {
20220
+ function createMetricInterceptor(userAgent) {
20224
20221
  return (next) => async (req) => {
20225
- req.header.set("x-yorkie-user-agent", pkg.name + "/" + pkg.version);
20222
+ if (userAgent) {
20223
+ req.header.set("x-yorkie-user-agent", userAgent);
20224
+ } else {
20225
+ req.header.set("x-yorkie-user-agent", pkg.name + "/" + pkg.version);
20226
+ }
20226
20227
  return await next(req);
20227
20228
  };
20228
20229
  }
@@ -20255,6 +20256,7 @@ var ClientCondition = /* @__PURE__ */ ((ClientCondition2) => {
20255
20256
  return ClientCondition2;
20256
20257
  })(ClientCondition || {});
20257
20258
  const DefaultClientOptions = {
20259
+ rpcAddr: "https://api.yorkie.dev",
20258
20260
  syncLoopDuration: 50,
20259
20261
  retrySyncLoopDelay: 1e3,
20260
20262
  reconnectStreamDelay: 1e3
@@ -20269,7 +20271,7 @@ class Client {
20269
20271
  * @param rpcAddr - the address of the RPC server.
20270
20272
  * @param opts - the options of the client.
20271
20273
  */
20272
- constructor(rpcAddr, opts) {
20274
+ constructor(opts) {
20273
20275
  __publicField(this, "id");
20274
20276
  __publicField(this, "key");
20275
20277
  __publicField(this, "metadata");
@@ -20287,7 +20289,8 @@ class Client {
20287
20289
  __publicField(this, "processing", false);
20288
20290
  __publicField(this, "keepalive", false);
20289
20291
  opts = opts || DefaultClientOptions;
20290
- this.key = opts.key ? opts.key : uuid();
20292
+ const rpcAddr = opts.rpcAddr || DefaultClientOptions.rpcAddr;
20293
+ this.key = opts.key || uuid();
20291
20294
  this.metadata = opts.metadata || {};
20292
20295
  this.status = "deactivated";
20293
20296
  this.attachmentMap = /* @__PURE__ */ new Map();
@@ -20308,11 +20311,14 @@ class Client {
20308
20311
  this.retrySyncLoopDelay = opts.retrySyncLoopDelay ?? DefaultClientOptions.retrySyncLoopDelay;
20309
20312
  const { authInterceptor, setToken } = createAuthInterceptor(this.apiKey);
20310
20313
  this.setAuthToken = setToken;
20311
- this.rpcClient = createPromiseClient(
20314
+ this.rpcClient = createClient(
20312
20315
  YorkieService,
20313
20316
  createGrpcWebTransport({
20314
20317
  baseUrl: rpcAddr,
20315
- interceptors: [authInterceptor, createMetricInterceptor()],
20318
+ interceptors: [
20319
+ authInterceptor,
20320
+ createMetricInterceptor(opts == null ? void 0 : opts.userAgent)
20321
+ ],
20316
20322
  fetch: (input, init) => {
20317
20323
  const newInit = {
20318
20324
  ...init,
@@ -20338,22 +20344,28 @@ class Client {
20338
20344
  this.setAuthToken(token);
20339
20345
  }
20340
20346
  return this.enqueueTask(async () => {
20341
- return this.rpcClient.activateClient(
20342
- {
20343
- clientKey: this.key,
20344
- metadata: this.metadata
20345
- },
20346
- { headers: { "x-shard-key": this.apiKey } }
20347
- ).then((res) => {
20347
+ try {
20348
+ const res = await this.rpcClient.activateClient(
20349
+ {
20350
+ clientKey: this.key,
20351
+ metadata: this.metadata
20352
+ },
20353
+ { headers: { "x-shard-key": this.apiKey } }
20354
+ );
20348
20355
  this.id = res.clientId;
20349
20356
  this.status = "activated";
20350
20357
  this.runSyncLoop();
20351
20358
  logger.info(`[AC] c:"${this.getKey()}" activated, id:"${this.id}"`);
20352
- }).catch(async (err) => {
20359
+ if (typeof window !== "undefined") {
20360
+ window.addEventListener("beforeunload", async () => {
20361
+ await this.deactivate({ keepalive: true });
20362
+ });
20363
+ }
20364
+ } catch (err) {
20353
20365
  logger.error(`[AC] c:"${this.getKey()}" err :`, err);
20354
20366
  await this.handleConnectError(err);
20355
20367
  throw err;
20356
- });
20368
+ }
20357
20369
  });
20358
20370
  }
20359
20371
  /**
@@ -20402,7 +20414,7 @@ class Client {
20402
20414
  * `attach` attaches the given document to this client. It tells the server that
20403
20415
  * this client will synchronize the given document.
20404
20416
  */
20405
- attach(doc, options = {}) {
20417
+ attach(doc, opts = {}) {
20406
20418
  if (!this.isActive()) {
20407
20419
  throw new YorkieError(
20408
20420
  Code.ErrClientNotActivated,
@@ -20416,32 +20428,30 @@ class Client {
20416
20428
  );
20417
20429
  }
20418
20430
  doc.setActor(this.id);
20419
- doc.update((_, p) => p.set(options.initialPresence || {}));
20420
- const unsubscribeBroacastEvent = doc.subscribe(
20421
- "local-broadcast",
20422
- async (event) => {
20423
- var _a2;
20424
- const { topic, payload } = event.value;
20425
- const errorFn = (_a2 = event.options) == null ? void 0 : _a2.error;
20426
- const options2 = event.options;
20427
- try {
20428
- await this.broadcast(doc.getKey(), topic, payload, options2);
20429
- } catch (error) {
20430
- if (error instanceof Error) {
20431
- errorFn == null ? void 0 : errorFn(error);
20432
- }
20431
+ doc.update((_, p) => p.set(opts.initialPresence || {}));
20432
+ const unsub = doc.subscribe("local-broadcast", async (event) => {
20433
+ var _a2;
20434
+ const { topic, payload } = event.value;
20435
+ const errorFn = (_a2 = event.options) == null ? void 0 : _a2.error;
20436
+ const options = event.options;
20437
+ try {
20438
+ await this.broadcast(doc.getKey(), topic, payload, options);
20439
+ } catch (error) {
20440
+ if (error instanceof Error) {
20441
+ errorFn == null ? void 0 : errorFn(error);
20433
20442
  }
20434
20443
  }
20435
- );
20436
- const syncMode = options.syncMode ?? "realtime";
20444
+ });
20445
+ const syncMode = opts.syncMode ?? "realtime";
20437
20446
  return this.enqueueTask(async () => {
20438
- return this.rpcClient.attachDocument(
20439
- {
20440
- clientId: this.id,
20441
- changePack: converter.toChangePack(doc.createChangePack())
20442
- },
20443
- { headers: { "x-shard-key": `${this.apiKey}/${doc.getKey()}` } }
20444
- ).then(async (res) => {
20447
+ try {
20448
+ const res = await this.rpcClient.attachDocument(
20449
+ {
20450
+ clientId: this.id,
20451
+ changePack: converter.toChangePack(doc.createChangePack())
20452
+ },
20453
+ { headers: { "x-shard-key": `${this.apiKey}/${doc.getKey()}` } }
20454
+ );
20445
20455
  const pack = converter.fromChangePack(res.changePack);
20446
20456
  doc.applyChangePack(pack);
20447
20457
  if (doc.getStatus() === DocStatus.Removed) {
@@ -20455,7 +20465,7 @@ class Client {
20455
20465
  doc,
20456
20466
  res.documentId,
20457
20467
  syncMode,
20458
- unsubscribeBroacastEvent
20468
+ unsub
20459
20469
  )
20460
20470
  );
20461
20471
  if (syncMode !== "manual") {
@@ -20463,8 +20473,8 @@ class Client {
20463
20473
  }
20464
20474
  logger.info(`[AD] c:"${this.getKey()}" attaches d:"${doc.getKey()}"`);
20465
20475
  const crdtObject = doc.getRootObject();
20466
- if (options.initialRoot) {
20467
- const initialRoot = options.initialRoot;
20476
+ if (opts.initialRoot) {
20477
+ const initialRoot = opts.initialRoot;
20468
20478
  doc.update((root) => {
20469
20479
  for (const [k, v] of Object.entries(initialRoot)) {
20470
20480
  if (!crdtObject.has(k)) {
@@ -20475,11 +20485,11 @@ class Client {
20475
20485
  });
20476
20486
  }
20477
20487
  return doc;
20478
- }).catch(async (err) => {
20488
+ } catch (err) {
20479
20489
  logger.error(`[AD] c:"${this.getKey()}" err :`, err);
20480
20490
  await this.handleConnectError(err);
20481
20491
  throw err;
20482
- });
20492
+ }
20483
20493
  });
20484
20494
  }
20485
20495
  /**
@@ -20490,7 +20500,7 @@ class Client {
20490
20500
  * the changes should be applied to other replicas before GC time. For this,
20491
20501
  * if the document is no longer used by this client, it should be detached.
20492
20502
  */
20493
- detach(doc, options = { keepalive: false }) {
20503
+ detach(doc, opts = { keepalive: false }) {
20494
20504
  if (!this.isActive()) {
20495
20505
  throw new YorkieError(
20496
20506
  Code.ErrClientNotActivated,
@@ -20506,15 +20516,16 @@ class Client {
20506
20516
  }
20507
20517
  doc.update((_, p) => p.clear());
20508
20518
  const task = async () => {
20509
- return await this.rpcClient.detachDocument(
20510
- {
20511
- clientId: this.id,
20512
- documentId: attachment.docID,
20513
- changePack: converter.toChangePack(doc.createChangePack()),
20514
- removeIfNotAttached: options.removeIfNotAttached ?? false
20515
- },
20516
- { headers: { "x-shard-key": `${this.apiKey}/${doc.getKey()}` } }
20517
- ).then((res) => {
20519
+ try {
20520
+ const res = await this.rpcClient.detachDocument(
20521
+ {
20522
+ clientId: this.id,
20523
+ documentId: attachment.docID,
20524
+ changePack: converter.toChangePack(doc.createChangePack()),
20525
+ removeIfNotAttached: opts.removeIfNotAttached ?? false
20526
+ },
20527
+ { headers: { "x-shard-key": `${this.apiKey}/${doc.getKey()}` } }
20528
+ );
20518
20529
  const pack = converter.fromChangePack(res.changePack);
20519
20530
  doc.applyChangePack(pack);
20520
20531
  if (doc.getStatus() !== DocStatus.Removed) {
@@ -20523,13 +20534,13 @@ class Client {
20523
20534
  this.detachInternal(doc.getKey());
20524
20535
  logger.info(`[DD] c:"${this.getKey()}" detaches d:"${doc.getKey()}"`);
20525
20536
  return doc;
20526
- }).catch(async (err) => {
20537
+ } catch (err) {
20527
20538
  logger.error(`[DD] c:"${this.getKey()}" err :`, err);
20528
20539
  await this.handleConnectError(err);
20529
20540
  throw err;
20530
- });
20541
+ }
20531
20542
  };
20532
- if (options.keepalive) {
20543
+ if (opts.keepalive) {
20533
20544
  this.keepalive = true;
20534
20545
  const resp = task();
20535
20546
  this.keepalive = false;
@@ -20638,23 +20649,24 @@ class Client {
20638
20649
  const pbChangePack = converter.toChangePack(doc.createChangePack());
20639
20650
  pbChangePack.isRemoved = true;
20640
20651
  return this.enqueueTask(async () => {
20641
- return this.rpcClient.removeDocument(
20642
- {
20643
- clientId: this.id,
20644
- documentId: attachment.docID,
20645
- changePack: pbChangePack
20646
- },
20647
- { headers: { "x-shard-key": `${this.apiKey}/${doc.getKey()}` } }
20648
- ).then((res) => {
20652
+ try {
20653
+ const res = await this.rpcClient.removeDocument(
20654
+ {
20655
+ clientId: this.id,
20656
+ documentId: attachment.docID,
20657
+ changePack: pbChangePack
20658
+ },
20659
+ { headers: { "x-shard-key": `${this.apiKey}/${doc.getKey()}` } }
20660
+ );
20649
20661
  const pack = converter.fromChangePack(res.changePack);
20650
20662
  doc.applyChangePack(pack);
20651
20663
  this.detachInternal(doc.getKey());
20652
20664
  logger.info(`[RD] c:"${this.getKey()}" removes d:"${doc.getKey()}"`);
20653
- }).catch(async (err) => {
20665
+ } catch (err) {
20654
20666
  logger.error(`[RD] c:"${this.getKey()}" err :`, err);
20655
20667
  await this.handleConnectError(err);
20656
20668
  throw err;
20657
- });
20669
+ }
20658
20670
  });
20659
20671
  }
20660
20672
  /**
@@ -20722,19 +20734,20 @@ class Client {
20722
20734
  };
20723
20735
  const doLoop = async () => {
20724
20736
  return this.enqueueTask(async () => {
20725
- return this.rpcClient.broadcast(
20726
- {
20727
- clientId: this.id,
20728
- documentId: attachment.docID,
20729
- topic,
20730
- payload: new TextEncoder().encode(JSON.stringify(payload))
20731
- },
20732
- { headers: { "x-shard-key": `${this.apiKey}/${docKey}` } }
20733
- ).then(() => {
20737
+ try {
20738
+ await this.rpcClient.broadcast(
20739
+ {
20740
+ clientId: this.id,
20741
+ documentId: attachment.docID,
20742
+ topic,
20743
+ payload: new TextEncoder().encode(JSON.stringify(payload))
20744
+ },
20745
+ { headers: { "x-shard-key": `${this.apiKey}/${docKey}` } }
20746
+ );
20734
20747
  logger.info(
20735
20748
  `[BC] c:"${this.getKey()}" broadcasts d:"${docKey}" t:"${topic}"`
20736
20749
  );
20737
- }).catch(async (err) => {
20750
+ } catch (err) {
20738
20751
  logger.error(`[BC] c:"${this.getKey()}" err:`, err);
20739
20752
  if (await this.handleConnectError(err)) {
20740
20753
  if (err instanceof ConnectError && errorCodeOf(err) === Code.ErrUnauthenticated) {
@@ -20763,7 +20776,7 @@ class Client {
20763
20776
  } else {
20764
20777
  throw err;
20765
20778
  }
20766
- });
20779
+ }
20767
20780
  });
20768
20781
  };
20769
20782
  return doLoop();
@@ -20948,19 +20961,20 @@ class Client {
20948
20961
  attachment.unsubscribeBroadcastEvent();
20949
20962
  this.attachmentMap.delete(docKey);
20950
20963
  }
20951
- syncInternal(attachment, syncMode) {
20964
+ async syncInternal(attachment, syncMode) {
20952
20965
  const { doc, docID } = attachment;
20953
20966
  const reqPack = doc.createChangePack();
20954
- return this.rpcClient.pushPullChanges(
20955
- {
20956
- clientId: this.id,
20957
- documentId: docID,
20958
- changePack: converter.toChangePack(reqPack),
20959
- pushOnly: syncMode === "realtime-pushonly"
20960
- /* RealtimePushOnly */
20961
- },
20962
- { headers: { "x-shard-key": `${this.apiKey}/${doc.getKey()}` } }
20963
- ).then((res) => {
20967
+ try {
20968
+ const res = await this.rpcClient.pushPullChanges(
20969
+ {
20970
+ clientId: this.id,
20971
+ documentId: docID,
20972
+ changePack: converter.toChangePack(reqPack),
20973
+ pushOnly: syncMode === "realtime-pushonly"
20974
+ /* RealtimePushOnly */
20975
+ },
20976
+ { headers: { "x-shard-key": `${this.apiKey}/${doc.getKey()}` } }
20977
+ );
20964
20978
  const respPack = converter.fromChangePack(res.changePack);
20965
20979
  if (respPack.hasChanges() && (attachment.syncMode === "realtime-pushonly" || attachment.syncMode === "realtime-syncoff")) {
20966
20980
  return doc;
@@ -20981,7 +20995,7 @@ class Client {
20981
20995
  `[PP] c:"${this.getKey()}" sync d:"${docKey}", push:${reqPack.getChangeSize()} pull:${remoteSize} cp:${respPack.getCheckpoint().toTestString()}`
20982
20996
  );
20983
20997
  return doc;
20984
- }).catch(async (err) => {
20998
+ } catch (err) {
20985
20999
  doc.publish([
20986
21000
  {
20987
21001
  type: DocEventType.SyncStatusChanged,
@@ -20990,7 +21004,7 @@ class Client {
20990
21004
  ]);
20991
21005
  logger.error(`[PP] c:"${this.getKey()}" err :`, err);
20992
21006
  throw err;
20993
- });
21007
+ }
20994
21008
  }
20995
21009
  /**
20996
21010
  * `handleConnectError` handles the given error. If the given error can be