@kokimoki/app 1.10.0 → 1.10.2

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.
@@ -13,7 +13,7 @@ export class KokimokiAwareness extends KokimokiStore {
13
13
  clientId: z.string(),
14
14
  lastPing: z.number(),
15
15
  data: dataSchema,
16
- })), mode);
16
+ })), {}, mode);
17
17
  this.dataSchema = dataSchema;
18
18
  this._data = _data;
19
19
  this._pingInterval = setInterval(async () => {
@@ -79,9 +79,9 @@ export declare class KokimokiClient<ClientContextT = any> extends KokimokiClient
79
79
  close(): Promise<void>;
80
80
  getRoomHash<T extends ZodType>(store: KokimokiStore<T>): number;
81
81
  /** Initializers */
82
- store<T extends ZodType>(name: string, schema: T, autoJoin?: boolean): KokimokiStore<T>;
83
- localStore<T>(name: string, schema: ZodType<T>): KokimokiLocalStore<T>;
84
- awareness<T>(name: string, dataSchema: ZodType<T>, initialData?: T, autoJoin?: boolean): KokimokiAwareness<T>;
82
+ store<T>(name: string, schema: ZodType<T>, defaultState: T, autoJoin?: boolean): KokimokiStore<T>;
83
+ localStore<T>(name: string, schema: ZodType<T>, defaultState: T): KokimokiLocalStore<T>;
84
+ awareness<T>(name: string, dataSchema: ZodType<T>, initialData: T, autoJoin?: boolean): KokimokiAwareness<T>;
85
85
  /**
86
86
  * Add a new entry to a leaderboard
87
87
  * @param leaderboardName
@@ -569,8 +569,8 @@ export class KokimokiClient extends EventEmitter {
569
569
  }
570
570
  /** Initializers */
571
571
  // store
572
- store(name, schema, autoJoin = true) {
573
- const store = new KokimokiStore(name, schema);
572
+ store(name, schema, defaultState, autoJoin = true) {
573
+ const store = new KokimokiStore(name, schema, defaultState);
574
574
  if (autoJoin) {
575
575
  this.join(store)
576
576
  .then(() => { })
@@ -579,8 +579,8 @@ export class KokimokiClient extends EventEmitter {
579
579
  return store;
580
580
  }
581
581
  // local store
582
- localStore(name, schema) {
583
- const store = new KokimokiLocalStore(name, schema);
582
+ localStore(name, schema, defaultState) {
583
+ const store = new KokimokiLocalStore(name, schema, defaultState);
584
584
  this.join(store)
585
585
  .then(() => { })
586
586
  .catch(() => { });
@@ -602,7 +602,7 @@ export class KokimokiClient extends EventEmitter {
602
602
  // return queue;
603
603
  // }
604
604
  // awareness
605
- awareness(name, dataSchema, initialData = dataSchema.parse({}), autoJoin = true) {
605
+ awareness(name, dataSchema, initialData, autoJoin = true) {
606
606
  const awareness = new KokimokiAwareness(name, dataSchema, initialData);
607
607
  if (autoJoin) {
608
608
  this.join(awareness)
@@ -4,7 +4,7 @@ export declare class KokimokiLocalStore<Data> extends KokimokiStore<Data> {
4
4
  private readonly localRoomName;
5
5
  private _stateKey?;
6
6
  private get stateKey();
7
- constructor(localRoomName: string, schema: ZodType<Data>);
7
+ constructor(localRoomName: string, schema: ZodType<Data>, defaultState: Data);
8
8
  getInitialUpdate(appId: string, clientId: string): {
9
9
  roomHash: number;
10
10
  initialUpdate: Uint8Array | undefined;
@@ -11,8 +11,8 @@ export class KokimokiLocalStore extends KokimokiStore {
11
11
  }
12
12
  return this._stateKey;
13
13
  }
14
- constructor(localRoomName, schema) {
15
- super(`/l/${localRoomName}`, schema, RoomSubscriptionMode.ReadWrite);
14
+ constructor(localRoomName, schema, defaultState) {
15
+ super(`/l/${localRoomName}`, schema, defaultState, RoomSubscriptionMode.ReadWrite);
16
16
  this.localRoomName = localRoomName;
17
17
  // Synchronize doc changes to local storage
18
18
  // TODO: maybe do not serialize full state every time
@@ -4,13 +4,13 @@ import type { KokimokiClient } from "./kokimoki-client";
4
4
  import { ZodType } from "zod";
5
5
  export declare class KokimokiStore<T> {
6
6
  readonly roomName: string;
7
+ readonly schema: ZodType<T>;
8
+ readonly defaultValue: T;
7
9
  readonly mode: RoomSubscriptionMode;
8
10
  readonly doc: Y.Doc;
9
11
  readonly proxy: T;
10
- readonly root: T;
11
- readonly defaultValue: T;
12
12
  readonly docRoot: Y.Map<unknown>;
13
- constructor(roomName: string, schema: ZodType<T>, mode?: RoomSubscriptionMode);
13
+ constructor(roomName: string, schema: ZodType<T>, defaultValue: T, mode?: RoomSubscriptionMode);
14
14
  get(): T;
15
15
  subscribe(set: (value: T) => void): () => void;
16
16
  onJoin(client: KokimokiClient): Promise<void>;
@@ -1,18 +1,20 @@
1
1
  import * as Y from "yjs";
2
2
  import { proxy as yjsProxy } from "valtio";
3
3
  import { bind as yjsBind } from "valtio-yjs";
4
- import DeepProxy from "proxy-deep";
4
+ // import DeepProxy from "proxy-deep";
5
5
  import { RoomSubscriptionMode } from "./room-subscription-mode";
6
6
  export class KokimokiStore {
7
7
  roomName;
8
+ schema;
9
+ defaultValue;
8
10
  mode;
9
11
  doc;
10
- proxy; //T["defaultValue"];
11
- root; //T["defaultValue"];
12
- defaultValue; //T["defaultValue"];
12
+ proxy;
13
13
  docRoot;
14
- constructor(roomName, schema, mode = RoomSubscriptionMode.ReadWrite) {
14
+ constructor(roomName, schema, defaultValue, mode = RoomSubscriptionMode.ReadWrite) {
15
15
  this.roomName = roomName;
16
+ this.schema = schema;
17
+ this.defaultValue = defaultValue;
16
18
  this.mode = mode;
17
19
  // Construct Y doc
18
20
  this.doc = new Y.Doc();
@@ -21,38 +23,34 @@ export class KokimokiStore {
21
23
  this.proxy = yjsProxy(); //T["defaultValue"];
22
24
  // @ts-ignore
23
25
  yjsBind(this.proxy, this.docRoot);
24
- // Create root proxy
25
- const store = this;
26
- // @ts-ignore
27
- this.root = new DeepProxy(this.proxy, {
28
- get() {
29
- return this.nest(function () { });
30
- },
31
- apply() {
32
- let obj = store.proxy;
33
- for (let i = 0; i < this.path.length - 1; i++) {
34
- obj = obj[this.path[i]];
35
- }
36
- return {
37
- roomName: store.roomName,
38
- doc: store.doc,
39
- path: this.path,
40
- obj,
41
- key: this.path[this.path.length - 1],
42
- };
43
- },
44
- });
45
- // Set default value
46
- this.defaultValue = schema.parse({}); //schema.defaultValue;
26
+ // // Create root proxy
27
+ // const store = this;
28
+ // // @ts-ignore
29
+ // this.root = new DeepProxy(this.proxy, {
30
+ // get() {
31
+ // return this.nest(function () {});
32
+ // },
33
+ // apply() {
34
+ // let obj: any = store.proxy;
35
+ // for (let i = 0; i < this.path.length - 1; i++) {
36
+ // obj = obj[this.path[i]];
37
+ // }
38
+ // return {
39
+ // roomName: store.roomName,
40
+ // doc: store.doc,
41
+ // path: this.path,
42
+ // obj,
43
+ // key: this.path[this.path.length - 1],
44
+ // };
45
+ // },
46
+ // });
47
47
  }
48
48
  get() {
49
49
  return this.proxy;
50
50
  }
51
51
  subscribe(set) {
52
- // @ts-ignore
53
52
  const handler = () => set(this.proxy);
54
53
  this.doc.on("update", handler);
55
- // @ts-ignore
56
54
  set(this.proxy);
57
55
  return () => this.doc.off("update", handler);
58
56
  }
@@ -33,13 +33,13 @@ declare enum RoomSubscriptionMode {
33
33
 
34
34
  declare class KokimokiStore<T> {
35
35
  readonly roomName: string;
36
+ readonly schema: ZodType<T>;
37
+ readonly defaultValue: T;
36
38
  readonly mode: RoomSubscriptionMode;
37
39
  readonly doc: Y.Doc;
38
40
  readonly proxy: T;
39
- readonly root: T;
40
- readonly defaultValue: T;
41
41
  readonly docRoot: Y.Map<unknown>;
42
- constructor(roomName: string, schema: ZodType<T>, mode?: RoomSubscriptionMode);
42
+ constructor(roomName: string, schema: ZodType<T>, defaultValue: T, mode?: RoomSubscriptionMode);
43
43
  get(): T;
44
44
  subscribe(set: (value: T) => void): () => void;
45
45
  onJoin(client: KokimokiClient): Promise<void>;
@@ -72,7 +72,7 @@ declare class KokimokiLocalStore<Data> extends KokimokiStore<Data> {
72
72
  private readonly localRoomName;
73
73
  private _stateKey?;
74
74
  private get stateKey();
75
- constructor(localRoomName: string, schema: ZodType<Data>);
75
+ constructor(localRoomName: string, schema: ZodType<Data>, defaultState: Data);
76
76
  getInitialUpdate(appId: string, clientId: string): {
77
77
  roomHash: number;
78
78
  initialUpdate: Uint8Array | undefined;
@@ -152,9 +152,9 @@ declare class KokimokiClient<ClientContextT = any> extends KokimokiClient_base {
152
152
  close(): Promise<void>;
153
153
  getRoomHash<T extends ZodType>(store: KokimokiStore<T>): number;
154
154
  /** Initializers */
155
- store<T extends ZodType>(name: string, schema: T, autoJoin?: boolean): KokimokiStore<T>;
156
- localStore<T>(name: string, schema: ZodType<T>): KokimokiLocalStore<T>;
157
- awareness<T>(name: string, dataSchema: ZodType<T>, initialData?: T, autoJoin?: boolean): KokimokiAwareness<T>;
155
+ store<T>(name: string, schema: ZodType<T>, defaultState: T, autoJoin?: boolean): KokimokiStore<T>;
156
+ localStore<T>(name: string, schema: ZodType<T>, defaultState: T): KokimokiLocalStore<T>;
157
+ awareness<T>(name: string, dataSchema: ZodType<T>, initialData: T, autoJoin?: boolean): KokimokiAwareness<T>;
158
158
  /**
159
159
  * Add a new entry to a leaderboard
160
160
  * @param leaderboardName
@@ -486,7 +486,7 @@ function eventTargetAgnosticAddListener(emitter, name, listener, flags) {
486
486
  var eventsExports = events.exports;
487
487
  var EventEmitter$1 = /*@__PURE__*/getDefaultExportFromCjs(eventsExports);
488
488
 
489
- const KOKIMOKI_APP_VERSION = "1.10.0";
489
+ const KOKIMOKI_APP_VERSION = "1.10.2";
490
490
 
491
491
  /**
492
492
  * Utility module to work with key-value stores.
@@ -890,7 +890,7 @@ const assign = Object.assign;
890
890
  /**
891
891
  * @param {Object<string,any>} obj
892
892
  */
893
- const keys$1 = Object.keys;
893
+ const keys = Object.keys;
894
894
 
895
895
  /**
896
896
  * @template V
@@ -907,7 +907,7 @@ const forEach$2 = (obj, f) => {
907
907
  * @param {Object<string,any>} obj
908
908
  * @return {number}
909
909
  */
910
- const length$1 = obj => keys$1(obj).length;
910
+ const length$1 = obj => keys(obj).length;
911
911
 
912
912
  /**
913
913
  * @param {Object|undefined} obj
@@ -11389,110 +11389,6 @@ class KokimokiTransaction {
11389
11389
  }
11390
11390
  }
11391
11391
 
11392
- function parsePath(text) {
11393
- return text.split('.')
11394
- }
11395
-
11396
- function push(arr, el) {
11397
- const newArr = arr.slice();
11398
- newArr.push(el);
11399
- return newArr;
11400
- }
11401
-
11402
- // names of the traps that can be registered with ES6's Proxy object
11403
- const trapNames = [
11404
- 'apply',
11405
- 'construct',
11406
- 'defineProperty',
11407
- 'deleteProperty',
11408
- 'enumerate', // deprecated
11409
- 'get',
11410
- 'getOwnPropertyDescriptor',
11411
- 'getPrototypeOf',
11412
- 'has',
11413
- 'isExtensible',
11414
- 'ownKeys',
11415
- 'preventExtensions',
11416
- 'set',
11417
- 'setPrototypeOf',
11418
- ];
11419
-
11420
- // a list of paramer indexes that indicate that the a recieves a key at that parameter
11421
- // this information will be used to update the path accordingly
11422
- const keys = {
11423
- get: 1,
11424
- set: 1,
11425
- deleteProperty: 1,
11426
- has: 1,
11427
- defineProperty: 1,
11428
- getOwnPropertyDescriptor: 1,
11429
- };
11430
-
11431
- function DeepProxy(rootTarget, traps, options) {
11432
-
11433
- let path = [];
11434
- let userData = {};
11435
-
11436
- if (options !== undefined && typeof options.path !== 'undefined') {
11437
- path = parsePath(options.path);
11438
- }
11439
- if (options !== undefined && typeof options.userData !== 'undefined') {
11440
- userData = options.userData;
11441
- }
11442
-
11443
- function createProxy(target, path) {
11444
-
11445
- // avoid creating a new object between two traps
11446
- const context = { rootTarget, path };
11447
- Object.assign(context, userData);
11448
-
11449
- const realTraps = {};
11450
-
11451
- for (const trapName of trapNames) {
11452
- const keyParamIdx = keys[trapName]
11453
- , trap = traps[trapName];
11454
-
11455
- if (typeof trap !== 'undefined') {
11456
-
11457
- if (typeof keyParamIdx !== 'undefined') {
11458
-
11459
- realTraps[trapName] = function () {
11460
-
11461
- const key = arguments[keyParamIdx];
11462
-
11463
- // update context for this trap
11464
- context.nest = function (nestedTarget) {
11465
- if (nestedTarget === undefined)
11466
- nestedTarget = rootTarget;
11467
- return createProxy(nestedTarget, push(path, key));
11468
- };
11469
-
11470
- return trap.apply(context, arguments);
11471
- };
11472
- } else {
11473
-
11474
- realTraps[trapName] = function () {
11475
-
11476
- // update context for this trap
11477
- context.nest = function (nestedTarget) {
11478
- if (nestedTarget === undefined)
11479
- nestedTarget = {};
11480
- return createProxy(nestedTarget, path);
11481
- };
11482
-
11483
- return trap.apply(context, arguments);
11484
- };
11485
- }
11486
- }
11487
- }
11488
-
11489
- return new Proxy(target, realTraps);
11490
- }
11491
-
11492
- return createProxy(rootTarget, path);
11493
-
11494
- }
11495
-
11496
11392
  var RoomSubscriptionMode;
11497
11393
  (function (RoomSubscriptionMode) {
11498
11394
  RoomSubscriptionMode["Read"] = "r";
@@ -11502,14 +11398,16 @@ var RoomSubscriptionMode;
11502
11398
 
11503
11399
  class KokimokiStore {
11504
11400
  roomName;
11401
+ schema;
11402
+ defaultValue;
11505
11403
  mode;
11506
11404
  doc;
11507
- proxy; //T["defaultValue"];
11508
- root; //T["defaultValue"];
11509
- defaultValue; //T["defaultValue"];
11405
+ proxy;
11510
11406
  docRoot;
11511
- constructor(roomName, schema, mode = RoomSubscriptionMode.ReadWrite) {
11407
+ constructor(roomName, schema, defaultValue, mode = RoomSubscriptionMode.ReadWrite) {
11512
11408
  this.roomName = roomName;
11409
+ this.schema = schema;
11410
+ this.defaultValue = defaultValue;
11513
11411
  this.mode = mode;
11514
11412
  // Construct Y doc
11515
11413
  this.doc = new Doc();
@@ -11518,38 +11416,34 @@ class KokimokiStore {
11518
11416
  this.proxy = proxy(); //T["defaultValue"];
11519
11417
  // @ts-ignore
11520
11418
  f(this.proxy, this.docRoot);
11521
- // Create root proxy
11522
- const store = this;
11523
- // @ts-ignore
11524
- this.root = new DeepProxy(this.proxy, {
11525
- get() {
11526
- return this.nest(function () { });
11527
- },
11528
- apply() {
11529
- let obj = store.proxy;
11530
- for (let i = 0; i < this.path.length - 1; i++) {
11531
- obj = obj[this.path[i]];
11532
- }
11533
- return {
11534
- roomName: store.roomName,
11535
- doc: store.doc,
11536
- path: this.path,
11537
- obj,
11538
- key: this.path[this.path.length - 1],
11539
- };
11540
- },
11541
- });
11542
- // Set default value
11543
- this.defaultValue = schema.parse({}); //schema.defaultValue;
11419
+ // // Create root proxy
11420
+ // const store = this;
11421
+ // // @ts-ignore
11422
+ // this.root = new DeepProxy(this.proxy, {
11423
+ // get() {
11424
+ // return this.nest(function () {});
11425
+ // },
11426
+ // apply() {
11427
+ // let obj: any = store.proxy;
11428
+ // for (let i = 0; i < this.path.length - 1; i++) {
11429
+ // obj = obj[this.path[i]];
11430
+ // }
11431
+ // return {
11432
+ // roomName: store.roomName,
11433
+ // doc: store.doc,
11434
+ // path: this.path,
11435
+ // obj,
11436
+ // key: this.path[this.path.length - 1],
11437
+ // };
11438
+ // },
11439
+ // });
11544
11440
  }
11545
11441
  get() {
11546
11442
  return this.proxy;
11547
11443
  }
11548
11444
  subscribe(set) {
11549
- // @ts-ignore
11550
11445
  const handler = () => set(this.proxy);
11551
11446
  this.doc.on("update", handler);
11552
- // @ts-ignore
11553
11447
  set(this.proxy);
11554
11448
  return () => this.doc.off("update", handler);
11555
11449
  }
@@ -16118,7 +16012,7 @@ class KokimokiAwareness extends KokimokiStore {
16118
16012
  clientId: z.string(),
16119
16013
  lastPing: z.number(),
16120
16014
  data: dataSchema,
16121
- })), mode);
16015
+ })), {}, mode);
16122
16016
  this.dataSchema = dataSchema;
16123
16017
  this._data = _data;
16124
16018
  this._pingInterval = setInterval(async () => {
@@ -18726,8 +18620,8 @@ class KokimokiLocalStore extends KokimokiStore {
18726
18620
  }
18727
18621
  return this._stateKey;
18728
18622
  }
18729
- constructor(localRoomName, schema) {
18730
- super(`/l/${localRoomName}`, schema, RoomSubscriptionMode.ReadWrite);
18623
+ constructor(localRoomName, schema, defaultState) {
18624
+ super(`/l/${localRoomName}`, schema, defaultState, RoomSubscriptionMode.ReadWrite);
18731
18625
  this.localRoomName = localRoomName;
18732
18626
  // Synchronize doc changes to local storage
18733
18627
  // TODO: maybe do not serialize full state every time
@@ -19312,8 +19206,8 @@ class KokimokiClient extends EventEmitter$1 {
19312
19206
  }
19313
19207
  /** Initializers */
19314
19208
  // store
19315
- store(name, schema, autoJoin = true) {
19316
- const store = new KokimokiStore(name, schema);
19209
+ store(name, schema, defaultState, autoJoin = true) {
19210
+ const store = new KokimokiStore(name, schema, defaultState);
19317
19211
  if (autoJoin) {
19318
19212
  this.join(store)
19319
19213
  .then(() => { })
@@ -19322,8 +19216,8 @@ class KokimokiClient extends EventEmitter$1 {
19322
19216
  return store;
19323
19217
  }
19324
19218
  // local store
19325
- localStore(name, schema) {
19326
- const store = new KokimokiLocalStore(name, schema);
19219
+ localStore(name, schema, defaultState) {
19220
+ const store = new KokimokiLocalStore(name, schema, defaultState);
19327
19221
  this.join(store)
19328
19222
  .then(() => { })
19329
19223
  .catch(() => { });
@@ -19345,7 +19239,7 @@ class KokimokiClient extends EventEmitter$1 {
19345
19239
  // return queue;
19346
19240
  // }
19347
19241
  // awareness
19348
- awareness(name, dataSchema, initialData = dataSchema.parse({}), autoJoin = true) {
19242
+ awareness(name, dataSchema, initialData, autoJoin = true) {
19349
19243
  const awareness = new KokimokiAwareness(name, dataSchema, initialData);
19350
19244
  if (autoJoin) {
19351
19245
  this.join(awareness)