@kokimoki/app 1.15.2 → 1.16.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.
@@ -1,22 +1,20 @@
1
1
  import { KokimokiStore } from "./kokimoki-store";
2
2
  import { RoomSubscriptionMode } from "./room-subscription-mode";
3
3
  import type { KokimokiClient } from "./kokimoki-client";
4
+ import type { Snapshot } from "valtio/vanilla";
4
5
  export declare class KokimokiAwareness<DataT> extends KokimokiStore<{
5
6
  [connectionId: string]: {
6
7
  clientId: string;
7
- lastPing: number;
8
8
  data: DataT;
9
9
  };
10
10
  }> {
11
11
  private _data;
12
- private _pingInterval;
13
12
  private _kmClients;
14
- constructor(roomName: string, _data: DataT, mode?: RoomSubscriptionMode, pingTimeout?: number);
13
+ constructor(roomName: string, _data: DataT, mode?: RoomSubscriptionMode);
15
14
  onJoin(client: KokimokiClient<any>): Promise<void>;
16
- onBeforeLeave(client: KokimokiClient<any>): Promise<void>;
17
15
  onLeave(client: KokimokiClient<any>): Promise<void>;
18
16
  getClients(): {
19
- [clientId: string]: DataT;
17
+ [clientId: string]: Snapshot<DataT>;
20
18
  };
21
19
  setData(data: DataT): Promise<void>;
22
20
  }
@@ -2,64 +2,29 @@ import { KokimokiStore } from "./kokimoki-store";
2
2
  import { RoomSubscriptionMode } from "./room-subscription-mode";
3
3
  export class KokimokiAwareness extends KokimokiStore {
4
4
  _data;
5
- _pingInterval = null;
6
5
  _kmClients = new Set();
7
- constructor(roomName, _data, mode = RoomSubscriptionMode.ReadWrite, pingTimeout = 3000) {
6
+ constructor(roomName, _data, mode = RoomSubscriptionMode.ReadWrite) {
8
7
  super(`/a/${roomName}`, {}, mode);
9
8
  this._data = _data;
10
- this._pingInterval = setInterval(async () => {
11
- const kmClients = Array.from(this._kmClients);
12
- await Promise.all(kmClients.map(async (client) => {
13
- try {
14
- await client.transact([this], ([state]) => {
15
- const timestamp = client.serverTimestamp();
16
- // Update self
17
- if (state[client.connectionId]) {
18
- state[client.connectionId].lastPing = timestamp;
19
- }
20
- else {
21
- state[client.connectionId] = {
22
- clientId: client.id,
23
- lastPing: timestamp,
24
- data: this._data,
25
- };
26
- }
27
- // Delete clients that haven't pinged in a while
28
- for (const connectionId in state) {
29
- const { lastPing } = state[connectionId];
30
- if (!lastPing || timestamp - lastPing > pingTimeout * 2) {
31
- delete state[connectionId];
32
- }
33
- }
34
- });
35
- }
36
- catch (e) { }
37
- }));
38
- }, pingTimeout);
39
9
  }
40
10
  async onJoin(client) {
41
11
  this._kmClients.add(client);
42
12
  await client.transact([this], ([state]) => {
43
13
  state[client.connectionId] = {
44
14
  clientId: client.id,
45
- lastPing: client.serverTimestamp(),
46
15
  data: this._data,
47
16
  };
48
17
  });
49
18
  }
50
- async onBeforeLeave(client) {
51
- await client.transact([this], ([state]) => {
52
- delete state[client.connectionId];
53
- });
54
- }
55
19
  async onLeave(client) {
56
20
  this._kmClients.delete(client);
57
21
  }
58
22
  getClients() {
59
23
  const clients = {};
60
- for (const connectionId in this.proxy) {
61
- clients[this.proxy[connectionId].clientId] =
62
- this.proxy[connectionId].data;
24
+ const connections = this.get();
25
+ for (const connectionId in connections) {
26
+ clients[connections[connectionId].clientId] =
27
+ connections[connectionId].data;
63
28
  }
64
29
  return clients;
65
30
  }
@@ -5,7 +5,10 @@ import type { Paginated } from "./types/common";
5
5
  import { KokimokiStore } from "./kokimoki-store";
6
6
  import { KokimokiAwareness } from "./kokimoki-awareness";
7
7
  import { KokimokiLocalStore } from "./kokimoki-local-store";
8
- type StoreValue<S> = S extends KokimokiStore<infer U> ? U : never;
8
+ type Mutable<T> = {
9
+ -readonly [K in keyof T]: T[K] extends object ? Mutable<T[K]> : T[K];
10
+ };
11
+ type StoreValue<S> = S extends KokimokiStore<infer U> ? Mutable<U> : never;
9
12
  declare const KokimokiClient_base: new () => TypedEmitter<KokimokiClientEvents>;
10
13
  export declare class KokimokiClient<ClientContextT = any> extends KokimokiClient_base {
11
14
  readonly host: string;
@@ -71,16 +74,16 @@ export declare class KokimokiClient<ClientContextT = any> extends KokimokiClient
71
74
  exposeScriptingContext(context: any): Promise<void>;
72
75
  private sendSubscribeReq;
73
76
  private sendUnsubscribeReq;
74
- join<T>(store: KokimokiStore<T>): Promise<void>;
75
- leave<T>(store: KokimokiStore<T>): Promise<void>;
77
+ join<T extends object>(store: KokimokiStore<T>): Promise<void>;
78
+ leave<T extends object>(store: KokimokiStore<T>): Promise<void>;
76
79
  transact<TStores extends readonly KokimokiStore<any>[], ReturnT = void>(stores: [...TStores], handler: (proxies: {
77
80
  [K in keyof TStores]: StoreValue<TStores[K]>;
78
81
  }) => ReturnT | Promise<ReturnT>): Promise<ReturnT>;
79
82
  close(): Promise<void>;
80
- getRoomHash<T>(store: KokimokiStore<T>): number;
83
+ getRoomHash<T extends object>(store: KokimokiStore<T>): number;
81
84
  /** Initializers */
82
- store<T>(name: string, defaultState: T, autoJoin?: boolean): KokimokiStore<T>;
83
- localStore<T>(name: string, defaultState: T): KokimokiLocalStore<T>;
85
+ store<T extends object>(name: string, defaultState: T, autoJoin?: boolean): KokimokiStore<T>;
86
+ localStore<T extends object>(name: string, defaultState: T): KokimokiLocalStore<T>;
84
87
  awareness<T>(name: string, initialData: T, autoJoin?: boolean): KokimokiAwareness<T>;
85
88
  /**
86
89
  * Add a new entry to a leaderboard
@@ -718,7 +718,12 @@ export class KokimokiClient extends EventEmitter {
718
718
  const res = await fetch(`${this._apiUrl}/ai/chat`, {
719
719
  method: "POST",
720
720
  headers: this.apiHeaders,
721
- body: JSON.stringify({ systemPrompt, userPrompt, temperature, maxTokens }),
721
+ body: JSON.stringify({
722
+ systemPrompt,
723
+ userPrompt,
724
+ temperature,
725
+ maxTokens,
726
+ }),
722
727
  });
723
728
  if (!res.ok) {
724
729
  throw await res.json();
@@ -1,5 +1,5 @@
1
1
  import { KokimokiStore } from "./kokimoki-store";
2
- export declare class KokimokiLocalStore<T> extends KokimokiStore<T> {
2
+ export declare class KokimokiLocalStore<T extends object> extends KokimokiStore<T> {
3
3
  private readonly localRoomName;
4
4
  private _stateKey?;
5
5
  private get stateKey();
@@ -1,16 +1,28 @@
1
1
  import * as Y from "yjs";
2
+ import { Snapshot } from "valtio/vanilla";
2
3
  import { RoomSubscriptionMode } from "./room-subscription-mode";
3
4
  import type { KokimokiClient } from "./kokimoki-client";
4
- export declare class KokimokiStore<T> {
5
+ export declare class KokimokiStore<T extends object> {
5
6
  readonly roomName: string;
6
7
  readonly defaultValue: T;
7
8
  readonly mode: RoomSubscriptionMode;
8
9
  readonly doc: Y.Doc;
9
- readonly proxy: T;
10
+ readonly proxy: T & {
11
+ _km_connections?: {
12
+ [connectionId: string]: string;
13
+ };
14
+ };
10
15
  readonly docRoot: Y.Map<unknown>;
16
+ readonly connections: {
17
+ clientIds: Set<string>;
18
+ };
11
19
  constructor(roomName: string, defaultValue: T, mode?: RoomSubscriptionMode);
12
- get(): T;
13
- subscribe(set: (value: T) => void): () => void;
20
+ get(): Snapshot<T & {
21
+ _km_connections?: {
22
+ [connectionId: string]: string;
23
+ } | undefined;
24
+ }>;
25
+ subscribe(set: (value: Snapshot<T>) => void): () => void;
14
26
  onJoin(client: KokimokiClient): Promise<void>;
15
27
  onBeforeLeave(client: KokimokiClient): Promise<void>;
16
28
  onLeave(client: KokimokiClient): Promise<void>;
@@ -1,5 +1,5 @@
1
1
  import * as Y from "yjs";
2
- import { proxy as yjsProxy } from "valtio/vanilla";
2
+ import { proxy, snapshot, subscribe } from "valtio/vanilla";
3
3
  import { bind as yjsBind } from "valtio-yjs";
4
4
  import { RoomSubscriptionMode } from "./room-subscription-mode";
5
5
  export class KokimokiStore {
@@ -9,6 +9,7 @@ export class KokimokiStore {
9
9
  doc;
10
10
  proxy;
11
11
  docRoot;
12
+ connections;
12
13
  constructor(roomName, defaultValue, mode = RoomSubscriptionMode.ReadWrite) {
13
14
  this.roomName = roomName;
14
15
  this.defaultValue = defaultValue;
@@ -17,38 +18,29 @@ export class KokimokiStore {
17
18
  this.doc = new Y.Doc();
18
19
  this.docRoot = this.doc.getMap("root");
19
20
  // Construct proxy object
20
- this.proxy = yjsProxy(); //T["defaultValue"];
21
+ this.proxy = proxy();
21
22
  // @ts-ignore
22
23
  yjsBind(this.proxy, this.docRoot);
23
- // // Create root proxy
24
- // const store = this;
25
- // // @ts-ignore
26
- // this.root = new DeepProxy(this.proxy, {
27
- // get() {
28
- // return this.nest(function () {});
29
- // },
30
- // apply() {
31
- // let obj: any = store.proxy;
32
- // for (let i = 0; i < this.path.length - 1; i++) {
33
- // obj = obj[this.path[i]];
34
- // }
35
- // return {
36
- // roomName: store.roomName,
37
- // doc: store.doc,
38
- // path: this.path,
39
- // obj,
40
- // key: this.path[this.path.length - 1],
41
- // };
42
- // },
43
- // });
24
+ // Construct connections proxy
25
+ this.connections = proxy({ clientIds: new Set() });
26
+ // Update connections.clientIds whenever _km_connections changes
27
+ subscribe(this.proxy, () => {
28
+ const kmConnections = this.proxy._km_connections;
29
+ if (kmConnections) {
30
+ this.connections.clientIds = new Set(Object.values(kmConnections));
31
+ }
32
+ else {
33
+ this.connections.clientIds = new Set();
34
+ }
35
+ });
44
36
  }
45
37
  get() {
46
- return this.proxy;
38
+ return snapshot(this.proxy);
47
39
  }
48
40
  subscribe(set) {
49
- const handler = () => set(this.proxy);
41
+ const handler = () => set(this.get());
50
42
  this.doc.on("update", handler);
51
- set(this.proxy);
43
+ set(this.get());
52
44
  return () => this.doc.off("update", handler);
53
45
  }
54
46
  async onJoin(client) { }
@@ -1,5 +1,5 @@
1
1
  import { KokimokiStore } from "./kokimoki-store";
2
- export declare class KokimokiTransaction<T extends unknown[]> {
2
+ export declare class KokimokiTransaction<T extends object[]> {
3
3
  private _updates;
4
4
  private _consumeMessagesInRooms;
5
5
  private _proxies;
@@ -1,5 +1,6 @@
1
1
  import TypedEmitter from 'typed-emitter';
2
2
  import * as Y from 'yjs';
3
+ import { Snapshot } from 'valtio/vanilla';
3
4
 
4
5
  interface Paginated<T> {
5
6
  items: T[];
@@ -30,16 +31,27 @@ declare enum RoomSubscriptionMode {
30
31
  ReadWrite = "b"
31
32
  }
32
33
 
33
- declare class KokimokiStore<T> {
34
+ declare class KokimokiStore<T extends object> {
34
35
  readonly roomName: string;
35
36
  readonly defaultValue: T;
36
37
  readonly mode: RoomSubscriptionMode;
37
38
  readonly doc: Y.Doc;
38
- readonly proxy: T;
39
+ readonly proxy: T & {
40
+ _km_connections?: {
41
+ [connectionId: string]: string;
42
+ };
43
+ };
39
44
  readonly docRoot: Y.Map<unknown>;
45
+ readonly connections: {
46
+ clientIds: Set<string>;
47
+ };
40
48
  constructor(roomName: string, defaultValue: T, mode?: RoomSubscriptionMode);
41
- get(): T;
42
- subscribe(set: (value: T) => void): () => void;
49
+ get(): Snapshot<T & {
50
+ _km_connections?: {
51
+ [connectionId: string]: string;
52
+ } | undefined;
53
+ }>;
54
+ subscribe(set: (value: Snapshot<T>) => void): () => void;
43
55
  onJoin(client: KokimokiClient): Promise<void>;
44
56
  onBeforeLeave(client: KokimokiClient): Promise<void>;
45
57
  onLeave(client: KokimokiClient): Promise<void>;
@@ -48,24 +60,21 @@ declare class KokimokiStore<T> {
48
60
  declare class KokimokiAwareness<DataT> extends KokimokiStore<{
49
61
  [connectionId: string]: {
50
62
  clientId: string;
51
- lastPing: number;
52
63
  data: DataT;
53
64
  };
54
65
  }> {
55
66
  private _data;
56
- private _pingInterval;
57
67
  private _kmClients;
58
- constructor(roomName: string, _data: DataT, mode?: RoomSubscriptionMode, pingTimeout?: number);
68
+ constructor(roomName: string, _data: DataT, mode?: RoomSubscriptionMode);
59
69
  onJoin(client: KokimokiClient<any>): Promise<void>;
60
- onBeforeLeave(client: KokimokiClient<any>): Promise<void>;
61
70
  onLeave(client: KokimokiClient<any>): Promise<void>;
62
71
  getClients(): {
63
- [clientId: string]: DataT;
72
+ [clientId: string]: Snapshot<DataT>;
64
73
  };
65
74
  setData(data: DataT): Promise<void>;
66
75
  }
67
76
 
68
- declare class KokimokiLocalStore<T> extends KokimokiStore<T> {
77
+ declare class KokimokiLocalStore<T extends object> extends KokimokiStore<T> {
69
78
  private readonly localRoomName;
70
79
  private _stateKey?;
71
80
  private get stateKey();
@@ -76,7 +85,10 @@ declare class KokimokiLocalStore<T> extends KokimokiStore<T> {
76
85
  };
77
86
  }
78
87
 
79
- type StoreValue<S> = S extends KokimokiStore<infer U> ? U : never;
88
+ type Mutable<T> = {
89
+ -readonly [K in keyof T]: T[K] extends object ? Mutable<T[K]> : T[K];
90
+ };
91
+ type StoreValue<S> = S extends KokimokiStore<infer U> ? Mutable<U> : never;
80
92
  declare const KokimokiClient_base: new () => TypedEmitter<KokimokiClientEvents>;
81
93
  declare class KokimokiClient<ClientContextT = any> extends KokimokiClient_base {
82
94
  readonly host: string;
@@ -142,16 +154,16 @@ declare class KokimokiClient<ClientContextT = any> extends KokimokiClient_base {
142
154
  exposeScriptingContext(context: any): Promise<void>;
143
155
  private sendSubscribeReq;
144
156
  private sendUnsubscribeReq;
145
- join<T>(store: KokimokiStore<T>): Promise<void>;
146
- leave<T>(store: KokimokiStore<T>): Promise<void>;
157
+ join<T extends object>(store: KokimokiStore<T>): Promise<void>;
158
+ leave<T extends object>(store: KokimokiStore<T>): Promise<void>;
147
159
  transact<TStores extends readonly KokimokiStore<any>[], ReturnT = void>(stores: [...TStores], handler: (proxies: {
148
160
  [K in keyof TStores]: StoreValue<TStores[K]>;
149
161
  }) => ReturnT | Promise<ReturnT>): Promise<ReturnT>;
150
162
  close(): Promise<void>;
151
- getRoomHash<T>(store: KokimokiStore<T>): number;
163
+ getRoomHash<T extends object>(store: KokimokiStore<T>): number;
152
164
  /** Initializers */
153
- store<T>(name: string, defaultState: T, autoJoin?: boolean): KokimokiStore<T>;
154
- localStore<T>(name: string, defaultState: T): KokimokiLocalStore<T>;
165
+ store<T extends object>(name: string, defaultState: T, autoJoin?: boolean): KokimokiStore<T>;
166
+ localStore<T extends object>(name: string, defaultState: T): KokimokiLocalStore<T>;
155
167
  awareness<T>(name: string, initialData: T, autoJoin?: boolean): KokimokiAwareness<T>;
156
168
  /**
157
169
  * Add a new entry to a leaderboard
@@ -11200,9 +11200,76 @@ const getUntracked = (obj) => {
11200
11200
  }
11201
11201
  return null;
11202
11202
  };
11203
+ /**
11204
+ * Mark object to be tracked.
11205
+ *
11206
+ * This function marks an object that will be passed into `createProxy`
11207
+ * as marked to track or not. By default only Array and Object are marked to track,
11208
+ * so this is useful for example to mark a class instance to track or to mark a object
11209
+ * to be untracked when creating your proxy.
11210
+ *
11211
+ * @param obj - Object to mark as tracked or not.
11212
+ * @param mark - Boolean indicating whether you want to track this object or not.
11213
+ * @returns - No return.
11214
+ *
11215
+ * @example
11216
+ * import { createProxy, markToTrack, isChanged } from 'proxy-compare';
11217
+ *
11218
+ * const nested = { e: "3" }
11219
+ *
11220
+ * markToTrack(nested, false)
11221
+ *
11222
+ * const original = { a: "1", c: "2", d: nested };
11223
+ * const affected = new WeakMap();
11224
+ *
11225
+ * const proxy = createProxy(original, affected);
11226
+ *
11227
+ * proxy.d.e
11228
+ *
11229
+ * isChanged(original, { d: { e: "3" } }, affected) // true
11230
+ */
11231
+ const markToTrack = (obj, mark = true) => {
11232
+ objectsToTrack.set(obj, mark);
11233
+ };
11203
11234
 
11204
11235
  const isObject = (x) => typeof x === "object" && x !== null;
11205
11236
  const canProxyDefault = (x) => isObject(x) && !refSet.has(x) && (Array.isArray(x) || !(Symbol.iterator in x)) && !(x instanceof WeakMap) && !(x instanceof WeakSet) && !(x instanceof Error) && !(x instanceof Number) && !(x instanceof Date) && !(x instanceof String) && !(x instanceof RegExp) && !(x instanceof ArrayBuffer) && !(x instanceof Promise);
11237
+ const createSnapshotDefault = (target, version) => {
11238
+ const cache = snapCache.get(target);
11239
+ if ((cache == null ? void 0 : cache[0]) === version) {
11240
+ return cache[1];
11241
+ }
11242
+ const snap = Array.isArray(target) ? [] : Object.create(Object.getPrototypeOf(target));
11243
+ markToTrack(snap, true);
11244
+ snapCache.set(target, [version, snap]);
11245
+ Reflect.ownKeys(target).forEach((key) => {
11246
+ if (Object.getOwnPropertyDescriptor(snap, key)) {
11247
+ return;
11248
+ }
11249
+ const value = Reflect.get(target, key);
11250
+ const { enumerable } = Reflect.getOwnPropertyDescriptor(
11251
+ target,
11252
+ key
11253
+ );
11254
+ const desc = {
11255
+ value,
11256
+ enumerable,
11257
+ // This is intentional to avoid copying with proxy-compare.
11258
+ // It's still non-writable, so it avoids assigning a value.
11259
+ configurable: true
11260
+ };
11261
+ if (refSet.has(value)) {
11262
+ markToTrack(value, false);
11263
+ } else if (proxyStateMap.has(value)) {
11264
+ const [target2, ensureVersion] = proxyStateMap.get(
11265
+ value
11266
+ );
11267
+ desc.value = createSnapshotDefault(target2, ensureVersion());
11268
+ }
11269
+ Object.defineProperty(snap, key, desc);
11270
+ });
11271
+ return Object.preventExtensions(snap);
11272
+ };
11206
11273
  const createHandlerDefault = (isInitializing, addPropListener, removePropListener, notifyUpdate) => ({
11207
11274
  deleteProperty(target, prop) {
11208
11275
  const prevValue = Reflect.get(target, prop);
@@ -11232,11 +11299,13 @@ const createHandlerDefault = (isInitializing, addPropListener, removePropListene
11232
11299
  });
11233
11300
  const proxyStateMap = /* @__PURE__ */ new WeakMap();
11234
11301
  const refSet = /* @__PURE__ */ new WeakSet();
11302
+ const snapCache = /* @__PURE__ */ new WeakMap();
11235
11303
  const versionHolder = [1, 1];
11236
11304
  const proxyCache = /* @__PURE__ */ new WeakMap();
11237
11305
  let objectIs = Object.is;
11238
11306
  let newProxy = (target, handler) => new Proxy(target, handler);
11239
11307
  let canProxy = canProxyDefault;
11308
+ let createSnapshot = createSnapshotDefault;
11240
11309
  let createHandler = createHandlerDefault;
11241
11310
  function proxy(baseObject = {}) {
11242
11311
  if (!isObject(baseObject)) {
@@ -11377,6 +11446,14 @@ function subscribe(proxyObject, callback, notifyInSync) {
11377
11446
  removeListener();
11378
11447
  };
11379
11448
  }
11449
+ function snapshot(proxyObject) {
11450
+ const proxyState = proxyStateMap.get(proxyObject);
11451
+ if ((import.meta.env ? import.meta.env.MODE : void 0) !== "production" && !proxyState) {
11452
+ console.warn("Please use proxy object");
11453
+ }
11454
+ const [target, ensureVersion] = proxyState;
11455
+ return createSnapshot(target, ensureVersion());
11456
+ }
11380
11457
 
11381
11458
  // do not edit .js files directly - edit src/index.jst
11382
11459
 
@@ -11927,6 +12004,7 @@ class KokimokiStore {
11927
12004
  doc;
11928
12005
  proxy;
11929
12006
  docRoot;
12007
+ connections;
11930
12008
  constructor(roomName, defaultValue, mode = RoomSubscriptionMode.ReadWrite) {
11931
12009
  this.roomName = roomName;
11932
12010
  this.defaultValue = defaultValue;
@@ -11935,38 +12013,29 @@ class KokimokiStore {
11935
12013
  this.doc = new Doc();
11936
12014
  this.docRoot = this.doc.getMap("root");
11937
12015
  // Construct proxy object
11938
- this.proxy = proxy(); //T["defaultValue"];
12016
+ this.proxy = proxy();
11939
12017
  // @ts-ignore
11940
12018
  bind$1(this.proxy, this.docRoot);
11941
- // // Create root proxy
11942
- // const store = this;
11943
- // // @ts-ignore
11944
- // this.root = new DeepProxy(this.proxy, {
11945
- // get() {
11946
- // return this.nest(function () {});
11947
- // },
11948
- // apply() {
11949
- // let obj: any = store.proxy;
11950
- // for (let i = 0; i < this.path.length - 1; i++) {
11951
- // obj = obj[this.path[i]];
11952
- // }
11953
- // return {
11954
- // roomName: store.roomName,
11955
- // doc: store.doc,
11956
- // path: this.path,
11957
- // obj,
11958
- // key: this.path[this.path.length - 1],
11959
- // };
11960
- // },
11961
- // });
12019
+ // Construct connections proxy
12020
+ this.connections = proxy({ clientIds: new Set() });
12021
+ // Update connections.clientIds whenever _km_connections changes
12022
+ subscribe(this.proxy, () => {
12023
+ const kmConnections = this.proxy._km_connections;
12024
+ if (kmConnections) {
12025
+ this.connections.clientIds = new Set(Object.values(kmConnections));
12026
+ }
12027
+ else {
12028
+ this.connections.clientIds = new Set();
12029
+ }
12030
+ });
11962
12031
  }
11963
12032
  get() {
11964
- return this.proxy;
12033
+ return snapshot(this.proxy);
11965
12034
  }
11966
12035
  subscribe(set) {
11967
- const handler = () => set(this.proxy);
12036
+ const handler = () => set(this.get());
11968
12037
  this.doc.on("update", handler);
11969
- set(this.proxy);
12038
+ set(this.get());
11970
12039
  return () => this.doc.off("update", handler);
11971
12040
  }
11972
12041
  async onJoin(client) { }
@@ -12120,64 +12189,29 @@ class RoomSubscription {
12120
12189
 
12121
12190
  class KokimokiAwareness extends KokimokiStore {
12122
12191
  _data;
12123
- _pingInterval = null;
12124
12192
  _kmClients = new Set();
12125
- constructor(roomName, _data, mode = RoomSubscriptionMode.ReadWrite, pingTimeout = 3000) {
12193
+ constructor(roomName, _data, mode = RoomSubscriptionMode.ReadWrite) {
12126
12194
  super(`/a/${roomName}`, {}, mode);
12127
12195
  this._data = _data;
12128
- this._pingInterval = setInterval(async () => {
12129
- const kmClients = Array.from(this._kmClients);
12130
- await Promise.all(kmClients.map(async (client) => {
12131
- try {
12132
- await client.transact([this], ([state]) => {
12133
- const timestamp = client.serverTimestamp();
12134
- // Update self
12135
- if (state[client.connectionId]) {
12136
- state[client.connectionId].lastPing = timestamp;
12137
- }
12138
- else {
12139
- state[client.connectionId] = {
12140
- clientId: client.id,
12141
- lastPing: timestamp,
12142
- data: this._data,
12143
- };
12144
- }
12145
- // Delete clients that haven't pinged in a while
12146
- for (const connectionId in state) {
12147
- const { lastPing } = state[connectionId];
12148
- if (!lastPing || timestamp - lastPing > pingTimeout * 2) {
12149
- delete state[connectionId];
12150
- }
12151
- }
12152
- });
12153
- }
12154
- catch (e) { }
12155
- }));
12156
- }, pingTimeout);
12157
12196
  }
12158
12197
  async onJoin(client) {
12159
12198
  this._kmClients.add(client);
12160
12199
  await client.transact([this], ([state]) => {
12161
12200
  state[client.connectionId] = {
12162
12201
  clientId: client.id,
12163
- lastPing: client.serverTimestamp(),
12164
12202
  data: this._data,
12165
12203
  };
12166
12204
  });
12167
12205
  }
12168
- async onBeforeLeave(client) {
12169
- await client.transact([this], ([state]) => {
12170
- delete state[client.connectionId];
12171
- });
12172
- }
12173
12206
  async onLeave(client) {
12174
12207
  this._kmClients.delete(client);
12175
12208
  }
12176
12209
  getClients() {
12177
12210
  const clients = {};
12178
- for (const connectionId in this.proxy) {
12179
- clients[this.proxy[connectionId].clientId] =
12180
- this.proxy[connectionId].data;
12211
+ const connections = this.get();
12212
+ for (const connectionId in connections) {
12213
+ clients[connections[connectionId].clientId] =
12214
+ connections[connectionId].data;
12181
12215
  }
12182
12216
  return clients;
12183
12217
  }
@@ -15465,7 +15499,12 @@ class KokimokiClient extends EventEmitter$1 {
15465
15499
  const res = await fetch(`${this._apiUrl}/ai/chat`, {
15466
15500
  method: "POST",
15467
15501
  headers: this.apiHeaders,
15468
- body: JSON.stringify({ systemPrompt, userPrompt, temperature, maxTokens }),
15502
+ body: JSON.stringify({
15503
+ systemPrompt,
15504
+ userPrompt,
15505
+ temperature,
15506
+ maxTokens,
15507
+ }),
15469
15508
  });
15470
15509
  if (!res.ok) {
15471
15510
  throw await res.json();