@kokimoki/app 1.2.0 → 1.3.0

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
+ data: Data["defaultValue"];
14
12
  private _pingInterval;
15
13
  private _kmClients;
16
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
  }
@@ -6,7 +6,7 @@ export class KokimokiAwareness extends KokimokiStore {
6
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(),
@@ -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
+ data: Data["defaultValue"];
376
374
  private _pingInterval;
377
375
  private _kmClients;
378
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.0";
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);
@@ -12497,7 +12497,7 @@ class KokimokiAwareness extends KokimokiStore {
12497
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(),
@@ -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