@kokimoki/app 1.2.0 → 1.3.1

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.
@@ -6,19 +6,17 @@ export declare class KokimokiAwareness<Data extends S.Generic<unknown>> extends
6
6
  clientId: S.String;
7
7
  lastPing: S.Number;
8
8
  data: Data;
9
- }>>, {
10
- [clientId: string]: Data["defaultValue"];
11
- }> {
9
+ }>>> {
12
10
  readonly dataSchema: Data;
13
- readonly data: Data["defaultValue"];
11
+ private _data;
14
12
  private _pingInterval;
15
13
  private _kmClients;
16
- constructor(roomName: string, dataSchema: Data, data: Data["defaultValue"], mode?: RoomSubscriptionMode, pingTimeout?: number);
14
+ constructor(roomName: string, dataSchema: Data, _data: Data["defaultValue"], mode?: RoomSubscriptionMode, pingTimeout?: number);
17
15
  onJoin(client: KokimokiClient<any>): Promise<void>;
18
16
  onBeforeLeave(client: KokimokiClient<any>): Promise<void>;
19
17
  onLeave(client: KokimokiClient<any>): Promise<void>;
20
- private getConnectedClients;
21
- subscribe(set: (value: {
18
+ getClients(): {
22
19
  [clientId: string]: Data["defaultValue"];
23
- }) => void): () => void;
20
+ };
21
+ setData(data: Data["defaultValue"]): Promise<void>;
24
22
  }
@@ -3,17 +3,17 @@ import { KokimokiSchema as S } from "./kokimoki-schema";
3
3
  import { RoomSubscriptionMode } from "./room-subscription-mode";
4
4
  export class KokimokiAwareness extends KokimokiStore {
5
5
  dataSchema;
6
- data;
6
+ _data;
7
7
  _pingInterval = null;
8
8
  _kmClients = new Set();
9
- constructor(roomName, dataSchema, data, mode = RoomSubscriptionMode.ReadWrite, pingTimeout = 2500) {
9
+ constructor(roomName, dataSchema, _data, mode = RoomSubscriptionMode.ReadWrite, pingTimeout = 3000) {
10
10
  super(`/a/${roomName}`, S.dict(S.struct({
11
11
  clientId: S.string(),
12
12
  lastPing: S.number(),
13
13
  data: dataSchema,
14
14
  })), mode);
15
15
  this.dataSchema = dataSchema;
16
- this.data = data;
16
+ this._data = _data;
17
17
  this._pingInterval = setInterval(async () => {
18
18
  const kmClients = Array.from(this._kmClients);
19
19
  await Promise.all(kmClients.map(async (client) => {
@@ -28,7 +28,7 @@ export class KokimokiAwareness extends KokimokiStore {
28
28
  t.set(this.root[client.connectionId], {
29
29
  clientId: client.id,
30
30
  lastPing: timestamp,
31
- data: this.data,
31
+ data: this._data,
32
32
  });
33
33
  }
34
34
  // Delete clients that haven't pinged in a while
@@ -50,7 +50,7 @@ export class KokimokiAwareness extends KokimokiStore {
50
50
  t.set(this.root[client.connectionId], {
51
51
  clientId: client.id,
52
52
  lastPing: client.serverTimestamp(),
53
- data: this.data,
53
+ data: this._data,
54
54
  });
55
55
  });
56
56
  }
@@ -62,37 +62,20 @@ export class KokimokiAwareness extends KokimokiStore {
62
62
  async onLeave(client) {
63
63
  this._kmClients.delete(client);
64
64
  }
65
- getConnectedClients() {
65
+ getClients() {
66
66
  const clients = {};
67
67
  for (const clientId in this.proxy) {
68
68
  clients[clientId] = this.proxy[clientId].data;
69
69
  }
70
70
  return clients;
71
71
  }
72
- subscribe(set) {
73
- let prevConnectionIds = {};
74
- const handler = () => {
75
- // Do nothing if only pings have changed
76
- let changed = false;
77
- const newConnectionIds = {};
78
- for (const connectionId in this.proxy) {
79
- newConnectionIds[connectionId] = true;
80
- if (!prevConnectionIds[connectionId]) {
81
- changed = true;
82
- }
83
- }
84
- for (const connectionId in prevConnectionIds) {
85
- if (!newConnectionIds[connectionId]) {
86
- changed = true;
87
- }
88
- }
89
- if (changed) {
90
- set(this.getConnectedClients());
91
- prevConnectionIds = newConnectionIds;
92
- }
93
- };
94
- this.doc.on("update", handler);
95
- set(this.getConnectedClients());
96
- return () => this.doc.off("update", handler);
72
+ async setData(data) {
73
+ this._data = data;
74
+ const kmClients = Array.from(this._kmClients);
75
+ await Promise.all(kmClients.map(async (client) => {
76
+ await client.transact((t) => {
77
+ t.set(this.root[client.connectionId].data, this._data);
78
+ });
79
+ }));
97
80
  }
98
81
  }
@@ -141,7 +141,7 @@ export class KokimokiClient extends EventEmitter {
141
141
  this.emit("disconnected");
142
142
  };
143
143
  this.ws.onmessage = (e) => {
144
- console.log(`Received WS message: ${e.data}`);
144
+ // console.log(`Received WS message: ${e.data}`);
145
145
  // Handle JSON messages
146
146
  if (typeof e.data === "string") {
147
147
  const message = JSON.parse(e.data);
@@ -368,21 +368,19 @@ declare class KokimokiAwareness<Data extends KokimokiSchema.Generic<unknown>> ex
368
368
  clientId: KokimokiSchema.String;
369
369
  lastPing: KokimokiSchema.Number;
370
370
  data: Data;
371
- }>>, {
372
- [clientId: string]: Data["defaultValue"];
373
- }> {
371
+ }>>> {
374
372
  readonly dataSchema: Data;
375
- readonly data: Data["defaultValue"];
373
+ private _data;
376
374
  private _pingInterval;
377
375
  private _kmClients;
378
- constructor(roomName: string, dataSchema: Data, data: Data["defaultValue"], mode?: RoomSubscriptionMode, pingTimeout?: number);
376
+ constructor(roomName: string, dataSchema: Data, _data: Data["defaultValue"], mode?: RoomSubscriptionMode, pingTimeout?: number);
379
377
  onJoin(client: KokimokiClient<any>): Promise<void>;
380
378
  onBeforeLeave(client: KokimokiClient<any>): Promise<void>;
381
379
  onLeave(client: KokimokiClient<any>): Promise<void>;
382
- private getConnectedClients;
383
- subscribe(set: (value: {
380
+ getClients(): {
384
381
  [clientId: string]: Data["defaultValue"];
385
- }) => void): () => void;
382
+ };
383
+ setData(data: Data["defaultValue"]): Promise<void>;
386
384
  }
387
385
 
388
386
  export { BooleanField, ConstField, EnumField, Field, type FieldOptions, FloatField, Form, FormArray, FormGroup, ImageField, IntegerField, KokimokiAwareness, KokimokiClient, type KokimokiClientEvents, KokimokiQueue, KokimokiSchema, KokimokiStore, type Paginated, RoomSubscription, RoomSubscriptionMode, TextField, type Upload };
@@ -634,7 +634,7 @@ function eventTargetAgnosticAddListener(emitter, name, listener, flags) {
634
634
  var eventsExports = events.exports;
635
635
  var EventEmitter$1 = /*@__PURE__*/getDefaultExportFromCjs(eventsExports);
636
636
 
637
- const KOKIMOKI_APP_VERSION = "1.2.0";
637
+ const KOKIMOKI_APP_VERSION = "1.3.1";
638
638
 
639
639
  /**
640
640
  * Utility module to work with key-value stores.
@@ -11785,7 +11785,7 @@ class KokimokiClient extends EventEmitter$1 {
11785
11785
  this.emit("disconnected");
11786
11786
  };
11787
11787
  this.ws.onmessage = (e) => {
11788
- console.log(`Received WS message: ${e.data}`);
11788
+ // console.log(`Received WS message: ${e.data}`);
11789
11789
  // Handle JSON messages
11790
11790
  if (typeof e.data === "string") {
11791
11791
  const message = JSON.parse(e.data);
@@ -12494,17 +12494,17 @@ class KokimokiQueue extends KokimokiStore {
12494
12494
 
12495
12495
  class KokimokiAwareness extends KokimokiStore {
12496
12496
  dataSchema;
12497
- data;
12497
+ _data;
12498
12498
  _pingInterval = null;
12499
12499
  _kmClients = new Set();
12500
- constructor(roomName, dataSchema, data, mode = RoomSubscriptionMode.ReadWrite, pingTimeout = 2500) {
12500
+ constructor(roomName, dataSchema, _data, mode = RoomSubscriptionMode.ReadWrite, pingTimeout = 3000) {
12501
12501
  super(`/a/${roomName}`, KokimokiSchema.dict(KokimokiSchema.struct({
12502
12502
  clientId: KokimokiSchema.string(),
12503
12503
  lastPing: KokimokiSchema.number(),
12504
12504
  data: dataSchema,
12505
12505
  })), mode);
12506
12506
  this.dataSchema = dataSchema;
12507
- this.data = data;
12507
+ this._data = _data;
12508
12508
  this._pingInterval = setInterval(async () => {
12509
12509
  const kmClients = Array.from(this._kmClients);
12510
12510
  await Promise.all(kmClients.map(async (client) => {
@@ -12519,7 +12519,7 @@ class KokimokiAwareness extends KokimokiStore {
12519
12519
  t.set(this.root[client.connectionId], {
12520
12520
  clientId: client.id,
12521
12521
  lastPing: timestamp,
12522
- data: this.data,
12522
+ data: this._data,
12523
12523
  });
12524
12524
  }
12525
12525
  // Delete clients that haven't pinged in a while
@@ -12541,7 +12541,7 @@ class KokimokiAwareness extends KokimokiStore {
12541
12541
  t.set(this.root[client.connectionId], {
12542
12542
  clientId: client.id,
12543
12543
  lastPing: client.serverTimestamp(),
12544
- data: this.data,
12544
+ data: this._data,
12545
12545
  });
12546
12546
  });
12547
12547
  }
@@ -12553,38 +12553,21 @@ class KokimokiAwareness extends KokimokiStore {
12553
12553
  async onLeave(client) {
12554
12554
  this._kmClients.delete(client);
12555
12555
  }
12556
- getConnectedClients() {
12556
+ getClients() {
12557
12557
  const clients = {};
12558
12558
  for (const clientId in this.proxy) {
12559
12559
  clients[clientId] = this.proxy[clientId].data;
12560
12560
  }
12561
12561
  return clients;
12562
12562
  }
12563
- subscribe(set) {
12564
- let prevConnectionIds = {};
12565
- const handler = () => {
12566
- // Do nothing if only pings have changed
12567
- let changed = false;
12568
- const newConnectionIds = {};
12569
- for (const connectionId in this.proxy) {
12570
- newConnectionIds[connectionId] = true;
12571
- if (!prevConnectionIds[connectionId]) {
12572
- changed = true;
12573
- }
12574
- }
12575
- for (const connectionId in prevConnectionIds) {
12576
- if (!newConnectionIds[connectionId]) {
12577
- changed = true;
12578
- }
12579
- }
12580
- if (changed) {
12581
- set(this.getConnectedClients());
12582
- prevConnectionIds = newConnectionIds;
12583
- }
12584
- };
12585
- this.doc.on("update", handler);
12586
- set(this.getConnectedClients());
12587
- return () => this.doc.off("update", handler);
12563
+ async setData(data) {
12564
+ this._data = data;
12565
+ const kmClients = Array.from(this._kmClients);
12566
+ await Promise.all(kmClients.map(async (client) => {
12567
+ await client.transact((t) => {
12568
+ t.set(this.root[client.connectionId].data, this._data);
12569
+ });
12570
+ }));
12588
12571
  }
12589
12572
  }
12590
12573