@kokimoki/app 3.1.3 → 3.1.4

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.
Files changed (61) hide show
  1. package/README.md +1 -2
  2. package/dist/core/kokimoki-client.d.ts +4 -4
  3. package/dist/core/kokimoki-client.js +4 -4
  4. package/dist/kokimoki.min.d.ts +3 -321
  5. package/dist/kokimoki.min.js +763 -2014
  6. package/dist/kokimoki.min.js.map +1 -1
  7. package/dist/utils/valtio.d.ts +2 -5
  8. package/dist/utils/valtio.js +4 -2
  9. package/dist/version.d.ts +1 -1
  10. package/dist/version.js +1 -1
  11. package/docs/kokimoki-ai.instructions.md +4 -2
  12. package/docs/kokimoki-dynamic-stores.instructions.md +3 -3
  13. package/docs/kokimoki-i18n.instructions.md +1 -1
  14. package/docs/kokimoki-sdk.instructions.md +46 -7
  15. package/package.json +8 -2
  16. package/dist/fields.d.ts +0 -110
  17. package/dist/fields.js +0 -158
  18. package/dist/kokimoki-ai.d.ts +0 -153
  19. package/dist/kokimoki-ai.js +0 -164
  20. package/dist/kokimoki-awareness.d.ts +0 -21
  21. package/dist/kokimoki-awareness.js +0 -48
  22. package/dist/kokimoki-client-refactored.d.ts +0 -80
  23. package/dist/kokimoki-client-refactored.js +0 -400
  24. package/dist/kokimoki-client.d.ts +0 -362
  25. package/dist/kokimoki-client.js +0 -823
  26. package/dist/kokimoki-leaderboard.d.ts +0 -175
  27. package/dist/kokimoki-leaderboard.js +0 -203
  28. package/dist/kokimoki-local-store.d.ts +0 -11
  29. package/dist/kokimoki-local-store.js +0 -40
  30. package/dist/kokimoki-queue.d.ts +0 -0
  31. package/dist/kokimoki-queue.js +0 -38
  32. package/dist/kokimoki-req-res.d.ts +0 -0
  33. package/dist/kokimoki-req-res.js +0 -198
  34. package/dist/kokimoki-schema.d.ts +0 -113
  35. package/dist/kokimoki-schema.js +0 -162
  36. package/dist/kokimoki-storage.d.ts +0 -156
  37. package/dist/kokimoki-storage.js +0 -208
  38. package/dist/kokimoki-store.d.ts +0 -23
  39. package/dist/kokimoki-store.js +0 -117
  40. package/dist/kokimoki-transaction.d.ts +0 -18
  41. package/dist/kokimoki-transaction.js +0 -143
  42. package/dist/message-queue.d.ts +0 -8
  43. package/dist/message-queue.js +0 -19
  44. package/dist/room-subscription-mode.d.ts +0 -5
  45. package/dist/room-subscription-mode.js +0 -6
  46. package/dist/room-subscription.d.ts +0 -15
  47. package/dist/room-subscription.js +0 -52
  48. package/dist/synced-schema.d.ts +0 -74
  49. package/dist/synced-schema.js +0 -83
  50. package/dist/synced-store.d.ts +0 -10
  51. package/dist/synced-store.js +0 -9
  52. package/dist/synced-types.d.ts +0 -47
  53. package/dist/synced-types.js +0 -67
  54. package/dist/ws-message-reader.d.ts +0 -11
  55. package/dist/ws-message-reader.js +0 -36
  56. package/dist/ws-message-type copy.d.ts +0 -6
  57. package/dist/ws-message-type copy.js +0 -7
  58. package/dist/ws-message-type.d.ts +0 -11
  59. package/dist/ws-message-type.js +0 -12
  60. package/dist/ws-message-writer.d.ts +0 -9
  61. package/dist/ws-message-writer.js +0 -45
@@ -1,117 +0,0 @@
1
- import * as Y from "yjs";
2
- import { snapshot, proxy, subscribe } from "valtio/vanilla";
3
- import { bind as yjsBind } from "valtio-yjs";
4
- import { RoomSubscriptionMode } from "./room-subscription-mode";
5
- export class KokimokiStore {
6
- roomName;
7
- defaultValue;
8
- mode;
9
- doc;
10
- proxy;
11
- docRoot;
12
- connections;
13
- _unsubscribeConnectionsHandler = () => { };
14
- constructor(roomName, defaultValue, mode = RoomSubscriptionMode.ReadWrite) {
15
- this.roomName = roomName;
16
- this.defaultValue = defaultValue;
17
- this.mode = mode;
18
- // "_connections" is a reserved key for tracking connections
19
- if ("_connections" in defaultValue) {
20
- throw new Error(`"_connections" is a reserved key in KokimokiStore`);
21
- }
22
- // Construct Y doc
23
- this.doc = new Y.Doc();
24
- this.docRoot = this.doc.getMap("root");
25
- // Construct proxy object
26
- this.proxy = proxy();
27
- // @ts-ignore
28
- yjsBind(this.proxy, this.docRoot);
29
- // Construct connections proxy
30
- this.connections = proxy({
31
- connectionIds: new Set(),
32
- clientIds: new Set(),
33
- });
34
- }
35
- get() {
36
- return snapshot(this.proxy);
37
- }
38
- subscribe(set) {
39
- const handler = () => set(this.get());
40
- this.doc.on("update", handler);
41
- set(this.get());
42
- return () => this.doc.off("update", handler);
43
- }
44
- async onJoin(client) {
45
- // Update connections whenever _connections changes
46
- let prevConnectionIds = new Set();
47
- let prevClientIds = new Set();
48
- this._unsubscribeConnectionsHandler = subscribe(this.proxy, () => {
49
- // @ts-ignore
50
- const newConnectionIds = new Set(
51
- // @ts-ignore
52
- Object.keys(this.proxy._connections || {}));
53
- // Update only if there are changes
54
- let connectionIdsChanged = false;
55
- if (newConnectionIds.size !== prevConnectionIds.size) {
56
- connectionIdsChanged = true;
57
- }
58
- if (!connectionIdsChanged) {
59
- for (const id of newConnectionIds) {
60
- if (!prevConnectionIds.has(id)) {
61
- connectionIdsChanged = true;
62
- break;
63
- }
64
- }
65
- }
66
- if (!connectionIdsChanged) {
67
- for (const id of prevConnectionIds) {
68
- if (!newConnectionIds.has(id)) {
69
- connectionIdsChanged = true;
70
- break;
71
- }
72
- }
73
- }
74
- if (connectionIdsChanged) {
75
- this.connections.connectionIds = newConnectionIds;
76
- prevConnectionIds = new Set(newConnectionIds);
77
- }
78
- // @ts-ignore
79
- const newClientIds = new Set(
80
- // @ts-ignore
81
- Object.values(this.proxy._connections || {}));
82
- // Update only if there are changes
83
- let clientIdsChanged = false;
84
- if (newClientIds.size !== prevClientIds.size) {
85
- clientIdsChanged = true;
86
- }
87
- if (!clientIdsChanged) {
88
- for (const id of newClientIds) {
89
- if (!prevClientIds.has(id)) {
90
- clientIdsChanged = true;
91
- break;
92
- }
93
- }
94
- }
95
- if (!clientIdsChanged) {
96
- for (const id of prevClientIds) {
97
- if (!newClientIds.has(id)) {
98
- clientIdsChanged = true;
99
- break;
100
- }
101
- }
102
- }
103
- if (clientIdsChanged) {
104
- this.connections.clientIds = newClientIds;
105
- prevClientIds = new Set(newClientIds);
106
- }
107
- });
108
- // Add client to _connections map
109
- await client.transact([this], ([state]) => {
110
- state._connections[client.connectionId] = client.id;
111
- });
112
- }
113
- async onBeforeLeave(client) { }
114
- async onLeave(client) {
115
- this._unsubscribeConnectionsHandler();
116
- }
117
- }
@@ -1,18 +0,0 @@
1
- import { KokimokiStore } from "./kokimoki-store";
2
- export declare class KokimokiTransaction<T extends object[]> {
3
- private _updates;
4
- private _consumeMessagesInRooms;
5
- private _proxies;
6
- private _unbindProxies;
7
- constructor(stores: {
8
- [K in keyof T]: KokimokiStore<T[K]>;
9
- });
10
- getProxies(): T;
11
- getUpdates(): Promise<{
12
- updates: {
13
- roomName: string;
14
- update: Uint8Array;
15
- }[];
16
- consumedMessages: Set<string>;
17
- }>;
18
- }
@@ -1,143 +0,0 @@
1
- import * as Y from "yjs";
2
- import { proxy as yjsProxy } from "valtio/vanilla";
3
- import { bind as yjsBind } from "valtio-yjs";
4
- export class KokimokiTransaction {
5
- // private _clones = new Map<
6
- // string,
7
- // { docClone: Y.Doc; proxyClone: any; unbindProxy: () => void }
8
- // >();
9
- _updates = new Map();
10
- _consumeMessagesInRooms = new Set();
11
- // private _queueMessageCounter = 0;
12
- _proxies = [];
13
- _unbindProxies = [];
14
- constructor(stores) {
15
- // Create a proxy for each store
16
- for (const store of stores) {
17
- // Clone initial state
18
- const docClone = new Y.Doc();
19
- const docRoot = docClone.getMap("root");
20
- Y.applyUpdate(docClone, Y.encodeStateAsUpdate(store.doc));
21
- // Set up proxy
22
- const proxyClone = yjsProxy();
23
- const unbindProxy = yjsBind(proxyClone, docRoot);
24
- this._proxies.push(proxyClone);
25
- this._unbindProxies.push(unbindProxy);
26
- // Listen for updates
27
- docClone.on("update", (update) => {
28
- if (this._updates.has(store.roomName)) {
29
- const prevUpdate = this._updates.get(store.roomName);
30
- const nextUpdate = Y.mergeUpdates([prevUpdate, update]);
31
- this._updates.set(store.roomName, nextUpdate);
32
- }
33
- else {
34
- this._updates.set(store.roomName, update);
35
- }
36
- });
37
- // this._clones.set(store.roomName, { docClone, proxyClone, unbindProxy });
38
- }
39
- }
40
- getProxies() {
41
- // @ts-ignore
42
- return this._proxies;
43
- }
44
- // private _parseTarget(target: any): {
45
- // roomName: string;
46
- // doc: Y.Doc;
47
- // obj: any;
48
- // key: string;
49
- // path: string[];
50
- // } {
51
- // return target();
52
- // }
53
- // private _parsePath(obj: any, path: string[]) {
54
- // for (let i = 0; i < path.length - 1; i++) {
55
- // if (!(path[i] in obj)) {
56
- // obj[path[i]] = {};
57
- // }
58
- // obj = obj[path[i]];
59
- // }
60
- // return { obj, key: path[path.length - 1] };
61
- // }
62
- // private _getClone(roomName: string, doc: Y.Doc) {
63
- // if (!this._clones.has(roomName)) {
64
- // // Clone doc
65
- // const docClone = new Y.Doc();
66
- // const docRoot = docClone.getMap("root");
67
- // Y.applyUpdate(docClone, Y.encodeStateAsUpdate(doc));
68
- // // Set up proxy
69
- // const proxyClone = yjsProxy() as any;
70
- // const unbindProxy = yjsBind(proxyClone, docRoot);
71
- // // Listen for updates
72
- // docClone.on("update", (update) => {
73
- // if (this._updates.has(roomName)) {
74
- // const prevUpdate = this._updates.get(roomName)!;
75
- // const nextUpdate = Y.mergeUpdates([prevUpdate, update]);
76
- // this._updates.set(roomName, nextUpdate);
77
- // } else {
78
- // this._updates.set(roomName, update);
79
- // }
80
- // });
81
- // this._clones.set(roomName, { docClone, proxyClone, unbindProxy });
82
- // }
83
- // return this._clones.get(roomName)!;
84
- // }
85
- // get<T>(target: T): T {
86
- // const { roomName, doc, path } = this._parseTarget(target);
87
- // const { proxyClone } = this._getClone(roomName, doc);
88
- // const { obj, key } = this._parsePath(proxyClone, path);
89
- // return obj[key];
90
- // }
91
- // set<T>(target: T, value: T) {
92
- // const { roomName, doc, path } = this._parseTarget(target);
93
- // const { proxyClone } = this._getClone(roomName, doc);
94
- // const { obj, key } = this._parsePath(proxyClone, path);
95
- // obj[key] = value;
96
- // }
97
- // delete<T>(target: T) {
98
- // const { roomName, doc, path } = this._parseTarget(target);
99
- // const { proxyClone } = this._getClone(roomName, doc);
100
- // const { obj, key } = this._parsePath(proxyClone, path);
101
- // delete obj[key];
102
- // }
103
- // push<T>(target: T[], value: T) {
104
- // const { roomName, doc, path } = this._parseTarget(target);
105
- // const { proxyClone } = this._getClone(roomName, doc);
106
- // const { obj, key } = this._parsePath(proxyClone, path);
107
- // if (!(key in obj)) {
108
- // obj[key] = [];
109
- // }
110
- // obj[key].push(value);
111
- // }
112
- // queueMessage<T>(queue: KokimokiQueue<S.Generic<T>>, payload: T) {
113
- // const messageId = `${this.kmClient.serverTimestamp()}/${this
114
- // ._queueMessageCounter++}/${Math.random().toString(36).slice(2)}`;
115
- // this.set(queue.root[messageId], payload);
116
- // return messageId;
117
- // }
118
- // consumeMessage<T>(queue: KokimokiQueue<S.Generic<T>>, messageId: string) {
119
- // if (!queue.proxy[messageId]) {
120
- // throw new Error(
121
- // `Message ${messageId} does not exist or has already been consumed`
122
- // );
123
- // }
124
- // this.delete(queue.root[messageId]);
125
- // this._consumeMessagesInRooms.add(queue.roomName);
126
- // }
127
- async getUpdates() {
128
- // Wait for doc update handling to finish
129
- return await new Promise((resolve) => setTimeout(() => {
130
- // Clean up
131
- for (const unbindProxy of this._unbindProxies) {
132
- unbindProxy();
133
- }
134
- // Generates updates
135
- const updates = Array.from(this._updates.entries()).map(([roomName, update]) => ({
136
- roomName,
137
- update,
138
- }));
139
- // Done
140
- resolve({ updates, consumedMessages: this._consumeMessagesInRooms });
141
- }));
142
- }
143
- }
@@ -1,8 +0,0 @@
1
- import { SyncedStore } from "./synced-store";
2
- export declare class MessageQueue<T> extends SyncedStore<{
3
- messages: T[];
4
- }> {
5
- name: string;
6
- constructor(name: string);
7
- push(message: T): Promise<Uint8Array>;
8
- }
@@ -1,19 +0,0 @@
1
- import EventEmitter from "events";
2
- import { SyncedStore } from "./synced-store";
3
- import Y from "yjs";
4
- export class MessageQueue extends SyncedStore {
5
- name;
6
- constructor(name) {
7
- super({ messages: [] });
8
- this.name = name;
9
- }
10
- async push(message) {
11
- // Construct Y update to push the message to the queue
12
- const id = crypto.randomUUID();
13
- const ydoc = new Y.Doc();
14
- const map = ydoc.getMap("messages");
15
- map.set(id, message);
16
- const update = Y.encodeStateAsUpdate(ydoc);
17
- return update;
18
- }
19
- }
@@ -1,5 +0,0 @@
1
- export declare enum RoomSubscriptionMode {
2
- Read = "r",
3
- Write = "w",
4
- ReadWrite = "b"
5
- }
@@ -1,6 +0,0 @@
1
- export var RoomSubscriptionMode;
2
- (function (RoomSubscriptionMode) {
3
- RoomSubscriptionMode["Read"] = "r";
4
- RoomSubscriptionMode["Write"] = "w";
5
- RoomSubscriptionMode["ReadWrite"] = "b";
6
- })(RoomSubscriptionMode || (RoomSubscriptionMode = {}));
@@ -1,15 +0,0 @@
1
- import type { KokimokiStore } from "./kokimoki-store";
2
- import type { KokimokiClient } from "./kokimoki-client";
3
- export declare class RoomSubscription {
4
- private kmClient;
5
- readonly store: KokimokiStore<any>;
6
- private _joined;
7
- private _roomHash?;
8
- private _onDisconnect;
9
- constructor(kmClient: KokimokiClient, store: KokimokiStore<any>);
10
- get roomName(): string;
11
- get roomHash(): number;
12
- get joined(): boolean;
13
- applyInitialResponse(roomHash: number, initialUpdate?: Uint8Array): Promise<void>;
14
- close(): void;
15
- }
@@ -1,52 +0,0 @@
1
- import * as Y from "yjs";
2
- import { RoomSubscriptionMode } from "./room-subscription-mode";
3
- export class RoomSubscription {
4
- kmClient;
5
- store;
6
- _joined = false;
7
- _roomHash;
8
- _onDisconnect = () => {
9
- this._joined = false;
10
- };
11
- constructor(kmClient, store) {
12
- this.kmClient = kmClient;
13
- this.store = store;
14
- kmClient.on("disconnected", this._onDisconnect);
15
- }
16
- get roomName() {
17
- return this.store.roomName;
18
- }
19
- get roomHash() {
20
- if (!this._roomHash) {
21
- throw new Error("Room not joined");
22
- }
23
- return this._roomHash;
24
- }
25
- get joined() {
26
- return this._joined;
27
- }
28
- async applyInitialResponse(roomHash, initialUpdate) {
29
- this._roomHash = roomHash;
30
- // Apply initial state
31
- if (initialUpdate) {
32
- Y.applyUpdate(this.store.doc, initialUpdate, this);
33
- }
34
- // Set defaults if doc is empty after sync (only possible in ReadWrite mode)
35
- if (this.store.mode === RoomSubscriptionMode.ReadWrite) {
36
- await this.kmClient.transact([this.store], ([state]) => {
37
- if (!("_connections" in state)) {
38
- state._connections = {};
39
- }
40
- for (const key in this.store.defaultValue) {
41
- if (!state.hasOwnProperty(key)) {
42
- state[key] = this.store.defaultValue[key];
43
- }
44
- }
45
- });
46
- }
47
- this._joined = true;
48
- }
49
- close() {
50
- this.kmClient.off("disconnected", this._onDisconnect);
51
- }
52
- }
@@ -1,74 +0,0 @@
1
- export declare abstract class SyncedGeneric<T> {
2
- abstract get defaultValue(): T;
3
- abstract set defaultValue(value: T);
4
- }
5
- export declare class SyncedNumber extends SyncedGeneric<number> {
6
- defaultValue: number;
7
- constructor(defaultValue?: number);
8
- }
9
- declare function syncedNumber(defaultValue?: number): SyncedNumber;
10
- export declare class SyncedString extends SyncedGeneric<string> {
11
- defaultValue: string;
12
- constructor(defaultValue?: string);
13
- }
14
- declare function syncedString(defaultValue?: string): SyncedString;
15
- export declare class SyncedBoolean extends SyncedGeneric<boolean> {
16
- defaultValue: boolean;
17
- constructor(defaultValue?: boolean);
18
- }
19
- declare function syncedBoolean(defaultValue?: boolean): SyncedBoolean;
20
- export declare class SyncedObject<
21
- Data extends Record<string, SyncedGeneric<unknown>>,
22
- > extends SyncedGeneric<{
23
- [key in keyof Data]: Data[key]["defaultValue"];
24
- }> {
25
- fields: Data;
26
- constructor(fields: Data);
27
- get defaultValue(): {
28
- [key in keyof Data]: Data[key]["defaultValue"];
29
- };
30
- set defaultValue(value: {
31
- [key in keyof Data]: Data[key]["defaultValue"];
32
- });
33
- }
34
- declare function syncedObject<
35
- Data extends Record<string, SyncedGeneric<unknown>>,
36
- >(schema: Data): SyncedObject<Data>;
37
- export declare class SyncedMap<T extends SyncedGeneric<unknown>> {
38
- schema: T;
39
- defaultValue: {
40
- [key: string]: T["defaultValue"];
41
- };
42
- constructor(
43
- schema: T,
44
- defaultValue?: {
45
- [key: string]: T["defaultValue"];
46
- },
47
- );
48
- }
49
- declare function syncedMap<T extends SyncedGeneric<unknown>>(
50
- schema: T,
51
- defaultValue?: {
52
- [key: string]: T["defaultValue"];
53
- },
54
- ): SyncedMap<T>;
55
- export declare class SyncedArray<
56
- T extends SyncedGeneric<unknown>,
57
- > extends SyncedGeneric<T["defaultValue"][]> {
58
- schema: T;
59
- defaultValue: T["defaultValue"][];
60
- constructor(schema: T, defaultValue?: T["defaultValue"][]);
61
- }
62
- declare function syncedArray<T extends SyncedGeneric<unknown>>(
63
- schema: T,
64
- defaultValue?: T["defaultValue"][],
65
- ): SyncedArray<T>;
66
- export declare const S: {
67
- number: typeof syncedNumber;
68
- string: typeof syncedString;
69
- boolean: typeof syncedBoolean;
70
- object: typeof syncedObject;
71
- map: typeof syncedMap;
72
- array: typeof syncedArray;
73
- };
74
- export {};
@@ -1,83 +0,0 @@
1
- export class SyncedGeneric {}
2
- export class SyncedNumber extends SyncedGeneric {
3
- defaultValue;
4
- constructor(defaultValue = 0) {
5
- super();
6
- this.defaultValue = defaultValue;
7
- }
8
- }
9
- function syncedNumber(defaultValue = 0) {
10
- return new SyncedNumber(defaultValue);
11
- }
12
- export class SyncedString extends SyncedGeneric {
13
- defaultValue;
14
- constructor(defaultValue = "") {
15
- super();
16
- this.defaultValue = defaultValue;
17
- }
18
- }
19
- function syncedString(defaultValue = "") {
20
- return new SyncedString(defaultValue);
21
- }
22
- export class SyncedBoolean extends SyncedGeneric {
23
- defaultValue;
24
- constructor(defaultValue = false) {
25
- super();
26
- this.defaultValue = defaultValue;
27
- }
28
- }
29
- function syncedBoolean(defaultValue = false) {
30
- return new SyncedBoolean(defaultValue);
31
- }
32
- export class SyncedObject extends SyncedGeneric {
33
- fields;
34
- constructor(fields) {
35
- super();
36
- this.fields = fields;
37
- }
38
- get defaultValue() {
39
- return Object.entries(this.fields).reduce((acc, [key, field]) => {
40
- acc[key] = field.defaultValue;
41
- return acc;
42
- }, {});
43
- }
44
- set defaultValue(value) {
45
- for (const [key, field] of Object.entries(this.fields)) {
46
- field.defaultValue = value[key];
47
- }
48
- }
49
- }
50
- function syncedObject(schema) {
51
- return new SyncedObject(schema);
52
- }
53
- export class SyncedMap {
54
- schema;
55
- defaultValue;
56
- constructor(schema, defaultValue = {}) {
57
- this.schema = schema;
58
- this.defaultValue = defaultValue;
59
- }
60
- }
61
- function syncedMap(schema, defaultValue = {}) {
62
- return new SyncedMap(schema, defaultValue);
63
- }
64
- export class SyncedArray extends SyncedGeneric {
65
- schema;
66
- defaultValue;
67
- constructor(schema, defaultValue = []) {
68
- super();
69
- this.schema = schema;
70
- this.defaultValue = defaultValue;
71
- }
72
- }
73
- function syncedArray(schema, defaultValue = []) {
74
- return new SyncedArray(schema, defaultValue);
75
- }
76
- export const S = {
77
- number: syncedNumber,
78
- string: syncedString,
79
- boolean: syncedBoolean,
80
- object: syncedObject,
81
- map: syncedMap,
82
- array: syncedArray,
83
- };
@@ -1,10 +0,0 @@
1
- import type * as Y from "yjs";
2
- import type {
3
- DocTypeDescription,
4
- MappedTypeDescription,
5
- } from "@syncedstore/core/types/doc";
6
- export declare class SyncedStore<T extends DocTypeDescription> {
7
- readonly data: MappedTypeDescription<T>;
8
- readonly doc: Y.Doc;
9
- constructor(initialState: T);
10
- }
@@ -1,9 +0,0 @@
1
- import { syncedStore, getYjsDoc } from "@syncedstore/core";
2
- export class SyncedStore {
3
- data;
4
- doc;
5
- constructor(initialState) {
6
- this.data = syncedStore(initialState);
7
- this.doc = getYjsDoc(this.data);
8
- }
9
- }
@@ -1,47 +0,0 @@
1
- export declare abstract class SyncedGeneric<T> {
2
- abstract get defaultValue(): T;
3
- abstract set defaultValue(value: T);
4
- }
5
- export declare class SyncedNumber extends SyncedGeneric<number> {
6
- defaultValue: number;
7
- constructor(defaultValue?: number);
8
- }
9
- export declare function syncedNumber(defaultValue?: number): SyncedNumber;
10
- export declare class SyncedString extends SyncedGeneric<string> {
11
- defaultValue: string;
12
- constructor(defaultValue?: string);
13
- }
14
- export declare function syncedString(defaultValue?: string): SyncedString;
15
- export declare class SyncedBoolean extends SyncedGeneric<boolean> {
16
- defaultValue: boolean;
17
- constructor(defaultValue?: boolean);
18
- }
19
- export declare function syncedBoolean(defaultValue?: boolean): SyncedBoolean;
20
- export declare class SyncedMap<
21
- Data extends Record<string, SyncedGeneric<unknown>>,
22
- > extends SyncedGeneric<{
23
- [key in keyof Data]: Data[key]["defaultValue"];
24
- }> {
25
- fields: Data;
26
- constructor(fields: Data);
27
- get defaultValue(): {
28
- [key in keyof Data]: Data[key]["defaultValue"];
29
- };
30
- set defaultValue(value: {
31
- [key in keyof Data]: Data[key]["defaultValue"];
32
- });
33
- }
34
- export declare function syncedMap<
35
- T extends Record<string, SyncedGeneric<unknown>>,
36
- >(schema: T): SyncedMap<T>;
37
- export declare class SyncedArray<
38
- T extends SyncedGeneric<unknown>,
39
- > extends SyncedGeneric<T["defaultValue"][]> {
40
- schema: T;
41
- defaultValue: T["defaultValue"][];
42
- constructor(schema: T, defaultValue?: T["defaultValue"][]);
43
- }
44
- export declare function syncedArray<T extends SyncedGeneric<unknown>>(
45
- schema: T,
46
- defaultValue?: T["defaultValue"][],
47
- ): SyncedArray<T>;
@@ -1,67 +0,0 @@
1
- import * as Y from "yjs";
2
- import { proxy as yjsProxy } from "valtio";
3
- import { bind as yjsBind } from "valtio-yjs";
4
- export class SyncedGeneric {}
5
- export class SyncedNumber extends SyncedGeneric {
6
- defaultValue;
7
- constructor(defaultValue = 0) {
8
- super();
9
- this.defaultValue = defaultValue;
10
- }
11
- }
12
- export function syncedNumber(defaultValue = 0) {
13
- return new SyncedNumber(defaultValue);
14
- }
15
- export class SyncedString extends SyncedGeneric {
16
- defaultValue;
17
- constructor(defaultValue = "") {
18
- super();
19
- this.defaultValue = defaultValue;
20
- }
21
- }
22
- export function syncedString(defaultValue = "") {
23
- return new SyncedString(defaultValue);
24
- }
25
- export class SyncedBoolean extends SyncedGeneric {
26
- defaultValue;
27
- constructor(defaultValue = false) {
28
- super();
29
- this.defaultValue = defaultValue;
30
- }
31
- }
32
- export function syncedBoolean(defaultValue = false) {
33
- return new SyncedBoolean(defaultValue);
34
- }
35
- export class SyncedMap extends SyncedGeneric {
36
- fields;
37
- constructor(fields) {
38
- super();
39
- this.fields = fields;
40
- }
41
- get defaultValue() {
42
- return Object.entries(this.fields).reduce((acc, [key, field]) => {
43
- acc[key] = field.defaultValue;
44
- return acc;
45
- }, {});
46
- }
47
- set defaultValue(value) {
48
- for (const [key, field] of Object.entries(this.fields)) {
49
- field.defaultValue = value[key];
50
- }
51
- }
52
- }
53
- export function syncedMap(schema) {
54
- return new SyncedMap(schema);
55
- }
56
- export class SyncedArray extends SyncedGeneric {
57
- schema;
58
- defaultValue;
59
- constructor(schema, defaultValue = []) {
60
- super();
61
- this.schema = schema;
62
- this.defaultValue = defaultValue;
63
- }
64
- }
65
- export function syncedArray(schema, defaultValue = []) {
66
- return new SyncedArray(schema, defaultValue);
67
- }