@pluv/client 0.17.3 → 0.18.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.
package/src/MockedRoom.ts CHANGED
@@ -1,15 +1,5 @@
1
- import type {
2
- AbstractCrdtDoc,
3
- AbstractCrdtType,
4
- InferCrdtStorageJson,
5
- } from "@pluv/crdt";
6
- import type {
7
- IOLike,
8
- Id,
9
- InferIOInput,
10
- InferIOOutput,
11
- JsonObject,
12
- } from "@pluv/types";
1
+ import type { AbstractCrdtDoc, AbstractCrdtType, InferCrdtStorageJson } from "@pluv/crdt";
2
+ import type { IOLike, Id, InferIOInput, InferIOOutput, JsonObject } from "@pluv/types";
13
3
  import { AbstractRoom } from "./AbstractRoom";
14
4
  import type { CrdtManagerOptions } from "./CrdtManager";
15
5
  import { CrdtManager } from "./CrdtManager";
@@ -18,35 +8,22 @@ import type { EventNotifierSubscriptionCallback } from "./EventNotifier";
18
8
  import { EventNotifier } from "./EventNotifier";
19
9
  import type { OtherNotifierSubscriptionCallback } from "./OtherNotifier";
20
10
  import { OtherNotifier } from "./OtherNotifier";
21
- import type {
22
- StateNotifierSubjects,
23
- SubscriptionCallback,
24
- } from "./StateNotifier";
11
+ import type { StateNotifierSubjects, SubscriptionCallback } from "./StateNotifier";
25
12
  import { StateNotifier } from "./StateNotifier";
26
13
  import type { UsersManagerConfig } from "./UsersManager";
27
14
  import { UsersManager } from "./UsersManager";
28
- import type {
29
- InternalSubscriptions,
30
- UserInfo,
31
- WebSocketConnection,
32
- WebSocketState,
33
- } from "./types";
15
+ import type { InternalSubscriptions, UserInfo, WebSocketConnection, WebSocketState } from "./types";
34
16
  import { ConnectionState } from "./types";
35
17
 
36
18
  export type MockedRoomEvents<TIO extends IOLike> = Partial<{
37
- [P in keyof InferIOInput<TIO>]: (
38
- data: Id<InferIOInput<TIO>[P]>,
39
- ) => Partial<InferIOOutput<TIO>>;
19
+ [P in keyof InferIOInput<TIO>]: (data: Id<InferIOInput<TIO>[P]>) => Partial<InferIOOutput<TIO>>;
40
20
  }>;
41
21
 
42
22
  export type MockedRoomConfig<
43
23
  TIO extends IOLike,
44
24
  TPresence extends JsonObject = {},
45
25
  TStorage extends Record<string, AbstractCrdtType<any, any>> = {},
46
- > = { events?: MockedRoomEvents<TIO> } & Omit<
47
- CrdtManagerOptions<TStorage>,
48
- "encodedState"
49
- > &
26
+ > = { events?: MockedRoomEvents<TIO> } & Omit<CrdtManagerOptions<TStorage>, "encodedState"> &
50
27
  UsersManagerConfig<TPresence>;
51
28
 
52
29
  export class MockedRoom<
@@ -77,10 +54,7 @@ export class MockedRoom<
77
54
  };
78
55
  private _usersManager: UsersManager<TIO, TPresence>;
79
56
 
80
- constructor(
81
- room: string,
82
- options: MockedRoomConfig<TIO, TPresence, TStorage>,
83
- ) {
57
+ constructor(room: string, options: MockedRoomConfig<TIO, TPresence, TStorage>) {
84
58
  const { events, initialPresence, initialStorage, presence } = options;
85
59
 
86
60
  super(room);
@@ -103,10 +77,7 @@ export class MockedRoom<
103
77
  return true;
104
78
  }
105
79
 
106
- public broadcast<TEvent extends keyof InferIOInput<TIO>>(
107
- event: TEvent,
108
- data: Id<InferIOInput<TIO>[TEvent]>,
109
- ): void {
80
+ public broadcast<TEvent extends keyof InferIOInput<TIO>>(event: TEvent, data: Id<InferIOInput<TIO>[TEvent]>): void {
110
81
  if (!this._events) return;
111
82
 
112
83
  const resolver = this._events[event];
@@ -142,9 +113,7 @@ export class MockedRoom<
142
113
 
143
114
  public getConnection(): WebSocketConnection {
144
115
  // Create a read-only clone of the connection state
145
- return Object.freeze(
146
- JSON.parse(JSON.stringify(this._state.connection)),
147
- );
116
+ return Object.freeze(JSON.parse(JSON.stringify(this._state.connection)));
148
117
  }
149
118
 
150
119
  public getDoc(): AbstractCrdtDoc<TStorage> {
@@ -159,9 +128,7 @@ export class MockedRoom<
159
128
  return this._usersManager.myself;
160
129
  };
161
130
 
162
- public getOther = (
163
- connectionId: string,
164
- ): Id<UserInfo<TIO, TPresence>> | null => {
131
+ public getOther = (connectionId: string): Id<UserInfo<TIO, TPresence>> | null => {
165
132
  return this._usersManager.getOther(connectionId);
166
133
  };
167
134
 
@@ -169,9 +136,7 @@ export class MockedRoom<
169
136
  return this._usersManager.getOthers();
170
137
  };
171
138
 
172
- public getStorage = <TKey extends keyof TStorage>(
173
- key: TKey,
174
- ): TStorage[TKey] | null => {
139
+ public getStorage = <TKey extends keyof TStorage>(key: TKey): TStorage[TKey] | null => {
175
140
  const sharedType = this._crdtManager.get(key);
176
141
 
177
142
  if (typeof sharedType === "undefined") return null;
@@ -179,10 +144,7 @@ export class MockedRoom<
179
144
  return sharedType.value;
180
145
  };
181
146
 
182
- public other = (
183
- connectionId: string,
184
- callback: OtherNotifierSubscriptionCallback<TIO>,
185
- ): (() => void) => {
147
+ public other = (connectionId: string, callback: OtherNotifierSubscriptionCallback<TIO>): (() => void) => {
186
148
  return this._otherNotifier.subscribe(connectionId, callback);
187
149
  };
188
150
 
@@ -205,19 +167,14 @@ export class MockedRoom<
205
167
  return this._crdtNotifier.subcribeRoot(fn);
206
168
  };
207
169
 
208
- public subscribe = <
209
- TSubject extends keyof StateNotifierSubjects<TIO, TPresence>,
210
- >(
170
+ public subscribe = <TSubject extends keyof StateNotifierSubjects<TIO, TPresence>>(
211
171
  name: TSubject,
212
172
  callback: SubscriptionCallback<TIO, TPresence, TSubject>,
213
173
  ): (() => void) => {
214
174
  return this._stateNotifier.subscribe(name, callback);
215
175
  };
216
176
 
217
- public transact = (
218
- fn: (storage: TStorage) => void,
219
- origin?: string,
220
- ): void => {
177
+ public transact = (fn: (storage: TStorage) => void, origin?: string): void => {
221
178
  const _origin = origin ?? this._state.connection.id;
222
179
  const crdtManager = this._crdtManager;
223
180
 
package/src/PluvClient.ts CHANGED
@@ -1,13 +1,6 @@
1
1
  import type { AbstractCrdtType } from "@pluv/crdt";
2
2
  import type { IOLike, JsonObject } from "@pluv/types";
3
- import {
4
- AuthEndpoint,
5
- PluvRoom,
6
- PluvRoomOptions,
7
- RoomConfig,
8
- RoomEndpoints,
9
- WsEndpoint,
10
- } from "./PluvRoom";
3
+ import { AuthEndpoint, PluvRoom, PluvRoomOptions, RoomConfig, RoomEndpoints, WsEndpoint } from "./PluvRoom";
11
4
 
12
5
  export type PluvClientOptions<TIO extends IOLike> = RoomEndpoints<TIO> & {
13
6
  debug?: boolean;
@@ -56,9 +49,7 @@ export class PluvClient<TIO extends IOLike = IOLike> {
56
49
  return newRoom;
57
50
  };
58
51
 
59
- public enter = async (
60
- room: string | PluvRoom<TIO, any, any>,
61
- ): Promise<PluvRoom<TIO, JsonObject, any>> => {
52
+ public enter = async (room: string | PluvRoom<TIO, any, any>): Promise<PluvRoom<TIO, JsonObject, any>> => {
62
53
  const toEnter = typeof room === "string" ? this.getRoom(room) : room;
63
54
 
64
55
  if (!toEnter) {
@@ -80,9 +71,7 @@ export class PluvClient<TIO extends IOLike = IOLike> {
80
71
  >(
81
72
  room: string,
82
73
  ): PluvRoom<TIO, TPresence, TStorage> | null => {
83
- const found = this._rooms.get(room) as
84
- | PluvRoom<TIO, TPresence, TStorage>
85
- | undefined;
74
+ const found = this._rooms.get(room) as PluvRoom<TIO, TPresence, TStorage> | undefined;
86
75
 
87
76
  return found ?? null;
88
77
  };
@@ -91,9 +80,7 @@ export class PluvClient<TIO extends IOLike = IOLike> {
91
80
  return Array.from(this._rooms.values());
92
81
  };
93
82
 
94
- public leave = async (
95
- room: string | PluvRoom<TIO, any, any>,
96
- ): Promise<void> => {
83
+ public leave = async (room: string | PluvRoom<TIO, any, any>): Promise<void> => {
97
84
  const toLeave = typeof room === "string" ? this.getRoom(room) : room;
98
85
 
99
86
  if (!toLeave) return;