@pluv/client 0.25.3 → 0.26.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/PluvRoom.ts CHANGED
@@ -46,10 +46,11 @@ const ORIGIN_STORAGE_UPDATED = "$STORAGE_UPDATED";
46
46
 
47
47
  export const DEFAULT_PLUV_CLIENT_ADDON = <
48
48
  TIO extends IOLike = IOLike,
49
+ TMetadata extends JsonObject = {},
49
50
  TPresence extends JsonObject = {},
50
51
  TStorage extends Record<string, CrdtType<any, any>> = {},
51
52
  >(
52
- input: PluvRoomAddonInput<TIO, TPresence, TStorage>,
53
+ input: PluvRoomAddonInput<TIO, TMetadata, TPresence, TStorage>,
53
54
  ): PluvRoomAddonResult => ({
54
55
  storage: new StorageStore(input.room.id),
55
56
  });
@@ -83,15 +84,22 @@ interface TimeoutIds {
83
84
  reconnect: number | null;
84
85
  }
85
86
 
86
- export type AuthEndpoint = string | ((room: string) => string | FetchOptions) | true;
87
- export type WsEndpoint = string | ((room: string) => string);
87
+ export interface EndpointParams<TMetadata extends JsonObject> {
88
+ metadata: TMetadata;
89
+ room: string;
90
+ }
91
+ export type AuthEndpoint<TMetadata extends JsonObject> =
92
+ | string
93
+ | ((params: EndpointParams<TMetadata>) => string | FetchOptions)
94
+ | true;
95
+ export type WsEndpoint<TMetadata extends JsonObject> = string | ((params: EndpointParams<TMetadata>) => string);
88
96
 
89
97
  type FetchOptions = { url: string; options?: RequestInit };
90
98
 
91
- export type RoomEndpoints<TIO extends IOLike> = {
92
- wsEndpoint?: WsEndpoint;
99
+ export type RoomEndpoints<TIO extends IOLike, TMetadata extends JsonObject> = {
100
+ wsEndpoint?: WsEndpoint<TMetadata>;
93
101
  } & (InferIOAuthorizeRequired<InferIOAuthorize<TIO>> extends true
94
- ? { authEndpoint: AuthEndpoint }
102
+ ? { authEndpoint: AuthEndpoint<TMetadata> }
95
103
  : { authEndpoint?: undefined });
96
104
 
97
105
  interface InternalListeners {
@@ -100,16 +108,18 @@ interface InternalListeners {
100
108
 
101
109
  export type PluvRoomAddon<
102
110
  TIO extends IOLike = IOLike,
111
+ TMetadata extends JsonObject = {},
103
112
  TPresence extends JsonObject = {},
104
113
  TStorage extends Record<string, CrdtType<any, any>> = {},
105
- > = (input: PluvRoomAddonInput<TIO, TPresence, TStorage>) => Partial<PluvRoomAddonResult>;
114
+ > = (input: PluvRoomAddonInput<TIO, TMetadata, TPresence, TStorage>) => Partial<PluvRoomAddonResult>;
106
115
 
107
116
  export interface PluvRoomAddonInput<
108
117
  TIO extends IOLike = IOLike,
118
+ TMetadata extends JsonObject = {},
109
119
  TPresence extends JsonObject = {},
110
120
  TStorage extends Record<string, CrdtType<any, any>> = {},
111
121
  > {
112
- room: PluvRoom<TIO, TPresence, TStorage>;
122
+ room: PluvRoom<TIO, TMetadata, TPresence, TStorage>;
113
123
  }
114
124
 
115
125
  export interface PluvRoomAddonResult {
@@ -123,27 +133,31 @@ export type PluvRoomDebug<TIO extends IOLike> = Id<{
123
133
 
124
134
  export type PluvRoomOptions<
125
135
  TIO extends IOLike,
136
+ TMetadata extends JsonObject = {},
126
137
  TPresence extends JsonObject = {},
127
138
  TStorage extends Record<string, CrdtType<any, any>> = {},
128
139
  > = {
129
- addons?: readonly PluvRoomAddon<TIO, TPresence, TStorage>[];
140
+ addons?: readonly PluvRoomAddon<TIO, TMetadata, TPresence, TStorage>[];
130
141
  debug?: boolean | PluvRoomDebug<TIO>;
131
142
  onAuthorizationFail?: (error: Error) => void;
132
143
  } & Omit<CrdtManagerOptions<TStorage>, "encodedState"> &
133
- UsersManagerConfig<TPresence>;
144
+ UsersManagerConfig<TPresence> &
145
+ (keyof TMetadata extends never ? { metadata?: undefined } : { metadata: TMetadata });
134
146
 
135
147
  export type RoomConfig<
136
148
  TIO extends IOLike,
149
+ TMetadata extends JsonObject = {},
137
150
  TPresence extends JsonObject = {},
138
151
  TStorage extends Record<string, CrdtType<any, any>> = {},
139
- > = RoomEndpoints<TIO> & PluvRoomOptions<TIO, TPresence, TStorage>;
152
+ > = RoomEndpoints<TIO, TMetadata> & PluvRoomOptions<TIO, TMetadata, TPresence, TStorage>;
140
153
 
141
154
  export class PluvRoom<
142
155
  TIO extends IOLike,
156
+ TMetadata extends JsonObject = {},
143
157
  TPresence extends JsonObject = {},
144
158
  TStorage extends Record<string, CrdtType<any, any>> = {},
145
159
  > extends AbstractRoom<TIO, TPresence, TStorage> {
146
- readonly _endpoints: RoomEndpoints<TIO>;
160
+ readonly _endpoints: RoomEndpoints<TIO, TMetadata>;
147
161
 
148
162
  private _crdtManager: CrdtManager<TStorage>;
149
163
  private _crdtNotifier = new CrdtNotifier<TStorage>();
@@ -153,6 +167,7 @@ export class PluvRoom<
153
167
  heartbeat: null,
154
168
  };
155
169
  private _listeners: InternalListeners;
170
+ private _metadata: TMetadata = {} as TMetadata;
156
171
  private _otherNotifier = new OtherNotifier<TIO>();
157
172
  private _state: WebSocketState<TIO> = {
158
173
  authorization: {
@@ -179,13 +194,14 @@ export class PluvRoom<
179
194
  private _wsListeners: WebSocketListeners | null = null;
180
195
  private _usersManager: UsersManager<TIO, TPresence>;
181
196
 
182
- constructor(room: string, options: RoomConfig<TIO, TPresence, TStorage>) {
197
+ constructor(room: string, options: RoomConfig<TIO, TMetadata, TPresence, TStorage>) {
183
198
  const {
184
199
  addons = [],
185
200
  authEndpoint,
186
201
  debug = false,
187
202
  initialPresence,
188
203
  initialStorage,
204
+ metadata,
189
205
  onAuthorizationFail,
190
206
  presence,
191
207
  wsEndpoint,
@@ -200,11 +216,11 @@ export class PluvRoom<
200
216
  ...addon({ room: this }),
201
217
  };
202
218
 
203
- this._storageStore = storage;
204
-
205
219
  this._debug = debug;
220
+ this._endpoints = { authEndpoint, wsEndpoint } as RoomEndpoints<TIO, TMetadata>;
221
+ this._storageStore = storage;
206
222
 
207
- this._endpoints = { authEndpoint, wsEndpoint } as RoomEndpoints<TIO>;
223
+ if (!!metadata) this._metadata = metadata;
208
224
 
209
225
  this._listeners = {
210
226
  onAuthorizationFail: (error) => {
@@ -607,9 +623,9 @@ export class PluvRoom<
607
623
  );
608
624
 
609
625
  private _getAddon = (
610
- addons: readonly PluvRoomAddon<TIO, TPresence, TStorage>[],
611
- ): PluvRoomAddon<TIO, TPresence, TStorage> => {
612
- return addons.reduce<PluvRoomAddon<TIO, TPresence, TStorage>>(
626
+ addons: readonly PluvRoomAddon<TIO, TMetadata, TPresence, TStorage>[],
627
+ ): PluvRoomAddon<TIO, TMetadata, TPresence, TStorage> => {
628
+ return addons.reduce<PluvRoomAddon<TIO, TMetadata, TPresence, TStorage>>(
613
629
  (acc, addon) => () => ({
614
630
  ...acc({ room: this }),
615
631
  ...addon({ room: this }),
@@ -635,7 +651,7 @@ export class PluvRoom<
635
651
  };
636
652
  }
637
653
 
638
- const result = this._endpoints.authEndpoint(room);
654
+ const result = this._endpoints.authEndpoint({ metadata: this._metadata, room });
639
655
 
640
656
  return typeof result === "string" ? { url: result, options: {} } : result;
641
657
  }
@@ -667,7 +683,7 @@ export class PluvRoom<
667
683
  case "string":
668
684
  return this._endpoints.wsEndpoint;
669
685
  default:
670
- return this._endpoints.wsEndpoint(room);
686
+ return this._endpoints.wsEndpoint({ metadata: this._metadata, room });
671
687
  }
672
688
  }
673
689
 
@@ -1,7 +1,9 @@
1
- import type { IOLike } from "@pluv/types";
1
+ import type { IOLike, JsonObject } from "@pluv/types";
2
2
  import type { PluvClientOptions } from "./PluvClient";
3
3
  import { PluvClient } from "./PluvClient";
4
4
 
5
- export const createClient = <TIO extends IOLike = IOLike>(options: PluvClientOptions<TIO>): PluvClient<TIO> => {
6
- return new PluvClient<TIO>(options);
5
+ export const createClient = <TIO extends IOLike = IOLike, TMetadata extends JsonObject = {}>(
6
+ options: PluvClientOptions<TIO, TMetadata>,
7
+ ): PluvClient<TIO, TMetadata> => {
8
+ return new PluvClient<TIO, TMetadata>(options);
7
9
  };
package/src/index.ts CHANGED
@@ -2,12 +2,18 @@ export type { CrdtType, InferCrdtJson } from "@pluv/crdt";
2
2
  export type { BaseUser, EventMessage, EventRecord, IOEventMessage } from "@pluv/types";
3
3
  export { AbstractRoom } from "./AbstractRoom";
4
4
  export { AbstractStorageStore } from "./AbstractStorageStore";
5
+ export { createClient } from "./createClient";
5
6
  export { MockedRoom } from "./MockedRoom";
6
7
  export type { MockedRoomConfig, MockedRoomEvents } from "./MockedRoom";
7
8
  export { PluvClient } from "./PluvClient";
8
9
  export type { PluvClientOptions } from "./PluvClient";
9
10
  export { PluvRoom } from "./PluvRoom";
10
- export type { PluvRoomAddon, PluvRoomAddonInput, PluvRoomAddonResult, PluvRoomDebug } from "./PluvRoom";
11
- export { createClient } from "./createClient";
11
+ export type {
12
+ PluvRoomAddon,
13
+ PluvRoomAddonInput,
14
+ PluvRoomAddonResult,
15
+ PluvRoomDebug,
16
+ PluvRoomOptions,
17
+ } from "./PluvRoom";
12
18
  export { ConnectionState } from "./types";
13
19
  export type { UserInfo, WebSocketConnection, WebSocketState } from "./types";